Archived
- The 192 places the rules flagged are fixed and the rules are errors again (npx eslint src: nothing). lib/hooks.ts: useStateSync (state that follows other state, set while rendering), useExternalEffect (API loads and browser reads), useNow; lib/navigation.ts hardNavigate for full reloads after sign-out or identity switches; other pages use the router. Resets moved into the handlers that cause them. - The banner shows and hides at its times on open pages. - HANDOVER, roadmap, README, FEATURES, SETTINGS, CHANGELOG updated; 2.0.0 section drafted in WHATS-NEW. e2e 102/102 (PayFast sandbox skipped); production build passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
2.9 KiB
TypeScript
73 lines
2.9 KiB
TypeScript
"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<HubNotice[]>([]);
|
|
const [dismissed, setDismissed] = useState<string[]>([]);
|
|
const canSee = user?.role === "admin" || user?.role === "supervisor";
|
|
|
|
useExternalEffect(() => {
|
|
if (!canSee || !token) return;
|
|
setDismissed(readDismissed());
|
|
(apiV2Data("get", "/hub/notices", { authToken: token }) as Promise<HubNotice[]>).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 (
|
|
<div key={n.id} role={n.level === "warning" ? "alert" : "status"} className={`border-b text-sm px-4 py-2 flex items-start gap-2 ${tone}`}>
|
|
<Icon className="w-4 h-4 shrink-0 mt-0.5" />
|
|
<div className="flex-1 min-w-0">
|
|
<span className="font-medium">{n.title}</span>
|
|
<span className="whitespace-pre-line"> — {n.message}</span>
|
|
</div>
|
|
<button onClick={() => dismiss(n)} className="shrink-0 rounded p-0.5 hover:bg-black/5" aria-label={`Close "${n.title}"`}>
|
|
<X className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
);
|
|
})}
|
|
</>
|
|
);
|
|
}
|