"use client"; /** * /settings/chat — chat preferences. * * Each control saves on change rather than behind a Save button: these are * independent switches, and a half-applied form is worse than an immediate * write that can be toggled back. */ import { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { toast } from "sonner"; import { SettingsShell, Section } from "@/components/settings/settings-ui"; import { SettingRow, ToggleSwitch } from "@/components/settings/setting-row"; import { chat, type ChatSettings } from "@/lib/chat"; import { cn } from "@/lib/utils"; const AGE_CHOICES = [1, 3, 6, 12] as const; export default function ChatSettingsPage() { const [settings, setSettings] = useState(null); const [state, setState] = useState<"loading" | "ready" | "error">("loading"); const [busy, setBusy] = useState(false); useEffect(() => { let cancelled = false; chat .settings() .then((s) => { if (cancelled) return; setSettings(s); setState("ready"); }) .catch(() => !cancelled && setState("error")); return () => { cancelled = true; }; }, []); const save = useCallback( async (patch: Partial>) => { setBusy(true); try { setSettings(await chat.updateSettings(patch)); } catch (err) { // The BFF explains *why* — notably "add your gender in profile // settings" — so surface its message rather than a generic failure. toast.error( err instanceof Error && err.message ? err.message : "Could not save that setting", ); } finally { setBusy(false); } }, [], ); return ( {state === "error" ? (

Could not load your chat settings.

) : state === "loading" || !settings ? (

Loading…

) : (
save({ read_receipts: !settings.read_receipts }) } /> } />
save({ who_can_message: "anyone" })} title="Anyone" description="No restriction." /> save({ who_can_message: "friends" })} title="Your friends" description="Only people you follow, or who follow you." /> save({ who_can_message: "account_age" })} title="Established accounts" description="Only accounts older than a chosen age." > {settings.who_can_message === "account_age" && (
{AGE_CHOICES.map((months) => ( ))}
)}
save({ who_can_message: "same_gender" })} title="Only accounts of your gender" description={ settings.gender_set ? "Only accounts whose gender matches yours." : undefined } > {!settings.gender_set && (

Add your gender in{" "} profile settings {" "} to use this option.

)}
save({ train_kautilya: !settings.train_kautilya }) } /> } />
)}
); } function Choice({ active, disabled, onClick, title, description, children, }: { active: boolean; disabled?: boolean; onClick: () => void; title: string; description?: string; children?: React.ReactNode; }) { return (
{children}
); }