"use client"; import { type ReactNode, useEffect, useState } from "react"; const PLAYER_NAME_STORAGE_KEY = "infiplot:playerName"; const VISION_CLICK_STORAGE_KEY = "infiplot:visionClick"; export function readStoredPlayerName(): string { try { return localStorage.getItem(PLAYER_NAME_STORAGE_KEY) ?? ""; } catch { return ""; } } export function writeStoredPlayerName(name: string): void { try { if (name) { localStorage.setItem(PLAYER_NAME_STORAGE_KEY, name); } else { localStorage.removeItem(PLAYER_NAME_STORAGE_KEY); } } catch { /* ignore */ } } export function readStoredVisionClick(): boolean { try { return localStorage.getItem(VISION_CLICK_STORAGE_KEY) !== "0"; } catch { return true; } } export function SettingsModal({ initialVisionClickEnabled = true, onClose, onSaved, footerNote, }: { initialVisionClickEnabled?: boolean; onClose: () => void; onSaved: (settings: { playerName: string; visionClickEnabled: boolean }) => void; footerNote?: ReactNode; }) { const [playerName, setPlayerName] = useState(() => readStoredPlayerName()); const [visionClick, setVisionClick] = useState(initialVisionClickEnabled); const [shown, setShown] = useState(false); useEffect(() => { const id = requestAnimationFrame(() => setShown(true)); return () => cancelAnimationFrame(id); }, []); const close = () => { setShown(false); setTimeout(onClose, 280); }; const save = () => { const name = playerName.trim(); writeStoredPlayerName(name); try { localStorage.setItem(VISION_CLICK_STORAGE_KEY, visionClick ? "1" : "0"); } catch { /* ignore */ } onSaved({ playerName: name, visionClickEnabled: visionClick }); close(); }; const clearAll = () => { writeStoredPlayerName(""); try { localStorage.removeItem(VISION_CLICK_STORAGE_KEY); } catch { /* ignore */ } onSaved({ playerName: "", visionClickEnabled: true }); close(); }; const hasAnySetting = readStoredPlayerName().length > 0; return (
{footerNote}