"use client"; import React, { useState } from "react"; import { Info, Wrench, AlertTriangle, X } from "lucide-react"; import { useAuth } from "@/hooks/useAuth"; import type { HubNotice } from "@/components/admin/LicenceTab"; import { apiV2Data } from "@/lib/apiV2"; import { useExternalEffect } from "@/lib/hooks"; const DISMISSED_KEY = "cross_events_dismissed_notices"; const LEVEL = { info: { icon: Info, tone: "bg-sky-50 border-sky-200 text-sky-900" }, maintenance: { icon: Wrench, tone: "bg-amber-50 border-amber-200 text-amber-900" }, warning: { icon: AlertTriangle, tone: "bg-red-50 border-red-200 text-red-800" }, } as const; // A notice the provider edits shows again, so it's remembered by id and last change. const noticeKey = (n: HubNotice) => `${n.id}:${n.updatedAt || ""}`; function readDismissed(): string[] { try { return JSON.parse(localStorage.getItem(DISMISSED_KEY) || "[]"); } catch { return []; } } /** * Notices from the provider (sent by the Cross Events Hub): maintenance, news and so on. Shown to * site admins and supervisors only. Each can be closed on this browser; it's still listed on Site * settings → Licence. */ export function ProviderNotices() { const { user, token } = useAuth(); const [notices, setNotices] = useState([]); const [dismissed, setDismissed] = useState([]); const canSee = user?.role === "admin" || user?.role === "supervisor"; useExternalEffect(() => { if (!canSee || !token) return; setDismissed(readDismissed()); (apiV2Data("get", "/hub/notices", { authToken: token }) as Promise).then(setNotices).catch(() => {}); }, [canSee, token]); if (!canSee) return null; const shown = notices.filter(n => !dismissed.includes(noticeKey(n))); if (!shown.length) return null; const dismiss = (n: HubNotice) => { // Keep only notices that still exist, so the list doesn't grow forever. const next = [...dismissed.filter(k => notices.some(x => noticeKey(x) === k)), noticeKey(n)]; setDismissed(next); try { localStorage.setItem(DISMISSED_KEY, JSON.stringify(next)); } catch { /* private window: closed until reload */ } }; return ( <> {shown.map(n => { const { icon: Icon, tone } = LEVEL[n.level] || LEVEL.info; return (
{n.title} — {n.message}
); })} ); }