OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix: use cookie store for theme as well
Carl-Gerhard Lindesvärd87d4ec2f33f7
diff --git a/apps/start/src/components/theme-provider.tsx b/apps/start/src/components/theme-provider.tsxindex 5b5a4c840..1a4b2388f 100644--- a/apps/start/src/components/theme-provider.tsx+++ b/apps/start/src/components/theme-provider.tsx@@ -1,5 +1,6 @@+import { useCookieStore } from '@/hooks/use-cookie-store';import { mapKeys } from '@openpanel/validation';-import { ScriptOnce } from '@tanstack/react-router';+import { ScriptOnce, useRouteContext } from '@tanstack/react-router';import { createIsomorphicFn } from '@tanstack/react-start';import { type ReactNode, createContext, use, useEffect, useState } from 'react';import { z } from 'zod';@@ -115,7 +116,10 @@ type ThemeProviderProps = {children: ReactNode;};export function ThemeProvider({ children }: ThemeProviderProps) {- const [userTheme, setUserTheme] = useState<UserTheme>(getStoredUserTheme);+ const [userTheme, setUserTheme] = useCookieStore<UserTheme>(+ 'ui-theme',+ 'system',+ );useEffect(() => {if (userTheme !== 'system') return;diff --git a/apps/start/src/hooks/use-cookie-store.tsx b/apps/start/src/hooks/use-cookie-store.tsxindex 032f3636f..360bc3a8e 100644--- a/apps/start/src/hooks/use-cookie-store.tsx+++ b/apps/start/src/hooks/use-cookie-store.tsx@@ -1,11 +1,14 @@import { useRouteContext } from '@tanstack/react-router';import { createServerFn, createServerOnlyFn } from '@tanstack/react-start';import { getCookies, setCookie } from '@tanstack/react-start/server';+import { pick } from 'ramda';import { useMemo, useState } from 'react';import { z } from 'zod';+const VALID_COOKIES = ['ui-theme', 'chartType', 'range'] as const;+const setCookieFn = createServerFn({ method: 'POST' })- .inputValidator(z.object({ key: z.string(), value: z.string() }))+ .inputValidator(z.object({ key: z.enum(VALID_COOKIES), value: z.string() })).handler(({ data: { key, value } }) => {setCookie(key, value);});@@ -13,10 +16,13 @@ const setCookieFn = createServerFn({ method: 'POST' })// Called in __root.tsx beforeLoad hook to get cookies from the server// And recieved with useRouteContext in the clientexport const getCookiesFn = createServerFn({ method: 'GET' }).handler(() =>- getCookies(),+ pick(VALID_COOKIES, getCookies()),);-export function useCookieStore<T>(key: string, defaultValue: T) {+export function useCookieStore<T>(+ key: (typeof VALID_COOKIES)[number],+ defaultValue: T,+) {const { cookies } = useRouteContext({ strict: false });const [value, setValue] = useState<T>((cookies?.[key] ?? defaultValue) as T);diff --git a/apps/start/src/routes/__root.tsx b/apps/start/src/routes/__root.tsxindex 869277a5e..46b5a68cb 100644--- a/apps/start/src/routes/__root.tsx+++ b/apps/start/src/routes/__root.tsx@@ -80,6 +80,11 @@ function RootDocument({ children }: { children: React.ReactNode }) {<html lang="en" suppressHydrationWarning><head><HeadContent />+ </head>+ <body className="grainy min-h-screen bg-def-100 font-sans text-base antialiased leading-normal">+ <Providers>{children}</Providers>+ <ThemeScriptOnce />+ <Scripts /><scriptdangerouslySetInnerHTML={{__html: `window.$ujq=window.$ujq||[];window.uj=window.uj||new Proxy({},{get:(_,p)=>(...a)=>window.$ujq.push([p,...a])});document.head.appendChild(Object.assign(document.createElement('script'),{src:'https://cdn.userjot.com/sdk/v2/uj.js',type:'module',async:!0}));`,@@ -90,11 +95,6 @@ function RootDocument({ children }: { children: React.ReactNode }) {__html: `window.uj.init('cm6thlmwr03xr13jghznx87gk', { widget: true, trigger: 'custom' });`,}}/>- </head>- <body className="grainy min-h-screen bg-def-100 font-sans text-base antialiased leading-normal">- <Providers>{children}</Providers>- <ThemeScriptOnce />- <Scripts /><div className="hidden"><div className="text-chart-0 bg-chart-0" /><div className="text-chart-1 bg-chart-1" />