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

settingsSearch.ts

255 lines6.2 KB

1import { isElectron } from "~/env";
2​
3export type SettingsPath =
4 | "/settings/general"
5 | "/settings/appearance"
6 | "/settings/keybindings"
7 | "/settings/providers"
8 | "/settings/source-control"
9 | "/settings/connections"
10 | "/settings/archived";
11​
12export interface SettingsSearchItem {
13 readonly id: string;
14 readonly title: string;
15 readonly to: SettingsPath;
16 readonly targetId?: string;
17 // Its row only renders in the desktop app, so a browser result would land on
18 // an anchor that isn't there.
19 readonly desktopOnly?: boolean;
21​
22/**
23 * Section labels in sidebar order. The sidebar nav and the search-result
24 * subtitles both render from this record, so each label exists once.
25 */
26export const SETTINGS_SECTION_LABELS: Readonly<Record<SettingsPath, string>> = {
27 "/settings/general": "General",
28 "/settings/appearance": "Appearance",
29 "/settings/keybindings": "Keybindings",
30 "/settings/providers": "Providers",
31 "/settings/source-control": "Source Control",
32 "/settings/connections": "Connections",
33 "/settings/archived": "Archive",
34};
35​
36/**
37 * Every searchable setting, in result order. This catalog is the single
38 * source of truth for anchor ids and visible titles: panels render both via
39 * `searchableSetting`, so a retitle (or, later, a translation pass) happens
40 * here once instead of separately in the panel and the index.
41 */
42export const SETTINGS_SEARCH_ITEMS = [
43 {
44 id: "color-scheme",
45 title: "Color scheme",
46 to: "/settings/appearance",
47 // The scheme tiles sit at the top of the Appearance section.
48 targetId: "appearance",
49 },
50 {
51 id: "theme",
52 title: "Themes",
53 to: "/settings/appearance",
54 // Theme cards live directly under the scheme tiles; the section is the
55 // stable scroll destination for both.
56 targetId: "appearance",
57 },
58 {
59 // Prefixed because the slider control already owns the `glass-opacity` id.
60 id: "setting-glass-opacity",
61 title: "Glass opacity",
62 to: "/settings/appearance",
63 },
64 {
65 id: "environment-identification",
66 title: "Environment identification",
67 to: "/settings/appearance",
68 // The setting is stage-dependent, so its parent section is the stable destination.
69 targetId: "appearance",
70 },
71 {
72 id: "interface-font",
73 title: "Interface font",
74 to: "/settings/appearance",
75 },
76 {
77 id: "prompt-font",
78 title: "Prompt font",
79 to: "/settings/appearance",
80 },
81 {
82 id: "code-font",
83 title: "Code font",
84 to: "/settings/appearance",
85 },
86 {
87 id: "terminal-font",
88 title: "Terminal font",
89 to: "/settings/appearance",
90 },
91 {
92 id: "font-smoothing",
93 title: "Font smoothing",
94 to: "/settings/appearance",
95 },
96 {
97 id: "word-wrap",
98 title: "Word wrap",
99 to: "/settings/appearance",
100 },
101 {
102 id: "project-grouping",
103 title: "Project grouping",
104 to: "/settings/general",
105 },
106 {
107 id: "auto-settle-inactive-threads",
108 title: "Auto-settle inactive threads",
109 to: "/settings/general",
110 },
111 {
112 id: "auto-settle-merged-threads",
113 title: "Auto-settle merged threads",
114 to: "/settings/general",
115 },
116 {
117 id: "time-format",
118 title: "Time format",
119 to: "/settings/general",
120 },
121 {
122 id: "hide-whitespace-changes",
123 title: "Hide whitespace changes",
124 to: "/settings/general",
125 },
126 {
127 id: "provider-update-checks",
128 title: "Provider update checks",
129 to: "/settings/general",
130 },
131 {
132 id: "new-threads",
133 title: "New threads",
134 to: "/settings/general",
135 },
136 {
137 id: "start-from-origin",
138 title: "Start from origin",
139 to: "/settings/general",
140 targetId: "new-threads",
141 },
142 {
143 id: "add-project-starts-in",
144 title: "Add project starts in",
145 to: "/settings/general",
146 },
147 {
148 id: "archive-confirmation",
149 title: "Archive confirmation",
150 to: "/settings/general",
151 },
152 {
153 id: "delete-confirmation",
154 title: "Delete confirmation",
155 to: "/settings/general",
156 },
157 {
158 id: "quit-confirmation",
159 title: "Hold to quit",
160 to: "/settings/general",
161 desktopOnly: true,
162 },
163 {
164 id: "text-generation-model",
165 title: "Text generation model",
166 to: "/settings/general",
167 },
168 {
169 id: "diagnostics",
170 title: "Diagnostics",
171 to: "/settings/general",
172 },
173 {
174 id: "legacy-plan-mode",
175 title: "Plan mode (legacy)",
176 to: "/settings/general",
177 },
178 {
179 id: "legacy-token-streaming",
180 title: "Stream token by token (legacy)",
181 to: "/settings/general",
182 },
183 {
184 id: "legacy-sidebar",
185 title: "Sidebar (legacy)",
186 to: "/settings/general",
187 },
188 {
189 id: "keybindings",
190 title: "Keybindings",
191 to: "/settings/keybindings",
192 },
193 {
194 id: "providers",
195 title: "Providers",
196 to: "/settings/providers",
197 },
198 {
199 id: "source-control",
200 title: "Source control",
201 to: "/settings/source-control",
202 },
203 {
204 id: "remote-environments",
205 title: "Remote environments",
206 to: "/settings/connections",
207 },
208 {
209 id: "archive",
210 title: "Archived threads",
211 to: "/settings/archived",
212 },
213] as const satisfies ReadonlyArray<SettingsSearchItem>;
214​
215export type SettingsSearchItemId = (typeof SETTINGS_SEARCH_ITEMS)[number]["id"];
216​
217const SEARCH_ITEMS_BY_ID = Object.fromEntries(
218 SETTINGS_SEARCH_ITEMS.map((item) => [item.id, item]),
219) as Readonly<Record<SettingsSearchItemId, SettingsSearchItem>>;
220​
221/**
222 * `id` and `title` props for the element a search item anchors to. Panels
223 * spread (or pick from) this instead of restating the strings, so the catalog
224 * and the rendered settings cannot drift apart.
225 */
226export function searchableSetting(id: SettingsSearchItemId): {
227 readonly id: string;
228 readonly title: string;
229} {
230 const { id: anchorId, title } = SEARCH_ITEMS_BY_ID[id];
231 return { id: anchorId, title };
233​
234function normalizeSearchText(value: string): string {
235 return value
236 .normalize("NFKD")
237 .replace(/[\u0300-\u036f]/g, "")
238 .toLocaleLowerCase()
239 .replace(/\s+/g, " ")
240 .trim();
242​
243export function searchSettings(
244 query: string,
245 items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS,
246): ReadonlyArray<SettingsSearchItem> {
247 const normalizedQuery = normalizeSearchText(query);
248 if (normalizedQuery.length === 0) return [];
249​
250 return items.filter(
251 (item) =>
252 (isElectron || item.desktopOnly !== true) &&
253 normalizeSearchText(item.title).includes(normalizedQuery),
254 );