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

settings-search.ts

167 lines4.4 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 "project grouping",
56 "group projects",
57 "organization",
58 "sidebar",
59 "working section",
60 "new threads",
61 "last project",
62 ],
63 SettingsThreads: [
64 "new threads",
65 "default workspace",
66 "default permissions",
67 "access",
68 "response streaming",
69 "preview browser",
70 "browser access",
71 "auto-settle",
72 "settle",
73 "snooze",
74 "usage limits",
75 "auto-resume",
76 "thread behavior",
77 ],
78 SettingsFollowUp: ["follow-ups", "queue", "steer", "while the agent is running"],
79 SettingsArchive: ["archive", "archived", "restore", "unarchive"],
80 SettingsProviderAccounts: [
81 "provider accounts",
82 "codex",
83 "claude",
84 "cursor",
85 "opencode",
86 "sign in",
87 "login",
88 "auth",
89 ],
90 SettingsEnvironmentSourceControl: [
91 "git",
92 "pull requests",
93 "co-authors",
94 "agent credits",
95 "default branch",
96 "automatically pull",
97 "clone into host folders",
98 "ghq",
99 ],
100 SettingsWorktrees: [
101 "worktree",
102 "branch naming",
103 "branch prefix",
104 "submodules",
105 "start from remote",
106 ],
107 SettingsLabs: [
108 "beta",
109 "linked environments",
110 "link machine",
111 "fast worktree checkout",
112 "clone cache",
113 ],
114 SettingsEnvironmentMaintenance: ["updates", "provider updates", "restart", "manage environments"],
115 SettingsAbout: [
116 "version",
117 "diagnostics",
118 "client storage",
119 "cache",
120 "legal",
121 "privacy",
122 "terms",
123 "open source licenses",
124 ],
125} satisfies Partial<Record<SettingsPageTarget | "account", readonly string[]>>;
126const termsById: ReadonlyMap<string, readonly string[]> = new Map(
127 Object.entries(SETTINGS_SEARCH_TERMS),
128);
129​
130export interface SettingsSearchRow {
131 readonly id: string;
132 readonly label: string;
134​
135export interface SettingsSearchSection<Row extends SettingsSearchRow> {
136 readonly title?: string;
137 readonly rows: readonly Row[];
139​
140/**
141 * Keeps the rows whose label, section, or page contents match `query`, in list order, and drops
142 * sections left empty. Matching is the command palette's, so both searches agree.
143 */
144export function filterSettingsSections<
145 Row extends SettingsSearchRow,
146 Section extends SettingsSearchSection<Row>,
147>(sections: readonly Section[], query: string): Section[] {
148 const normalizedQuery = foldSearchQuery(query);
149 const rows = sections.flatMap((section) =>
150 section.rows.map((row) => ({ row, section: section.title })),
151 );
152 const matched = normalizedQuery
153 ? new Set(
154 rankSearchItems(rows, ({ row, section }, options) =>
155 scoreSearchTerms(
156 normalizedQuery,
157 [row.label, ...(section ? [section] : []), ...(termsById.get(row.id) ?? [])],
158 options,
159 ),
160 ).map(({ item }) => item.row),
161 )
162 : null;
163 return sections.flatMap((section) => {
164 const kept = matched ? section.rows.filter((row) => matched.has(row)) : section.rows;
165 return kept.length ? [{ ...section, rows: kept }] : [];
166 });