useDeploymentMode.ts 1.7 KB

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