import { useEffect, useMemo, useState } from "react"; import { createPortal } from "react-dom"; import { listBlockedUsers, updateMyProfile } from "../api/users"; import type { AuthUser } from "../chat/types"; import { useAuthStore } from "../store/authStore"; import { AppPreferences, getAppPreferences, updateAppPreferences } from "../utils/preferences"; type SettingsPage = "main" | "general" | "notifications" | "privacy"; interface Props { open: boolean; onClose: () => void; } export function SettingsPanel({ open, onClose }: Props) { const me = useAuthStore((s) => s.me); const [page, setPage] = useState("main"); const [prefs, setPrefs] = useState(() => getAppPreferences()); const [blockedCount, setBlockedCount] = useState(0); const [savingPrivacy, setSavingPrivacy] = useState(false); const [allowPrivateMessages, setAllowPrivateMessages] = useState(true); const [profileDraft, setProfileDraft] = useState({ name: "", username: "", bio: "", avatarUrl: "", }); useEffect(() => { if (!me) { return; } setAllowPrivateMessages(me.allow_private_messages); setProfileDraft({ name: me.name || "", username: me.username || "", bio: me.bio || "", avatarUrl: me.avatar_url || "", }); }, [me]); useEffect(() => { if (!open) { return; } setPrefs(getAppPreferences()); }, [open]); useEffect(() => { if (!open || page !== "privacy") { return; } let cancelled = false; void (async () => { try { const blocked = await listBlockedUsers(); if (!cancelled) { setBlockedCount(blocked.length); } } catch { if (!cancelled) { setBlockedCount(0); } } })(); return () => { cancelled = true; }; }, [open, page]); const title = useMemo(() => { if (page === "general") return "General"; if (page === "notifications") return "Notifications"; if (page === "privacy") return "Privacy and Security"; return "Settings"; }, [page]); if (!open || !me) { return null; } function updatePrefs(patch: Partial) { setPrefs(updateAppPreferences(patch)); } const notificationStatus = "Notification" in window ? Notification.permission : "denied"; return createPortal(
, document.body ); } function SettingsRow({ label, value, onClick }: { label: string; value: string; onClick: () => void }) { return ( ); } function SettingsTextRow({ label, value }: { label: string; value: string }) { return (

{label}

{value}

); } function RadioOption({ checked, label, onChange }: { checked: boolean; label: string; onChange: () => void }) { return ( ); } function CheckboxOption({ checked, label, onChange, disabled, }: { checked: boolean; label: string; onChange: (checked: boolean) => void; disabled?: boolean; }) { return ( ); }