Spaces:
Running
Running
Download src/components/NotificationPollerBridge.tsx from ahmdelbaz28/AHMDRTAP: direct link, hf CLI and curl.
- Browser
- Download file 7.13 kB
-
https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/src/components/NotificationPollerBridge.tsx
- Command line
-
hf download hf://spaces/ahmdelbaz28/AHMDRTAP/src/components/NotificationPollerBridge.tsx
-
curl -L -o NotificationPollerBridge.tsx https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/src/components/NotificationPollerBridge.tsx
7.13 kB
| import { useCallback } from "react"; | |
| import { useNavigate } from "react-router"; | |
| import { useRealtimeNotifications, useShowNotification } from "@/components/NotificationCenter"; | |
| import { playApprovalChime, showDeviceNotification, vibrateApproval } from "@/lib/approval-chime"; | |
| import { resolveNotificationTarget, type ResolvedEventTarget } from "@/lib/notification-link"; | |
| const KNOWN_TOAST_TYPES = new Set(["youtube", "lesson", "file", "software"]); | |
| type ToastType = "youtube" | "lesson" | "file" | "software" | "enrollment" | "info"; | |
| /** Map a server event type onto the toast taxonomy (unknown types → "info"). */ | |
| function toastTypeFor(type: string): ToastType { | |
| if (KNOWN_TOAST_TYPES.has(type)) return type as ToastType; | |
| if (type === "enrollment") return "enrollment"; | |
| return "info"; | |
| } | |
| // ─── ✅ S3776/S3358 (Sonar): the event→toast assembly lived as one deeply | |
| // ─── nested callback (cognitive complexity 16 > 15) — split into flat, | |
| // ─── single-purpose helpers below. | |
| /** Break-through alert for a join request — chime + buzz + device-level ping. */ | |
| function announceApprovalRequest( | |
| approvalData: { username?: string; name?: string }, | |
| eventId: string, | |
| isRtl: boolean, | |
| ): void { | |
| playApprovalChime(); | |
| vibrateApproval(); | |
| showDeviceNotification( | |
| isRtl ? "طلب انضمام جديد للمنصة" : "New join request", | |
| isRtl | |
| ? `${approvalData.name || approvalData.username || "مستخدم جديد"} سجل في المنصة ويحتاج موافقتك` | |
| : `${approvalData.name || approvalData.username || "A new user"} registered and needs your approval`, | |
| `approval-${approvalData.username ?? eventId}`, | |
| ); | |
| } | |
| /** ✅ S3776 (Sonar): verbs as a LOOKUP MAP — a switch + per-case ternaries | |
| * pushed the function's cognitive complexity over the scanner's budget. */ | |
| const MEDIA_ACTION_LABELS: Record<string, { readonly ar: string; readonly en: string }> = { | |
| youtube: { ar: "شاهد الحلقة الآن", en: "Watch now" }, | |
| lesson: { ar: "افتح الكورس", en: "Open course" }, | |
| file: { ar: "حمّل الملف", en: "Get the file" }, | |
| software: { ar: "حمّل البرنامج", en: "Download" }, | |
| }; | |
| /** ✅ ACTION VERBS: every clickable event gets a verb, not just a URL. */ | |
| function actionLabelFor( | |
| url: string | null, | |
| target: ResolvedEventTarget, | |
| type: string, | |
| isRtl: boolean, | |
| ): string | undefined { | |
| if (!url) return undefined; | |
| if (target.approvalRequest) return isRtl ? "مراجعة الطلب" : "Review request"; | |
| if (target.approvalAccepted) return isRtl ? "ابدأ التعلم" : "Start learning"; | |
| const labels = MEDIA_ACTION_LABELS[type]; | |
| if (!labels) return undefined; | |
| return isRtl ? labels.ar : labels.en; | |
| } | |
| /** Approval decisions are time-critical — 12s dwell; media gets longer too. */ | |
| function toastDurationFor(target: ResolvedEventTarget, type: string): number { | |
| if (target.approvalRequest) return 12_000; | |
| if (type === "youtube") return 9_000; | |
| return 7_000; | |
| } | |
| /** | |
| * NotificationPollerBridge — the invisible component that makes server | |
| * notifications REAL for the user. | |
| * | |
| * Mounted once (inside NotificationProvider) for logged-in users. It polls | |
| * /api/notifications/poll every 15s and turns every new event into: | |
| * - a live toast (bottom/top, auto-dismiss, card body + action clickable), | |
| * - a persisted entry in the bell history (via useShowNotification). | |
| * | |
| * Event → toast mapping: | |
| * approval request → chime + buzz + device alert + "مراجعة الطلب" action | |
| * that lands on the accept/reject queue in ONE tap | |
| * youtube → red toast, action opens the video in a new tab | |
| * lesson → action navigates to the course page inside the SPA | |
| * file → action navigates to /references | |
| * software → action navigates home (downloads section) | |
| * promo/system → informational toast (clickable when a url exists) | |
| */ | |
| export default function NotificationPollerBridge() { | |
| const { showNotification } = useShowNotification(); | |
| const navigate = useNavigate(); | |
| const onNewEvent = useCallback( | |
| (event: { | |
| id: string; | |
| type: string; | |
| title: string; | |
| message: string; | |
| url?: string; | |
| channelUrl?: string; | |
| data?: unknown; | |
| }) => { | |
| const isRtl = document.documentElement.dir !== "ltr"; | |
| const openUrl = (url: string) => { | |
| if (url.startsWith("/")) navigate(url); // NOSONAR: S9383 — fire-and-forget by framework contract (void|Promise union) | |
| // ✅ SECURITY FIX (audit run-1): http(s) URLs only — never open a | |
| // javascript:/data: destination from a notification payload. | |
| else if (/^https?:\/\//i.test(url)) window.open(url, "_blank", "noopener,noreferrer"); | |
| }; | |
| // ✅ DEEP-LINK ENGINE (owner directive, 2026-12-01): one resolver for | |
| // every event — normalized, safe, and guaranteed to land where the | |
| // ACTION happens. Join requests can never render link-less even if a | |
| // server deploy is behind the client (payload fallbacks). | |
| const target = resolveNotificationTarget(event); | |
| // ✅ ADMIN APPROVAL GATE (owner directive, 2026-11-24): a registration | |
| // that needs approval is the ONE event class that must break through — | |
| // chime + haptic buzz + device-level notification on every open client | |
| // of an admin (the toast below still fires; only admins ever receive | |
| // these events because the fan-out targets admin streams only). | |
| const approvalData = (event.data as { approvalRequest?: { username?: string; name?: string } } | undefined)?.approvalRequest; | |
| if (target.approvalRequest && approvalData) { | |
| announceApprovalRequest(approvalData, event.id, isRtl); | |
| } | |
| const destination = target.url; | |
| const actionLabel = actionLabelFor(destination, target, event.type, isRtl); | |
| const openDestination = () => { | |
| if (destination) openUrl(destination); | |
| }; | |
| showNotification({ | |
| // Pass the server type through — the toast system colors/icons it | |
| // (youtube → red YouTube icon) and the bell stores it for display. | |
| type: toastTypeFor(event.type), | |
| title: event.title, | |
| message: event.message, | |
| eventId: event.id, | |
| url: destination ?? undefined, | |
| channelUrl: event.channelUrl, | |
| // ✅ ACTIONABLE TOAST (owner report 2026-12-01): the card body itself | |
| // is clickable whenever there is a destination — the approval toast | |
| // used to pop with NO way to reach the queue except hunting the bell. | |
| onOpen: destination ? openDestination : undefined, | |
| duration: toastDurationFor(target, event.type), | |
| ...(actionLabel && destination | |
| ? { action: { label: actionLabel, onClick: openDestination } } | |
| : {}), | |
| }); | |
| }, | |
| [showNotification, navigate], | |
| ); | |
| useRealtimeNotifications({ interval: 15_000, onNewEvent }); | |
| // Polling only activates when authenticated (handled inside the hook); | |
| // this component renders nothing. | |
| return null; | |
| } | |