Open History to see the last commit
1import { ChevronRightIcon } from "lucide-react";
2import { type ReactNode, useState } from "react";
3
4import { cn } from "~/lib/utils";
5import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
6import { useSettingsSearchTarget, useSettingsSearchTargetId } from "./settingsLayout";
7
8/**
9 * A grouped settings section that starts closed. The header carries the title,
10 * a one line summary of what is set inside, and an optional control such as
11 * the section's own switch. A settings search that targets the section opens it.
12 */
13export function FoldedSettingsSection({
14 id,
15 title,
16 summary,
17 control,
18 children,
19}: {
20 readonly id: string;
21 readonly title: string;
22 readonly summary?: string | null;
23 readonly control?: ReactNode;
24 readonly children: ReactNode;
25}) {
26 const [open, setOpen] = useState(false);
27 const searchTargetId = useSettingsSearchTargetId();
28 const targetRef = useSettingsSearchTarget<HTMLElement>(id);
29 // A search jump lands inside the fold, so open it before the scroll runs.
30 const [openedForTarget, setOpenedForTarget] = useState<string | null>(null);
31 if (searchTargetId === id && openedForTarget !== id) {
32 setOpenedForTarget(id);
33 if (!open) setOpen(true);
34 }
35
36 return (
37 <section id={id} ref={targetRef} tabIndex={-1} className="outline-none">
38 <Collapsible
39 open={open}
40 onOpenChange={setOpen}
41 className="rounded-xl border border-border/60 bg-card/40 text-foreground shadow-xs/5"
42 >
43 <div className="flex items-center gap-4 px-3 sm:px-4">
44 <CollapsibleTrigger className="flex min-h-11 min-w-0 flex-1 items-center gap-2 py-2 text-left outline-none focus-visible:ring-2 focus-visible:ring-ring rounded-md">
45 <ChevronRightIcon
46 aria-hidden
47 className={cn(
48 "size-4 shrink-0 text-muted-foreground transition-transform duration-150 motion-reduce:transition-none",
49 open && "rotate-90",
50 )}
51 />
52 <span className="shrink-0 text-sm font-medium">{title}</span>
53 {summary ? (
54 <span className="min-w-0 truncate text-xs text-muted-foreground">{summary}</span>
55 ) : null}
56 </CollapsibleTrigger>
57 {control ? <div className="flex shrink-0 items-center">{control}</div> : null}
58 </div>
59 <CollapsiblePanel>
60 <div className="border-t border-border/50 [&>*+*]:border-t [&>*+*]:border-border/50">
61 {children}
62 </div>
63 </CollapsiblePanel>
64 </Collapsible>
65 </section>
66 );
67}