useDeploymentMode.ts38 lines · main
| 1 | import { useMemo } from 'react' |
| 2 | |
| 3 | import { useDeploymentModeQuery } from '@/data/config/deployment-mode-query' |
| 4 | import { IS_PLATFORM } from '@/lib/constants' |
| 5 | |
| 6 | export type DeploymentMode = { |
| 7 | isPlatform: boolean |
| 8 | isCli: boolean |
| 9 | isSelfHosted: boolean |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Resolves the current Studio deployment mode (platform / CLI / self-hosted). |
| 14 | * |
| 15 | * `isPlatform` is the build-time `IS_PLATFORM` constant — prefer that constant |
| 16 | * directly when you only need the platform-vs-not split (build-time, server- |
| 17 | * side, or module-scope). Reach for this hook when you need to distinguish |
| 18 | * CLI from self-hosted at runtime, which is something `IS_PLATFORM` can't |
| 19 | * express on its own. |
| 20 | * |
| 21 | * CLI vs self-hosted is resolved server-side by /platform/deployment-mode |
| 22 | * (reading `CURRENT_CLI_VERSION`). The underlying query is disabled on |
| 23 | * platform builds, so the hook is a no-op there. |
| 24 | * |
| 25 | * The return is memoized on the primitive flags so consumers can safely list |
| 26 | * the whole `DeploymentMode` object in their `useMemo`/`useCallback` deps. |
| 27 | */ |
| 28 | export function useDeploymentMode(): DeploymentMode { |
| 29 | const { data } = useDeploymentModeQuery() |
| 30 | // Default to CLI (`?? true`) during the loading window. `'direct'` is the only |
| 31 | // method valid in every environment, so a self-hosted user briefly seeing CLI |
| 32 | // defaults lands on a valid (if not preferred) choice — whereas a CLI user |
| 33 | // briefly seeing self-hosted defaults gets `connectionMethod` pinned to |
| 34 | // `'session'`, which isn't a valid CLI method. |
| 35 | const isCli = !IS_PLATFORM && (data?.is_cli_mode ?? true) |
| 36 | const isSelfHosted = !IS_PLATFORM && !isCli |
| 37 | return useMemo(() => ({ isPlatform: IS_PLATFORM, isCli, isSelfHosted }), [isCli, isSelfHosted]) |
| 38 | } |