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

settings-search.ts

120 lines3.8 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", "checkouts", "worktrees", "delete project"],
35 SettingsOrganization: ["project grouping", "group projects", "sidebar"],
36 SettingsThreads: [
37 "auto-settle",
38 "settle",
39 "snooze",
40 "usage limits",
41 "auto-resume",
42 "plan mode",
43 "working section",
44 ],
45 SettingsFollowUp: ["queue", "steer", "while the agent is running"],
46 SettingsArchive: ["archive", "archived", "restore", "unarchive"],
47 SettingsProviderAccounts: ["codex", "claude", "cursor", "opencode", "sign in", "login", "auth"],
48 SettingsEnvironmentNewThreads: [
49 "default workspace",
50 "worktree",
51 "submodules",
52 "default permissions",
53 "access",
54 ],
55 SettingsEnvironmentSourceControl: [
56 "git",
57 "branch naming",
58 "pull requests",
59 "co-authors",
60 "agent credits",
61 "default branch",
62 "automatically pull",
63 "worktrees",
64 "start from remote",
65 ],
66 SettingsEnvironmentAgentBehavior: ["response streaming", "preview browser", "browser access"],
67 SettingsEnvironmentMaintenance: ["updates", "provider updates", "restart", "manage environments"],
68 SettingsAbout: [
69 "version",
70 "diagnostics",
71 "client storage",
72 "cache",
73 "legal",
74 "privacy",
75 "terms",
76 "open source licenses",
77 ],
78} satisfies Partial<Record<SettingsPageTarget | "account", readonly string[]>>;
79const termsById: ReadonlyMap<string, readonly string[]> = new Map(
80 Object.entries(SETTINGS_SEARCH_TERMS),
81);
82​
83export interface SettingsSearchRow {
84 readonly id: string;
85 readonly label: string;
87​
88export interface SettingsSearchSection<Row extends SettingsSearchRow> {
89 readonly title?: string;
90 readonly rows: readonly Row[];
92​
93/**
94 * Keeps the rows whose label, section, or page contents match `query`, in list order, and drops
95 * sections left empty. Matching is the command palette's, so both searches agree.
96 */
97export function filterSettingsSections<
98 Row extends SettingsSearchRow,
99 Section extends SettingsSearchSection<Row>,
100>(sections: readonly Section[], query: string): Section[] {
101 const normalizedQuery = foldSearchQuery(query);
102 const rows = sections.flatMap((section) =>
103 section.rows.map((row) => ({ row, section: section.title })),
104 );
105 const matched = normalizedQuery
106 ? new Set(
107 rankSearchItems(rows, ({ row, section }, options) =>
108 scoreSearchTerms(
109 normalizedQuery,
110 [row.label, ...(section ? [section] : []), ...(termsById.get(row.id) ?? [])],
111 options,
112 ),
113 ).map(({ item }) => item.row),
114 )
115 : null;
116 return sections.flatMap((section) => {
117 const kept = matched ? section.rows.filter((row) => matched.has(row)) : section.rows;
118 return kept.length ? [{ ...section, rows: kept }] : [];
119 });