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

settings-search.ts

140 lines4.0 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 ],
30 SettingsNavigation: ["tabs", "tab order", "reorder", "hide tabs"],
31 SettingsKeyboard: ["return key", "send message", "new line", "hardware keyboard"],
32 SettingsMicrophone: ["voice", "dictation", "audio input", "bluetooth"],
33 SettingsScheduledTasks: ["automations", "schedule", "cron", "recurring", "webhook"],
34 SettingsProjectOverview: ["project", "overview", "checkouts", "delete project"],
35 SettingsOrganization: [
36 "project grouping",
37 "group projects",
38 "organization",
39 "sidebar",
40 "working section",
41 "new threads",
42 "last project",
43 ],
44 SettingsThreads: [
45 "new threads",
46 "default workspace",
47 "default permissions",
48 "access",
49 "response streaming",
50 "preview browser",
51 "browser access",
52 "auto-settle",
53 "settle",
54 "snooze",
55 "usage limits",
56 "auto-resume",
57 "thread behavior",
58 ],
59 SettingsFollowUp: ["follow-ups", "queue", "steer", "while the agent is running"],
60 SettingsArchive: ["archive", "archived", "restore", "unarchive"],
61 SettingsProviderAccounts: [
62 "provider accounts",
63 "codex",
64 "claude",
65 "cursor",
66 "opencode",
67 "sign in",
68 "login",
69 "auth",
70 ],
71 SettingsEnvironmentSourceControl: [
72 "git",
73 "pull requests",
74 "co-authors",
75 "agent credits",
76 "default branch",
77 "automatically pull",
78 ],
79 SettingsWorktrees: [
80 "worktree",
81 "branch naming",
82 "branch prefix",
83 "submodules",
84 "start from remote",
85 ],
86 SettingsLabs: ["legacy", "plan mode", "beta", "linked environments", "link machine"],
87 SettingsEnvironmentMaintenance: ["updates", "provider updates", "restart", "manage environments"],
88 SettingsAbout: [
89 "version",
90 "diagnostics",
91 "client storage",
92 "cache",
93 "legal",
94 "privacy",
95 "terms",
96 "open source licenses",
97 ],
98} satisfies Partial<Record<SettingsPageTarget | "account", readonly string[]>>;
99const termsById: ReadonlyMap<string, readonly string[]> = new Map(
100 Object.entries(SETTINGS_SEARCH_TERMS),
101);
102​
103export interface SettingsSearchRow {
104 readonly id: string;
105 readonly label: string;
107​
108export interface SettingsSearchSection<Row extends SettingsSearchRow> {
109 readonly title?: string;
110 readonly rows: readonly Row[];
112​
113/**
114 * Keeps the rows whose label, section, or page contents match `query`, in list order, and drops
115 * sections left empty. Matching is the command palette's, so both searches agree.
116 */
117export function filterSettingsSections<
118 Row extends SettingsSearchRow,
119 Section extends SettingsSearchSection<Row>,
120>(sections: readonly Section[], query: string): Section[] {
121 const normalizedQuery = foldSearchQuery(query);
122 const rows = sections.flatMap((section) =>
123 section.rows.map((row) => ({ row, section: section.title })),
124 );
125 const matched = normalizedQuery
126 ? new Set(
127 rankSearchItems(rows, ({ row, section }, options) =>
128 scoreSearchTerms(
129 normalizedQuery,
130 [row.label, ...(section ? [section] : []), ...(termsById.get(row.id) ?? [])],
131 options,
132 ),
133 ).map(({ item }) => item.row),
134 )
135 : null;
136 return sections.flatMap((section) => {
137 const kept = matched ? section.rows.filter((row) => matched.has(row)) : section.rows;
138 return kept.length ? [{ ...section, rows: kept }] : [];
139 });