import React, { useEffect, useRef, useState, type ReactNode } from 'react' import { Paintbrush, Eye, LayoutDashboard, Sun, Moon, Monitor, RotateCcw } from 'lucide-react' import { useTranslation } from '../../i18n' import { useSettingsStore } from '../../store/settingsStore' import { useToast } from '../shared/Toast' import { DialogSection, fs } from '../shared/DialogShell' import { Segmented } from '../shared/dialogParts' import Section from './Section' import ToggleSwitch from './ToggleSwitch' import { SETTINGS_BUTTON, SettingRow, SettingRows, StatusPill } from './settingsKit' import { applyAppearance } from '../../theme/applyAppearance' import { APPEARANCE_SCHEMES, CUSTOM_ACCENT_PRESETS } from '../../theme/schemes' import { DEFAULT_APPEARANCE, normalizeAppearance, APPEARANCE_SCALE_MIN, APPEARANCE_SCALE_MAX, type AppearanceConfig, } from '@trek/shared' // ── WCAG contrast helpers (for the custom-accent legibility hint) ──────────── function channelLum(v: number): number { return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4) } function relLuminance(hex: string): number { const c = hex.replace('#', '') const full = c.length === 3 ? c.split('').map((x) => x + x).join('') : c const r = channelLum(Number.parseInt(full.slice(0, 2), 16) / 255) const g = channelLum(Number.parseInt(full.slice(2, 4), 16) / 255) const b = channelLum(Number.parseInt(full.slice(4, 6), 16) / 255) return 0.2126 * r + 0.7152 * g + 0.0722 * b } function contrastRatio(a: string, b: string): number { const la = relLuminance(a) const lb = relLuminance(b) const [hi, lo] = la > lb ? [la, lb] : [lb, la] return (hi + 0.05) / (lo + 0.05) } const isHex = (v: string) => /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(v) type DesktopWidgetKey = keyof AppearanceConfig['dashboard']['desktop'] type MobileWidgetKey = keyof AppearanceConfig['dashboard']['mobile'] const WIDGET_LABELS: Record = { sidebar: 'Right sidebar', currency: 'Currency', collections: 'Collections', timezones: 'Timezones', upcomingReservations: 'Upcoming reservations', atlas: 'Atlas / countries', tripsTotal: 'Trips total', daysTraveled: 'Days traveled', distanceFlown: 'Distance flown', } // Grouped by where the widgets actually sit on the dashboard. The right sidebar // has a master toggle (off → no sidebar, layout centers); its individual // widgets only matter while the sidebar is shown. const DESKTOP_GROUPS: { id: string; fallback: string; master?: DesktopWidgetKey; keys: DesktopWidgetKey[] }[] = [ { id: 'belowHero', fallback: 'Below the hero', keys: ['atlas', 'tripsTotal', 'daysTraveled', 'distanceFlown'] }, { id: 'rightSidebar', fallback: 'Right sidebar', master: 'sidebar', keys: ['currency', 'collections', 'timezones', 'upcomingReservations'] }, ] const MOBILE_GROUPS: { id: string; fallback: string; keys: MobileWidgetKey[] }[] = [ { id: 'belowHero', fallback: 'Below the hero', keys: ['tripsTotal', 'daysTraveled'] }, { id: 'bottomOfPage', fallback: 'Bottom of page', keys: ['currency', 'collections', 'timezones', 'upcomingReservations'] }, ] /** A tile of the scheme grid: a colour dot and a name, raised and outlined while chosen. */ const SWATCH = 'flex min-w-0 items-center gap-2 rounded-[12px] border bg-surface-card px-3 py-2.5 text-left font-medium text-content transition-colors' const swatchLook = (active: boolean) => active ? 'border-[color:var(--text-primary)] shadow-sm' : 'border-edge hover:border-content-faint' /** The round colour sample in a swatch tile. */ const DOT = 'h-4 w-4 flex-none rounded-full shadow-[inset_0_0_0_1px_var(--border-faint)]' export default function AppearanceSettingsTab(): React.ReactElement { const { settings, updateSetting } = useSettingsStore() const { t } = useTranslation() const toast = useToast() const tr = (key: string, fallback: string) => t(key) || fallback const [cfg, setCfg] = useState(() => normalizeAppearance(settings.appearance)) const persistTimer = useRef(undefined) // What the pending timer would have written, so leaving the tab inside the // debounce window still saves instead of silently dropping the change. const pendingWrite = useRef(null) // Re-sync when settings change elsewhere (e.g. server reconcile / another tab). useEffect(() => { setCfg(normalizeAppearance(settings.appearance)) }, [settings.appearance]) // Flush any pending persist on unmount. useEffect(() => () => { if (!persistTimer.current) return window.clearTimeout(persistTimer.current) // The component is gone, so a failure has nowhere to be shown. if (pendingWrite.current) updateSetting('appearance', pendingWrite.current).catch(() => {}) }, [updateSetting]) const isDark = settings.dark_mode === true || settings.dark_mode === 'dark' || (settings.dark_mode === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) // Live preview now (DOM), persist after a short debounce (API). const update = (patch: Partial) => { const next = { ...cfg, ...patch } setCfg(next) applyAppearance({ darkMode: settings.dark_mode, appearance: next, isSharedPage: false }) if (persistTimer.current) window.clearTimeout(persistTimer.current) pendingWrite.current = next persistTimer.current = window.setTimeout(() => { pendingWrite.current = null updateSetting('appearance', next).catch((e: unknown) => toast.error(e instanceof Error ? e.message : t('common.error')) ) }, 350) } const setMode = async (mode: string) => { try { await updateSetting('dark_mode', mode) } catch (e: unknown) { toast.error(e instanceof Error ? e.message : t('common.error')) } } const setWidget = (device: 'desktop' | 'mobile', key: string, on: boolean) => { update({ dashboard: { ...cfg.dashboard, [device]: { ...cfg.dashboard[device], [key]: on }, }, }) } const resetAll = () => update({ ...DEFAULT_APPEARANCE }) const accentLight = cfg.accent?.light ?? '#4f46e5' const accentDark = cfg.accent?.dark ?? '#6366f1' const customRatio = contrastRatio(isDark ? accentDark : accentLight, '#ffffff') // The stored mode, read the way the old boolean values meant it. const cur = settings.dark_mode const mode = cur === true ? 'dark' : cur === false ? 'light' : String(cur ?? '') return ( <> {/* ── Theme ───────────────────────────────────────────────── */}
{/* Color mode */} }, { value: 'dark', label: tr('settings.dark', 'Dark'), icon: }, { value: 'auto', icon: , label: ( <> {tr('settings.auto', 'Auto')} Auto ), }, ]} /> } /> {/* Color scheme swatches */}
{APPEARANCE_SCHEMES.map((s) => { const active = cfg.schemeId === s.id const dot = isDark ? s.swatch.dark : s.swatch.light return ( ) })} {/* Custom */}
{/* Custom accent picker */} {cfg.schemeId === 'custom' && (
{CUSTOM_ACCENT_PRESETS.map((c) => { const picked = accentLight === c && accentDark === c return (
update({ accent: { light: v, dark: accentDark } })} /> update({ accent: { light: accentLight, dark: v } })} /> = 4.5 ? 'success' : 'warning'}> {customRatio >= 4.5 ? `${tr('settings.appearance.contrastOk', 'Good contrast')} (${customRatio.toFixed(1)}:1)` : `${tr('settings.appearance.contrastLow', 'Low contrast')} (${customRatio.toFixed(1)}:1)`}
)}
{/* ── Readability ─────────────────────────────────────────── */}
{tr('settings.appearance.experimental', 'Experimental')}} > update({ transparency: !cfg.transparency })} /> update({ reduceMotion: !cfg.reduceMotion })} /> {/* Density */} update({ density: value })} options={[ { value: 'comfortable', label: tr('settings.appearance.comfortable', 'Comfortable') }, { value: 'compact', label: tr('settings.appearance.compact', 'Compact') }, ]} /> } /> {/* Text size — global, plus an always-visible row per size class with a live sample and an example of what each size affects. */} update({ fontScale: v })} />
update({ typeScale: { ...cfg.typeScale, title: v } })} /> update({ typeScale: { ...cfg.typeScale, subtitle: v } })} /> update({ typeScale: { ...cfg.typeScale, body: v } })} /> update({ typeScale: { ...cfg.typeScale, caption: v } })} />
{/* ── Dashboard widgets ───────────────────────────────────── */}
{DESKTOP_GROUPS.map((g) => { const masterOn = g.master ? cfg.dashboard.desktop[g.master] : true const rows = g.keys.map((k) => ( setWidget('desktop', k, !cfg.dashboard.desktop[k])} /> )) if (!g.master) { return {rows} } return ( setWidget('desktop', g.master as string, !masterOn)} label={tr(`settings.appearance.widget.${g.master}`, WIDGET_LABELS[g.master])} />} > {/* Its widgets only matter while the column is shown: dimmed, not hidden, while it is off. */} {rows} ) })}
{MOBILE_GROUPS.map((g) => ( {g.keys.map((k) => ( setWidget('mobile', k, !cfg.dashboard.mobile[k])} /> ))} ))}
) } function schemeFallback(id: string): string { const map: Record = { default: 'Default', highContrast: 'High contrast', indigo: 'Indigo', teal: 'Teal', rose: 'Rose', amber: 'Amber', violet: 'Violet', } return map[id] || id } function ToggleRow({ label, hint, on, onToggle }: { label: string; hint?: string; on: boolean; onToggle: () => void }) { return } /> } /** Where a set of widgets sits on the dashboard: a caption over its box of switches. */ function WidgetGroup({ caption, children }: { caption: string; children: ReactNode }) { return (
{caption}
{children}
) } /** A colour input with its name, in the box look of the fields. */ function ColorField({ label, value, onChange }: { label: string; value: string; onChange: (v: string) => void }) { return ( ) } const fill = (value: number) => ({ '--fill': `${((value - APPEARANCE_SCALE_MIN) / (APPEARANCE_SCALE_MAX - APPEARANCE_SCALE_MIN)) * 100}%` } as React.CSSProperties) /** A percentage in the corner of a slider. */ function Percent({ value }: { value: number }) { return {Math.round(value * 100)}% } function SliderRow({ label, value, onChange }: { label: string; value: number; onChange: (v: number) => void }) { return (
{label}
onChange(Number(e.target.value))} className="trek-range" style={fill(value)} />
) } function SizeRow({ sampleClass, name, example, sample, value, onChange }: { sampleClass: string; name: string; example: string; sample: string; value: number; onChange: (v: number) => void }) { return (
{sample}
{name} · {example}
onChange(Number(e.target.value))} className="trek-range" style={fill(value)} />
) }