Files
faunosteo/apps/app/src/components/shell/AppLayout.tsx
T
bchanot 580ff28214 feat(app): editor sidebar hidden below 1 280 px, menu one tap away
AC9: on the draft editor path AppLayout uses the xl class set (sidebar
from 1 280 px, menu button and brand below); other pages keep lg.
AppLayout split: Sidebar and TopBar in ShellChrome.tsx. MobileDrawer
takes closesFrom ('64rem' | '80rem') and also closes at setup when
its query already matches.

Rewritten assertions (R-24):
- shell.AppLayout "opens the mobile drawer" and shell.navigation
  "carries the same entries into the mobile drawer": stub a narrow
  matchMedia, the drawer now closes at setup at the happy-dom width.
New: unit tests (class sets, 64rem/80rem close, setup close, leave
guard on a drawer link) and editor-shell.spec F journeys (sidebar at
1024/1180/1279/1280, one-tap menu, drawer open at 1180, guard).
2026-11-08 19:25:49 +01:00

96 lines
3.5 KiB
TypeScript

import { useState, type MouseEvent } from 'react'
import { Outlet, matchPath, useLocation, useNavigate } from 'react-router-dom'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import MobileDrawer from './MobileDrawer'
import { DEFAULT_SHELL, EDITOR_SHELL, Sidebar, TopBar } from './ShellChrome'
import { usePurgeForeignCopies } from '@/app/usePurgeForeignCopies'
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
import { useT } from '@/i18n/useT'
/** The draft editor takes the whole width (board 04's three columns). */
const DRAFT_EDITOR_PATH = '/app/admin/sheets/:sheetId/draft'
/** 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()
usePurgeForeignCopies()
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 fullWidth = matchPath(DRAFT_EDITOR_PATH, location.pathname) !== null
const shell = fullWidth ? EDITOR_SHELL : DEFAULT_SHELL
return (
<div className="flex min-h-dvh bg-[var(--color-bg)]" onClickCapture={askBeforeLeaving}>
<Sidebar shell={shell} />
<div className="flex min-w-0 flex-1 flex-col">
<TopBar shell={shell} menuOpen={drawerOpen} onOpenMenu={() => setDrawerOpen(true)} />
<main className="flex-1 px-4 py-6 sm:px-8 sm:py-8">
<div className={`mx-auto w-full ${fullWidth ? '' : 'max-w-4xl'}`}>
<Outlet />
</div>
</main>
</div>
<MobileDrawer
open={drawerOpen}
onClose={() => setDrawerOpen(false)}
closesFrom={shell.closesFrom}
/>
<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>
)
}