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(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 (

{t(pageTitleKey(location.pathname))}

{displayName}
setDrawerOpen(false)} /> setLeaveTo(null)} title={t('leave.title')} description={

{t('leave.body')}

} size="sm" >
) }