Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settings-search.ts

171 lines4.5 KB

1import { foldSearchQuery, rankSearchItems, scoreSearchTerms } from "@t3tools/shared/paletteSearch";
2​
3import type { SettingsPageTarget } from "./components/settings-page-targets";
4​
5/** What each page holds besides its row's label, so search finds a setting by its own name. */
6const SETTINGS_SEARCH_TERMS = {
7 account: ["sign in", "sign out", "login", "clerk", "connect"],
8 SettingsEnvironments: [
9 "servers",
10 "connections",
11 "pair",
12 "add environment",
13 "remote",
14 "tailscale",
15 ],
16 SettingsNotifications: ["push", "alerts", "live activity", "agent activity"],
17 SettingsAppearance: [
18 "theme",
19 "dark mode",
20 "light mode",
21 "colors",
22 "font size",
23 "text size",
24 "code",
25 "diffs",
26 "terminal",
27 "word break",
28 "math",
29 "tool calls in chat",
30 "hide tool calls",
31 "commands edits searches",
32 "work log",
33 "zen",
34 "final answers only",
35 ],
36 SettingsNavigation: ["tabs", "tab order", "reorder", "hide tabs"],
37 SettingsKeyboard: ["return key", "send message", "new line", "hardware keyboard"],
38 SettingsVoice: [
39 "microphone",
40 "dictation",
41 "transcription",
42 "transcribe on",
43 "transcription model",
44 "this device",
45 "environment",
46 "on-device",
47 "audio input",
48 "preferred microphone",
49 "microphone order",
50 "bluetooth",
51 ],
52 SettingsScheduledTasks: ["automations", "schedule", "cron", "recurring", "webhook"],
53 SettingsProjectOverview: ["project", "overview", "checkouts", "delete project"],
54 SettingsOrganization: [
55 "language",
56 "locale",
57 "translation",
58 "english",
59 "project grouping",
60 "group projects",
61 "organization",
62 "sidebar",
63 "working section",
64 "new threads",
65 "last project",
66 ],
67 SettingsThreads: [
68 "new threads",
69 "default workspace",
70 "default permissions",
71 "access",
72 "response streaming",
73 "preview browser",
74 "browser access",
75 "auto-settle",
76 "settle",
77 "snooze",
78 "usage limits",
79 "auto-resume",
80 "thread behavior",
81 ],
82 SettingsFollowUp: ["follow-ups", "queue", "steer", "while the agent is running"],
83 SettingsArchive: ["archive", "archived", "restore", "unarchive"],
84 SettingsProviderAccounts: [
85 "provider accounts",
86 "codex",
87 "claude",
88 "cursor",
89 "opencode",
90 "sign in",
91 "login",
92 "auth",
93 ],
94 SettingsEnvironmentSourceControl: [
95 "git",
96 "pull requests",
97 "co-authors",
98 "agent credits",
99 "default branch",
100 "automatically pull",
101 "clone into host folders",
102 "ghq",
103 ],
104 SettingsWorktrees: [
105 "worktree",
106 "branch naming",
107 "branch prefix",
108 "submodules",
109 "start from remote",
110 ],
111 SettingsLabs: [
112 "beta",
113 "linked environments",
114 "link machine",
115 "fast worktree checkout",
116 "clone cache",
117 ],
118 SettingsEnvironmentMaintenance: ["updates", "provider updates", "restart", "manage environments"],
119 SettingsAbout: [
120 "version",
121 "diagnostics",
122 "client storage",
123 "cache",
124 "legal",
125 "privacy",
126 "terms",
127 "open source licenses",
128 ],
129} satisfies Partial<Record<SettingsPageTarget | "account", readonly string[]>>;
130const termsById: ReadonlyMap<string, readonly string[]> = new Map(
131 Object.entries(SETTINGS_SEARCH_TERMS),
132);
133​
134export interface SettingsSearchRow {
135 readonly id: string;
136 readonly label: string;
138​
139export interface SettingsSearchSection<Row extends SettingsSearchRow> {
140 readonly title?: string;
141 readonly rows: readonly Row[];
143​
144/**
145 * Keeps the rows whose label, section, or page contents match `query`, in list order, and drops
146 * sections left empty. Matching is the command palette's, so both searches agree.
147 */
148export function filterSettingsSections<
149 Row extends SettingsSearchRow,
150 Section extends SettingsSearchSection<Row>,
151>(sections: readonly Section[], query: string): Section[] {
152 const normalizedQuery = foldSearchQuery(query);
153 const rows = sections.flatMap((section) =>
154 section.rows.map((row) => ({ row, section: section.title })),
155 );
156 const matched = normalizedQuery
157 ? new Set(
158 rankSearchItems(rows, ({ row, section }, options) =>
159 scoreSearchTerms(
160 normalizedQuery,
161 [row.label, ...(section ? [section] : []), ...(termsById.get(row.id) ?? [])],
162 options,
163 ),
164 ).map(({ item }) => item.row),
165 )
166 : null;
167 return sections.flatMap((section) => {
168 const kept = matched ? section.rows.filter((row) => matched.has(row)) : section.rows;
169 return kept.length ? [{ ...section, rows: kept }] : [];
170 });