import { useCallback, useState } from "react"; import { X } from "lucide-react"; import { useNavigate } from "react-router-dom"; import { Button } from "@/components/ui/button"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { useAdminNotifications } from "@/contexts/AdminNotificationContext"; import { NotificationIcon, getTypeAccent, resolveStickyStyle } from "@/components/generic/notificationDisplay"; // Admin counterpart to StickyAnnouncementBar (client). Only supports the // explicit link_url from the "On Open" section — the type-based fallback // resolver in notificationDisplay.jsx points at client-only routes // (/course/:id, /plans, /group/:id), which don't exist in the admin app. export default function AdminStickyAnnouncementBar() { const navigate = useNavigate(); const { stickyAnnouncement, markSeen } = useAdminNotifications(); const [detailsOpen, setDetailsOpen] = useState(false); const onDismiss = useCallback(async () => { if (!stickyAnnouncement) return; await markSeen(stickyAnnouncement.notification_id); }, [stickyAnnouncement, markSeen]); // Opening the dialog must NOT mark it seen — markSeen clears // stickyAnnouncement, which would unmount this component (dialog included) // before it ever shows. Only the X button dismisses/marks seen. const onClickBanner = useCallback(() => { if (!stickyAnnouncement) return; setDetailsOpen(true); }, [stickyAnnouncement]); if (!stickyAnnouncement) return null; const accentClass = getTypeAccent(stickyAnnouncement.type); const inlineStyle = resolveStickyStyle(stickyAnnouncement.data); const linkUrl = stickyAnnouncement.data?.linkUrl || null; const openLink = () => { if (!linkUrl) return; if (linkUrl.startsWith("/")) navigate(linkUrl); else window.open(linkUrl, "_blank", "noopener,noreferrer"); }; return ( <>

{stickyAnnouncement.title || "Announcement"}

{stickyAnnouncement.message || ""}

{stickyAnnouncement.title || "Announcement"}

{stickyAnnouncement.message || ""}

Close {linkUrl && ( Open Link )}
); }