| 1 | import { useMemo } from "react"; |
| 2 | import { AuthEnvironmentMaintainScope } from "@t3tools/contracts"; |
| 3 | |
| 4 | import { usePrimaryCloudLinkState } from "~/cloud/primaryCloudLinkState"; |
| 5 | import { hasCloudPublicConfig } from "~/cloud/publicConfig"; |
| 6 | import { isElectron } from "~/env"; |
| 7 | import { isLocalEnvironmentDisabled } from "~/localEnvironment"; |
| 8 | import { desktopWslStateAtom } from "~/state/desktopWslState"; |
| 9 | import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments"; |
| 10 | import { useEnvironmentQuery } from "~/state/query"; |
| 11 | import { useEnvironmentScope } from "~/state/session"; |
| 12 | import { isWslSettingsRowVisible } from "./ConnectionsSettings.logic"; |
| 13 | import { isProviderSettingsEnvironmentAvailable } from "./ProviderSettingsPanel.logic"; |
| 14 | import type { SettingsScopeSearch } from "./settingsScope"; |
| 15 | import { |
| 16 | filterAvailableSettingsSearchItems, |
| 17 | getThreadAutoSettlementSearchAvailability, |
| 18 | } from "./settingsSearch"; |
| 19 | |
| 20 | export function useAvailableSettingsSearchItems(scopeSearch: SettingsScopeSearch = {}) { |
| 21 | const { environments } = useEnvironments(); |
| 22 | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 23 | const localEnvironmentDisabled = isLocalEnvironmentDisabled(); |
| 24 | const canMaintain = useEnvironmentScope(primaryEnvironmentId, AuthEnvironmentMaintainScope); |
| 25 | const canManageLocalBackend = !localEnvironmentDisabled && canMaintain; |
| 26 | const desktopWsl = useEnvironmentQuery( |
| 27 | isElectron && canManageLocalBackend ? desktopWslStateAtom : null, |
| 28 | ); |
| 29 | const cloudLinkState = usePrimaryCloudLinkState().data; |
| 30 | // Same fallback as the Connections row: older servers imply a tunnel from `linked`. |
| 31 | const managedTunnelActive = |
| 32 | cloudLinkState?.linkRevoked !== true && |
| 33 | (cloudLinkState?.managedTunnelActive ?? cloudLinkState?.linked ?? false); |
| 34 | |
| 35 | return useMemo( |
| 36 | () => |
| 37 | filterAvailableSettingsSearchItems({ |
| 38 | localEnvironmentDisabled, |
| 39 | hasCloudPublicConfig: hasCloudPublicConfig(), |
| 40 | hasEnvironment: environments.some((environment) => environment.serverConfig !== null), |
| 41 | hasProviderSettingsEnvironment: environments.some((environment) => |
| 42 | isProviderSettingsEnvironmentAvailable({ |
| 43 | connectionPhase: environment.connection.phase, |
| 44 | hasServerConfig: environment.serverConfig !== null, |
| 45 | }), |
| 46 | ), |
| 47 | hasMacProviderSettingsEnvironment: environments.some( |
| 48 | (environment) => |
| 49 | (scopeSearch.machine === undefined || |
| 50 | environment.environmentId === scopeSearch.machine) && |
| 51 | environment.serverConfig?.environment.platform.os === "darwin" && |
| 52 | isProviderSettingsEnvironmentAvailable({ |
| 53 | connectionPhase: environment.connection.phase, |
| 54 | hasServerConfig: true, |
| 55 | }), |
| 56 | ), |
| 57 | canManageLocalBackend, |
| 58 | isWslSettingsRowVisible: isWslSettingsRowVisible({ |
| 59 | state: desktopWsl.data, |
| 60 | error: desktopWsl.error, |
| 61 | }), |
| 62 | hasThreadAutoSettlement: |
| 63 | getThreadAutoSettlementSearchAvailability(environments).eligibleEnvironmentIds.length > 0, |
| 64 | managedTunnelActive, |
| 65 | }), |
| 66 | [ |
| 67 | managedTunnelActive, |
| 68 | canManageLocalBackend, |
| 69 | desktopWsl.data, |
| 70 | desktopWsl.error, |
| 71 | environments, |
| 72 | localEnvironmentDisabled, |
| 73 | scopeSearch.machine, |
| 74 | ], |
| 75 | ); |
| 76 | } |