Files
faunosteo/apps/app/src/components/shell/AppLayout.tsx
T
bchanot b38e62770e feat(app): clients list, page, create, edit, archive and restore
Routes /app/clients, /new, /:clientId, /:clientId/edit behind a workspace
gate that remounts per workspace; debounced POST search, archived toggle and
« Afficher plus » pages; form on field-limits (D-R7) with the phone entry
rule and ISO countries; request id per creation intent; conflict shows the
saved values; archive confirmation with the sole-owner preview; animals
read-only; leave guard on in-app links; Clients entry after the dashboard.
2026-10-26 01:59:25 +01:00

143 lines
5.9 KiB
TypeScript

import { useState, type MouseEvent } from 'react'
import { Link, Outlet, useLocation, useNavigate } from 'react-router-dom'
import Brand from '@/components/brand/Brand'
import LanguageSwitcher from '@/components/brand/LanguageSwitcher'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import Icon from '@/components/ui/Icon'
import MobileDrawer from './MobileDrawer'
import SidebarNav, { NAV_ITEMS } from './SidebarNav'
import SignOutButton from './SignOutButton'
import UserBadge, { AvatarCircle } from './UserBadge'
import { useAuthStore } from '@/state/authStore'
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
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'
}
/** An in-app link a plain click would follow: same origin, same tab, no modifier key. */
function followedLink(event: MouseEvent): HTMLAnchorElement | null {
if (event.defaultPrevented || event.button !== 0) return null
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return null
const anchor = (event.target as Element).closest('a[href]') as HTMLAnchorElement | null
if (!anchor || anchor.target === '_blank' || anchor.origin !== window.location.origin) return null
return anchor
}
/**
* Authenticated shell: fixed sidebar on large screens, light top bar with a
* drawer button below. The page content scrolls on its own. While a page
* holds unsaved changes (useLeaveGuard), following any link of the shell or
* of the page asks first; staying is the default.
*/
export default function AppLayout() {
const t = useT()
const location = useLocation()
const navigate = useNavigate()
const [drawerOpen, setDrawerOpen] = useState(false)
const guarded = useLeaveGuardStore((s) => s.armed)
const arm = useLeaveGuardStore((s) => s.arm)
const [leaveTo, setLeaveTo] = useState<string | null>(null)
function askBeforeLeaving(event: MouseEvent) {
if (!guarded) return
const anchor = followedLink(event)
if (!anchor) return
event.preventDefault()
event.stopPropagation()
setLeaveTo(anchor.pathname + anchor.search + anchor.hash)
}
function leave() {
const to = leaveTo
if (!to) return
arm(false)
setLeaveTo(null)
setDrawerOpen(false)
navigate(to)
}
const profile = useAuthStore((s) => s.profile)
const user = useAuthStore((s) => s.user)
const displayName = profile?.display_name ?? user?.email ?? ''
return (
<div className="flex min-h-dvh bg-[var(--color-bg)]" onClickCapture={askBeforeLeaving}>
<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 sm:gap-3">
<LanguageSwitcher />
<Link
to="/app/account"
aria-label={t('shell.nav.account')}
data-testid="topbar-account"
className="flex min-h-11 items-center gap-2 rounded-full pr-1 pl-1 text-sm text-[var(--color-ink)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] sm:pr-3"
>
<AvatarCircle size="sm" />
<span className="hidden max-w-[12rem] truncate sm:inline">{displayName}</span>
</Link>
</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)} />
<Dialog
open={leaveTo !== null}
onClose={() => setLeaveTo(null)}
title={t('leave.title')}
description={<p className="text-sm text-[var(--color-ink-muted)]">{t('leave.body')}</p>}
size="sm"
>
<div className="mt-4 flex flex-wrap justify-end gap-2">
<Button type="button" variant="ghost" onClick={() => setLeaveTo(null)}>
{t('leave.stay')}
</Button>
<Button type="button" variant="danger" onClick={leave}>
{t('leave.confirm')}
</Button>
</div>
</Dialog>
</div>
)
}