Skip to content

Commit

Permalink
fix: invalid environment index can break the app (hoppscotch#3041)
Browse files Browse the repository at this point in the history
  • Loading branch information
amk-dev authored and AndrewBastin committed May 11, 2023
1 parent a642658 commit b7c2d13
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions packages/hoppscotch-common/src/newstore/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ type EnvironmentStore = typeof defaultEnvironmentsState

const dispatchers = defineDispatchers({
setSelectedEnvironmentIndex(
_: EnvironmentStore,
store: EnvironmentStore,
{
selectedEnvironmentIndex,
}: { selectedEnvironmentIndex: SelectedEnvironmentIndex }
) {
return {
selectedEnvironmentIndex,
if (
selectedEnvironmentIndex.type === "MY_ENV" &&
!!store.environments[selectedEnvironmentIndex.index]
) {
return {
selectedEnvironmentIndex,
}
} else {
return {
type: "NO_ENV_SELECTED",
}
}
},
appendEnvironments(
Expand Down Expand Up @@ -325,21 +334,22 @@ export const selectedEnvironmentIndex$ = environmentsStore.subject$.pipe(
distinctUntilChanged()
)

export const currentEnvironment$ = environmentsStore.subject$.pipe(
map(({ environments, selectedEnvironmentIndex }) => {
if (selectedEnvironmentIndex.type === "NO_ENV_SELECTED") {
const env: Environment = {
name: "No environment",
variables: [],
export const currentEnvironment$: Observable<Environment | undefined> =
environmentsStore.subject$.pipe(
map(({ environments, selectedEnvironmentIndex }) => {
if (selectedEnvironmentIndex.type === "NO_ENV_SELECTED") {
const env: Environment = {
name: "No environment",
variables: [],
}
return env
} else if (selectedEnvironmentIndex.type === "MY_ENV") {
return environments[selectedEnvironmentIndex.index]
} else {
return selectedEnvironmentIndex.environment
}
return env
} else if (selectedEnvironmentIndex.type === "MY_ENV") {
return environments[selectedEnvironmentIndex.index]
} else {
return selectedEnvironmentIndex.environment
}
})
)
})
)

export type AggregateEnvironment = {
key: string
Expand All @@ -358,7 +368,7 @@ export const aggregateEnvs$: Observable<AggregateEnvironment[]> = combineLatest(
map(([selectedEnv, globalVars]) => {
const results: AggregateEnvironment[] = []

selectedEnv.variables.forEach(({ key, value }) =>
selectedEnv?.variables.forEach(({ key, value }) =>
results.push({ key, value, sourceEnv: selectedEnv.name })
)
globalVars.forEach(({ key, value }) =>
Expand Down

0 comments on commit b7c2d13

Please sign in to comment.