feat(app): identity screens and /app shell — auth store, login/signup/reset flows, dashboard, account

- state/authStore: session subscription (initialize + onAuthStateChange), profile + roles load, recovery flag, URL failure; services/localeSync: profiles.locale <-> localeStore
- routes: /login, /signup, /forgot-password, /reset-password, /auth/callback; guards RequireAuth (next=) and RedirectIfSignedIn; /app (dashboard), /app/account behind the guard
- shell: AppLayout (desktop sidebar, light top bar, mobile drawer), SidebarNav, UserBadge, SignOutButton
- account: personal / professional / preferences / security sections, one save each, Zod validation with i18n keys
- ui: Icon, FormField, Notice, ButtonLink (buttonClassName shared), PasswordField, OAuthButtons (config-driven)
- Home header: Mon espace / Se connecter when Supabase is configured; local tool routes untouched
- i18n: auth.*, shell.*, dashboard.*, account.*, home.space.* in fr + en
This commit is contained in:
bchanot committed 2026-10-11 23:29:13 +02:00
1 parent 0f7292f9b3
commit c517ea267b
46 files changed
+2798 -11

No files matched your search

@@ -0,0 +1,74 @@
import type { Profile } from '@faunosteo/supabase'
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
import Input from '@/components/ui/Input'
import SaveRow from './SaveRow'
import SectionCard from './SectionCard'
import { personalPatch, personalSchema, translateFieldError, type PersonalValues } from '@/account/validation'
import { useProfileForm } from '@/account/useProfileForm'
import { useT } from '@/i18n/useT'
function initialValues(profile: Profile): PersonalValues {
return {
display_name: profile.display_name,
first_name: profile.first_name ?? '',
last_name: profile.last_name ?? '',
phone: profile.phone ?? '',
}
}
export default function PersonalInfoForm({ profile }: { profile: Profile }) {
const t = useT()
const form = useProfileForm(initialValues(profile), personalSchema, personalPatch)
const err = (key: keyof PersonalValues) => translateFieldError(t, form.errors[key])
return (
<SectionCard
id="personal"
title={t('account.personal.title')}
description={t('account.personal.description')}
>
<form onSubmit={form.submit} noValidate className="space-y-4">
<FormField id="display_name" label={t('account.personal.display_name')} error={err('display_name')}>
<Input
id="display_name"
value={form.values.display_name}
onChange={form.field('display_name')}
autoComplete="nickname"
maxLength={80}
aria-invalid={Boolean(form.errors.display_name)}
aria-describedby={fieldDescribedBy('display_name', undefined, form.errors.display_name)}
required
/>
</FormField>
<div className="grid gap-4 sm:grid-cols-2">
<FormField id="first_name" label={t('account.personal.first_name')} optional={t('account.optional')} error={err('first_name')}>
<Input id="first_name" value={form.values.first_name} onChange={form.field('first_name')} autoComplete="given-name" maxLength={80} />
</FormField>
<FormField id="last_name" label={t('account.personal.last_name')} optional={t('account.optional')} error={err('last_name')}>
<Input id="last_name" value={form.values.last_name} onChange={form.field('last_name')} autoComplete="family-name" maxLength={80} />
</FormField>
</div>
<FormField
id="phone"
label={t('account.personal.phone')}
optional={t('account.optional')}
hint={t('account.personal.phone_hint')}
error={err('phone')}
>
<Input
id="phone"
type="tel"
inputMode="tel"
autoComplete="tel"
placeholder="+33612345678"
value={form.values.phone}
onChange={form.field('phone')}
aria-invalid={Boolean(form.errors.phone)}
aria-describedby={fieldDescribedBy('phone', 'hint', form.errors.phone)}
/>
</FormField>
<SaveRow state={form.state} />
</form>
</SectionCard>
)
}
@@ -0,0 +1,80 @@
import type { Profile } from '@faunosteo/supabase'
import FormField from '@/components/ui/FormField'
import Select from '@/components/ui/Select'
import SaveRow from './SaveRow'
import SectionCard from './SectionCard'
import {
preferencesPatch,
preferencesSchema,
translateFieldError,
} from '@/account/validation'
import { useProfileForm } from '@/account/useProfileForm'
import { useLocaleStore } from '@/state/localeStore'
import type { Locale } from '@/i18n/types'
import { useT } from '@/i18n/useT'
const LOCALES: { value: Locale; labelKey: string }[] = [
{ value: 'fr', labelKey: 'lang.switch_to_fr' },
{ value: 'en', labelKey: 'lang.switch_to_en' },
]
/** Browser list when available, else the current value alone. */
function timezoneOptions(current: string): string[] {
const intl = Intl as unknown as { supportedValuesOf?: (key: string) => string[] }
const list = intl.supportedValuesOf?.('timeZone') ?? []
return list.includes(current) ? list : [current, ...list]
}
/**
* Language is saved through the locale store (the sync writes profiles.locale)
* so switching here and in the header behave the same. Timezone saves directly.
*/
export default function PreferencesForm({ profile }: { profile: Profile }) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const setLocale = useLocaleStore((s) => s.setLocale)
const form = useProfileForm(
{ timezone: profile.timezone },
preferencesSchema,
preferencesPatch,
)
return (
<SectionCard
id="preferences"
title={t('account.preferences.title')}
description={t('account.preferences.description')}
>
<form onSubmit={form.submit} noValidate className="space-y-4">
<FormField id="locale" label={t('account.preferences.language')}>
<Select
id="locale"
value={locale}
onChange={(e) => setLocale(e.target.value as Locale)}
>
{LOCALES.map((item) => (
<option key={item.value} value={item.value}>
{t(item.labelKey)}
</option>
))}
</Select>
</FormField>
<FormField
id="timezone"
label={t('account.preferences.timezone')}
hint={t('account.preferences.timezone_hint')}
error={translateFieldError(t, form.errors.timezone)}
>
<Select id="timezone" value={form.values.timezone} onChange={form.field('timezone')}>
{timezoneOptions(form.values.timezone).map((tz) => (
<option key={tz} value={tz}>
{tz}
</option>
))}
</Select>
</FormField>
<SaveRow state={form.state} />
</form>
</SectionCard>
)
}
@@ -0,0 +1,85 @@
import type { Profile } from '@faunosteo/supabase'
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
import Input from '@/components/ui/Input'
import SaveRow from './SaveRow'
import SectionCard from './SectionCard'
import {
professionalPatch,
professionalSchema,
translateFieldError,
type ProfessionalValues,
} from '@/account/validation'
import { useProfileForm } from '@/account/useProfileForm'
import { useT } from '@/i18n/useT'
function initialValues(profile: Profile): ProfessionalValues {
return {
professional_title: profile.professional_title ?? '',
practice_name: profile.practice_name ?? '',
siret: profile.siret ?? '',
}
}
export default function ProfessionalInfoForm({ profile }: { profile: Profile }) {
const t = useT()
const form = useProfileForm(initialValues(profile), professionalSchema, professionalPatch)
const err = (key: keyof ProfessionalValues) => translateFieldError(t, form.errors[key])
return (
<SectionCard
id="professional"
title={t('account.professional.title')}
description={t('account.professional.description')}
>
<form onSubmit={form.submit} noValidate className="space-y-4">
<FormField
id="professional_title"
label={t('account.professional.professional_title')}
optional={t('account.optional')}
error={err('professional_title')}
>
<Input
id="professional_title"
value={form.values.professional_title}
onChange={form.field('professional_title')}
placeholder={t('account.professional.professional_title_placeholder')}
autoComplete="organization-title"
maxLength={80}
/>
</FormField>
<FormField
id="practice_name"
label={t('account.professional.practice_name')}
optional={t('account.optional')}
error={err('practice_name')}
>
<Input
id="practice_name"
value={form.values.practice_name}
onChange={form.field('practice_name')}
autoComplete="organization"
maxLength={120}
/>
</FormField>
<FormField
id="siret"
label={t('account.professional.siret')}
optional={t('account.optional')}
hint={t('account.professional.siret_hint')}
error={err('siret')}
>
<Input
id="siret"
inputMode="numeric"
value={form.values.siret}
onChange={form.field('siret')}
aria-invalid={Boolean(form.errors.siret)}
aria-describedby={fieldDescribedBy('siret', 'hint', form.errors.siret)}
className="tabular-nums"
/>
</FormField>
<SaveRow state={form.state} />
</form>
</SectionCard>
)
}
@@ -0,0 +1,19 @@
import Badge from '@/components/ui/Badge'
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
/** Platform roles, read-only. Nothing when the account has none. */
export default function RoleBadges() {
const roles = useAuthStore((s) => s.roles)
const t = useT()
if (roles.length === 0) return null
return (
<ul className="flex flex-wrap gap-2" aria-label={t('dashboard.account.roles')}>
{roles.map((role) => (
<li key={role}>
<Badge>{t(`shell.role.${role}`)}</Badge>
</li>
))}
</ul>
)
}
@@ -0,0 +1,20 @@
import Button from '@/components/ui/Button'
import Notice from '@/components/ui/Notice'
import type { SaveState } from '@/account/useProfileForm'
import { useT } from '@/i18n/useT'
/** Save button plus the outcome line of a section form. */
export default function SaveRow({ state }: { state: SaveState }) {
const t = useT()
return (
<div className="flex flex-wrap items-center gap-3 pt-1">
<Button type="submit" disabled={state === 'saving'}>
{state === 'saving' ? t('account.saving') : t('account.save')}
</Button>
{state === 'saved' && <Notice tone="success">{t('account.saved')}</Notice>}
{state === 'failed' && (
<Notice tone="error">{t('account.error.save_failed')}</Notice>
)}
</div>
)
}
@@ -0,0 +1,28 @@
import type { ReactNode } from 'react'
import Card from '@/components/ui/Card'
export interface SectionCardProps {
id: string
title: string
description?: string
children: ReactNode
}
/** One account section: heading column on large screens, content beside it. */
export default function SectionCard({ id, title, description, children }: SectionCardProps) {
return (
<Card as="section" aria-labelledby={`${id}-title`} className="p-5 sm:p-6">
<div className="grid gap-5 md:grid-cols-[14rem_1fr] md:gap-8">
<header className="space-y-1">
<h2 id={`${id}-title`} className="font-serif text-xl text-[var(--color-ink)]">
{title}
</h2>
{description && (
<p className="text-sm text-[var(--color-ink-muted)]">{description}</p>
)}
</header>
<div className="min-w-0">{children}</div>
</div>
</Card>
)
}
@@ -0,0 +1,133 @@
import { useState, type FormEvent } from 'react'
import { updatePassword } from '@faunosteo/supabase'
import PasswordField from '@/components/auth/PasswordField'
import Button from '@/components/ui/Button'
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
import Notice from '@/components/ui/Notice'
import SignOutButton from '@/components/shell/SignOutButton'
import SectionCard from './SectionCard'
import {
fieldErrors,
newPasswordSchema,
PASSWORD_MIN_LENGTH,
type FieldErrors,
} from '@/auth/validation'
import { useAuthFailureMessage } from '@/auth/useAuthFailureMessage'
import { getSupabaseClient } from '@/lib/supabase'
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
type Values = { password: string; passwordConfirm: string }
type State = 'idle' | 'saving' | 'saved'
function ChangePasswordForm() {
const t = useT()
const describe = useAuthFailureMessage()
const [values, setValues] = useState<Values>({ password: '', passwordConfirm: '' })
const [errors, setErrors] = useState<FieldErrors<Values>>({})
const [failure, setFailure] = useState<string | null>(null)
const [state, setState] = useState<State>('idle')
async function submit(e: FormEvent) {
e.preventDefault()
const client = getSupabaseClient()
const parsed = newPasswordSchema.safeParse(values)
if (!client) return
if (!parsed.success) {
setErrors(fieldErrors(parsed.error))
return
}
setErrors({})
setFailure(null)
setState('saving')
try {
await updatePassword(client, parsed.data.password)
setValues({ password: '', passwordConfirm: '' })
setState('saved')
} catch (err) {
setFailure(describe(err))
setState('idle')
}
}
return (
<form onSubmit={submit} noValidate className="space-y-4">
<FormField
id="new-password"
label={t('account.security.new_password')}
hint={t('auth.password_hint').replace('{min}', String(PASSWORD_MIN_LENGTH))}
error={errors.password && t(errors.password)}
>
<PasswordField
id="new-password"
autoComplete="new-password"
value={values.password}
onChange={(e) => setValues({ ...values, password: e.target.value })}
aria-invalid={Boolean(errors.password)}
aria-describedby={fieldDescribedBy('new-password', 'hint', errors.password)}
/>
</FormField>
<FormField
id="new-password-confirm"
label={t('auth.password_confirm')}
error={errors.passwordConfirm && t(errors.passwordConfirm)}
>
<PasswordField
id="new-password-confirm"
autoComplete="new-password"
value={values.passwordConfirm}
onChange={(e) => setValues({ ...values, passwordConfirm: e.target.value })}
aria-invalid={Boolean(errors.passwordConfirm)}
aria-describedby={fieldDescribedBy('new-password-confirm', undefined, errors.passwordConfirm)}
/>
</FormField>
<div className="flex flex-wrap items-center gap-3">
<Button type="submit" variant="secondary" disabled={state === 'saving'}>
{state === 'saving'
? t('account.security.changing_password')
: t('account.security.change_password')}
</Button>
{state === 'saved' && (
<Notice tone="success">{t('account.security.password_changed')}</Notice>
)}
{failure && <Notice tone="error">{failure}</Notice>}
</div>
</form>
)
}
export default function SecurityPanel() {
const t = useT()
const email = useAuthStore((s) => s.user?.email)
return (
<SectionCard
id="security"
title={t('account.security.title')}
description={t('account.security.description')}
>
<div className="space-y-6">
<div>
<p className="text-sm font-medium text-[var(--color-ink)]">
{t('account.security.email_label')}
</p>
<p className="text-sm text-[var(--color-ink-muted)]">{email}</p>
</div>
<ChangePasswordForm />
<div className="rounded-lg border border-dashed border-[var(--color-border)] p-4">
<p className="text-sm font-medium text-[var(--color-ink)]">
{t('account.security.mfa_title')}
</p>
<p className="text-sm text-[var(--color-ink-muted)]">
{t('account.security.mfa_note')}
</p>
</div>
<div className="space-y-2 border-t border-[var(--color-border)] pt-4">
<p className="text-xs text-[var(--color-ink-muted)]">
{t('account.security.sign_out_everywhere_note')}
</p>
<SignOutButton className="-ml-3" />
</div>
</div>
</SectionCard>
)
}
@@ -0,0 +1,78 @@
import type { ReactNode } from 'react'
import { Link } from 'react-router-dom'
import Brand from '@/components/brand/Brand'
import LanguageSwitcher from '@/components/brand/LanguageSwitcher'
import Card from '@/components/ui/Card'
import { useConfig } from '@/app/providers/ConfigProvider'
import { useT } from '@/i18n/useT'
export interface AuthLayoutProps {
title: string
subtitle?: string
children: ReactNode
/** Links under the card (switch between login / signup, back links). */
footer?: ReactNode
}
/**
* Two columns on large screens: a warm brand panel and the form card. On
* small screens the panel collapses to the logo above the card.
*/
export default function AuthLayout({
title,
subtitle,
children,
footer,
}: AuthLayoutProps) {
const cfg = useConfig()
const t = useT()
return (
<div className="flex min-h-dvh flex-col lg:flex-row">
<aside className="hidden lg:flex lg:w-[42%] lg:flex-col lg:justify-between bg-[var(--color-surface-strong)] px-12 py-10">
<Link to="/" aria-label={t('common.home')} className="w-fit">
<Brand size="lg" />
</Link>
<div className="max-w-md space-y-4">
<p className="font-serif text-3xl leading-tight text-[var(--color-ink)]">
{t('auth.brand_claim')}
</p>
<p className="text-sm leading-relaxed text-[var(--color-ink-muted)]">
{t('auth.brand_note')}
</p>
</div>
<p className="text-xs text-[var(--color-ink-muted)]">{cfg.tagline}</p>
</aside>
<main className="flex flex-1 flex-col px-4 py-6 sm:px-8 sm:py-10">
<div className="flex items-center justify-between lg:justify-end">
<Link to="/" aria-label={t('common.home')} className="lg:hidden">
<Brand size="md" />
</Link>
<LanguageSwitcher />
</div>
<div className="flex flex-1 items-center justify-center py-8">
<div className="w-full max-w-md space-y-5">
<Card as="section" className="p-6 sm:p-8 space-y-6">
<header className="space-y-1">
<h1 className="font-serif text-2xl sm:text-3xl text-[var(--color-ink)] [text-wrap:balance]">
{title}
</h1>
{subtitle && (
<p className="text-sm text-[var(--color-ink-muted)]">
{subtitle}
</p>
)}
</header>
{children}
</Card>
{footer && (
<div className="text-center text-sm text-[var(--color-ink-muted)] space-y-1">
{footer}
</div>
)}
</div>
</div>
</main>
</div>
)
}
+11
View File
@@ -0,0 +1,11 @@
import { Link, type LinkProps } from 'react-router-dom'
/** Accent link used under the auth cards. */
export default function AuthLink({ className = '', ...rest }: LinkProps) {
return (
<Link
className={`font-medium text-[var(--color-accent)] underline-offset-2 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] rounded ${className}`}
{...rest}
/>
)
}
@@ -0,0 +1,65 @@
import { useState } from 'react'
import { signInWithOAuth, type OAuthProvider } from '@faunosteo/supabase'
import Button from '@/components/ui/Button'
import Icon from '@/components/ui/Icon'
import Notice from '@/components/ui/Notice'
import { useConfig } from '@/app/providers/ConfigProvider'
import { getSupabaseClient } from '@/lib/supabase'
import { absoluteUrl, AUTH_CALLBACK_PATH } from '@/auth/redirects'
import { useAuthFailureMessage } from '@/auth/useAuthFailureMessage'
import { useT } from '@/i18n/useT'
const LABEL: Record<OAuthProvider, string> = {
google: 'auth.continue_with_google',
apple: 'auth.continue_with_apple',
}
/** One button per configured provider, with the "or" rule under them. Nothing when none is configured. */
export default function OAuthButtons() {
const { authProviders } = useConfig()
const t = useT()
const describe = useAuthFailureMessage()
const [error, setError] = useState<string | null>(null)
const [pending, setPending] = useState<OAuthProvider | null>(null)
if (authProviders.length === 0) return null
async function start(provider: OAuthProvider) {
const client = getSupabaseClient()
if (!client) return
setPending(provider)
setError(null)
try {
await signInWithOAuth(client, provider, absoluteUrl(AUTH_CALLBACK_PATH))
} catch (err) {
setError(describe(err))
setPending(null)
}
}
return (
<div className="space-y-3">
<div className="grid gap-2">
{authProviders.map((provider) => (
<Button
key={provider}
type="button"
variant="secondary"
onClick={() => start(provider)}
disabled={pending !== null}
className="gap-2"
>
<Icon name={provider} size={18} />
{t(LABEL[provider])}
</Button>
))}
</div>
{error && <Notice tone="error">{error}</Notice>}
<div className="flex items-center gap-3 text-xs text-[var(--color-ink-muted)]">
<span className="h-px flex-1 bg-[var(--color-border)]" />
{t('auth.or')}
<span className="h-px flex-1 bg-[var(--color-border)]" />
</div>
</div>
)
}
@@ -0,0 +1,30 @@
import { useState, type InputHTMLAttributes } from 'react'
import Input from '@/components/ui/Input'
import Icon from '@/components/ui/Icon'
import { useT } from '@/i18n/useT'
type Props = Omit<InputHTMLAttributes<HTMLInputElement>, 'type'>
/** Password input with a show/hide toggle; the toggle is a labelled button. */
export default function PasswordField({ className = '', ...rest }: Props) {
const [visible, setVisible] = useState(false)
const t = useT()
return (
<div className="relative">
<Input
type={visible ? 'text' : 'password'}
className={`pr-11 ${className}`}
{...rest}
/>
<button
type="button"
onClick={() => setVisible((v) => !v)}
aria-label={visible ? t('auth.hide_password') : t('auth.show_password')}
aria-pressed={visible}
className="absolute inset-y-0 right-0 flex w-11 items-center justify-center rounded-r-lg text-[var(--color-ink-muted)] hover:text-[var(--color-ink)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]"
>
<Icon name={visible ? 'eye-off' : 'eye'} />
</button>
</div>
)
}
@@ -0,0 +1,21 @@
import { useNavigate } from 'react-router-dom'
import Button from '@/components/ui/Button'
import Notice from '@/components/ui/Notice'
import { useT } from '@/i18n/useT'
/** Shown on the auth screens when the deployment has no Supabase. */
export default function UnavailableNotice() {
const t = useT()
const navigate = useNavigate()
return (
<div className="space-y-4">
<Notice tone="info">
<p className="font-medium">{t('auth.unavailable_title')}</p>
<p>{t('auth.unavailable_description')}</p>
</Notice>
<Button variant="secondary" onClick={() => navigate('/')}>
{t('auth.back_to_tool')}
</Button>
</div>
)
}
@@ -0,0 +1,25 @@
import ButtonLink from '@/components/ui/ButtonLink'
import { useConfig } from '@/app/providers/ConfigProvider'
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
/**
* Header entry to the signed-in space, only when a backend is configured:
* "Mon espace" for a signed-in user, "Se connecter" otherwise.
*/
export default function HomeSpaceLink() {
const { supabase } = useConfig()
const status = useAuthStore((s) => s.status)
const t = useT()
if (!supabase) return null
const signedIn = status === 'signed_in'
return (
<ButtonLink
to={signedIn ? '/app' : '/login'}
variant={signedIn ? 'primary' : 'secondary'}
size="sm"
>
{signedIn ? t('home.space.open') : t('home.space.signin')}
</ButtonLink>
)
}
@@ -0,0 +1,77 @@
import { useState } from 'react'
import { Link, Outlet, useLocation } from 'react-router-dom'
import Brand from '@/components/brand/Brand'
import LanguageSwitcher from '@/components/brand/LanguageSwitcher'
import Icon from '@/components/ui/Icon'
import MobileDrawer from './MobileDrawer'
import SidebarNav, { NAV_ITEMS } from './SidebarNav'
import SignOutButton from './SignOutButton'
import UserBadge from './UserBadge'
import { useT } from '@/i18n/useT'
/** Title of the current /app page for the top bar, from the nav table. */
function pageTitleKey(pathname: string): string {
const match = NAV_ITEMS.find((item) =>
item.end ? item.to === pathname : pathname.startsWith(item.to) && item.to !== '/',
)
return match?.labelKey ?? 'shell.nav.dashboard'
}
/**
* Authenticated shell: fixed sidebar on large screens, light top bar with a
* drawer button below. The page content scrolls on its own.
*/
export default function AppLayout() {
const t = useT()
const location = useLocation()
const [drawerOpen, setDrawerOpen] = useState(false)
return (
<div className="flex min-h-dvh bg-[var(--color-bg)]">
<aside className="hidden lg:flex lg:w-64 lg:shrink-0 lg:flex-col border-r border-[var(--color-border)] bg-[var(--color-surface)]">
<div className="px-5 py-5">
<Link to="/app" aria-label={t('shell.nav.dashboard')} className="inline-block">
<Brand size="md" />
</Link>
</div>
<div className="flex-1 px-3">
<SidebarNav />
</div>
<div className="space-y-3 border-t border-[var(--color-border)] px-4 py-4">
<UserBadge />
<SignOutButton className="w-full" />
</div>
</aside>
<div className="flex min-w-0 flex-1 flex-col">
<header className="sticky top-0 z-30 flex h-14 items-center gap-3 border-b border-[var(--color-border)] bg-[var(--color-surface)]/95 px-3 backdrop-blur-sm sm:px-6 pt-[env(safe-area-inset-top)]">
<button
type="button"
onClick={() => setDrawerOpen(true)}
aria-label={t('shell.open_menu')}
aria-expanded={drawerOpen}
className="grid h-11 w-11 place-items-center rounded-lg text-[var(--color-ink)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] lg:hidden"
>
<Icon name="menu" />
</button>
<Link to="/app" className="lg:hidden" aria-label={t('shell.nav.dashboard')}>
<Brand size="sm" />
</Link>
<h1 className="hidden lg:block text-sm font-medium text-[var(--color-ink-muted)]">
{t(pageTitleKey(location.pathname))}
</h1>
<div className="ml-auto flex items-center gap-2">
<LanguageSwitcher />
</div>
</header>
<main className="flex-1 px-4 py-6 sm:px-8 sm:py-8">
<div className="mx-auto w-full max-w-4xl">
<Outlet />
</div>
</main>
</div>
<MobileDrawer open={drawerOpen} onClose={() => setDrawerOpen(false)} />
</div>
)
}
@@ -0,0 +1,74 @@
import { useEffect } from 'react'
import { Link } from 'react-router-dom'
import { AnimatePresence, motion } from 'motion/react'
import Brand from '@/components/brand/Brand'
import Icon from '@/components/ui/Icon'
import SidebarNav from './SidebarNav'
import SignOutButton from './SignOutButton'
import UserBadge from './UserBadge'
import { useT } from '@/i18n/useT'
export interface MobileDrawerProps {
open: boolean
onClose: () => void
}
/** Left-side navigation drawer for small screens (transform + opacity only). */
export default function MobileDrawer({ open, onClose }: MobileDrawerProps) {
const t = useT()
useEffect(() => {
if (!open) return
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose()
window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey)
}, [open, onClose])
return (
<AnimatePresence>
{open && (
<div className="fixed inset-0 z-50 lg:hidden">
<motion.div
data-testid="drawer-backdrop"
className="absolute inset-0 bg-black/40"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
onClick={onClose}
/>
<motion.aside
role="dialog"
aria-modal="true"
aria-label={t('shell.nav_label')}
initial={{ x: '-100%' }}
animate={{ x: 0 }}
exit={{ x: '-100%' }}
transition={{ duration: 0.2, ease: 'easeOut' }}
className="absolute inset-y-0 left-0 flex w-[min(20rem,85vw)] flex-col bg-[var(--color-surface)] shadow-xl pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)]"
>
<div className="flex items-center justify-between px-4 py-3 border-b border-[var(--color-border)]">
<Link to="/app" onClick={onClose} aria-label={t('shell.nav.dashboard')}>
<Brand size="md" />
</Link>
<button
type="button"
onClick={onClose}
aria-label={t('shell.close_menu')}
className="grid h-11 w-11 place-items-center rounded-lg text-[var(--color-ink-muted)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]"
>
<Icon name="close" />
</button>
</div>
<div className="flex-1 overflow-y-auto px-3 py-4">
<SidebarNav onNavigate={onClose} />
</div>
<div className="space-y-3 border-t border-[var(--color-border)] px-4 py-4">
<UserBadge />
<SignOutButton className="w-full" />
</div>
</motion.aside>
</div>
)}
</AnimatePresence>
)
}
@@ -0,0 +1,60 @@
import { NavLink } from 'react-router-dom'
import Icon, { type IconName } from '@/components/ui/Icon'
import { useT } from '@/i18n/useT'
interface NavItem {
to: string
icon: IconName
labelKey: string
hintKey?: string
end?: boolean
}
export const NAV_ITEMS: readonly NavItem[] = [
{ to: '/app', icon: 'dashboard', labelKey: 'shell.nav.dashboard', end: true },
{ to: '/app/account', icon: 'user', labelKey: 'shell.nav.account' },
{
to: '/',
icon: 'tool',
labelKey: 'shell.nav.local_tool',
hintKey: 'shell.nav.local_tool_hint',
},
]
function linkClass(active: boolean): string {
return [
'relative flex items-center gap-3 rounded-lg px-3 py-2.5 text-sm transition-colors min-h-11',
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]',
active
? 'bg-[var(--color-surface-strong)] text-[var(--color-ink)] font-medium before:absolute before:left-0 before:top-2 before:bottom-2 before:w-0.5 before:rounded-full before:bg-[var(--color-accent)]'
: 'text-[var(--color-ink-muted)] hover:bg-[var(--color-surface-strong)] hover:text-[var(--color-ink)]',
].join(' ')
}
/** The three destinations of the shell; `onNavigate` closes the mobile drawer. */
export default function SidebarNav({ onNavigate }: { onNavigate?: () => void }) {
const t = useT()
return (
<nav aria-label={t('shell.nav_label')} className="space-y-1">
{NAV_ITEMS.map((item) => (
<NavLink
key={item.to}
to={item.to}
end={item.end}
onClick={onNavigate}
className={({ isActive }) => linkClass(isActive)}
>
<Icon name={item.icon} size={20} className="shrink-0" />
<span className="flex min-w-0 flex-col leading-tight">
<span className="truncate">{t(item.labelKey)}</span>
{item.hintKey && (
<span className="truncate text-xs font-normal text-[var(--color-ink-muted)]">
{t(item.hintKey)}
</span>
)}
</span>
</NavLink>
))}
</nav>
)
}
@@ -0,0 +1,36 @@
import { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import Button from '@/components/ui/Button'
import Icon from '@/components/ui/Icon'
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
export default function SignOutButton({ className = '' }: { className?: string }) {
const t = useT()
const navigate = useNavigate()
const signOut = useAuthStore((s) => s.signOut)
const [busy, setBusy] = useState(false)
async function onClick() {
setBusy(true)
try {
await signOut()
} finally {
// The guard would redirect anyway; going home keeps the local tool one step away.
navigate('/', { replace: true })
}
}
return (
<Button
type="button"
variant="ghost"
onClick={onClick}
disabled={busy}
className={`justify-start gap-3 text-[var(--color-ink-muted)] hover:text-[var(--color-ink)] ${className}`}
>
<Icon name="logout" size={20} />
{busy ? t('shell.signing_out') : t('shell.sign_out')}
</Button>
)
}
@@ -0,0 +1,38 @@
import { useAuthStore } from '@/state/authStore'
/** Two-letter monogram from the display name, else the email. */
export function monogram(name: string | undefined, email: string | undefined) {
const source = (name ?? '').trim() || (email ?? '').trim()
if (!source) return '?'
const parts = source.split(/\s+/).filter(Boolean)
const letters =
parts.length >= 2 ? parts[0][0] + parts[1][0] : source.slice(0, 2)
return letters.toUpperCase()
}
/** Avatar monogram + display name + email, as shown at the bottom of the sidebar. */
export default function UserBadge() {
const profile = useAuthStore((s) => s.profile)
const user = useAuthStore((s) => s.user)
const name = profile?.display_name ?? user?.email ?? ''
return (
<div className="flex items-center gap-3 min-w-0">
<span
aria-hidden="true"
className="grid h-9 w-9 shrink-0 place-items-center rounded-full bg-[var(--color-accent)] text-sm font-semibold text-white"
>
{monogram(profile?.display_name, user?.email)}
</span>
<span className="flex min-w-0 flex-col leading-tight">
<span className="truncate text-sm font-medium text-[var(--color-ink)]">
{name}
</span>
{user?.email && profile?.display_name && (
<span className="truncate text-xs text-[var(--color-ink-muted)]">
{user.email}
</span>
)}
</span>
</div>
)
}
+15 -6
View File
@@ -23,6 +23,20 @@ const SIZES: Record<NonNullable<ButtonProps['size']>, string> = {
lg: 'h-12 px-5 sm:px-6 text-base rounded-lg',
}
/** The button look, reusable by links that act as buttons (ButtonLink). */
export function buttonClassName(
variant: NonNullable<ButtonProps['variant']> = 'primary',
size: NonNullable<ButtonProps['size']> = 'md',
className = '',
): string {
return [
'inline-flex items-center justify-center font-medium transition disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2',
VARIANTS[variant],
SIZES[size],
className,
].join(' ')
}
export default function Button({
variant = 'primary',
size = 'md',
@@ -32,12 +46,7 @@ export default function Button({
}: ButtonProps) {
return (
<button
className={[
'inline-flex items-center justify-center font-medium transition disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2',
VARIANTS[variant],
SIZES[size],
className,
].join(' ')}
className={buttonClassName(variant, size, className)}
{...rest}
>
{children}
+17
View File
@@ -0,0 +1,17 @@
import { Link, type LinkProps } from 'react-router-dom'
import { buttonClassName, type ButtonProps } from './Button'
export interface ButtonLinkProps extends LinkProps {
variant?: ButtonProps['variant']
size?: ButtonProps['size']
}
/** A router link that looks like a Button: navigation stays a link for the browser and assistive tech. */
export default function ButtonLink({
variant = 'primary',
size = 'md',
className = '',
...rest
}: ButtonLinkProps) {
return <Link className={buttonClassName(variant, size, className)} {...rest} />
}
+54
View File
@@ -0,0 +1,54 @@
import type { ReactNode } from 'react'
import Label from './Label'
export interface FormFieldProps {
id: string
label: string
/** Rendered under the control, greyed; the error replaces it when present. */
hint?: string
error?: string
optional?: string
children: ReactNode
}
/** Describes a field for assistive tech: ids for hint and error, wired through aria-describedby. */
export function fieldDescribedBy(id: string, hint?: string, error?: string) {
const ids: string[] = []
if (error) ids.push(`${id}-error`)
else if (hint) ids.push(`${id}-hint`)
return ids.length ? ids.join(' ') : undefined
}
export default function FormField({
id,
label,
hint,
error,
optional,
children,
}: FormFieldProps) {
return (
<div className="space-y-1">
<Label htmlFor={id} className="flex items-baseline justify-between">
<span>{label}</span>
{optional && (
<span className="text-xs font-normal text-[var(--color-ink-muted)]">
{optional}
</span>
)}
</Label>
{children}
{error ? (
<p id={`${id}-error`} role="alert" className="text-sm text-[var(--color-danger)]">
{error}
</p>
) : (
hint && (
<p id={`${id}-hint`} className="text-xs text-[var(--color-ink-muted)]">
{hint}
</p>
)
)}
</div>
)
}
+63
View File
@@ -0,0 +1,63 @@
import type { SVGProps } from 'react'
export type IconName =
| 'menu'
| 'close'
| 'dashboard'
| 'user'
| 'tool'
| 'logout'
| 'eye'
| 'eye-off'
| 'check'
| 'alert'
| 'google'
| 'apple'
/** Stroke paths on a 24-unit grid; decorative by default (aria-hidden). */
const PATHS: Record<IconName, string> = {
menu: 'M4 7h16M4 12h16M4 17h16',
close: 'M6 6l12 12M18 6L6 18',
dashboard: 'M4 4h7v7H4zM13 4h7v4h-7zM13 11h7v9h-7zM4 14h7v6H4z',
user: 'M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM4 21a8 8 0 0 1 16 0',
tool: 'M14 3l7 7-4 4-7-7zM10 10l-6 6 4 4 6-6M3 21l3-3',
logout: 'M10 4H5v16h5M15 8l5 4-5 4M20 12H9',
eye: 'M2 12s4-7 10-7 10 7 10 7-4 7-10 7S2 12 2 12zM12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6z',
'eye-off':
'M3 3l18 18M10.6 10.6A3 3 0 0 0 13.4 13.4M9.9 5.2A10.5 10.5 0 0 1 12 5c6 0 10 7 10 7a17 17 0 0 1-3.2 3.9M6.1 6.1A17 17 0 0 0 2 12s4 7 10 7a10 10 0 0 0 4-.8',
check: 'M5 12l5 5L20 7',
alert: 'M12 9v4M12 17h.01M10.3 3.9L2.8 17a2 2 0 0 0 1.7 3h15a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0z',
google:
'M21 12.2c0-.7-.1-1.3-.2-1.9H12v3.6h5.1a4.4 4.4 0 0 1-1.9 2.9v2.4h3.1c1.8-1.7 2.7-4.1 2.7-7zM12 21c2.4 0 4.5-.8 6-2.2l-3.1-2.4c-.8.6-1.8.9-2.9.9-2.3 0-4.2-1.5-4.9-3.6H4v2.5A9 9 0 0 0 12 21zM7.1 13.7a5.4 5.4 0 0 1 0-3.4V7.8H4a9 9 0 0 0 0 8.4zM12 6.6c1.3 0 2.5.5 3.4 1.3l2.6-2.6A9 9 0 0 0 4 7.8l3.1 2.5c.7-2.1 2.6-3.7 4.9-3.7z',
apple:
'M16.4 12.6c0-2.3 1.9-3.4 2-3.5a4.3 4.3 0 0 0-3.4-1.8c-1.4-.1-2.8.9-3.5.9s-1.9-.8-3.1-.8A4.6 4.6 0 0 0 4.5 9.8c-1.7 2.9-.4 7.2 1.2 9.5.8 1.1 1.7 2.4 3 2.4s1.7-.8 3.2-.8 1.9.8 3.2.8 2.2-1.2 3-2.3a10 10 0 0 0 1.3-2.8 4.1 4.1 0 0 1-3-4zM13.8 5.6A4 4 0 0 0 14.7 2.7a4.1 4.1 0 0 0-2.7 1.4 3.8 3.8 0 0 0-1 2.8 3.4 3.4 0 0 0 2.8-1.3z',
}
const FILLED: ReadonlySet<IconName> = new Set(['google', 'apple'])
export interface IconProps extends SVGProps<SVGSVGElement> {
name: IconName
size?: number
}
export default function Icon({ name, size = 20, className, ...rest }: IconProps) {
const filled = FILLED.has(name)
return (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill={filled ? 'currentColor' : 'none'}
stroke={filled ? 'none' : 'currentColor'}
strokeWidth={1.8}
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
focusable="false"
className={className}
{...rest}
>
<path d={PATHS[name]} />
</svg>
)
}
+31
View File
@@ -0,0 +1,31 @@
import type { ReactNode } from 'react'
import Icon from './Icon'
export interface NoticeProps {
tone: 'success' | 'error' | 'info'
children: ReactNode
className?: string
}
const TONE: Record<NoticeProps['tone'], string> = {
success: 'border-[var(--severity-mild)]/40 bg-[color-mix(in_srgb,var(--severity-mild)_10%,white)] text-[var(--color-ink)]',
error: 'border-[var(--color-danger)]/40 bg-[color-mix(in_srgb,var(--color-danger)_8%,white)] text-[var(--color-danger-strong)]',
info: 'border-[var(--color-border)] bg-[var(--color-surface-strong)] text-[var(--color-ink)]',
}
/** Inline status line: icon + text, never colour alone. */
export default function Notice({ tone, children, className = '' }: NoticeProps) {
return (
<div
role={tone === 'error' ? 'alert' : 'status'}
className={`flex items-start gap-2 rounded-lg border px-3 py-2 text-sm ${TONE[tone]} ${className}`}
>
<Icon
name={tone === 'success' ? 'check' : 'alert'}
size={18}
className="mt-0.5 shrink-0"
/>
<div className="min-w-0">{children}</div>
</div>
)
}