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