import { useRef, useState } from 'react'; import { useGameStore } from '@/store'; import { ConfirmModal } from '@/components/common/ConfirmModal'; import { getTokenPayload, isRegistered, isAdmin } from '@/lib/api'; export function SettingsPage() { const settings = useGameStore((s) => s.meta.settings); const companyName = useGameStore((s) => s.meta.companyName); const updateState = useGameStore((s) => s.updateState); const addNotification = useGameStore((s) => s.addNotification); const fileInputRef = useRef(null); const [showResetConfirm, setShowResetConfirm] = useState(false); const [importData, setImportData] = useState<{ data: unknown; name: string } | null>(null); const toggleSound = () => { updateState({ meta: { ...useGameStore.getState().meta, settings: { ...settings, soundEnabled: !settings.soundEnabled } } }); }; const setMusicVolume = (v: number) => { updateState({ meta: { ...useGameStore.getState().meta, settings: { ...settings, musicVolume: v } } }); }; const handleReset = () => { localStorage.removeItem('token-empire-save'); window.location.reload(); }; const handleExport = () => { const state = useGameStore.getState(); const { activePage, notifications, ...gameState } = state; const blob = new Blob([JSON.stringify(gameState)], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `token-empire-${companyName.replace(/\s+/g, '-').toLowerCase()}.json`; a.click(); URL.revokeObjectURL(url); }; const handleImport = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; const reader = new FileReader(); reader.onload = (event) => { try { const data = JSON.parse(event.target?.result as string); if (!data.meta?.companyName) { addNotification({ title: 'Import Failed', message: 'Invalid save file: missing company data.', type: 'danger', tick: useGameStore.getState().meta.tickCount }); return; } setImportData({ data, name: data.meta.companyName }); } catch { addNotification({ title: 'Import Failed', message: 'Could not read save file. Make sure it is a valid Token Empire export.', type: 'danger', tick: useGameStore.getState().meta.tickCount }); } }; reader.readAsText(file); if (fileInputRef.current) fileInputRef.current.value = ''; }; const confirmImport = () => { if (!importData) return; localStorage.setItem('token-empire-save', JSON.stringify({ state: importData.data })); window.location.reload(); }; const payload = getTokenPayload(); const registered = isRegistered(); const admin = isAdmin(); return (

Settings

Account

{registered ? (
{payload?.email && (
Email
{payload.email}
)} {payload?.username && (
Username
{payload.username}
)} {admin && (
Admin
)}
) : (
Playing as guest.
)}

Game

Sound Effects
Play UI sounds and notifications
Music Volume
Background music level
setMusicVolume(Number(e.target.value) / 100)} className="w-32 accent-accent" /> {Math.round(settings.musicVolume * 100)}%

Save Data

{showResetConfirm && ( setShowResetConfirm(false)} /> )} {importData && ( setImportData(null)} /> )}
); } function ToggleSwitch({ checked, onChange }: { checked: boolean; onChange: () => void }) { return ( ); }