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: "provider-update-checks",
303 title: "Provider update checks",
304 to: "/settings/general",
305 searchTerms: ["installed cli versions newer available codex claude cursor grok opencode"],
306 scope: "environment-defaults",
307 },
308 {
309 id: "continue-threads-after-server-update",
310 title: "Continue threads after restarts",
311 to: "/settings/general",
312 scope: "project-defaults",
313 searchTerms: [
314 "resume running active interrupted work restart reboot machine crash desktop update automatically",
315 ],
316 },
317 {
318 id: "background-activity",
319 title: "Background activity",
320 to: "/settings/general",
321 scope: "environment-defaults",
322 searchTerms: [
323 "balanced performance battery saver advanced git fetch provider health refresh host power monitor idle policy",
324 ],
325 },
326 {
327 id: "new-threads",
328 title: "New threads",
329 to: "/settings/general",
330 scope: "project-defaults",
331 searchTerms: ["default workspace mode draft local worktree"],
332 },
333 {
334 id: "start-from-origin",
335 title: "Start from origin",
336 to: "/settings/general",
337 scope: "project-defaults",
338 searchTerms: ["new worktrees latest matching remote branch local"],
339 },
340 {
341 id: "add-project-starts-in",
342 title: "Add project starts in",
343 to: "/settings/general",
344 scope: "environment-defaults",
345 searchTerms: ["base directory folder browser path home"],
346 },
347 {
348 id: "unpin-confirmation",
349 title: "Unpin confirmation",
350 to: "/settings/general",
351 searchTerms: ["ask before thread pinned section"],
352 },
353 {
354 id: "archive-confirmation",
355 title: "Archive confirmation",
356 to: "/settings/general",
357 searchTerms: ["ask before thread second click inline action"],
358 },
359 {
360 id: "delete-confirmation",
361 title: "Delete confirmation",
362 to: "/settings/general",
363 searchTerms: ["ask before thread chat history"],
364 },
365 {
366 id: "quit-confirmation",
367 title: "Quit shortcut",
368 to: "/settings/general",
369 searchTerms: ["confirmation desktop app exit direct hold double click press twice"],
370 desktopOnly: true,
371 },
372 {
373 id: "text-generation-model",
374 title: "Text generation model",
375 to: "/settings/general",
376 scope: "project-defaults",
377 searchTerms: ["generated thread titles source control content default provider"],
378 },
379 {
380 id: "diagnostics",
381 title: "Diagnostics",
382 to: "/settings/general",
383 searchTerms: ["logs traces processes resource history failures spans cpu memory"],
384 },
385 {
386 id: "open-source-licenses",
387 title: "Open source licenses",
388 to: "/settings/general",
389 },
390 {
391 id: "legacy-plan-mode",
392 title: "Plan mode (legacy)",
393 to: "/settings/general",
394 searchTerms: ["build plan composer old"],
395 },
396 {
397 id: "legacy-context-window-indicator",
398 title: "Context window indicator (legacy)",
399 to: "/settings/general",
400 searchTerms: ["composer meter usage tokens circle old"],
401 },
402 {
403 id: "legacy-sidebar",
404 title: "Sidebar (legacy)",
405 to: "/settings/general",
406 searchTerms: ["project thread tree old flat list"],
407 },
408 {
409 id: "keybindings",
410 title: "Keybindings",
411 to: "/settings/keybindings",
412 searchTerms: ["keyboard shortcuts hotkeys commands bindings json"],
413 },
414 {
415 id: "snap-shot-enabled",
416 title: "SnapShots",
417 searchTerms: ["window capture screenshot"],
418 to: "/settings/snap-shot",
419 },
420 {
421 id: "snap-shot-accessibility",
422 title: "Include app text",
423 to: "/settings/snap-shot",
424 targetId: "snap-shot-enabled",
425 searchTerms: [
426 "capture accessibility data text UI structure elements privacy omit agent context",
427 ],
428 },
429 {
430 id: "snap-shot-shortcut",
431 title: "Capture shortcut",
432 to: "/settings/snap-shot",
433 targetId: "snap-shot-enabled",
434 },
435 {
436 id: "snap-shot-sound",
437 title: "Capture sound",
438 to: "/settings/snap-shot",
439 targetId: "snap-shot-enabled",
440 },
441 {
442 id: "snap-shot-flash",
443 title: "Capture flash",
444 to: "/settings/snap-shot",
445 targetId: "snap-shot-enabled",
446 },
447 {
448 id: "snap-shot-animations",
449 title: "Capture animations",
450 to: "/settings/snap-shot",
451 targetId: "snap-shot-enabled",
452 },
453 {
454 id: "providers",
455 title: "Providers",
456 to: "/settings/providers",
457 searchTerms: [
458 "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",
459 ],
460 },
461 {
462 id: "usage-providers",
463 title: "Usage providers",
464 to: "/settings/providers",
465 searchTerms: [
466 "usage sources CLIProxyAPI CLI proxy hub quota subscription limits management key add remove",
467 ],
468 providerSettingsOnly: true,
469 },
470 {
471 id: "provider-health-check-interval",
472 title: "Health check interval",
473 to: "/settings/providers",
474 searchTerms: ["refresh availability versions auth state models background probes seconds off"],
475 providerSettingsOnly: true,
476 },
477 {
478 id: "agent-browser-access",
479 title: "Agent browser access",
480 to: "/settings/integrations",
481 scope: "project-defaults",
482 searchTerms: ["allow disable enable open drive preview tools sessions project override"],
483 },
484 {
485 id: "device-hosts",
486 title: "Device hosts",
487 to: "/settings/integrations",
488 searchTerms: ["ssh remote simulator emulator ios android mac mini identity key connection"],
489 },
490 {
491 id: "agent-device-access",
492 title: "Agent device access",
493 to: "/settings/integrations",
494 targetId: "devices",
495 searchTerms: ["allow simulator emulator ios android drive tools sessions"],
496 },
497 {
498 id: "device-hub",
499 title: "Device hub",
500 to: "/settings/integrations",
501 targetId: "devices",
502 searchTerms: ["simulator emulator ios android install start"],
503 },
504 {
505 id: "device-platform-support",
506 title: "Simulator support",
507 to: "/settings/integrations",
508 targetId: "devices",
509 searchTerms: ["xcode android studio sdk avd runtime"],
510 },
511 {
512 id: "browser-profiles",
513 title: "Browser profiles",
514 to: "/settings/integrations",
515 targetId: "browser",
516 },
517 {
518 id: "browser-default-profile",
519 title: "Default browser profile",
520 to: "/settings/integrations",
521 targetId: "browser-profiles",
522 },
523 {
524 id: "browser-default-viewport",
525 title: "Default browser viewport",
526 to: "/settings/integrations",
527 searchTerms: ["preview size width height device desktop mobile rotate"],
528 },
529 {
530 id: "browser-default-zoom",
531 title: "Default browser zoom",
532 to: "/settings/integrations",
533 searchTerms: ["preview page scale tabs percent"],
534 },
535 {
536 id: "browser-default-appearance",
537 title: "Default browser appearance",
538 to: "/settings/integrations",
539 searchTerms: ["preview color scheme light dark system os"],
540 },
541 {
542 id: "browser-recording-frame-rate",
543 title: "Browser recording frame rate",
544 to: "/settings/integrations",
545 },
546 {
547 id: "browser-link-target",
548 title: "Open links in",
549 to: "/settings/integrations",
550 searchTerms: ["links default browser in-app browser external open"],
551 },
552 {
553 id: "browser-auto-show-floating-preview",
554 title: "Auto-show floating preview",
555 to: "/settings/integrations",
556 searchTerms: ["agent opens browser device simulator pop into view hide"],
557 },
558 {
559 id: "automatic-pull",
560 title: "Automatically pull",
561 to: "/settings/source-control",
562 scope: "project-defaults",
563 searchTerms: ["auto pull default branch current checkout fast forward upstream"],
564 },
565 {
566 id: "pull-request-merge-method",
567 title: "Default merge method",
568 to: "/settings/source-control",
569 scope: "project-defaults",
570 searchTerms: ["pull request merge squash rebase last selected"],
571 },
572 {
573 id: "source-control",
574 title: "Source control",
575 to: "/settings/source-control",
576 scope: "environment-defaults",
577 searchTerms: [
578 "version control git github gitlab forgejo gitea tea codeberg bitbucket azure devops hosting integrations credentials scan server environment",
579 ],
580 },
581 {
582 id: "git-fetch-interval",
583 title: "Git fetch interval",
584 to: "/settings/source-control",
585 searchTerms: [
586 "automatic remote branch refresh background credentials security keys seconds off",
587 ],
588 environmentOnly: true,
589 scope: "environment-defaults",
590 },
591 {
592 id: "source-control-writing-style",
593 title: "Source control writing style",
594 to: "/settings/source-control",
595 searchTerms: [
596 "repository conventions conventional commits custom instructions change descriptions request titles",
597 ],
598 environmentOnly: true,
599 },
600 {
601 id: "follow-change-request-templates",
602 title: "Follow change request templates",
603 to: "/settings/source-control",
604 searchTerms: ["repository pr pull request description structure"],
605 environmentOnly: true,
606 },
607 {
608 id: "source-control-writer-model",
609 title: "Source control writer model",
610 to: "/settings/source-control",
611 searchTerms: [
612 "override generated commit change request pr titles descriptions branch bookmark",
613 ],
614 environmentOnly: true,
615 scope: "project-defaults",
616 },
617 {
618 id: "project-actions",
619 title: "Actions",
620 to: "/settings/projects",
621 searchTerms: ["commands scripts setup run dev server checkout worktree t3.json import"],
622 },
623 {
624 id: "environment-icon",
625 title: "Environment icon",
626 to: "/settings/connections",
627 targetId: "connections-environment",
628 searchTerms: ["machine glyph sidebar mac mini studio laptop desktop server cloud vm"],
629 localBackendManagementOnly: true,
630 },
631 {
632 id: "local-environment",
633 title: "Local environment",
634 to: "/settings/connections",
635 targetId: "connections-environment",
636 searchTerms: ["turn off on disable enable local server agents remote only restart"],
637 desktopOnly: true,
638 },
639 {
640 id: "network-access",
641 title: "Network access",
642 to: "/settings/connections",
643 targetId: "connections-environment",
644 searchTerms: ["expose backend remote pairing local machine interfaces host restart"],
645 localBackendManagementOnly: true,
646 },
647 {
648 id: "tailscale-https",
649 title: "Tailscale HTTPS",
650 to: "/settings/connections",
651 targetId: "connections-environment",
652 searchTerms: ["serve magicdns endpoint remote secure network"],
653 desktopOnly: true,
654 localBackendManagementOnly: true,
655 },
656 {
657 id: "wsl-backend",
658 title: "WSL backend",
659 to: "/settings/connections",
660 searchTerms: [
661 "windows subsystem linux distro second server projects stop windows backend restart",
662 ],
663 desktopOnly: true,
664 windowsOnly: true,
665 localBackendManagementOnly: true,
666 wslAvailableOnly: true,
667 },
668 {
669 id: "t3-connect",
670 localEnvironmentOnly: true,
671 title: "T3 Connect",
672 to: "/settings/connections",
673 targetId: "connections-environment",
674 searchTerms: ["managed tunnel cloud other devices remote"],
675 desktopOnly: true,
676 cloudOnly: true,
677 },
678 {
679 id: "publish-agent-activity",
680 localEnvironmentOnly: true,
681 title: "Publish agent activity",
682 to: "/settings/connections",
683 targetId: "connections-environment",
684 searchTerms: ["mobile push notifications live activities cloud tunnel"],
685 cloudOnly: true,
686 },
687 {
688 id: "connections-environment",
689 title: "This machine",
690 to: "/settings/connections",
691 searchTerms: [
692 "connections server backend local remote access administrative permissions scope pairing links qr code authorized clients sessions revoke endpoint",
693 ],
694 },
695 {
696 id: "remote-environments",
697 title: "Environments",
698 to: "/settings/connections",
699 searchTerms: ["add pair backend host code ssh config agent tunnel saved t3 connect"],
700 },
701 {
702 id: "load-balancing",
703 title: "Load balancing",
704 to: "/settings/connections",
705 searchTerms: [
706 "automatic machine environment resources cpu memory capacity preference weight shared projects",
707 ],
708 },
709 {
710 id: "github-routing",
711 title: "GitHub sharing",
712 to: "/settings/connections",
713 searchTerms: ["pull request trusted environments shared credentials permissions read actions"],
714 },
715 {
716 id: "archive",
717 title: "Archived threads",
718 to: "/settings/archived",
719 searchTerms: ["restore reopen deleted history projects"],
720 },
721] as const satisfies ReadonlyArray<SettingsSearchItem>;
722
723export type SettingsSearchItemId = (typeof SETTINGS_SEARCH_ITEMS)[number]["id"];
724
725const SEARCH_ITEMS_BY_ID = new Map(SETTINGS_SEARCH_ITEMS.map((item) => [item.id, item] as const));
726
727const SETTINGS_CATEGORY_SCOPES: Readonly<Record<SettingsPath, SettingsSearchScope | null>> = {
728 "/settings/projects": "project",
729 "/settings/general": null,
730 "/settings/appearance": null,
731 "/settings/snap-shot": null,
732 // Keybindings fan out to the selection; Providers shows the representative
733 // environment at any selection. Neither needs a particular scope to render.
734 "/settings/keybindings": null,
735 "/settings/providers": null,
736 "/settings/integrations": null,
737 "/settings/source-control": "environment-defaults",
738 "/settings/connections": "connections",
739 "/settings/archived": "project-defaults",
740};
741
742/** Search keeps the selected target. A missing row can explain its owning scope instead. */
743export function getSettingsSearchTargetScope(targetId: string) {
744 const items: readonly SettingsSearchItem[] = SETTINGS_SEARCH_ITEMS;
745 const item =
746 items.find((candidate) => candidate.id === targetId) ??
747 items.find((candidate) => candidate.targetId === targetId);
748 return item
749 ? {
750 title: item.title,
751 scope: item.scope ?? SETTINGS_CATEGORY_SCOPES[item.to],
752 ...(item.requiresThreadAutoSettlement ? { requiresThreadAutoSettlement: true } : {}),
753 }
754 : null;
755}
756
757interface AutoSettlementSearchEnvironment {
758 readonly environmentId: EnvironmentId;
759 readonly connection: { readonly phase: EnvironmentConnectionPhase };
760 readonly serverConfig: {
761 readonly environment: {
762 readonly capabilities: { readonly threadAutoSettlement?: boolean };
763 };
764 } | null;
765}
766
767/** Discovery needs one capable environment; the selected page needs every connected target to support it. */
768export function getThreadAutoSettlementSearchAvailability(
769 environments: readonly AutoSettlementSearchEnvironment[],
770 scope?: Pick<ResolvedSettingsScope, "kind" | "environmentIds">,
771) {
772 const connected = environments.filter(
773 (environment) =>
774 environment.connection.phase === "connected" && environment.serverConfig !== null,
775 );
776 const eligibleEnvironmentIds = connected
777 .filter(
778 (environment) =>
779 environment.serverConfig?.environment.capabilities.threadAutoSettlement === true,
780 )
781 .map((environment) => environment.environmentId);
782 const selected = connected.filter((environment) =>
783 scope?.environmentIds.includes(environment.environmentId),
784 );
785 return {
786 eligibleEnvironmentIds,
787 isTargetAvailable:
788 scope !== undefined &&
789 scope.kind !== "unavailable" &&
790 selected.length > 0 &&
791 selected.every((environment) => eligibleEnvironmentIds.includes(environment.environmentId)),
792 };
793}
794
795export function isSettingsSearchScopeAvailable(
796 requiredScope: SettingsSearchScope | null,
797 scopeKind: ResolvedSettingsScope["kind"],
798): boolean {
799 switch (requiredScope) {
800 case null:
801 case "connections":
802 return true;
803 case "environment":
804 case "checkout":
805 return requiredScope === scopeKind;
806 case "project":
807 return scopeKind === "project" || scopeKind === "checkout";
808 case "environment-defaults":
809 return scopeKind === "environment" || scopeKind === "all";
810 case "project-defaults":
811 return (
812 scopeKind === "environment" ||
813 scopeKind === "all" ||
814 scopeKind === "project" ||
815 scopeKind === "checkout"
816 );
817 }
818}
819
820function settingsScopeKindFromSearch(search: SettingsScopeSearch): ResolvedSettingsScope["kind"] {
821 const target = validateSettingsScopeSearch({ ...search });
822 if (target.checkout && !target.project) return "unavailable";
823 if (target.project) return target.checkout ? "checkout" : "project";
824 return target.machine ? "environment" : "all";
825}
826
827export function isSettingsOverviewVisible(search: SettingsScopeSearch): boolean {
828 const kind = settingsScopeKindFromSearch(search);
829 return kind === "project" || kind === "checkout";
830}
831
832/**
833 * `id` and `title` props for the element a search item anchors to. Panels
834 * spread (or pick from) this instead of restating the strings, so the catalog
835 * and the rendered settings cannot drift apart.
836 */
837export function searchableSetting(id: SettingsSearchItemId): {
838 readonly id: string;
839 readonly title: string;
840} {
841 const { id: anchorId, title } = SEARCH_ITEMS_BY_ID.get(id)!;
842 return { id: anchorId, title };
843}
844
845export function filterAvailableSettingsSearchItems(
846 availability: SettingsSearchAvailability,
847): ReadonlyArray<SettingsSearchItem> {
848 const items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS;
849 return items.filter(
850 (item) =>
851 (!item.cloudOnly || availability.hasCloudPublicConfig) &&
852 (!item.environmentOnly || availability.hasEnvironment) &&
853 (!item.providerSettingsOnly || availability.hasProviderSettingsEnvironment) &&
854 (!item.localBackendManagementOnly || availability.canManageLocalBackend) &&
855 (!item.localEnvironmentOnly || !availability.localEnvironmentDisabled) &&
856 (!item.wslAvailableOnly || availability.isWslSettingsRowVisible) &&
857 (!item.requiresThreadAutoSettlement || availability.hasThreadAutoSettlement),
858 );
859}
860
861export function searchSettings(
862 query: string,
863 items: ReadonlyArray<SettingsSearchItem> = SETTINGS_SEARCH_ITEMS,
864): ReadonlyArray<SettingsSearchItem> {
865 const normalizedQuery = normalizeSearchText(query);
866 if (normalizedQuery.length === 0) return [];
867 const queryTokens = normalizedQuery.split(" ");
868 const platform = typeof navigator === "undefined" ? "" : navigator.platform;
869
870 return items
871 .flatMap((item, index) => {
872 if (!isElectron && item.desktopOnly === true) return [];
873 if (item.macOnly && !isMacPlatform(platform)) return [];
874 if (item.windowsOnly && !isWindowsPlatform(platform)) return [];
875
876 const title = normalizeSearchText(item.title);
877 const fields = [
878 title,
879 normalizeSearchText(SETTINGS_SECTION_LABELS[item.to]),
880 ...(item.searchTerms ?? []).map(normalizeSearchText),
881 ];
882 if (!queryTokens.every((token) => fields.some((field) => field.includes(token)))) return [];
883
884 const exactPhraseField = fields.findIndex((field) => field.includes(normalizedQuery));
885 const rank =
886 title === normalizedQuery
887 ? 5
888 : title.startsWith(normalizedQuery)
889 ? 4
890 : title.includes(normalizedQuery)
891 ? 3
892 : queryTokens.every((token) => title.includes(token))
893 ? 2
894 : exactPhraseField >= 0
895 ? 1
896 : 0;
897 return [{ item, index, rank }];
898 })
899 .toSorted((left, right) => right.rank - left.rank || left.index - right.index)
900 .map(({ item }) => item);
901}