Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : apps/social-web/src/prefs.tsx9 * Module : social-web10 * Repository : https://git.spboucher.ai/maclustr-social11 * Project Website : https://social.spboucher.ai12 * Created : 2026-12-0113 * Last Modified : 2026-12-0114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Language and theme: stored in localStorage, available before sign-in.43import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from 'react';44import { labelOf, localeOf, translate, type Key, type Lang, type TFn, type Vars } from './i18n.ts';45import { DEFAULT_TZ, fmtDate, fmtDateTime } from './time.ts';4647export type Theme = 'system' | 'light' | 'dark';48const read = (k: string): string | null => { try { return window.localStorage.getItem(k); } catch { return null; } };49const write = (k: string, v: string) => { try { window.localStorage.setItem(k, v); } catch { /* private mode: the choice lasts for this page only */ } };5051export function readTheme(): Theme { const v = read('mls.theme'); return v === 'light' || v === 'dark' ? v : 'system'; }52export function applyTheme(t: Theme): void { const el = document.documentElement; if (t === 'system') el.removeAttribute('data-theme'); else el.setAttribute('data-theme', t); }53function readLang(): Lang { const v = read('mls.lang'); if (v === 'fr' || v === 'en') return v; return 'fr'; }5455export type Prefs = {56 lang: Lang; setLang: (l: Lang) => void; theme: Theme; setTheme: (t: Theme) => void; locale: string;57 t: TFn; tl: (prefix: string, value: string | null | undefined) => string;58 /** Date and time in a named zone, with the zone abbreviation. */59 dt: (iso: string | Date | null | undefined, tz?: string) => string;60 d: (iso: string | Date | null | undefined, tz?: string) => string;61 num: (n: number, opts?: Intl.NumberFormatOptions) => string;62};63const Ctx = createContext<Prefs | null>(null);6465export function PrefsProvider({ children }: { children: ReactNode }) {66 const [lang, setLangState] = useState<Lang>(readLang);67 const [theme, setThemeState] = useState<Theme>(readTheme);68 const setLang = useCallback((l: Lang) => { write('mls.lang', l); setLangState(l); }, []);69 const setTheme = useCallback((t: Theme) => { write('mls.theme', t); applyTheme(t); setThemeState(t); }, []);70 useEffect(() => { document.documentElement.lang = lang; }, [lang]);71 const value = useMemo<Prefs>(() => {72 const locale = localeOf(lang);73 return {74 lang, setLang, theme, setTheme, locale,75 t: (key: Key, vars?: Vars) => translate(lang, key, vars),76 tl: (prefix, v) => labelOf(lang, prefix, v),77 dt: (iso, tz = DEFAULT_TZ) => fmtDateTime(iso, tz, locale),78 d: (iso, tz = DEFAULT_TZ) => fmtDate(iso, tz, locale),79 num: (n, opts) => new Intl.NumberFormat(locale, opts).format(n),80 };81 }, [lang, theme, setLang, setTheme]);82 return <Ctx.Provider value={value}>{children}</Ctx.Provider>;83}84export function usePrefs(): Prefs { const p = useContext(Ctx); if (!p) throw new Error('PrefsProvider missing'); return p; }85