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