/* ============================================================ SELECT PAGE — Universal full-screen option picker Design: iOS Settings style selection list Usage: Navigate to /select with state object State params: title - Page title shown in header options - Array of option strings selected - Currently selected index (number) back - Route to navigate back to after selection key - Identifier for which setting this controls ============================================================ */ import { useLocation } from "wouter"; import { ChevronLeft, Check } from "lucide-react"; import { useDevice } from "@/contexts/DeviceContext"; import { useEffect, useState } from "react"; /* ── Key → API field mapping ── */ const KEY_TO_SETTING: Record = { effect_value: "effect_value", crossfeed_value: "crossfeed_value", language: "language", analogGain: "analogGain", soundStep: "soundStep", bootSound: "bootSound", filterCharacteristic: "pcm", mutePolar: "hdmimutepolar", IISMode: "hdmiType", xlr: "xlr", dacGain: "dacGain", dacImpedance: "dacImpedance", vu: "vu", input: "input", output: "output", screenLight: "screenLight", screenOff: "screenOff", buttonLight: "buttonLight", autoHome: "autoHome", sleep: "sleep", headphone: "peqSelect", }; // Global state for select page (to avoid URL params) export let selectPageState: { title: string; options: string[]; selected: number; back: string; key: string; } | null = null; // Last select result for pages that need local state update export let selectPageResult: { key: string; selected: number } | null = null; export function consumeSelectPageResult() { const result = selectPageResult; selectPageResult = null; return result; } // Helper function to set select page state export function navigateToSelect(title: string, options: string[], selected: number, back: string, key: string) { selectPageState = { title, options, selected, back, key }; } export default function SelectPage() { const [location, setLocation] = useLocation(); const { updateSetting } = useDevice(); const [state, setState] = useState<{ title: string; options: string[]; selected: number; back: string; key: string; } | null>(() => selectPageState); const [saving, setSaving] = useState(false); // Re-read global state whenever we enter /select (avoids stale key/options after remount) useEffect(() => { if (location === "/select" && selectPageState) { setState({ ...selectPageState }); } }, [location]); if (!state) { return null; } const { title, options, selected: selectedIdx, back, key } = state; const handleSelect = async (idx: number) => { if (saving) return; selectPageResult = { key, selected: idx }; const apiField = KEY_TO_SETTING[key]; if (apiField) { setSaving(true); try { await updateSetting({ [apiField]: idx }); } finally { setSaving(false); } } selectPageState = null; setLocation(back); }; return (
{/* ── Header ── */}

{title}

{/* ── Options list ── */}
{options.map((opt, idx) => { const isSelected = idx === selectedIdx; return ( ); })}
); }