Skip to content

Commit 94e6a30

Browse files
authored
Fix: Synchronize selected environment in all tabs (#2127)
1 parent c602f8b commit 94e6a30

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

web/client/src/App.tsx

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ import {
2020
} from '@api/client'
2121
import { type PlanApplyTracker } from '@models/tracker-plan-apply'
2222
import { type PlanCancelTracker } from '@models/tracker-plan-cancel'
23+
import { type EnvironmentName } from '@models/environment'
2324

2425
export default function App(): JSX.Element {
2526
const environment = useStoreContext(s => s.environment)
27+
const environments = useStoreContext(s => s.environments)
2628
const version = useStoreContext(s => s.version)
2729
const modules = useStoreContext(s => s.modules)
2830
const setVersion = useStoreContext(s => s.setVersion)
2931
const setModules = useStoreContext(s => s.setModules)
32+
const addLocalEnvironment = useStoreContext(s => s.addLocalEnvironment)
3033
const addRemoteEnvironments = useStoreContext(s => s.addRemoteEnvironments)
34+
const setEnvironment = useStoreContext(s => s.setEnvironment)
3135

3236
const planOverview = useStorePlan(s => s.planOverview)
3337
const planApply = useStorePlan(s => s.planApply)
@@ -55,20 +59,25 @@ export default function App(): JSX.Element {
5559
} = useApiMeta()
5660

5761
useEffect(() => {
58-
const channelPlanOverview = channel<any>(
62+
const channelPlanOverview = channel(
5963
'plan-overview',
6064
updatePlanOverviewTracker,
6165
)
62-
const channelPlanApply = channel<any>('plan-apply', updatePlanApplyTracker)
63-
const channelPlanCancel = channel<any>(
64-
'plan-cancel',
65-
updatePlanCancelTracker,
66-
)
66+
const channelPlanApply = channel('plan-apply', updatePlanApplyTracker)
67+
const channelPlanCancel = channel('plan-cancel', updatePlanCancelTracker)
6768

6869
channelPlanOverview.subscribe()
6970
channelPlanApply.subscribe()
7071
channelPlanCancel.subscribe()
7172

73+
return () => {
74+
channelPlanApply.unsubscribe()
75+
channelPlanCancel.unsubscribe()
76+
channelPlanOverview.unsubscribe()
77+
}
78+
}, [environment, environments])
79+
80+
useEffect(() => {
7281
void getMeta().then(({ data }) => {
7382
setVersion(data?.version)
7483
setModules(Array.from(new Set(modules.concat(data?.modules ?? []))))
@@ -83,10 +92,6 @@ export default function App(): JSX.Element {
8392
})
8493

8594
return () => {
86-
channelPlanOverview.unsubscribe()
87-
channelPlanApply.unsubscribe()
88-
channelPlanCancel.unsubscribe()
89-
9095
void cancelRequestMeta()
9196
void cancelRequestEnvironments()
9297
}
@@ -95,18 +100,20 @@ export default function App(): JSX.Element {
95100
function updatePlanOverviewTracker(data: PlanOverviewTracker): void {
96101
planOverview.update(data)
97102

98-
setPlanOverview(planOverview)
99-
}
100-
101-
function updatePlanCancelTracker(data: PlanCancelTracker): void {
102-
planCancel.update(data)
103+
if (data.environment !== environment.name) {
104+
synchronizeEnvironment(data.environment)
105+
}
103106

104-
setPlanCancel(planCancel)
107+
setPlanOverview(planOverview)
105108
}
106109

107110
function updatePlanApplyTracker(data: PlanApplyTracker): void {
108111
planApply.update(data, planOverview)
109112

113+
if (data.environment !== environment.name) {
114+
synchronizeEnvironment(data.environment)
115+
}
116+
110117
const isFinished =
111118
isTrue(data.meta?.done) && data.meta?.status !== Status.init
112119

@@ -125,6 +132,16 @@ export default function App(): JSX.Element {
125132
setPlanApply(planApply)
126133
}
127134

135+
function updatePlanCancelTracker(data: PlanCancelTracker): void {
136+
planCancel.update(data)
137+
138+
if (data.environment !== environment.name) {
139+
synchronizeEnvironment(data.environment)
140+
}
141+
142+
setPlanCancel(planCancel)
143+
}
144+
128145
function updateEnviroments(data: Optional<Environments>): void {
129146
const { environments, default_target_environment, pinned_environments } =
130147
data ?? {}
@@ -138,6 +155,16 @@ export default function App(): JSX.Element {
138155
}
139156
}
140157

158+
function synchronizeEnvironment(environment: EnvironmentName): void {
159+
const found = Array.from(environments).find(e => e.name === environment)
160+
161+
if (isNil(found)) {
162+
addLocalEnvironment(environment)
163+
} else {
164+
setEnvironment(found)
165+
}
166+
}
167+
141168
const router = getBrowserRouter(modules)
142169
const isLoadingMeta = isNil(version) || isFetchingMeta
143170

web/client/src/context/context.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ interface ContextStore {
3434
getNextEnvironment: () => ModelEnvironment
3535
setEnvironment: (environment: ModelEnvironment) => void
3636
addLocalEnvironment: (
37-
environments: EnvironmentName,
38-
created_from: EnvironmentName,
37+
environment: EnvironmentName,
38+
created_from?: EnvironmentName,
3939
) => void
4040
removeLocalEnvironment: (environments: ModelEnvironment) => void
4141
addRemoteEnvironments: (
@@ -46,6 +46,8 @@ interface ContextStore {
4646
hasRemoteEnvironments: () => boolean
4747
}
4848

49+
const PROD = 'prod'
50+
4951
const environments = new Set(ModelEnvironment.getEnvironments())
5052
const environment =
5153
ModelEnvironment.getEnvironment() ?? environments.values().next().value
@@ -163,7 +165,7 @@ export const useStoreContext = create<ContextStore>((set, get) => ({
163165
}
164166
})
165167
},
166-
addLocalEnvironment(localEnvironment, created_from) {
168+
addLocalEnvironment(localEnvironment, created_from = PROD) {
167169
set(s => {
168170
if (isStringEmptyOrNil(localEnvironment)) return s
169171

@@ -260,7 +262,7 @@ export const useStoreContext = create<ContextStore>((set, get) => ({
260262

261263
environments.forEach(env => {
262264
switch (env.name) {
263-
case 'prod':
265+
case PROD:
264266
prodEnv = env
265267
break
266268
case profileEnv?.name:

0 commit comments

Comments
 (0)