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