Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/layout/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
}
Expand Down
32 changes: 32 additions & 0 deletions src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
};

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<string, string>)[cookieKey] = parts.join("; ");
}