Files
faunosteo/apps/app/src/components/shell/ShellChrome.tsx
T

114 lines
4.2 KiB
TypeScript

import { Link, useLocation } from 'react-router-dom'
import Brand from '@/components/brand/Brand'
import LanguageSwitcher from '@/components/brand/LanguageSwitcher'
import Icon from '@/components/ui/Icon'
import SidebarNav, { NAV_ITEMS } from './SidebarNav'
import SignOutButton from './SignOutButton'
import UserBadge, { AvatarCircle } from './UserBadge'
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
/**
* Where the sidebar shows and the top bar swaps its menu button for the page title.
* Tailwind needs literal class strings: two static sets, never a built string.
*/
export interface ShellBreakpoint {
sidebar: string
menuControl: string
title: string
/** The width from which the drawer has no reason to stay open. */
closesFrom: '64rem' | '80rem'
}
/** Every page but the editor: the sidebar from 1 024 px (Tailwind `lg`). */
export const DEFAULT_SHELL: ShellBreakpoint = {
sidebar: 'hidden lg:flex lg:w-64 lg:shrink-0 lg:flex-col',
menuControl: 'lg:hidden',
title: 'hidden lg:block',
closesFrom: '64rem',
}
/** The draft editor needs the width for its three columns: sidebar from 1 280 px (`xl`). */
export const EDITOR_SHELL: ShellBreakpoint = {
sidebar: 'hidden xl:flex xl:w-64 xl:shrink-0 xl:flex-col',
menuControl: 'xl:hidden',
title: 'hidden xl:block',
closesFrom: '80rem',
}
/** 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'
}
export function Sidebar({ shell }: { shell: ShellBreakpoint }) {
const t = useT()
return (
<aside
className={`${shell.sidebar} sticky top-0 h-dvh self-start overflow-y-auto 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>
)
}
interface TopBarProps {
shell: ShellBreakpoint
menuOpen: boolean
onOpenMenu: () => void
}
export function TopBar({ shell, menuOpen, onOpenMenu }: TopBarProps) {
const t = useT()
const { pathname } = useLocation()
const profile = useAuthStore((s) => s.profile)
const user = useAuthStore((s) => s.user)
const displayName = profile?.display_name ?? user?.email ?? ''
return (
<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={onOpenMenu}
aria-label={t('shell.open_menu')}
aria-expanded={menuOpen}
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)] ${shell.menuControl}`}
>
<Icon name="menu" />
</button>
<Link to="/app" className={shell.menuControl} aria-label={t('shell.nav.dashboard')}>
<Brand size="sm" />
</Link>
<h1 className={`${shell.title} text-sm font-medium text-[var(--color-ink-muted)]`}>
{t(pageTitleKey(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>
)
}