All files / src/shared/components AppSidebar.tsx

100% Statements 5/5
100% Branches 9/9
100% Functions 4/4
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                                                                                                                  22x   22x                                                             66x               462x               154x                                          
import type { ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'
 
import { cn } from '@/lib/utils'
import { BrandSignature } from './BrandSignature'
import { PlusIcon } from '@/shared/icons/plus-icon'
import { AccountMenu } from './AccountMenu'
import { SidebarNavItem } from './SidebarNavItem'
 
export interface SidebarNavEntry {
  to: string
  label: string
  icon: ReactNode
  disabled?: boolean
}
 
interface AppSidebarProps {
  showAccount?: boolean
  items: SidebarNavEntry[]
  accountAvatarUrl?: string | null
  accountName: string
  accountEmail: string
  onAddBaby: () => void
  onLogout: () => void
  logoutPending?: boolean
  /** Called after any navigation, so the phone drawer closes behind the tap. */
  onNavigate?: () => void
  className?: string
}
 
/**
 * The side menu: brand, "add a child", the sections, and the account at the
 * foot. It is the same component in both places — a column on wide screens, the
 * contents of the drawer on phones — because the two must not drift; the bottom
 * bar and the top nav it replaces were two lists of the same routes, written
 * twice.
 *
 * It takes everything it renders as props and holds no state. That is what lets
 * the story mount it and measure it: the real shell reaches into three stores, a
 * mutation and two queries to build the same tree, and none of that is available
 * to a story. It is also the reason the media query lives in the shell — an
 * element that is `display: none` has no boxes, and a measurement of nothing
 * passes.
 */
export function AppSidebar({
  showAccount = false,
  items,
  accountName,
  accountAvatarUrl,
  accountEmail,
  onAddBaby,
  onLogout,
  logoutPending,
  onNavigate,
  className,
}: AppSidebarProps) {
  const { t } = useTranslation()
 
  return (
    <div
      className={cn(
        'flex h-full min-h-0 w-full flex-col border-r border-border bg-card',
        className,
      )}
    >
      <Link
        to="/dashboard"
        onClick={onNavigate}
        className="flex flex-shrink-0 items-center gap-3 px-5 py-4"
      >
        <BrandSignature compact />
      </Link>
 
      <div className="flex-shrink-0 px-3 pb-3">
        <button
          type="button"
          onClick={onAddBaby}
          className="flex min-h-11 w-full items-center gap-3 rounded-xl border border-dashed border-emerald-200 px-3 py-2 text-sm font-bold text-emerald-700 transition-colors hover:bg-emerald-50 dark:border-emerald-900 dark:text-emerald-300 dark:hover:bg-emerald-950/40"
        >
          <PlusIcon className="h-5 w-5 flex-shrink-0" />
          <span>{t('babies.addChild')}</span>
        </button>
      </div>
 
      {/* Scrolls on its own, so the account foot stays reachable on a short
          phone in landscape — 320x360 is where a seventh section first pushes
          "Sair da conta" off the column. */}
      <nav aria-label={t('nav.sections')} className="min-h-0 flex-1 overflow-y-auto px-3 pb-3">
        {['overview', 'care', 'development'].map((group) => (
          <div key={group} className="mb-3 space-y-1">
            {group !== 'overview' && (
              <p className="px-3 pb-1 pt-2 text-[11px] font-bold uppercase tracking-wider text-ink-muted">
                {t(`nav.shell.${group}`)}
              </p>
            )}
            {items
              .filter(
                (item) =>
                  (item.to === '/dashboard'
                    ? 'overview'
                    : ['/crescimento', '/milestones'].includes(item.to)
                      ? 'development'
                      : 'care') === group,
              )
              .map((item) => (
                <SidebarNavItem key={item.to} {...item} onNavigate={onNavigate} />
              ))}
          </div>
        ))}
      </nav>
 
      {showAccount && (
        <div className="shrink-0 border-t border-border p-3">
          <AccountMenu
            avatarUrl={accountAvatarUrl}
            name={accountName}
            email={accountEmail}
            onLogout={onLogout}
            pending={logoutPending}
            onNavigate={onNavigate}
          />
        </div>
      )}
    </div>
  )
}