"use client"; import { useEffect, useState } from "react"; import { useQueryClient } from "@tanstack/react-query"; import { useRouter } from "next/navigation"; import { KeyRound, Save } from "lucide-react"; import { api } from "@/lib/client/api"; import { Button } from "@/components/ui/button"; import { Input, Label } from "@/components/ui/input"; import { Spinner } from "@/components/ui/primitives"; import { toast } from "@/components/ui/toast"; import type { SettingsProfile } from "./SettingsView"; export function ProfileSection({ profile, refresh }: { profile: SettingsProfile; refresh: () => void }) { const r = useRouter(); const qc = useQueryClient(); const [name, setName] = useState(profile.displayName); const [email, setEmail] = useState(profile.email ?? ""); const [saving, setSaving] = useState(false); useEffect(() => { setName(profile.displayName); setEmail(profile.email ?? ""); }, [profile.displayName, profile.email]); const [pw, setPw] = useState({ current: "", next: "", confirm: "" }); const [pwBusy, setPwBusy] = useState(false); const dirty = name.trim() !== profile.displayName || (email.trim() || "") !== (profile.email ?? ""); async function save() { setSaving(true); try { await api("/api/settings", { method: "PATCH", json: { displayName: name.trim(), email: email.trim() || "" } }); toast("Profile saved", { tone: "success" }); refresh(); qc.invalidateQueries({ queryKey: ["dashboard"] }); r.refresh(); } catch (e: any) { toast("Could not save", { description: e.message, tone: "danger" }); } finally { setSaving(false); } } async function changePw() { if (pw.next !== pw.confirm) { toast("Passwords do not match", { tone: "danger" }); return; } setPwBusy(true); try { await api("/api/settings/password", { method: "POST", json: { current: pw.current, next: pw.next } }); setPw({ current: "", next: "", confirm: "" }); toast("Password changed", { description: "Every other session has been signed out.", tone: "success" }); } catch (e: any) { toast("Password not changed", { description: e.message, tone: "danger" }); } finally { setPwBusy(false); } } return (
Signed in as @{profile.username} ยท member since {new Date(profile.createdAt).toLocaleDateString("en-CA", { dateStyle: "medium" })}
At least 12 characters with upper-case, lower-case letters and a digit. Other devices are signed out afterwards.