import { zodResolver } from '@hookform/resolvers/zod' import { createFileRoute, useNavigate } from '@tanstack/react-router' import { useForm } from 'react-hook-form' import { useTranslation } from 'react-i18next' import { z } from 'zod' import { changePassword, changeUserName, clearSession, deleteAccount } from '@/features/auth/api' import { useAuthStore } from '@/features/auth/store' import { useRequireAuth } from '@/features/auth/guards' import { HttpError } from '@/shared/api/client' import { Button } from '@/shared/ui/button' import { Card, CardContent, CardHeader, CardTitle } from '@/shared/ui/card' import { Input } from '@/shared/ui/input' import { Label } from '@/shared/ui/label' import { toast } from '@/shared/ui/toast-store' export const Route = createFileRoute('/settings')({ component: SettingsPage }) const userNameSchema = z.object({ newUserName: z.string().min(3).max(64) }) const passwordSchema = z.object({ currentPassword: z.string().min(1), newPassword: z.string().min(8), }) function SettingsPage() { const { t } = useTranslation() const { isReady } = useRequireAuth() const navigate = useNavigate() const userNameForm = useForm>({ resolver: zodResolver(userNameSchema), }) const passwordForm = useForm>({ resolver: zodResolver(passwordSchema), }) if (!isReady) return null const onSaveUserName = async (values: z.infer) => { try { await changeUserName(values.newUserName) useAuthStore .getState() .setUser({ ...useAuthStore.getState().user!, userName: values.newUserName }) toast.success(t('settings.saved')) userNameForm.reset() } catch (error) { toast.error( error instanceof HttpError && error.status === 409 ? t('auth.userNameTaken') : t('common.error'), ) } } const onSavePassword = async (values: z.infer) => { try { await changePassword(values.currentPassword, values.newPassword) toast.success(t('settings.saved')) passwordForm.reset() } catch { toast.error(t('common.error')) } } const onDeleteAccount = async () => { if (!window.confirm(t('settings.deleteAccountConfirm'))) return try { await deleteAccount() clearSession() void navigate({ to: '/' }) } catch { toast.error(t('common.error')) } } return (

{t('settings.title')}

{t('settings.changeUserName')}
{t('settings.changePassword')}
{t('settings.dangerZone')}
) }