"use client"; import {useEffect,useState} from 'react'; import {THEME_STORAGE_KEY,THEME_CHANGE_EVENT,THEME_OPTIONS,validTheme} from '../../../shared/config'; function readPreference(){try{const value=localStorage.getItem(THEME_STORAGE_KEY);return validTheme(value)?value:'system';}catch{return 'system';}} export function ThemeControl(){ const [theme,setTheme]=useState('system');const [ready,setReady]=useState(false); useEffect(()=>{ const sync=event=>{const value=validTheme(event?.detail)?event.detail:readPreference();setTheme(value);setReady(true);document.documentElement.dataset.theme=value;}; const onStorage=event=>{if(event.key===THEME_STORAGE_KEY||event.key===null)sync();}; sync();window.addEventListener(THEME_CHANGE_EVENT,sync);window.addEventListener('storage',onStorage); return()=>{window.removeEventListener(THEME_CHANGE_EVENT,sync);window.removeEventListener('storage',onStorage);}; },[]); function change(value){ if(!validTheme(value))return; setTheme(value);setReady(true);document.documentElement.dataset.theme=value; try{localStorage.setItem(THEME_STORAGE_KEY,value);}catch{/* Preference remains usable for this page without storage. */} window.dispatchEvent(new CustomEvent(THEME_CHANGE_EVENT,{detail:value})); } const index=THEME_OPTIONS.findIndex(option=>option.value===theme); const next=THEME_OPTIONS[(index+1)%THEME_OPTIONS.length]; const name=`Тема: ${THEME_OPTIONS[index].label}. Переключить на: ${next.label}`; return ; }