Open History to see the last commit
1import { InfoIcon, 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
65/** The row id a settings-search jump is currently trying to reach, if any. */
66export function useSettingsSearchTargetId(): string | null {
67 return useContext(SettingsSearchTargetContext).targetId;
68}
69
70function useSettingsSearchTarget<T extends HTMLElement>(id: string | undefined) {
71 const { targetId, onTargetHandled } = useContext(SettingsSearchTargetContext);
72 const isSearchTarget = id !== undefined && id === targetId;
73 const targetRef = useCallback(
74 (target: T | null) => {
75 if (target && isSearchTarget) {
76 scrollAndFocusSettingsTarget(target);
77 onTargetHandled();
78 }
79 },
80 [isSearchTarget, onTargetHandled],
81 );
82
83 return targetRef;
84}
85
86/** Info affordance explaining how a setting interacts with the shared background policy. */
87export function PolicyTooltip({ children }: { readonly children: string }) {
88 return (
89 <Tooltip>
90 <TooltipTrigger
91 render={
92 <button
93 type="button"
94 className="inline-flex size-5 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground"
95 aria-label="Background policy details"
96 >
97 <InfoIcon className="size-3.5" />
98 </button>
99 }
100 />
101 <TooltipPopup side="top" className="max-w-72">
102 {children}
103 </TooltipPopup>
104 </Tooltip>
105 );
106}
107
108/** Re-render every `intervalMs`; return a stable timestamp snapshot for render-time relative labels. */
109export function useRelativeTimeTick(intervalMs = 1_000) {
110 const [nowMs, setNowMs] = useState(() => Date.now());
111 useEffect(() => {
112 const id = setInterval(() => setNowMs(Date.now()), intervalMs);
113 return () => clearInterval(id);
114 }, [intervalMs]);
115 return nowMs;
116}
117
118export function SettingsSection({
119 title,
120 icon,
121 headerAction,
122 children,
123 className,
124 ...sectionProps
125}: ComponentPropsWithoutRef<"section"> & {
126 title: string;
127 icon?: ReactNode;
128 headerAction?: ReactNode;
129 children: ReactNode;
130}) {
131 const targetRef = useSettingsSearchTarget<HTMLElement>(sectionProps.id);
132
133 return (
134 <section
135 {...sectionProps}
136 ref={targetRef}
137 tabIndex={sectionProps.id ? -1 : sectionProps.tabIndex}
138 className={cn("space-y-3", className)}
139 >
140 <div className="flex min-h-8 items-center justify-between gap-4 px-3 sm:px-4">
141 <h2 className="flex items-center gap-2 text-lg font-semibold tracking-[-0.025em] text-foreground">
142 {icon}
143 {title}
144 </h2>
145 <div className="flex min-h-7 min-w-7 items-center justify-end">{headerAction}</div>
146 </div>
147 <div className="relative space-y-1 overflow-visible text-foreground">{children}</div>
148 </section>
149 );
150}
151
152export function SettingsRow({
153 title,
154 description,
155 status,
156 resetAction,
157 control,
158 children,
159 className,
160 ...rowProps
161}: Omit<ComponentPropsWithoutRef<"div">, "title"> & {
162 title: ReactNode;
163 description: ReactNode;
164 status?: ReactNode;
165 resetAction?: ReactNode;
166 control?: ReactNode;
167 children?: ReactNode;
168}) {
169 const targetRef = useSettingsSearchTarget<HTMLDivElement>(rowProps.id);
170
171 return (
172 <div
173 {...rowProps}
174 ref={targetRef}
175 tabIndex={rowProps.id ? -1 : rowProps.tabIndex}
176 className={cn("rounded-xl px-3 sm:px-4", children ? "pt-3 pb-1" : "py-3", className)}
177 >
178 <div className="flex flex-col gap-3 sm:grid sm:grid-cols-[minmax(0,1fr)_minmax(10rem,auto)] sm:items-center sm:gap-8">
179 <div className="min-w-0 flex-1 space-y-1">
180 <div className="flex min-h-5 items-center gap-1.5">
181 <h3 className="text-sm font-medium tracking-[-0.005em] text-foreground">{title}</h3>
182 <span className="inline-flex h-5 w-5 shrink-0 items-center justify-center">
183 {resetAction}
184 </span>
185 </div>
186 <p className="max-w-xl text-[13px] leading-[1.45] text-muted-foreground/80">
187 {description}
188 </p>
189 {status ? <div className="pt-0.5 text-xs text-muted-foreground">{status}</div> : null}
190 </div>
191 {control ? (
192 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
193 {control}
194 </div>
195 ) : null}
196 </div>
197 {children}
198 </div>
199 );
200}
201
202export function SettingResetButton({ label, onClick }: { label: string; onClick: () => void }) {
203 return (
204 <Tooltip>
205 <TooltipTrigger
206 render={
207 <Button
208 size="icon-xs"
209 variant="ghost"
210 aria-label={`Reset ${label} to default`}
211 className="size-5 rounded-sm p-0 text-muted-foreground hover:text-foreground"
212 onClick={(event) => {
213 event.stopPropagation();
214 onClick();
215 }}
216 >
217 <Undo2Icon className="size-3" />
218 </Button>
219 }
220 />
221 <TooltipPopup side="top">Reset to default</TooltipPopup>
222 </Tooltip>
223 );
224}
225
226export function SettingsPageContainer({
227 children,
228 className,
229}: {
230 children: ReactNode;
231 className?: string;
232}) {
233 const navigate = useNavigate();
234 const hash = useLocation({ select: (location) => location.hash });
235 const targetId = hash.replace(/^#/, "") || null;
236 const clearTargetHash = useCallback(() => {
237 void navigate({ hash: "", replace: true, resetScroll: false, hashScrollIntoView: false });
238 }, [navigate]);
239
240 return (
241 <SettingsSearchTargetProvider targetId={targetId} onTargetHandled={clearTargetHash}>
242 <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">
243 <div className={cn("mx-auto flex w-full max-w-4xl flex-col gap-12", className)}>
244 {children}
245 </div>
246 </div>
247 </SettingsSearchTargetProvider>
248 );
249}
250
251export function scrollToSettingsTarget(targetId: string): boolean {
252 const target = document.getElementById(targetId);
253 if (!target) return false;
254 scrollAndFocusSettingsTarget(target);
255 return true;
256}