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