'use client'; import { useEffect, useState } from 'react'; import { useLocale } from 'next-intl'; interface ApiStatement { id: number; text: string; active?: boolean; } export default function PoliticalStatementBanner() { const locale = useLocale(); const [statement, setStatement] = useState(null); const [visible, setVisible] = useState(false); useEffect(() => { const today = new Date().toISOString().split('T')[0]; const storageKey = `hoerdle_political_statement_shown_${today}_${locale}`; try { const alreadyShown = typeof window !== 'undefined' && window.localStorage.getItem(storageKey); if (alreadyShown) { return; } } catch { // ignore localStorage errors } let timeoutId: number | undefined; const fetchStatement = async () => { try { const res = await fetch(`/api/political-statements?locale=${encodeURIComponent(locale)}`, { cache: 'no-store', }); if (!res.ok) return; const data = await res.json(); if (!data || !data.text) return; setStatement(data); setVisible(true); timeoutId = window.setTimeout(() => { setVisible(false); try { window.localStorage.setItem(storageKey, 'true'); } catch { // ignore } }, 5000); } catch (e) { console.warn('[PoliticalStatementBanner] Failed to load statement', e); } }; fetchStatement(); return () => { if (timeoutId) { window.clearTimeout(timeoutId); } }; }, [locale]); if (!visible || !statement) return null; return (
{statement.text}
); }