import { useState } from 'react' import { useTranslation } from 'react-i18next' import { Compass, Palette, Save, Check, Cloud } from 'lucide-react' import ThemedSelect from './ThemedSelect.tsx' import PushNotificationSettings from './PushNotificationSettings.tsx' import PwaInstallPrompt from './PwaInstallPrompt.tsx' import { notifyAppearanceChanged } from '../services/appearance.js' import { saveAppearancePrefsToServer } from '../services/appearancePrefs.js' import { useAppTour } from '../context/AppTourContext.tsx' import { getColorSchemePreference, getOwmApiKey, getThemePreference, setColorSchemePreference, setOwmApiKey, setThemePreference } from '../services/userPreferences.js' interface UserProfilePreferencesProps { userId: string } export default function UserProfilePreferences({ userId }: UserProfilePreferencesProps) { const { t } = useTranslation() const { restartTour } = useAppTour() const [apiKey, setApiKey] = useState(() => getOwmApiKey(userId)) const [theme, setTheme] = useState(() => getThemePreference(userId)) const [colorScheme, setColorScheme] = useState(() => getColorSchemePreference(userId)) const [savingOwm, setSavingOwm] = useState(false) const [owmSaved, setOwmSaved] = useState(false) const persistAppearance = (nextTheme: string, nextColorScheme: string) => { setThemePreference(userId, nextTheme) setColorSchemePreference(userId, nextColorScheme) notifyAppearanceChanged() void saveAppearancePrefsToServer(nextTheme, nextColorScheme).catch((err) => { console.warn('Failed to save appearance prefs to server:', err) }) } const handleThemeChange = (nextTheme: string) => { setTheme(nextTheme) persistAppearance(nextTheme, colorScheme) } const handleColorSchemeChange = (nextColorScheme: string) => { setColorScheme(nextColorScheme) persistAppearance(theme, nextColorScheme) } const handleSaveOwm = (e: React.FormEvent) => { e.preventDefault() setSavingOwm(true) setOwmSaved(false) setOwmApiKey(userId, apiKey) setSavingOwm(false) setOwmSaved(true) window.setTimeout(() => setOwmSaved(false), 3000) } return ( <> {t('profile.appearance_title')} {t('profile.appearance_desc')} {t('profile.theme_label')} {t('profile.color_scheme_label')} {t('profile.tour_title')} {t('profile.tour_desc')} restartTour()}> {t('profile.tour_restart')} {t('profile.integrations_title')} {t('profile.owm_help')} {t('profile.owm_key')} setApiKey(e.target.value)} disabled={savingOwm} autoComplete="off" /> {owmSaved && ( {t('profile.prefs_saved')} )} {savingOwm ? t('profile.prefs_saving') : t('profile.prefs_save')} > ) }
{t('profile.appearance_desc')}
{t('profile.tour_desc')}
{t('profile.owm_help')}