This commit is contained in:
Kismet Hasanaj
2026-05-03 01:09:44 +02:00
parent d66c54088a
commit c22d54abb0
4 changed files with 14 additions and 70 deletions
+6 -17
View File
@@ -22,7 +22,7 @@
// =============================================================================
import Link from "next/link";
import { useEffect, useState } from "react";
import { useEffect, useLayoutEffect, useState } from "react";
import { site } from "@/content";
const CONSENT_KEY = "novarix-cookie-consent";
@@ -33,30 +33,20 @@ const INTRO_EVENT = "novarix:start-intro";
type Consent = "unknown" | "ack" | "rst";
export default function CookieBanner() {
// Start from the pre-hydration decision made in layout.tsx so the banner
// can appear immediately on first visit without waiting for a delayed effect.
const [visible, setVisible] = useState(() => {
if (typeof document === "undefined") return false;
return document.documentElement.dataset.showCookieBanner === "1";
});
// Hidden by default on the server, then synchronously shown on first paint
// if no consent choice exists yet.
const [visible, setVisible] = useState(false);
// Read the stored consent on mount and decide whether to show the banner.
useEffect(() => {
useLayoutEffect(() => {
try {
const stored = window.localStorage.getItem(CONSENT_KEY) as Consent | null;
if (stored !== "ack" && stored !== "rst") {
document.documentElement.dataset.showCookieBanner = "1";
// eslint-disable-next-line react-hooks/set-state-in-effect
setVisible(true);
} else {
document.documentElement.dataset.showCookieBanner = "0";
}
} catch {
// localStorage unavailable (private mode, blocked, etc.) — show the
// banner so the user is still told. Sync-once-on-mount is a legitimate
// use of setState in an effect.
// eslint-disable-next-line react-hooks/set-state-in-effect
document.documentElement.dataset.showCookieBanner = "1";
// banner so the user is still told.
setVisible(true);
}
}, []);
@@ -80,7 +70,6 @@ export default function CookieBanner() {
} catch {
/* storage unavailable — nothing to do */
}
document.documentElement.dataset.showCookieBanner = "0";
setVisible(false);
}