From 7be3622a28108594559cf79d4b7e04b32f9e840e Mon Sep 17 00:00:00 2001 From: Panthevm Date: Thu, 21 May 2026 11:27:58 +0300 Subject: [PATCH] Navbar: fix New UI switch crash in Firefox/Safari cookieStore is a Chromium-only experimental API, so toggling the "New UI" switch threw a ReferenceError in non-Chromium browsers. Add a setCookie helper that uses cookieStore when available and falls back to document.cookie everywhere else. --- src/layout/navbar.tsx | 7 +++---- src/utils/cookie.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/layout/navbar.tsx b/src/layout/navbar.tsx index cc06816..919148e 100644 --- a/src/layout/navbar.tsx +++ b/src/layout/navbar.tsx @@ -37,6 +37,7 @@ const ClaudeChatToggle = import.meta.env.DEV import { PREFERRED_UI_KEY, THEME_KEY, VIM_MODE_KEY } from "../shared/const"; import { getAidboxBaseURL } from "../utils"; +import { setCookie } from "../utils/cookie"; function inferResourceTypeFromPath(path: string): string | null { if (/^\/analytics\/views\/edit\//.test(path)) return "ViewDefinition"; @@ -148,11 +149,9 @@ function NavbarButtons() { checked={true} onCheckedChange={(checked) => { if (!checked) { - cookieStore.set({ - name: PREFERRED_UI_KEY, - value: "old", + setCookie(PREFERRED_UI_KEY, "old", { path: "/", - expires: Date.now() + 365 * 24 * 60 * 60 * 1000, + maxAgeSeconds: 365 * 24 * 60 * 60, }); window.location.href = getAidboxBaseURL(); } diff --git a/src/utils/cookie.ts b/src/utils/cookie.ts index e7045df..6f002f3 100644 --- a/src/utils/cookie.ts +++ b/src/utils/cookie.ts @@ -2,3 +2,35 @@ export function getCookie(name: string): string | null { const match = document.cookie.match(new RegExp(`(?:^|; )${name}=([^;]*)`)); return match?.[1] ? decodeURIComponent(match[1]) : null; } + +type CookieStore = { + set: (options: { + name: string; + value: string; + path?: string; + expires?: number; + }) => Promise; +}; + +export function setCookie( + name: string, + value: string, + options: { path?: string; maxAgeSeconds?: number } = {}, +): void { + const path = options.path ?? "/"; + const maxAge = options.maxAgeSeconds; + const store = (globalThis as { cookieStore?: CookieStore }).cookieStore; + if (store) { + store.set({ + name, + value, + path, + expires: maxAge !== undefined ? Date.now() + maxAge * 1000 : undefined, + }); + return; + } + const parts = [`${name}=${encodeURIComponent(value)}`, `path=${path}`]; + if (maxAge !== undefined) parts.push(`max-age=${maxAge}`); + const cookieKey = "cookie"; + (document as unknown as Record)[cookieKey] = parts.join("; "); +}