feat(website): persist language choice in a cookie

useLang now seeds from a first-party `dezky-lang` cookie on SSR init and
toggleLang writes it (12 months, SameSite=Lax). The chosen language now
survives reloads/return visits (fixes the reset-to-Danish quirk) and makes
the cookie-policy entry accurate.
This commit is contained in:
Ronni Baslund
2026-06-06 16:42:46 +02:00
parent 3123d8a5a1
commit 1686b71411
+16 -2
View File
@@ -6,7 +6,18 @@ import { makeTheme } from '~/utils/landingTokens'
// translated). `dark` is kept as machinery from the design's Tweaks panel but // translated). `dark` is kept as machinery from the design's Tweaks panel but
// defaults to light — the primary theme the user landed on — and no toggle is // defaults to light — the primary theme the user landed on — and no toggle is
// surfaced. Flip the default (or add a control) to enable dark later. // surfaced. Flip the default (or add a control) to enable dark later.
export const useLang = () => useState<Lang>('dz-lang', () => 'da')
// Language choice persists in the first-party `dezky-lang` cookie (see the
// cookie policy). useState stays the reactive source of truth so a toggle in
// one component updates the whole page live; we seed it from the cookie on SSR
// init (so a reload keeps the chosen language) and write the cookie on change.
const LANG_COOKIE = 'dezky-lang'
const LANG_MAX_AGE = 60 * 60 * 24 * 365 // 12 months
export const useLang = () => useState<Lang>('dz-lang', () => {
const saved = useCookie<Lang>(LANG_COOKIE).value
return saved === 'en' ? 'en' : 'da'
})
export const useDark = () => useState<boolean>('dz-dark', () => false) export const useDark = () => useState<boolean>('dz-dark', () => false)
export const useTheme = () => { export const useTheme = () => {
@@ -21,7 +32,10 @@ export const useCopy = () => {
export function toggleLang() { export function toggleLang() {
const lang = useLang() const lang = useLang()
lang.value = lang.value === 'da' ? 'en' : 'da' const next: Lang = lang.value === 'da' ? 'en' : 'da'
lang.value = next
// Persist so the choice survives a reload / return visit.
useCookie<Lang>(LANG_COOKIE, { maxAge: LANG_MAX_AGE, sameSite: 'lax', path: '/' }).value = next
} }
// Smooth-scroll to an in-page anchor, accounting for the sticky 72px nav. // Smooth-scroll to an in-page anchor, accounting for the sticky 72px nav.