environmentSelection.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { SETTING_SOURCES, type SettingSource } from '../settings/constants.js'
  2. import {
  3. getSettings_DEPRECATED,
  4. getSettingsForSource,
  5. } from '../settings/settings.js'
  6. import { type EnvironmentResource, fetchEnvironments } from './environments.js'
  7. export type EnvironmentSelectionInfo = {
  8. availableEnvironments: EnvironmentResource[]
  9. selectedEnvironment: EnvironmentResource | null
  10. selectedEnvironmentSource: SettingSource | null
  11. }
  12. /**
  13. * Gets information about available environments and the currently selected one.
  14. *
  15. * @returns Promise<EnvironmentSelectionInfo> containing:
  16. * - availableEnvironments: all environments from the API
  17. * - selectedEnvironment: the environment that would be used (based on settings or first available),
  18. * or null if no environments are available
  19. * - selectedEnvironmentSource: the SettingSource where defaultEnvironmentId is configured,
  20. * or null if using the default (first environment)
  21. */
  22. export async function getEnvironmentSelectionInfo(): Promise<EnvironmentSelectionInfo> {
  23. // Fetch available environments
  24. const environments = await fetchEnvironments()
  25. if (environments.length === 0) {
  26. return {
  27. availableEnvironments: [],
  28. selectedEnvironment: null,
  29. selectedEnvironmentSource: null,
  30. }
  31. }
  32. // Get the merged settings to see what would actually be used
  33. const mergedSettings = getSettings_DEPRECATED()
  34. const defaultEnvironmentId = mergedSettings?.remote?.defaultEnvironmentId
  35. // Find which environment would be selected
  36. let selectedEnvironment: EnvironmentResource =
  37. environments.find(env => env.kind !== 'bridge') ?? environments[0]!
  38. let selectedEnvironmentSource: SettingSource | null = null
  39. if (defaultEnvironmentId) {
  40. const matchingEnvironment = environments.find(
  41. env => env.environment_id === defaultEnvironmentId,
  42. )
  43. if (matchingEnvironment) {
  44. selectedEnvironment = matchingEnvironment
  45. // Find which source has this setting
  46. // Iterate from lowest to highest priority, so the last match wins (highest priority)
  47. for (let i = SETTING_SOURCES.length - 1; i >= 0; i--) {
  48. const source = SETTING_SOURCES[i]
  49. if (!source || source === 'flagSettings') {
  50. // Skip flagSettings as it's not a normal source we check
  51. continue
  52. }
  53. const sourceSettings = getSettingsForSource(source)
  54. if (
  55. sourceSettings?.remote?.defaultEnvironmentId === defaultEnvironmentId
  56. ) {
  57. selectedEnvironmentSource = source
  58. break
  59. }
  60. }
  61. }
  62. }
  63. return {
  64. availableEnvironments: environments,
  65. selectedEnvironment,
  66. selectedEnvironmentSource,
  67. }
  68. }