Open History to see the last commit
1import { Undo2Icon } from "lucide-react";
2import { useLocation, useNavigate } from "@tanstack/react-router";
3import {
4 createContext,
5 type ComponentPropsWithoutRef,
6 type ReactNode,
7 useCallback,
8 useContext,
9 useEffect,
10 useMemo,
11 useState,
12} from "react";
13
14import { cn } from "../../lib/utils";
15import { Button } from "../ui/button";
16import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
17
18interface SettingsSearchTargetContextValue {
19 readonly targetId: string | null;
20 readonly onTargetHandled: () => void;
21}
22
23const noop = () => undefined;
24const SettingsSearchTargetContext = createContext<SettingsSearchTargetContextValue>({
25 targetId: null,
26 onTargetHandled: noop,
27});
28
29export function SettingsSearchTargetProvider({
30 targetId,
31 onTargetHandled = noop,
32 children,
33}: {
34 targetId: string | null;
35 onTargetHandled?: () => void;
36 children: ReactNode;
37}) {
38 const value = useMemo(() => ({ targetId, onTargetHandled }), [onTargetHandled, targetId]);
39 return <SettingsSearchTargetContext value={value}>{children}</SettingsSearchTargetContext>;
40}
41
42function scrollAndFocusSettingsTarget(target: HTMLElement): void {
43 const prefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
44 const scrollTarget =
45 target.tagName === "SECTION" && target.firstElementChild
46 ? (target.firstElementChild as HTMLElement)
47 : target;
48
49 scrollTarget.scrollIntoView({
50 behavior: prefersReducedMotion ? "auto" : "smooth",
51 block: "center",
52 });
53 target.focus({ preventScroll: true });
54 target.classList.remove("settings-search-target-pulse");
55 if (prefersReducedMotion) return;
56 void target.offsetWidth;
57 target.classList.add("settings-search-target-pulse");
58 // The class also suppresses the focus outline (the pulse is the destination
59 // indicator), so drop it once the element is no longer the destination.
60 target.addEventListener("blur", () => target.classList.remove("settings-search-target-pulse"), {
61 once: true,
62 });
63}
64
65function useSettingsSearchTarget<T extends HTMLElement>(id: string | undefined) {
66 const { targetId, onTargetHandled } = useContext(SettingsSearchTargetContext);
67 const isSearchTarget = id !== undefined && id === targetId;
68 const targetRef = useCallback(
69 (target: T | null) => {
70 if (target && isSearchTarget) {
71 scrollAndFocusSettingsTarget(target);
72 onTargetHandled();
73 }
74 },
75 [isSearchTarget, onTargetHandled],
76 );
77
78 return targetRef;
79}
80
81/** Re-render every `intervalMs`; return a stable timestamp snapshot for render-time relative labels. */
82export function useRelativeTimeTick(intervalMs = 1_000) {
83 const [nowMs, setNowMs] = useState(() => Date.now());
84 useEffect(() => {
85 const id = setInterval(() => setNowMs(Date.now()), intervalMs);
86 return () => clearInterval(id);
87 }, [intervalMs]);
88 return nowMs;
89}
90
91export function SettingsSection({
92 title,
93 icon,
94 headerAction,
95 children,
96 className,
97 ...sectionProps
98}: ComponentPropsWithoutRef<"section"> & {
99 title: string;
100 icon?: ReactNode;
101 headerAction?: ReactNode;
102 children: ReactNode;
103}) {
104 const targetRef = useSettingsSearchTarget<HTMLElement>(sectionProps.id);
105
106 return (
107 <section
108 {...sectionProps}
109 ref={targetRef}
110 tabIndex={sectionProps.id ? -1 : sectionProps.tabIndex}
111 className={cn("space-y-3", className)}
112 >
113 <div className="flex min-h-8 items-center justify-between gap-4 px-3 sm:px-4">
114 <h2 className="flex items-center gap-2 text-lg font-semibold tracking-[-0.025em] text-foreground">
115 {icon}
116 {title}
117 </h2>
118 <div className="flex min-h-7 min-w-7 items-center justify-end">{headerAction}</div>
119 </div>
120 <div className="relative space-y-1 overflow-visible text-foreground">{children}</div>
121 </section>
122 );
123}
124
125export function SettingsRow({
126 title,
127 description,
128 status,
129 resetAction,
130 control,
131 children,
132 className,
133 ...rowProps
134}: Omit<ComponentPropsWithoutRef<"div">, "title"> & {
135 title: ReactNode;
136 description: ReactNode;
137 status?: ReactNode;
138 resetAction?: ReactNode;
139 control?: ReactNode;
140 children?: ReactNode;
141}) {
142 const targetRef = useSettingsSearchTarget<HTMLDivElement>(rowProps.id);
143
144 return (
145 <div
146 {...rowProps}
147 ref={targetRef}
148 tabIndex={rowProps.id ? -1 : rowProps.tabIndex}
149 className={cn("rounded-xl px-3 sm:px-4", children ? "pt-3 pb-1" : "py-3", className)}
150 >
151 <div className="flex flex-col gap-3 sm:grid sm:grid-cols-[minmax(0,1fr)_minmax(10rem,auto)] sm:items-center sm:gap-8">
152 <div className="min-w-0 flex-1 space-y-1">
153 <div className="flex min-h-5 items-center gap-1.5">
154 <h3 className="text-sm font-medium tracking-[-0.005em] text-foreground">{title}</h3>
155 <span className="inline-flex h-5 w-5 shrink-0 items-center justify-center">
156 {resetAction}
157 </span>
158 </div>
159 <p className="max-w-xl text-[13px] leading-[1.45] text-muted-foreground/80">
160 {description}
161 </p>
162 {status ? <div className="pt-0.5 text-xs text-muted-foreground">{status}</div> : null}
163 </div>
164 {control ? (
165 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
166 {control}
167 </div>
168 ) : null}
169 </div>
170 {children}
171 </div>
172 );
173}
174
175export function SettingResetButton({ label, onClick }: { label: string; onClick: () => void }) {
176 return (
177 <Tooltip>
178 <TooltipTrigger
179 render={
180 <Button
181 size="icon-xs"
182 variant="ghost"
183 aria-label={`Reset ${label} to default`}
184 className="size-5 rounded-sm p-0 text-muted-foreground hover:text-foreground"
185 onClick={(event) => {
186 event.stopPropagation();
187 onClick();
188 }}
189 >
190 <Undo2Icon className="size-3" />
191 </Button>
192 }
193 />
194 <TooltipPopup side="top">Reset to default</TooltipPopup>
195 </Tooltip>
196 );
197}
198
199export function SettingsPageContainer({
200 children,
201 className,
202}: {
203 children: ReactNode;
204 className?: string;
205}) {
206 const navigate = useNavigate();
207 const hash = useLocation({ select: (location) => location.hash });
208 const targetId = hash.replace(/^#/, "") || null;
209 const clearTargetHash = useCallback(() => {
210 void navigate({ hash: "", replace: true, resetScroll: false, hashScrollIntoView: false });
211 }, [navigate]);
212
213 return (
214 <SettingsSearchTargetProvider targetId={targetId} onTargetHandled={clearTargetHash}>
215 <div className="settings-page-scroll-fade scrollbar-gutter-both flex-1 overflow-y-auto px-4 pt-10 pb-7 sm:px-8 sm:pt-12 sm:pb-10">
216 <div className={cn("mx-auto flex w-full max-w-4xl flex-col gap-12", className)}>
217 {children}
218 </div>
219 </div>
220 </SettingsSearchTargetProvider>
221 );
222}
223
224export function scrollToSettingsTarget(targetId: string): boolean {
225 const target = document.getElementById(targetId);
226 if (!target) return false;
227 scrollAndFocusSettingsTarget(target);
228 return true;
229}