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

settingsSearch.ts

228 lines5.5 KB

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