| 1 | import { ChevronDown } from "lucide-react"; |
| 2 | import { type ReactNode, useId, useState } from "react"; |
| 3 | |
| 4 | import { cn } from "../lib/cn"; |
| 5 | |
| 6 | /** |
| 7 | * A page's sidebar: beside the page from `lg` up, and on anything narrower |
| 8 | * a disclosure at the top that opens it in place, so a phone reaches the |
| 9 | * conversation first and the metadata one tap away. |
| 10 | */ |
| 11 | export function DetailsDisclosure({ |
| 12 | label = "Details", |
| 13 | summary, |
| 14 | icon, |
| 15 | className, |
| 16 | bodyClassName = "space-y-6", |
| 17 | children, |
| 18 | }: { |
| 19 | /** What the button says. */ |
| 20 | label?: string; |
| 21 | /** A few words beside it, such as who reviews and which labels. */ |
| 22 | summary?: ReactNode; |
| 23 | icon?: ReactNode; |
| 24 | className?: string; |
| 25 | /** Classes of the body, as the sidebar lays it out. */ |
| 26 | bodyClassName?: string; |
| 27 | children: ReactNode; |
| 28 | }) { |
| 29 | const [open, setOpen] = useState(false); |
| 30 | const id = useId(); |
| 31 | return ( |
| 32 | <aside className={className}> |
| 33 | <button |
| 34 | type="button" |
| 35 | aria-expanded={open} |
| 36 | aria-controls={id} |
| 37 | onClick={() => setOpen(!open)} |
| 38 | className="flex min-h-11 w-full items-center gap-2.5 rounded-xl border border-line bg-surface px-4 py-2.5 text-left text-sm transition-colors hover:border-line-strong lg:hidden" |
| 39 | > |
| 40 | {icon} |
| 41 | <span className="shrink-0 font-medium">{label}</span> |
| 42 | {summary && <span className="min-w-0 grow truncate text-muted">{summary}</span>} |
| 43 | <ChevronDown size={15} className={cn("ml-auto shrink-0 text-faint transition-transform", open && "rotate-180")} /> |
| 44 | </button> |
| 45 | <div id={id} className={cn(open ? "mt-4 block" : "hidden", "lg:mt-0 lg:block", bodyClassName)}> |
| 46 | {children} |
| 47 | </div> |
| 48 | </aside> |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** `2 reviewers · 1 label`: what the sidebar holds, by count, leaving out what is empty. */ |
| 53 | export function detailsSummary(counts: [count: number, one: string, many: string][]): string { |
| 54 | return counts |
| 55 | .filter(([count]) => count > 0) |
| 56 | .map(([count, one, many]) => `${count} ${count === 1 ? one : many}`) |
| 57 | .join(" · "); |
| 58 | } |