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

useAvailableSettingsSearchItems.ts

77 lines3.5 KB

1import { AuthEnvironmentMaintainScope } from "@t3tools/contracts";
2​
3import { usePrimaryCloudLinkState } from "~/cloud/cloudLinkState";
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 { usePeerLinkHosts } from "~/state/peerLinks";
10import { useEnvironmentQuery } from "~/state/query";
11import { useEnvironmentScope } from "~/state/session";
12import { isWslSettingsRowVisible } from "./ConnectionsSettings.logic";
13import { isProviderSettingsEnvironmentAvailable } from "./ProviderSettingsPanel.logic";
14import {
15 filterAvailableSettingsSearchItems,
16 getThreadAutoSettlementSearchAvailability,
17} from "./settingsSearch";
18​
19export function useAvailableSettingsSearchItems() {
20 const { environments } = useEnvironments();
21 // Explicit booleans keep flags out of the filter cache so environment updates do not rebuild the list.
22 const hasEnvironment =
23 environments.some((environment) => environment.serverConfig !== null) === true;
24 const hasProviderSettingsEnvironment =
25 environments.some((environment) =>
26 isProviderSettingsEnvironmentAvailable({
27 connectionPhase: environment.connection.phase,
28 hasServerConfig: environment.serverConfig !== null,
29 }),
30 ) === true;
31 // Any machine that offers it; the row then shows for a settings scope where all selected ones do.
32 const hasFastWorktreeCheckoutEnvironment =
33 environments.some(
34 (environment) =>
35 environment.connection.phase === "connected" &&
36 environment.serverConfig?.environment.capabilities.fastWorktreeCheckout === true,
37 ) === true;
38 const hasTextGeneratorsEnvironment =
39 environments.some(
40 (environment) =>
41 environment.serverConfig !== null &&
42 environment.serverConfig.managedPolicy?.limits?.providers?.allowedDrivers === undefined,
43 ) === true;
44 const hasThreadAutoSettlement =
45 getThreadAutoSettlementSearchAvailability(environments).eligibleEnvironmentIds.length > 0;
46 const primaryEnvironmentId = usePrimaryEnvironmentId();
47 const canMaintain = useEnvironmentScope(primaryEnvironmentId, AuthEnvironmentMaintainScope);
48 const hasPeerLinkHost = usePeerLinkHosts().length > 0;
49 const cloudLinkState = usePrimaryCloudLinkState().data;
50 // Same fallback as the Connections row: older servers imply a tunnel from `linked`.
51 const managedTunnelActive =
52 cloudLinkState?.linkRevoked !== true &&
53 (cloudLinkState?.managedTunnelActive ?? cloudLinkState?.linked ?? false) === true;
54 const localEnvironmentDisabled = isLocalEnvironmentDisabled() === true;
55 const canManageLocalBackend = !localEnvironmentDisabled && canMaintain;
56 const desktopWsl = useEnvironmentQuery(
57 isElectron && canManageLocalBackend ? desktopWslStateAtom : null,
58 );
59 const cloudPublicConfig = hasCloudPublicConfig() === true;
60 const wslSettingsRowVisible =
61 isWslSettingsRowVisible({ state: desktopWsl.data, error: desktopWsl.error }) === true;
62​
63 return filterAvailableSettingsSearchItems({
64 localEnvironmentDisabled,
65 hasCloudPublicConfig: cloudPublicConfig,
66 hasEnvironment,
67 hasProviderSettingsEnvironment,
68 canManageLocalBackend,
69 isWslSettingsRowVisible: wslSettingsRowVisible,
70 hasThreadAutoSettlement,
71 managedTunnelActive,
72 hasPeerLinkHost,
73 hasFastWorktreeCheckoutEnvironment,
74 hasTextGeneratorsEnvironment,
75 hasPrimaryEnvironment: primaryEnvironmentId !== null,
76 });