import { createCookieSessionStorage } from 'remix'; import { isTheme, Theme } from '~/components'; export const themeStorage = createCookieSessionStorage({ cookie: { name: 'theme', secure: true, secrets: ['secret'], sameSite: 'lax', path: '/', expires: new Date('2100-01-01'), httpOnly: true, }, }); async function getThemeSession(request: Request) { const session = await themeStorage.getSession(request.headers.get('Cookie')); return { getTheme: () => { const themeValue = session.get('theme'); return isTheme(themeValue) ? themeValue : Theme.light; }, setTheme: (theme: Theme) => session.set('theme', theme), commit: () => themeStorage.commitSession(session), }; } export { getThemeSession };