Files
superlist/apps/web/src/routes/_authenticated/settings.tsx
Jose Selesan d5feb65d62 feat(web): show the build's commit and date in settings
Lets you tell whether an installed PWA picked up a new release. The
commit is read from .git at build time (the Docker build stage has no
git binary), so .dockerignore now lets HEAD and the refs through.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 20:00:33 -03:00

160 lines
5.0 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
import { MonitorIcon, MoonIcon, SunIcon } from 'lucide-react'
import { useForm } from 'react-hook-form'
import { toast } from 'sonner'
import { z } from 'zod'
import { type Theme, useTheme } from '@/components/theme-provider'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from '@/components/ui/field'
import { Input } from '@/components/ui/input'
import { appVersion } from '@/lib/app-version'
import { authClient, signOut, useSession } from '@/lib/auth-client'
export const Route = createFileRoute('/_authenticated/settings')({
component: SettingsPage,
})
function SettingsPage() {
const navigate = useNavigate()
return (
<div className="mx-auto grid w-full max-w-2xl gap-4 md:gap-6">
<ProfileCard />
<AppearanceCard />
<Card>
<CardHeader>
<CardTitle>Sesión</CardTitle>
<CardDescription>Cierra sesión en este dispositivo.</CardDescription>
</CardHeader>
<CardContent>
<Button
variant="outline"
onClick={async () => {
await signOut()
await navigate({ to: '/sign-in' })
}}
>
Cerrar sesión
</Button>
</CardContent>
</Card>
<AppVersion />
</div>
)
}
const builtAtFormat = new Intl.DateTimeFormat('es', { dateStyle: 'short', timeStyle: 'short' })
function AppVersion() {
return (
<p className="text-center text-muted-foreground text-xs">
Versión <span className="font-mono">{appVersion.commit}</span> ·{' '}
{builtAtFormat.format(appVersion.builtAt)}
</p>
)
}
const profileSchema = z.object({
name: z.string().trim().min(1, 'El nombre es obligatorio').max(80, 'Máximo 80 caracteres'),
})
type ProfileValues = z.infer<typeof profileSchema>
function ProfileCard() {
const { data: session } = useSession()
const form = useForm<ProfileValues>({
resolver: zodResolver(profileSchema),
defaultValues: { name: session?.user.name ?? '' },
})
const onSubmit = form.handleSubmit(async (values) => {
const { error } = await authClient.updateUser({ name: values.name })
if (error) {
toast.error(error.message ?? 'No pudimos guardar tu nombre.')
return
}
form.reset(values)
toast.success('Nombre actualizado.')
})
return (
<Card>
<CardHeader>
<CardTitle>Perfil</CardTitle>
<CardDescription>Así te ven las personas con las que compartes listas.</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={onSubmit}>
<FieldGroup>
<Field data-invalid={!!form.formState.errors.name}>
<FieldLabel htmlFor="name">Nombre</FieldLabel>
<Input id="name" autoComplete="name" {...form.register('name')} />
<FieldError errors={[form.formState.errors.name]} />
</Field>
<Field>
<FieldLabel htmlFor="email">Correo electrónico</FieldLabel>
<Input id="email" type="email" value={session?.user.email ?? ''} readOnly disabled />
<FieldDescription>
Lo usas para iniciar sesión y recibir invitaciones.
</FieldDescription>
</Field>
<Button
type="submit"
className="w-full sm:w-auto sm:self-start"
disabled={!form.formState.isDirty || form.formState.isSubmitting}
>
{form.formState.isSubmitting ? 'Guardando…' : 'Guardar cambios'}
</Button>
</FieldGroup>
</form>
</CardContent>
</Card>
)
}
const THEME_OPTIONS: { value: Theme; label: string; icon: typeof SunIcon }[] = [
{ value: 'light', label: 'Claro', icon: SunIcon },
{ value: 'dark', label: 'Oscuro', icon: MoonIcon },
{ value: 'system', label: 'Sistema', icon: MonitorIcon },
]
function AppearanceCard() {
const { theme, setTheme } = useTheme()
return (
<Card>
<CardHeader>
<CardTitle>Apariencia</CardTitle>
<CardDescription>Elige el tema de la app en este dispositivo.</CardDescription>
</CardHeader>
<CardContent>
<fieldset className="grid grid-cols-3 gap-2">
<legend className="sr-only">Tema</legend>
{THEME_OPTIONS.map((option) => (
<Button
key={option.value}
type="button"
variant={theme === option.value ? 'default' : 'outline'}
aria-pressed={theme === option.value}
onClick={() => setTheme(option.value)}
className="h-auto flex-col gap-1.5 py-3"
>
<option.icon className="size-5" />
{option.label}
</Button>
))}
</fieldset>
</CardContent>
</Card>
)
}