NGU-Web/src/components/Banner.tsx

64 lines
2.1 KiB
TypeScript

import { useState, useEffect } from "react";
import { Link } from "react-router-dom";
import { SITE_BANNER } from "../data/bannerConfig.js";
export default function Banner() {
const [visible, setVisible] = useState(false);
const storageKey = `ngu-banner-dismissed:${SITE_BANNER.id}`;
useEffect(() => {
if (!SITE_BANNER.enabled) return;
if (!SITE_BANNER.dismissible) return setVisible(true);
try {
if (!localStorage.getItem(storageKey)) setVisible(true);
} catch {
setVisible(true); // storage blocked — show it anyway
}
}, [storageKey]);
const dismiss = () => {
setVisible(false);
try { localStorage.setItem(storageKey, "1"); } catch {}
};
if (!visible) return null;
const linkClass =
"underline underline-offset-4 decoration-[#aac992] hover:text-white transition-colors";
return (
<div
className="relative w-full px-12 py-2.5 text-center"
style={{ background: "rgba(0, 47, 57, 0.95)" }}
role="region"
aria-label="Site announcement"
>
<p className="text-sm text-white/85">
{SITE_BANNER.content.map((part, i) => {
if (!part.href) return <span key={i}>{part.text}</span>;
return part.external ? (
<a key={i} href={part.href} target="_blank" rel="noopener noreferrer" className={linkClass}>
{part.text}
</a>
) : (
<Link key={i} to={part.href} className={linkClass}>
{part.text}
</Link>
);
})}
</p>
{SITE_BANNER.dismissible && (
<button
onClick={dismiss}
aria-label="Dismiss announcement"
className="absolute right-3 top-1/2 -translate-y-1/2 p-1.5 rounded-md text-white/50 hover:text-white hover:bg-white/10 transition-colors"
>
<svg className="w-4 h-4" viewBox="0 0 20 20" fill="currentColor">
<path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />
</svg>
</button>
)}
</div>
);
}