Explorar o código

Wrong environment fix (#4246)

* reset state for disabled env selection

* restored env selection for dev

---------

Co-authored-by: Artem <Artem Slugin>
attemka hai 1 ano
pai
achega
2018bbd1fa

+ 1 - 3
packages/atlas/src/components/_overlays/AdminModal/AdminModal.tsx

@@ -14,7 +14,7 @@ import { Select } from '@/components/_inputs/Select'
 import { Switch } from '@/components/_inputs/Switch'
 import { DialogModal } from '@/components/_overlays/DialogModal'
 import { atlasConfig } from '@/config'
-import { NODE_URL, availableEnvs, getEnvName } from '@/config/env'
+import { ENV_SELECTION_ENABLED, NODE_URL, availableEnvs } from '@/config/env'
 import { absoluteRoutes } from '@/config/routes'
 import { useConfirmationModal } from '@/providers/confirmationModal'
 import { useEnvironmentStore } from '@/providers/environment'
@@ -31,8 +31,6 @@ import {
   VerticalSpacedContainer,
 } from './AdminModal.styles'
 
-const ENV_SELECTION_ENABLED: boolean = import.meta.env[getEnvName('ENV_SELECTION_ENABLED')] === 'true'
-
 const ENVIRONMENT_NAMES: Record<string, string> = {
   production: 'Joystream Mainnet',
   development: `${atlasConfig.general.appName} Dev Testnet`,

+ 11 - 0
packages/atlas/src/config/env.ts

@@ -8,8 +8,19 @@ export const getEnvName = (name: string) => {
   return `${ENV_PREFIX}_${name}`
 }
 
+export const ENV_SELECTION_ENABLED: boolean = import.meta.env[getEnvName('ENV_SELECTION_ENABLED')] === 'true'
+
 export const BUILD_ENV = (import.meta.env[getEnvName('ENV')] || 'production') as BuildEnv
 
+// if it's a case that the environment selection is disabled, but user has different value in the localstorage
+// we need to reset the state
+if (ENV_SELECTION_ENABLED === false) {
+  const environmentState = useEnvironmentStore.getState()
+
+  if (environmentState.actions.getInitialState().targetDevEnv !== environmentState.targetDevEnv) {
+    useEnvironmentStore.getState().actions.reset()
+  }
+}
 export const availableEnvs = () => {
   return Array.from(
     new Set(

+ 6 - 0
packages/atlas/src/providers/environment/store.ts

@@ -15,6 +15,8 @@ const INITIAL_STATE: EnvironmentState = {
 export type EnvironmentStoreActions = {
   setTargetDevEnv: (env: string) => void
   setNodeOverride: (node: string | null) => void
+  reset: () => void
+  getInitialState: () => EnvironmentState
 }
 
 export const useEnvironmentStore = createStore<EnvironmentState, EnvironmentStoreActions>(
@@ -31,6 +33,10 @@ export const useEnvironmentStore = createStore<EnvironmentState, EnvironmentStor
           state.targetDevEnv = env
         })
       },
+      reset: () => {
+        set(() => INITIAL_STATE)
+      },
+      getInitialState: () => INITIAL_STATE,
     }),
   },
   {