fix: improvements in the dashboard

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-10-17 18:15:23 +02:00
parent c8bea685db
commit 077a47a263
29 changed files with 1133 additions and 526 deletions

View File

@@ -0,0 +1,35 @@
import { useRouteContext } from '@tanstack/react-router';
import { createServerFn, createServerOnlyFn } from '@tanstack/react-start';
import { getCookies, setCookie } from '@tanstack/react-start/server';
import { useMemo, useState } from 'react';
import { z } from 'zod';
const setCookieFn = createServerFn({ method: 'POST' })
.inputValidator(z.object({ key: z.string(), value: z.string() }))
.handler(({ data: { key, value } }) => {
setCookie(key, value);
});
// Called in __root.tsx beforeLoad hook to get cookies from the server
// And recieved with useRouteContext in the client
export const getCookiesFn = createServerFn({ method: 'GET' }).handler(() =>
getCookies(),
);
export function useCookieStore<T>(key: string, defaultValue: T) {
const { cookies } = useRouteContext({ strict: false });
const [value, setValue] = useState<T>((cookies?.[key] ?? defaultValue) as T);
return useMemo(
() =>
[
value,
(value: T) => {
console.log('setting cookie', key, value);
setValue(value);
setCookieFn({ data: { key, value: String(value) } });
},
] as const,
[value, key],
);
}