SPB Git

Repository Public

store.ts 4.3 KB 143 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : apps/web/src/lib/store.ts9 * Module           : lib10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *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: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/** État local de l'appareil : parties connues (identifiants publics seulement) et préférences. */44import { useSyncExternalStore } from "react";45import type { Mode } from "./types";4647export interface KnownGame {48  id: string;49  mode: Mode;50  competitionId: string;51  name: string;52  displayName: string;53  resumeCode?: string;54  startedAt: number;55}5657export interface Prefs {58  sound: boolean;59  reduceMotion: boolean;60  lastName: string;61  lastCourse: string | null;62}6364const GAMES_KEY = "gdi.parties.v1";65const PREFS_KEY = "gdi.preferences.v1";66const listeners = new Set<() => void>();6768function read<T>(key: string, fallback: T): T {69  try {70    const raw = localStorage.getItem(key);71    return raw ? ({ ...fallback, ...(JSON.parse(raw) as object) } as T) : fallback;72  } catch {73    return fallback;74  }75}7677let games: KnownGame[] = (() => {78  try {79    return JSON.parse(localStorage.getItem(GAMES_KEY) ?? "[]") as KnownGame[];80  } catch {81    return [];82  }83})();84let prefs: Prefs = read<Prefs>(PREFS_KEY, { sound: false, reduceMotion: false, lastName: "", lastCourse: null });8586function emit(): void {87  try {88    localStorage.setItem(GAMES_KEY, JSON.stringify(games));89    localStorage.setItem(PREFS_KEY, JSON.stringify(prefs));90  } catch {91    /* stockage indisponible (navigation privée) : la partie reste récupérable par code */92  }93  document.documentElement.classList.toggle("reduce-motion", prefs.reduceMotion);94  for (const listener of listeners) listener();95}9697const subscribe = (listener: () => void): (() => void) => {98  listeners.add(listener);99  return () => listeners.delete(listener);100};101102export function rememberGame(game: KnownGame): void {103  games = [game, ...games.filter((g) => g.id !== game.id)].slice(0, 12);104  emit();105}106107export function forgetGame(id: string): void {108  games = games.filter((g) => g.id !== id);109  emit();110}111112export function getGames(): KnownGame[] {113  return games;114}115116export function getGame(id: string): KnownGame | undefined {117  return games.find((g) => g.id === id);118}119120export function setPrefs(patch: Partial<Prefs>): void {121  prefs = { ...prefs, ...patch };122  emit();123}124125export function getPrefs(): Prefs {126  return prefs;127}128129export function useGames(): KnownGame[] {130  return useSyncExternalStore(subscribe, () => games);131}132133export function usePrefs(): Prefs {134  return useSyncExternalStore(subscribe, () => prefs);135}136137document.documentElement.classList.toggle("reduce-motion", prefs.reduceMotion);138139/** Vrai si l'utilisateur (système ou réglage du jeu) demande moins d'animations. */140export function prefersReducedMotion(): boolean {141  return prefs.reduceMotion || window.matchMedia("(prefers-reduced-motion: reduce)").matches;142}143
Files main
Menu

Theme