Open History to see the last commit
1import { isElectron } from "~/env";
2import { isMacPlatform, isWindowsPlatform, normalizeSearchText } from "~/lib/utils";
3import { STATIC_KEYBINDING_COMMANDS, type KeybindingCommand } from "@t3tools/contracts";
4import type { EnvironmentId } from "@t3tools/contracts";
5import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
6import { DEFAULT_KEYBINDINGS } from "@t3tools/shared/keybindings";
7import { commandLabel } from "./KeybindingsSettings.logic";
8import {
9 validateSettingsScopeSearch,
10 type ResolvedSettingsScope,
11 type SettingsScopeSearch,
12} from "./settingsScope";
13
14export type SettingsPath =
15 | "/settings/projects"
16 | "/settings/general"
17 | "/settings/appearance"
18 | "/settings/keybindings"
19 | "/settings/snap-shot"
20 | "/settings/providers"
21 | "/settings/integrations"
22 | "/settings/source-control"
23 | "/settings/storage"
24 | "/settings/connections"
25 | "/settings/archived";
26
27/**
28 * Where a setting can be edited. Device-local rows have no scope: they render
29 * at every selection. `project-defaults` rows accept project overrides, so
30 * they are reachable from any server-backed selection.
31 */
32export type SettingsSearchScope =
33 | "environment"
34 | "environment-defaults"
35 | "project-defaults"
36 | "project"
37 | "checkout"
38 | "connections";
39
40export interface SettingsSearchItem {
41 readonly id: string;
42 readonly title: string;
43 readonly to: SettingsPath;
44 readonly targetId?: string;
45 /** Descriptions, option labels, and aliases people may remember instead of the title. */
46 readonly searchTerms?: ReadonlyArray<string>;
47 readonly scope?: SettingsSearchScope;
48 // Its row only renders in the desktop app, so a browser result would land on
49 // an anchor that isn't there.
50 readonly desktopOnly?: boolean;
51 readonly macOnly?: boolean;
52 // Its row only renders on Windows desktop, so other desktop platforms must
53 // not expose a result that points to a missing anchor.
54 readonly windowsOnly?: boolean;
55 readonly cloudOnly?: boolean;
56 readonly environmentOnly?: boolean;
57 readonly providerSettingsOnly?: boolean;
58 readonly macProviderSettingsOnly?: boolean;
59 readonly localBackendManagementOnly?: boolean;
60 readonly localEnvironmentOnly?: boolean;
61 readonly wslAvailableOnly?: boolean;
62 /**
63 * Sorts after every other match. Keybinding commands mirror rows on other
64 * surfaces, so "model" must still lead with Default model, not Model Picker.
65 */
66 readonly secondary?: boolean;
67 readonly requiresThreadAutoSettlement?: boolean;
68}
69
70export interface SettingsSearchAvailability {
71 readonly localEnvironmentDisabled?: boolean;
72 readonly hasCloudPublicConfig: boolean;
73 readonly hasEnvironment: boolean;
74 readonly hasProviderSettingsEnvironment: boolean;
75 readonly hasMacProviderSettingsEnvironment: boolean;
76 readonly canManageLocalBackend: boolean;
77 readonly isWslSettingsRowVisible: boolean;
78 readonly hasThreadAutoSettlement: boolean;
79}
80
81/**
82 * Section labels in sidebar order. The sidebar nav and the search-result
83 * subtitles both render from this record, so each label exists once.
84 */
85export const SETTINGS_SECTION_LABELS: Readonly<Record<SettingsPath, string>> = {
86 "/settings/projects": "Project",
87 "/settings/general": "General",
88 "/settings/appearance": "Appearance",
89 "/settings/keybindings": "Keybindings",
90 "/settings/snap-shot": "SnapShots",
91 "/settings/providers": "Providers",
92 "/settings/integrations": "Integrations",
93 "/settings/source-control": "Source Control",
94 "/settings/storage": "Storage",
95 "/settings/connections": "Connections",
96 "/settings/archived": "Archive",
97};
98
99/** Anchor id of the first row bound to `command` on the Keybindings page. */
100export function keybindingSearchAnchorId<Command extends KeybindingCommand>(command: Command) {
101 return `keybinding-${command}` as const;
102}
103
104/**
105 * One result per built-in command, alphabetical by label. The anchor is
106 * the command's first row; default keys are searchable so "mod+b" lands on
107 * Sidebar: Toggle. A command with no default binding may have no row, so it
108 * points at the section instead.
109 */
110const KEYBINDING_SEARCH_ITEMS = STATIC_KEYBINDING_COMMANDS.toSorted((left, right) =>
111 commandLabel(left).localeCompare(commandLabel(right)),
112).map((command) => {
113 const defaultKeys = DEFAULT_KEYBINDINGS.filter((binding) => binding.command === command).map(
114 (binding) => binding.key,
115 );
116 return {
117 id: keybindingSearchAnchorId(command),
118 title: commandLabel(command),
119 to: "/settings/keybindings" as const,
120 searchTerms: [command, ...defaultKeys],
121 secondary: true,
122 ...(defaultKeys.length === 0 ? { targetId: "keybindings" } : {}),
123 };
124});
125
126/**
127 * Searchable settings and stable destinations, in result order. Rows with a
128 * dedicated anchor render their id and title via `searchableSetting`; items
129 * that may not be mounted point at their nearest stable section instead.
130 */
131export const SETTINGS_SEARCH_ITEMS = [
132 {
133 id: "storage-worktrees",
134 title: "Worktree cleanup",
135 to: "/settings/storage",
136 scope: "project-defaults",
137 searchTerms: [
138 "disk storage delete deleted archived threads old inactive merged unchanged worktrees retention days project inherit off custom",
139 ],
140 },
141 {
142 id: "storage-artifacts",
143 title: "Artifacts and logs",
144 to: "/settings/storage",
145 scope: "environment-defaults",
146 searchTerms: ["disk storage browser screenshots captures rotated logs cleanup retention"],
147 },
148 {
149 id: "project-defaults",
150 title: "Project defaults and overrides",
151 to: "/settings/general",
152 scope: "project-defaults",
153 searchTerms: ["model workspace environments projects inheritance checkout"],
154 },
155 {
156 id: "project-overview",
157 title: "Project overview",
158 to: "/settings/projects",
159 searchTerms: ["name icon emoji image checkout remove delete"],
160 },
161 {
162 id: "default-model",
163 title: "Default model",
164 to: "/settings/general",
165 scope: "project-defaults",
166 searchTerms: ["new thread project provider reasoning effort"],
167 },
168 {
169 id: "default-permissions",
170 title: "Permissions",
171 to: "/settings/general",
172 scope: "project-defaults",
173 searchTerms: [
174 "new thread default runtime mode supervised approvals auto accept edits full access",
175 ],
176 },
177 {
178 id: "color-scheme",
179 title: "Color scheme",
180 to: "/settings/appearance",
181 searchTerms: ["appearance light dark system mode"],
182 // The scheme tiles sit at the top of the Appearance section.
183 targetId: "appearance",
184 },
185 {
186 id: "theme",
187 title: "Themes",
188 to: "/settings/appearance",
189 searchTerms: ["appearance colors palette custom import"],
190 // Theme cards live directly under the scheme tiles; the section is the
191 // stable scroll destination for both.
192 targetId: "appearance",
193 },
194 {
195 // Prefixed because the slider control already owns the `appearance-contrast` id.
196 id: "setting-appearance-contrast",
197 title: "Contrast",
198 to: "/settings/appearance",
199 searchTerms: ["colors borders interface"],
200 },
201 {
202 // Prefixed because the slider control already owns the `glass-opacity` id.
203 id: "setting-glass-opacity",
204 title: "Glass opacity",
205 to: "/settings/appearance",
206 searchTerms: ["transparent transparency solid menus dialogs composer"],
207 },
208 {
209 id: "diff-color-scheme",
210 title: "Diff colors",
211 to: "/settings/appearance",
212 searchTerms: ["red green blue orange additions deletions changes counts palette colorblind"],
213 },
214 {
215 id: "chat-width",
216 title: "Chat width",
217 to: "/settings/appearance",
218 searchTerms: ["wide full width column layout messages composer monitor"],
219 },
220 {
221 id: "panel-animations",
222 title: "Panel animations",
223 to: "/settings/appearance",
224 },
225 {
226 id: "environment-identification",
227 title: "Environment identification",
228 to: "/settings/appearance",
229 searchTerms: ["dev nightly artwork pill label hide none"],
230 // The setting is stage-dependent, so its parent section is the stable destination.
231 targetId: "appearance-interface",
232 },
233 {
234 id: "interface-font",
235 title: "Interface font",
236 to: "/settings/appearance",
237 searchTerms: ["typography family size system sans"],
238 },
239 {
240 id: "prompt-font",
241 title: "Prompt font",
242 to: "/settings/appearance",
243 searchTerms: ["typography family size composer input"],
244 },
245 {
246 id: "code-font",
247 title: "Code font",
248 to: "/settings/appearance",
249 searchTerms: ["typography family size monospace code blocks diffs file previews"],
250 },
251 {
252 id: "terminal-font",
253 title: "Terminal font",
254 to: "/settings/appearance",
255 searchTerms: ["typography family size monospace output"],
256 },
257 {
258 id: "font-smoothing",
259 title: "Font smoothing",
260 to: "/settings/appearance",
261 searchTerms: ["typography text grayscale anti aliasing macos thin"],
262 macOnly: true,
263 },
264 {
265 id: "word-wrap",
266 title: "Word wrap",
267 to: "/settings/appearance",
268 searchTerms: ["long lines code blocks tables diffs file previews"],
269 },
270 {
271 id: "project-grouping",
272 title: "Project grouping",
273 to: "/settings/general",
274 searchTerms: ["combine matching repositories environments sidebar"],
275 },
276 {
277 id: "auto-settle-inactive-threads",
278 title: "Auto-settle inactive threads",
279 to: "/settings/general",
280 searchTerms: ["sidebar inactivity days no activity automatically"],
281 requiresThreadAutoSettlement: true,
282 scope: "project-defaults",
283 },
284 {
285 id: "auto-settle-merged-threads",
286 title: "Auto-settle merged threads",
287 to: "/settings/general",
288 searchTerms: ["pull request merge closed automatically sidebar"],
289 requiresThreadAutoSettlement: true,
290 scope: "project-defaults",
291 },
292 {
293 id: "days-before-auto-settle",
294 title: "Days of inactivity before auto-settle",
295 to: "/settings/general",
296 targetId: "auto-settle-inactive-threads",
297 searchTerms: ["thread timeout activity sidebar"],
298 requiresThreadAutoSettlement: true,
299 scope: "project-defaults",
300 },
301 {
302 id: "thread-notifications",
303 title: "Thread notifications",
304 to: "/settings/general",
305 searchTerms: ["notification sound alert completion input approval desktop"],
306 },
307 {
308 id: "in-app-notifications",
309 title: "In-app notifications",
310 to: "/settings/general",
311 searchTerms: ["notification toast popup completion input approval failure"],
312 },
313 {
314 id: "time-format",
315 title: "Time format",
316 to: "/settings/general",
317 searchTerms: ["timestamp clock locale system browser os 12 hour 24 hour"],
318 },
319 {
320 id: "response-streaming",
321 title: "Response streaming",
322 to: "/settings/general",
323 scope: "project-defaults",
324 searchTerms: ["output token paragraph buffered wait turn legacy"],
325 },
326 {
327 id: "hide-whitespace-changes",
328 title: "Hide whitespace changes",
329 to: "/settings/general",
330 searchTerms: ["diff ignore spaces edits default"],
331 },
332 {
333 id: "default-diff-file-state",
334 title: "Default diff file state",
335 to: "/settings/general",
336 searchTerms: ["collapsed expanded collapse expand files pull request pr code tab"],
337 },
338 {
339 id: "diff-layout",
340 title: "Diff layout",
341 to: "/settings/general",
342 searchTerms: ["stacked split side by side unified inline view"],
343 },
344 {
345 id: "proactive-panels",
346 title: "Proactive panels",
347 to: "/settings/general",
348 searchTerms: ["automatically open diff pull request pr right panel agent completion"],
349 },
350 {
351 id: "skills-in-slash-menu",
352 title: "Show skills in slash menu",
353 to: "/settings/general",
354 searchTerms: ["command menu dollar $ slash /"],
355 },
356 {
357 id: "composer-rich-text",
358 title: "Rich text composer",
359 to: "/settings/general",
360 searchTerms: ["composer rich text tiptap bold italic markdown styled wysiwyg"],
361 },
362 {
363 id: "composer-collapse",
364 title: "Collapse composer on scroll",
365 to: "/settings/general",
366 searchTerms: ["composer rest resting scroll wheel conversation timeline shrink minimize"],
367 },
368 {
369 id: "send-shortcut",
370 title: "Send shortcut",
371 to: "/settings/general",
372 searchTerms: ["enter return command ctrl multiline prompt new line composer"],
373 },
374 {
375 id: "follow-up-behavior",
376 title: "Follow-up behavior",
377 to: "/settings/general",
378 searchTerms: ["queue steer running turn send default behavior composer"],
379 },
380 {
381 id: "provider-update-checks",
382 title: "Provider update checks",
383 to: "/settings/general",
384 searchTerms: ["installed cli versions newer available codex claude cursor grok opencode"],
385 scope: "environment-defaults",
386 },
387 {
388 id: "continue-threads-after-server-update",
389 title: "Continue threads after restarts",
390 to: "/settings/general",
391 scope: "project-defaults",
392 searchTerms: [
393 "resume running active interrupted work restart reboot machine crash desktop update automatically",
394 ],
395 },
396 {
397 id: "background-activity",
398 title: "Background activity",
399 to: "/settings/general",
400 scope: "environment-defaults",
401 searchTerms: [
402 "balanced performance battery saver advanced git fetch provider health refresh host power monitor idle policy",
403 ],
404 },
405 {
406 id: "new-threads",
407 title: "New threads",
408 to: "/settings/general",
409 scope: "project-defaults",
410 searchTerms: ["default workspace mode draft local worktree"],
411 },
412 {
413 id: "worktree-submodules",
414 title: "Submodules",
415 to: "/settings/general",
416 scope: "project-defaults",
417 searchTerms: ["git submodule init recursive top-level none worktree t3.json"],
418 },
419 {
420 id: "start-from-origin",
421 title: "Start from origin",
422 to: "/settings/general",
423 scope: "project-defaults",
424 searchTerms: ["new worktrees latest matching remote branch local"],
425 },
426 {
427 id: "add-project-starts-in",
428 title: "Add project starts in",
429 to: "/settings/general",
430 scope: "environment-defaults",
431 searchTerms: ["base directory folder browser path home"],
432 },
433 {
434 id: "unpin-confirmation",
435 title: "Unpin confirmation",
436 to: "/settings/general",
437 searchTerms: ["ask before thread pinned section"],
438 },
439 {
440 id: "archive-confirmation",
441 title: "Archive confirmation",
442 to: "/settings/general",
443 searchTerms: ["ask before thread second click inline action"],
444 },
445 {
446 id: "delete-confirmation",
447 title: "Delete confirmation",
448 to: "/settings/general",
449 searchTerms: ["ask before thread chat history"],
450 },
451 {
452 id: "quit-confirmation",
453 title: "Quit shortcut",
454 to: "/settings/general",
455 searchTerms: ["confirmation desktop app exit direct hold double click press twice"],
456 desktopOnly: true,
457 },
458 {
459 id: "text-generation-model",
460 title: "Text generation model",
461 to: "/settings/general",
462 scope: "project-defaults",
463 searchTerms: ["generated thread titles source control content default provider"],
464 },
465 {
466 id: "diagnostics",
467 title: "Diagnostics",
468 to: "/settings/general",
469 searchTerms: ["logs traces processes resource history failures spans cpu memory"],
470 },
471 {
472 id: "open-source-licenses",
473 title: "Open source licenses",
474 to: "/settings/general",
475 },
476 {
477 id: "legacy-plan-mode",
478 title: "Plan mode (legacy)",
479 to: "/settings/general",
480 searchTerms: ["build plan composer old"],
481 },
482 {
483 id: "legacy-context-window-indicator",
484 title: "Context window indicator (legacy)",
485 to: "/settings/general",
486 searchTerms: ["composer meter usage tokens circle old"],
487 },
488 {
489 id: "legacy-sidebar",
490 title: "Sidebar (legacy)",
491 to: "/settings/general",
492 searchTerms: ["project thread tree old flat list"],
493 },
494 {
495 id: "keybindings",
496 title: "Keybindings",
497 to: "/settings/keybindings",
498 searchTerms: ["keyboard shortcuts hotkeys commands bindings json"],
499 },
500 ...KEYBINDING_SEARCH_ITEMS,
501 {
502 id: "snap-shot-enabled",
503 title: "SnapShots",
504 searchTerms: ["window capture screenshot"],
505 to: "/settings/snap-shot",
506 },
507 {
508 id: "snap-shot-accessibility",
509 title: "Include app text",
510 to: "/settings/snap-shot",
511 targetId: "snap-shot-enabled",
512 searchTerms: [
513 "capture accessibility data text UI structure elements privacy omit agent context",
514 ],
515 },
516 {
517 id: "snap-shot-shortcut",
518 title: "Capture shortcut",
519 to: "/settings/snap-shot",
520 targetId: "snap-shot-enabled",
521 },
522 {
523 id: "snap-shot-sound",
524 title: "Capture sound",
525 to: "/settings/snap-shot",
526 targetId: "snap-shot-enabled",
527 },
528 {
529 id: "snap-shot-flash",
530 title: "Capture flash",
531 to: "/settings/snap-shot",
532 targetId: "snap-shot-enabled",
533 },
534 {
535 id: "snap-shot-animations",
536 title: "Capture animations",
537 to: "/settings/snap-shot",
538 targetId: "snap-shot-enabled",
539 },
540 {
541 id: "providers",
542 title: "Providers",
543 to: "/settings/providers",
544 searchTerms: [
545 "agents cli codex claude cursor grok opencode antigravity google sign in sign out install subscription instances authentication api key models configuration binary path config directory endpoint arguments environment variables display name accent color custom favorite hidden auto compact",
546 ],
547 },
548 {
549 id: "usage-providers",
550 title: "Usage providers",
551 to: "/settings/providers",
552 searchTerms: [
553 "usage sources CLIProxyAPI CLI proxy hub quota subscription limits management key add remove",
554 ],
555 providerSettingsOnly: true,
556 },
557 {
558 id: "cursor-keychain-usage",
559 title: "Cursor account usage",
560 to: "/settings/providers",
561 searchTerms: ["cursor macOS keychain usage tokens cost limits permission"],
562 providerSettingsOnly: true,
563 macProviderSettingsOnly: true,
564 },
565 {
566 id: "provider-health-check-interval",
567 title: "Health check interval",
568 to: "/settings/providers",
569 searchTerms: ["refresh availability versions auth state models background probes seconds off"],
570 providerSettingsOnly: true,
571 },
572 {
573 id: "agent-browser-access",
574 title: "Agent browser access",
575 to: "/settings/integrations",
576 scope: "project-defaults",
577 searchTerms: ["allow disable enable open drive preview tools sessions project override"],
578 },
579 {
580 id: "device-hosts",
581 title: "Device hosts",
582 to: "/settings/integrations",
583 searchTerms: ["ssh remote simulator emulator ios android mac mini identity key connection"],
584 },
585 {
586 id: "agent-device-access",
587 title: "Agent device access",
588 to: "/settings/integrations",
589 targetId: "devices",
590 searchTerms: ["allow simulator emulator ios android drive tools sessions"],
591 },
592 {
593 id: "device-hub",
594 title: "Device hub",
595 to: "/settings/integrations",
596 targetId: "devices",
597 searchTerms: ["simulator emulator ios android install start"],
598 },
599 {
600 id: "device-platform-support",
601 title: "Simulator support",
602 to: "/settings/integrations",
603 targetId: "devices",
604 searchTerms: ["xcode android studio sdk avd runtime"],
605 },
606 {
607 id: "browser-profiles",
608 title: "Browser profiles",
609 to: "/settings/integrations",
610 targetId: "browser",
611 },
612 {
613 id: "browser-default-profile",
614 title: "Default browser profile",
615 to: "/settings/integrations",
616 targetId: "browser-profiles",
617 },
618 {
619 id: "browser-default-viewport",
620 title: "Default browser viewport",
621 to: "/settings/integrations",
622 searchTerms: ["preview size width height device desktop mobile rotate"],
623 },
624 {
625 id: "browser-default-zoom",
626 title: "Default browser zoom",
627 to: "/settings/integrations",
628 searchTerms: ["preview page scale tabs percent"],
629 },
630 {
631 id: "browser-default-appearance",
632 title: "Default browser appearance",
633 to: "/settings/integrations",
634 searchTerms: ["preview color scheme light dark system os"],
635 },
636 {
637 id: "browser-recording-frame-rate",
638 title: "Browser recording frame rate",
639 to: "/settings/integrations",
640 },
641 {
642 id: "browser-recording-key-presses",
643 title: "Show key presses in recordings",
644 to: "/settings/integrations",
645 searchTerms: ["browser preview keyboard shortcuts keystrokes overlay capture"],
646 },
647 {
648 id: "browser-recording-mouse-presses",
649 title: "Show mouse presses in recordings",
650 to: "/settings/integrations",
651 searchTerms: ["browser preview clicks buttons drag overlay capture"],
652 },
653 {
654 id: "browser-link-target",
655 title: "Open links in",
656 to: "/settings/integrations",
657 searchTerms: ["links default browser in-app browser external open"],
658 },
659 {
660 id: "browser-auto-show-floating-preview",
661 title: "Auto-show floating preview",
662 to: "/settings/integrations",
663 searchTerms: ["agent opens browser device simulator pop into view hide"],
664 },
665 {
666 id: "automatic-pull",
667 title: "Automatically pull",
668 to: "/settings/source-control",
669 scope: "project-defaults",
670 searchTerms: ["auto pull default branch current checkout fast forward upstream"],
671 },
672 {
673 id: "pull-request-merge-method",
674 title: "Default merge method",
675 to: "/settings/source-control",
676 scope: "project-defaults",
677 searchTerms: ["pull request merge squash rebase last selected"],
678 },
679 {
680 id: "source-control",
681 title: "Source control",
682 to: "/settings/source-control",
683 scope: "environment-defaults",
684 searchTerms: [
685 "version control git github gitlab forgejo gitea tea codeberg bitbucket azure devops hosting integrations credentials scan server environment",
686 ],
687 },
688 {
689 id: "git-fetch-interval",
690 title: "Git fetch interval",
691 to: "/settings/source-control",
692 searchTerms: [
693 "automatic remote branch refresh background credentials security keys seconds off",
694 ],
695 environmentOnly: true,
696 scope: "environment-defaults",
697 },
698 {
699 id: "source-control-writing-style",
700 title: "Source control writing style",
701 to: "/settings/source-control",
702 searchTerms: [
703 "repository conventions conventional commits custom instructions change descriptions request titles",
704 ],
705 environmentOnly: true,
706 },
707 {
708 id: "follow-change-request-templates",
709 title: "Follow change request templates",
710 to: "/settings/source-control",
711 searchTerms: ["repository pr pull request description structure"],
712 environmentOnly: true,
713 },
714 {
715 id: "source-control-writer-model",
716 title: "Source control writer model",
717 to: "/settings/source-control",
718 searchTerms: [
719 "override generated commit change request pr titles descriptions branch bookmark",
720 ],
721 environmentOnly: true,
722 scope: "project-defaults",
723 },
724 {
725 id: "project-actions",
726 title: "Actions",
727 to: "/settings/projects",
728 searchTerms: ["commands scripts setup run dev server checkout worktree t3.json import"],
729 },
730 {
731 id: "environment-icon",
732 title: "Environment icon",
733 to: "/settings/connections",
734 targetId: "connections-environment",
735 searchTerms: ["machine glyph sidebar mac mini studio laptop desktop server cloud vm"],
736 localBackendManagementOnly: true,
737 },
738 {
739 id: "local-environment",
740 title: "Local environment",
741 to: "/settings/connections",
742 targetId: "connections-environment",
743 searchTerms: ["turn off on disable enable local server agents remote only restart"],
744 desktopOnly: true,
745 },
746 {
747 id: "network-access",
748 title: "Network access",
749 to: "/settings/connections",
750 targetId: "connections-environment",
751 searchTerms: ["expose backend remote pairing local machine interfaces host restart"],
752 localBackendManagementOnly: true,
753 },
754 {
755 id: "tailscale-https",
756 title: "Tailscale HTTPS",
757 to: "/settings/connections",
758 targetId: "connections-environment",
759 searchTerms: ["serve magicdns endpoint remote secure network"],
760 desktopOnly: true,
761 localBackendManagementOnly: true,
762 },
763 {
764 id: "wsl-backend",
765 title: "WSL backend",
766 to: "/settings/connections",
767 searchTerms: [
768 "windows subsystem linux distro second server projects stop windows backend restart",
769 ],
770 desktopOnly: true,
771 windowsOnly: true,
772 localBackendManagementOnly: true,
773 wslAvailableOnly: true,
774 },
775 {
776 id: "t3-connect",
777 localEnvironmentOnly: true,
778 title: "T3 Connect",
779 to: "/settings/connections",
780 targetId: "connections-environment",
781 searchTerms: ["managed tunnel cloud other devices remote"],
782 desktopOnly: true,
783 cloudOnly: true,
784 },
785 {
786 id: "publish-agent-activity",
787 localEnvironmentOnly: true,
788 title: "Publish agent activity",
789 to: "/settings/connections",
790 targetId: "connections-environment",
791 searchTerms: ["mobile push notifications live activities cloud tunnel"],
792 cloudOnly: true,
793 },
794 {
795 id: "connections-environment",
796 title: "This machine",
797 to: "/settings/connections",
798 searchTerms: [
799 "connections server backend local remote access administrative permissions scope pairing links qr code authorized clients sessions revoke endpoint",
800 ],
801 },
802 {
803 id: "remote-environments",
804 title: "Environments",
805 to: "/settings/connections",
806 searchTerms: ["add pair backend host code ssh config agent tunnel saved t3 connect"],
807 },
808 {
809 id: "load-balancing",
810 title: "Load balancing",
811 to: "/settings/connections",
812 searchTerms: [
813 "automatic machine environment resources cpu memory capacity preference weight shared projects",
814 ],
815 },
816 {
817 id: "github-routing",
818 title: "GitHub sharing",
819 to: "/settings/connections",
820 searchTerms: ["pull request trusted environments shared credentials permissions read actions"],
821 },
822 {
823 id: "archive",
824 title: "Archived threads",
825 to: "/settings/archived",
826 searchTerms: ["restore reopen deleted history projects"],
827 },
828] as const satisfies ReadonlyArray<SettingsSearchItem>;
829
830export type SettingsSearchItemId = (typeof SETTINGS_SEARCH_ITEMS)[number]["id"];
831
832const SEARCH_ITEMS_BY_ID = new Map(SETTINGS_SEARCH_ITEMS.map((item) => [item.id, item] as const));
833
834const SETTINGS_CATEGORY_SCOPES: Readonly<Record<SettingsPath, SettingsSearchScope | null>> = {
835 "/settings/projects": "project",
836 "/settings/general": null,
837 "/settings/appearance": null,
838 "/settings/snap-shot": null,
839 // Keybindings fan out to the selection; Providers shows the representative
840 // environment at any selection. Neither needs a particular scope to render.
841 "/settings/keybindings": null,
842 "/settings/providers": null,
843 "/settings/integrations": null,
844 "/settings/source-control": "environment-defaults",
845 "/settings/storage": "project-defaults",
846 "/settings/connections": "connections",
847 "/settings/archived": "project-defaults",
848};
849
850/** Search keeps the selected target. A missing row can explain its owning scope instead. */
851export function getSettingsSearchTargetScope(targetId: string) {
852 const items: readonly SettingsSearchItem[] = SETTINGS_SEARCH_ITEMS;
853 const item =
854 items.find((candidate) => candidate.id === targetId) ??
855 items.find((candidate) => candidate.targetId === targetId);
856 return item
857 ? {
858 title: item.title,
859 scope: item.scope ?? SETTINGS_CATEGORY_SCOPES[item.to],
860 ...(item.requiresThreadAutoSettlement ? { requiresThreadAutoSettlement: true } : {}),
861 }
862 : null;
863}
864
865interface AutoSettlementSearchEnvironment {
866 readonly environmentId: EnvironmentId;
867 readonly connection: { readonly phase: EnvironmentConnectionPhase };
868 readonly serverConfig: {
869 readonly environment: {
870 readonly capabilities: { readonly threadAutoSettlement?: boolean };
871 };
872 } | null;
873}
874
875/** Discovery needs one capable environment; the selected page needs every connected target to support it. */
876export function getThreadAutoSettlementSearchAvailability(
877 environments: readonly AutoSettlementSearchEnvironment[],
878 scope?: Pick<ResolvedSettingsScope, "kind" | "environmentIds">,
879) {
880 const connected = environments.filter(
881 (environment) =>
882 environment.connection.phase === "connected" && environment.serverConfig !== null,
883 );
884 const eligibleEnvironmentIds = connected
885 .filter(
886 (environment) =>
887 environment.serverConfig?.environment.capabilities.threadAutoSettlement === true,
888 )
889 .map((environment) => environment.environmentId);
890 const selected = connected.filter((environment) =>
891 scope?.environmentIds.includes(environment.environmentId),
892 );
893 return {
894 eligibleEnvironmentIds,
895 isTargetAvailable:
896 scope !== undefined &&
897 scope.kind !== "unavailable" &&
898 selected.length > 0 &&
899 selected.every((environment) => eligibleEnvironmentIds.includes(environment.environmentId)),
900 };
901}
902
903export function isSettingsSearchScopeAvailable(
904 requiredScope: SettingsSearchScope | null,
905 scopeKind: ResolvedSettingsScope["kind"],
906): boolean {
907 switch (requiredScope) {
908 case null:
909 case "connections":
910 return true;
911 case "environment":
912 case "checkout":
913 return requiredScope === scopeKind;
914 case "project":
915 return scopeKind === "project" || scopeKind === "checkout";
916 case "environment-defaults":
917 return scopeKind === "environment" || scopeKind === "all";
918 case "project-defaults":
919 return (
920 scopeKind === "environment" ||
921 scopeKind === "all" ||
922 scopeKind === "project" ||
923 scopeKind === "checkout"
924 );
925 }
926}
927
928function settingsScopeKindFromSearch(search: SettingsScopeSearch): ResolvedSettingsScope["kind"] {
929 const target = validateSettingsScopeSearch({ ...search });
930 if (target.checkout && !target.project) return "unavailable";
931 if (target.project) return target.checkout ? "checkout" : "project";
932 return target.machine ? "environment" : "all";
933}
934
935export function isSettingsOverviewVisible(search: SettingsScopeSearch): boolean {
936 const kind = settingsScopeKindFromSearch(search);
937 return kind === "project" || kind === "checkout";
938}
939
940/**
941 * `id` and `title` props for the element a search item anchors to. Panels
942 * spread (or pick from) this instead of restating the strings, so the catalog
943 * and the rendered settings cannot drift apart.
944 */
945export function searchableSetting(id: SettingsSearchItemId): {
946 readonly id: string;
947 readonly title: string;
948} {
949 const { id: anchorId, title } = SEARCH_ITEMS_BY_ID.get(id)!;
950 return { id: anchorId, title };
951}
952
953export function filterAvailableSettingsSearchItems(
954 availability: SettingsSearchAvailability,
955): ReadonlyArray<SettingsSearchItem> {
956 const items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS;
957 return items.filter(
958 (item) =>
959 (!item.cloudOnly || availability.hasCloudPublicConfig) &&
960 (!item.environmentOnly || availability.hasEnvironment) &&
961 (!item.providerSettingsOnly || availability.hasProviderSettingsEnvironment) &&
962 (!item.macProviderSettingsOnly || availability.hasMacProviderSettingsEnvironment) &&
963 (!item.localBackendManagementOnly || availability.canManageLocalBackend) &&
964 (!item.localEnvironmentOnly || !availability.localEnvironmentDisabled) &&
965 (!item.wslAvailableOnly || availability.isWslSettingsRowVisible) &&
966 (!item.requiresThreadAutoSettlement || availability.hasThreadAutoSettlement),
967 );
968}
969
970export function searchSettings(
971 query: string,
972 items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS,
973): ReadonlyArray<SettingsSearchItem> {
974 const normalizedQuery = normalizeSearchText(query);
975 if (normalizedQuery.length === 0) return [];
976 const queryTokens = normalizedQuery.split(" ");
977 const platform = typeof navigator === "undefined" ? "" : navigator.platform;
978
979 return items
980 .flatMap((item, index) => {
981 if (!isElectron && item.desktopOnly === true) return [];
982 if (item.macOnly && !isMacPlatform(platform)) return [];
983 if (item.windowsOnly && !isWindowsPlatform(platform)) return [];
984
985 const title = normalizeSearchText(item.title);
986 const fields = [
987 title,
988 normalizeSearchText(SETTINGS_SECTION_LABELS[item.to]),
989 ...(item.searchTerms ?? []).map(normalizeSearchText),
990 ];
991 if (!queryTokens.every((token) => fields.some((field) => field.includes(token)))) return [];
992
993 const exactPhraseField = fields.findIndex((field) => field.includes(normalizedQuery));
994 const rank =
995 title === normalizedQuery
996 ? 5
997 : title.startsWith(normalizedQuery)
998 ? 4
999 : title.includes(normalizedQuery)
1000 ? 3
1001 : queryTokens.every((token) => title.includes(token))
1002 ? 2
1003 : exactPhraseField >= 0
1004 ? 1
1005 : 0;
1006 return [{ item, index, rank }];
1007 })
1008 .toSorted(
1009 (left, right) =>
1010 Number(left.item.secondary ?? false) - Number(right.item.secondary ?? false) ||
1011 right.rank - left.rank ||
1012 left.index - right.index,
1013 )
1014 .map(({ item }) => item);
1015}