"use client"; import { useI18n } from "@/components/I18nProvider"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { UserRound, LockKeyhole, Mail } from "lucide-react"; import { api } from "./StartGame"; import { PasswordForm } from "./PasswordForm"; import { displayNameSchema } from "@/shared/account"; export function AccountSettings({ email, displayName, passwordUpdated = false, }: { email: string; displayName: string; passwordUpdated?: boolean; }) { const { t } = useI18n(); const [name, setName] = useState(displayName), [message, setMessage] = useState(""), [error, setError] = useState(""), [busy, setBusy] = useState(false); const [recoveryMessage, setRecoveryMessage] = useState(""), [recoveryError, setRecoveryError] = useState(""), [sending, setSending] = useState(false); const router = useRouter(); async function save(event: React.FormEvent) { event.preventDefault(); setError(""); setMessage(""); const valid = displayNameSchema.safeParse(name); if (!valid.success) { setError(valid.error.issues[0].message); return; } setBusy(true); try { const data = await api<{ profile: { display_name: string } }>( "profile", { displayName: valid.data }, "PATCH", ); setName(data.profile.display_name); setMessage("Display name saved."); router.refresh(); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } async function recover() { setSending(true); setRecoveryError(""); try { const data = await api<{ message: string }>("auth", { action: "recover", email, }); setRecoveryMessage(data.message); } catch (e) { setRecoveryError((e as Error).message); } finally { setSending(false); } } return (

{t("Your public profile.")}

{t("A name for the maps and places you share.")}

{t("Email address")} {email} {t("Used for sign-in and recovery. Only you can see it.")}
{error && (

{t(error)}

)} {message && (

{t(message)}

)}

{t("Password & security.")}

{t("Keep your account yours.")}

{passwordUpdated && (

{t("Your password has been updated.")}

)}

{t("Forgot your password?")}

{t( "Set your first password, or recover a forgotten one, using a link sent to your account email.", )}

{recoveryMessage && (

{recoveryMessage}

)} {recoveryError && (

{recoveryError}

)}
); }