Files
co-lite/solvway-admin-portal/app/components/app/NewCodeNotice.tsx
T

47 lines
1.8 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import { X } from "lucide-react";
import { getMyLinks } from "@/app/lib/services/adminRegistration";
import { useT } from "@/app/lib/i18n/MessagesProvider";
const WEEK_MS = 7 * 86_400_000;
const key = (token: string) => `qrNoticeDismissed:${token}`;
const dismissed = (token: string) => {
try { return localStorage.getItem(key(token)) !== null; } catch { return false; }
};
/** Banner for personal codes added in the last 7 days; dismissed per code. */
export function NewCodeNotice() {
const t = useT();
const [tokens, setTokens] = useState<string[]>([]);
useEffect(() => {
getMyLinks()
.then((links) => setTokens(links
.filter((l) => l.addedAt && Date.now() - Date.parse(l.addedAt) < WEEK_MS && !dismissed(l.token))
.map((l) => l.token)))
.catch(() => {}); // a notice is optional; stay silent if the codes cannot load
}, []);
if (tokens.length === 0) return null;
const dismiss = () => {
try { tokens.forEach((tk) => localStorage.setItem(key(tk), "1")); } catch { /* the notice just returns next load */ }
setTokens([]);
};
return (
<div role="status" className="mb-4 flex items-center justify-between gap-3 rounded-lg border bg-card p-3 text-sm">
<p>
{t("QrNewCode")}{" "}
<Link href="/my-qr" className="font-medium text-primary underline-offset-4 hover:underline">{t("QrMyTitle")}</Link>
</p>
<button type="button" aria-label={t("QrDismiss")} onClick={dismiss} className="flex size-11 shrink-0 items-center justify-center rounded-md hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none">
<X aria-hidden="true" className="size-4" />
</button>
</div>
);
}