Repository Public
1// Préférences propres à l'appareil (jamais synchronisées) : disposition, largeur des volets, barre latérale réduite,2// transparence, dossiers repliés, recherches récentes. Conservées dans localStorage ; fonctions pures de normalisation.34export type Layout = "right" | "below" | "none"5export interface PaneWidths {6 /** largeur de la barre latérale (px) */7 sidebar: number8 /** largeur de la liste, lecture à droite (px) */9 list: number10 /** hauteur de la liste, lecture en bas (px) */11 listHeight: number12}13export type DeviceTheme = "system" | "light" | "dark" | "midnight"14export interface DevicePrefs {15 /** thème choisi sur cet appareil ; null = celui du compte (jamais choisi ici) */16 theme: DeviceTheme | null17 layout: Layout18 paneWidths: PaneWidths19 sidebarCollapsed: boolean20 /** null = suivre le réglage du système (prefers-reduced-transparency) */21 reduceTransparency: boolean | null22 /** dossiers personnels repliés dans l'arborescence */23 foldersCollapsed: string[]24 recentSearches: string[]25}2627export const PANE_LIMITS = {28 sidebar: { min: 200, max: 360, initial: 264 },29 list: { min: 300, max: 560, initial: 400 },30 listHeight: { min: 180, max: 720, initial: 340 },31} as const32export type PaneKey = keyof typeof PANE_LIMITS3334export const DEVICE_KEY = "spmail.device"35export const MAX_RECENT_SEARCHES = 83637export const defaultDevice = (): DevicePrefs => ({38 theme: null,39 layout: "right",40 paneWidths: { sidebar: PANE_LIMITS.sidebar.initial, list: PANE_LIMITS.list.initial, listHeight: PANE_LIMITS.listHeight.initial },41 sidebarCollapsed: false,42 reduceTransparency: null,43 foldersCollapsed: [],44 recentSearches: [],45})4647export function clampPane(key: PaneKey, value: unknown): number {48 const l = PANE_LIMITS[key]49 const n = typeof value === "number" && Number.isFinite(value) ? Math.round(value) : l.initial50 return Math.min(l.max, Math.max(l.min, n))51}5253export function normalizeDevice(raw: unknown): DevicePrefs {54 const d = defaultDevice()55 const r = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : {}56 const w = r.paneWidths && typeof r.paneWidths === "object" ? (r.paneWidths as Record<string, unknown>) : {}57 const strings = (v: unknown, max: number, len: number) => (Array.isArray(v) ? v.filter((x): x is string => typeof x === "string" && x.length > 0 && x.length <= len).slice(0, max) : [])58 return {59 theme: r.theme === "system" || r.theme === "light" || r.theme === "dark" || r.theme === "midnight" ? r.theme : null,60 layout: r.layout === "below" || r.layout === "none" ? r.layout : d.layout,61 paneWidths: { sidebar: clampPane("sidebar", w.sidebar), list: clampPane("list", w.list), listHeight: clampPane("listHeight", w.listHeight) },62 sidebarCollapsed: r.sidebarCollapsed === true,63 reduceTransparency: typeof r.reduceTransparency === "boolean" ? r.reduceTransparency : null,64 foldersCollapsed: strings(r.foldersCollapsed, 200, 64),65 recentSearches: strings(r.recentSearches, MAX_RECENT_SEARCHES, 300),66 }67}6869/** Ajoute une recherche en tête des recherches récentes (sans doublon, casse ignorée). */70export function pushRecentSearch(list: readonly string[], q: string): string[] {71 const query = q.trim()72 if (!query) return [...list]73 return [query, ...list.filter((x) => x.toLowerCase() !== query.toLowerCase())].slice(0, MAX_RECENT_SEARCHES)74}7576export function readDevice(): DevicePrefs {77 try {78 return normalizeDevice(JSON.parse(localStorage.getItem(DEVICE_KEY) ?? "{}"))79 } catch {80 return defaultDevice()81 }82}83export function writeDevice(d: DevicePrefs) {84 try {85 localStorage.setItem(DEVICE_KEY, JSON.stringify(d))86 } catch {87 /* stockage indisponible : les réglages durent le temps de la visite */88 }89}90