Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useAvailableSettingsSearchItems.ts

73 lines3.5 KB

1import { AuthEnvironmentMaintainScope } from "@t3tools/contracts";
2​
3import { usePrimaryCloudLinkState } from "~/cloud/primaryCloudLinkState";
4import { hasCloudPublicConfig } from "~/cloud/publicConfig";
5import { isElectron } from "~/env";
6import { isLocalEnvironmentDisabled } from "~/localEnvironment";
7import { desktopWslStateAtom } from "~/state/desktopWslState";
8import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments";
9import { useEnvironmentQuery } from "~/state/query";
10import { useEnvironmentScope } from "~/state/session";
11import { isWslSettingsRowVisible } from "./ConnectionsSettings.logic";
12import { isProviderSettingsEnvironmentAvailable } from "./ProviderSettingsPanel.logic";
13import type { SettingsScopeSearch } from "./settingsScope";
14import {
15 filterAvailableSettingsSearchItems,
16 getThreadAutoSettlementSearchAvailability,
17} from "./settingsSearch";
18​
19export function useAvailableSettingsSearchItems(scopeSearch: SettingsScopeSearch = {}) {
20 const { environments } = useEnvironments();
21 // The filtered list must be cached on boolean flags so it stays stable for the search and
22 // the command palette while environments update. The compiler cannot tell that `.some()`
23 // and other plain calls return primitives, so `=== true` marks each flag as one; without
24 // it the flags join the filter's cache scope and the list is rebuilt every render.
25 const hasEnvironment =
26 environments.some((environment) => environment.serverConfig !== null) === true;
27 const hasProviderSettingsEnvironment =
28 environments.some((environment) =>
29 isProviderSettingsEnvironmentAvailable({
30 connectionPhase: environment.connection.phase,
31 hasServerConfig: environment.serverConfig !== null,
32 }),
33 ) === true;
34 const hasMacProviderSettingsEnvironment =
35 environments.some(
36 (environment) =>
37 (scopeSearch.machine === undefined || environment.environmentId === scopeSearch.machine) &&
38 environment.serverConfig?.environment.platform.os === "darwin" &&
39 isProviderSettingsEnvironmentAvailable({
40 connectionPhase: environment.connection.phase,
41 hasServerConfig: true,
42 }),
43 ) === true;
44 const hasThreadAutoSettlement =
45 getThreadAutoSettlementSearchAvailability(environments).eligibleEnvironmentIds.length > 0;
46 const primaryEnvironmentId = usePrimaryEnvironmentId();
47 const canMaintain = useEnvironmentScope(primaryEnvironmentId, AuthEnvironmentMaintainScope);
48 const cloudLinkState = usePrimaryCloudLinkState().data;
49 // Same fallback as the Connections row: older servers imply a tunnel from `linked`.
50 const managedTunnelActive =
51 cloudLinkState?.linkRevoked !== true &&
52 (cloudLinkState?.managedTunnelActive ?? cloudLinkState?.linked ?? false) === true;
53 const localEnvironmentDisabled = isLocalEnvironmentDisabled() === true;
54 const canManageLocalBackend = !localEnvironmentDisabled && canMaintain;
55 const desktopWsl = useEnvironmentQuery(
56 isElectron && canManageLocalBackend ? desktopWslStateAtom : null,
57 );
58 const cloudPublicConfig = hasCloudPublicConfig() === true;
59 const wslSettingsRowVisible =
60 isWslSettingsRowVisible({ state: desktopWsl.data, error: desktopWsl.error }) === true;
61​
62 return filterAvailableSettingsSearchItems({
63 localEnvironmentDisabled,
64 hasCloudPublicConfig: cloudPublicConfig,
65 hasEnvironment,
66 hasProviderSettingsEnvironment,
67 hasMacProviderSettingsEnvironment,
68 canManageLocalBackend,
69 isWslSettingsRowVisible: wslSettingsRowVisible,
70 hasThreadAutoSettlement,
71 managedTunnelActive,
72 });