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