All files / apps / web/src / features / shell / components / Rail index.tsx

100% Statements 10/10
100% Branches 8/8
100% Functions 5/5
100% Lines 10/10

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                                                                          19x               19x             19x     19x 9x       2x     9x                 45x                                                                                                     27x     1x                              
import {
  Bell,
  House,
  MessagesSquare,
  Moon,
  Plus,
  Shield,
  Sun,
  Users,
  type LucideIcon,
} from 'lucide-react'
import type { ReactElement } from 'react'
import { Avatar, AvatarBadge, AvatarFallback } from '@/components/Avatar'
import { CountBadge } from '@/components/CountBadge'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuLabel,
  DropdownMenuRadioGroup,
  DropdownMenuRadioItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from '@/components/DropdownMenu'
import { IconButton } from '@/components/IconButton'
import { initials } from '@chattra/utils/string'
import { toneClass } from '@/helpers/tone'
import type { ThemeChoice } from '@/lib/theme'
import { useTheme } from '../../hooks/use-theme'
import { AppStatus } from '../AppStatus'
import { Logo } from '../Logo'
 
interface RailProps {
  /** The signed-in person, for the avatar at the bottom. */
  userName: string
}
 
interface RailItem {
  label: string
  icon: LucideIcon
  isCurrent: boolean
  unreadCount: number
}
 
// shortcut: only Home leads somewhere and the counts are sample numbers; DMs, Activity and People
// get their screens in M2 and M3, Admin in M1, and the counts come from the socket in M3.
const ITEMS: RailItem[] = [
  { label: 'Home', icon: House, isCurrent: true, unreadCount: 4 },
  { label: 'DMs', icon: MessagesSquare, isCurrent: false, unreadCount: 2 },
  { label: 'Activity', icon: Bell, isCurrent: false, unreadCount: 5 },
  { label: 'People', icon: Users, isCurrent: false, unreadCount: 0 },
  { label: 'Admin', icon: Shield, isCurrent: false, unreadCount: 0 },
]
 
const THEMES: { choice: ThemeChoice; label: string }[] = [
  { choice: 'system', label: 'System' },
  { choice: 'light', label: 'Light' },
  { choice: 'dark', label: 'Dark' },
]
 
const ITEM_CLASS =
  'relative grid h-9.5 w-11 place-items-center rounded-xl text-sidebar-foreground outline-none hover:bg-hover focus-visible:ring-3 focus-visible:ring-ring aria-[current=page]:bg-sidebar-primary aria-[current=page]:text-sidebar-primary-foreground'
 
// The narrow strip left of the sidebar: where you are, what is new, and your own menu.
export const Rail = ({ userName }: RailProps): ReactElement => {
  const theme = useTheme()
 
  // Read from the page, not from the choice: "system" is light or dark depending on the OS.
  function toggleTheme(): void {
    theme.setChoice(document.documentElement.classList.contains('dark') ? 'light' : 'dark')
  }
 
  return (
    <div className="flex w-[73px] shrink-0 flex-col items-center gap-1 border-r bg-rail pt-3 pb-2.5">
      <Logo className="mb-2 size-10 shrink-0 drop-shadow-md" />
      <nav
        aria-label="Main navigation"
        // overflow-y-auto turns x to auto as well: hide x and leave room so the count badges are not cut.
        className="flex min-h-0 w-full flex-col items-center gap-3.5 overflow-x-hidden overflow-y-auto pt-2 pb-1"
      >
        {ITEMS.map((item) => (
          <div key={item.label} className="flex shrink-0 flex-col items-center gap-1">
            <button
              type="button"
              aria-label={
                item.unreadCount > 0 ? `${item.label}, ${item.unreadCount} unread` : item.label
              }
              aria-current={item.isCurrent ? 'page' : undefined}
              className={ITEM_CLASS}
            >
              <item.icon className="size-5" />
              {item.unreadCount > 0 ? (
                <CountBadge
                  aria-hidden="true"
                  className="absolute -top-1.5 -right-2 h-4.5 min-w-4.5 px-1 ring-2 ring-sidebar"
                >
                  {item.unreadCount}
                </CountBadge>
              ) : null}
            </button>
            <span aria-hidden="true" className="text-2xs font-semibold text-muted-foreground">
              {item.label}
            </span>
          </div>
        ))}
      </nav>
      <span className="flex-1" />
      {/* shortcut: the create button opens nothing yet; creating a channel or DM is M2. */}
      <button
        type="button"
        aria-label="Create"
        className="grid size-9 shrink-0 place-items-center rounded-full border border-border-strong bg-glass-strong outline-none hover:bg-accent focus-visible:ring-3 focus-visible:ring-ring"
      >
        <Plus className="size-4" />
      </button>
      <IconButton label="Toggle theme" size="icon" onClick={toggleTheme} className="mt-1 shrink-0">
        <Moon className="size-5 dark:hidden" />
        <Sun className="hidden size-5 dark:block" />
      </IconButton>
      <DropdownMenu>
        <DropdownMenuTrigger className="mt-1 shrink-0 rounded-[10px] outline-none focus-visible:ring-3 focus-visible:ring-ring">
          {/* Named by its content, so the visible initials stay part of the name (WCAG 2.5.3). */}
          <span className="sr-only">{`${userName}'s account`}</span>
          <Avatar>
            <AvatarFallback className={toneClass(userName)}>{initials(userName)}</AvatarFallback>
            <AvatarBadge className="bg-ok" />
          </Avatar>
        </DropdownMenuTrigger>
        <DropdownMenuContent side="right" align="end" className="w-60">
          <DropdownMenuLabel>Theme</DropdownMenuLabel>
          <DropdownMenuRadioGroup value={theme.choice}>
            {THEMES.map(({ choice, label }) => (
              <DropdownMenuRadioItem
                key={choice}
                value={choice}
                onSelect={() => theme.setChoice(choice)}
              >
                {label}
              </DropdownMenuRadioItem>
            ))}
          </DropdownMenuRadioGroup>
          <DropdownMenuSeparator />
          <div className="px-2 py-1.5 text-xs text-muted-foreground">
            <AppStatus />
          </div>
        </DropdownMenuContent>
      </DropdownMenu>
    </div>
  )
}