fix: invalid environment index can break the app (#3041)

This commit is contained in:
Akash K
2023-05-10 19:14:16 +05:30
committed by GitHub
parent b2693d6ba2
commit 8765c1a8ac

View File

@@ -37,14 +37,23 @@ type EnvironmentStore = typeof defaultEnvironmentsState
const dispatchers = defineDispatchers({ const dispatchers = defineDispatchers({
setSelectedEnvironmentIndex( setSelectedEnvironmentIndex(
_: EnvironmentStore, store: EnvironmentStore,
{ {
selectedEnvironmentIndex, selectedEnvironmentIndex,
}: { selectedEnvironmentIndex: SelectedEnvironmentIndex } }: { selectedEnvironmentIndex: SelectedEnvironmentIndex }
) {
if (
selectedEnvironmentIndex.type === "MY_ENV" &&
!!store.environments[selectedEnvironmentIndex.index]
) { ) {
return { return {
selectedEnvironmentIndex, selectedEnvironmentIndex,
} }
} else {
return {
type: "NO_ENV_SELECTED",
}
}
}, },
appendEnvironments( appendEnvironments(
{ environments }: EnvironmentStore, { environments }: EnvironmentStore,
@@ -325,7 +334,8 @@ export const selectedEnvironmentIndex$ = environmentsStore.subject$.pipe(
distinctUntilChanged() distinctUntilChanged()
) )
export const currentEnvironment$ = environmentsStore.subject$.pipe( export const currentEnvironment$: Observable<Environment | undefined> =
environmentsStore.subject$.pipe(
map(({ environments, selectedEnvironmentIndex }) => { map(({ environments, selectedEnvironmentIndex }) => {
if (selectedEnvironmentIndex.type === "NO_ENV_SELECTED") { if (selectedEnvironmentIndex.type === "NO_ENV_SELECTED") {
const env: Environment = { const env: Environment = {
@@ -358,7 +368,7 @@ export const aggregateEnvs$: Observable<AggregateEnvironment[]> = combineLatest(
map(([selectedEnv, globalVars]) => { map(([selectedEnv, globalVars]) => {
const results: AggregateEnvironment[] = [] const results: AggregateEnvironment[] = []
selectedEnv.variables.forEach(({ key, value }) => selectedEnv?.variables.forEach(({ key, value }) =>
results.push({ key, value, sourceEnv: selectedEnv.name }) results.push({ key, value, sourceEnv: selectedEnv.name })
) )
globalVars.forEach(({ key, value }) => globalVars.forEach(({ key, value }) =>