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