@@ -20,14 +20,18 @@ import {
2020} from '@api/client'
2121import { type PlanApplyTracker } from '@models/tracker-plan-apply'
2222import { type PlanCancelTracker } from '@models/tracker-plan-cancel'
23+ import { type EnvironmentName } from '@models/environment'
2324
2425export 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
0 commit comments