( set, get )
| 144 | } |
| 145 | |
| 146 | export const createWorkspaceSlice: AppSliceCreator<WorkspaceSlice> = ( |
| 147 | set, |
| 148 | get |
| 149 | ) => { |
| 150 | // Listen on the shared cross-tab channel (see store/workspaceSwitchChannel.ts). |
| 151 | // Using `addEventListener` rather than `onmessage = ...` allows the Vue-side |
| 152 | // store to register its own handler on the same object, and source-object |
| 153 | // exclusion correctly suppresses both handlers when a post originates from |
| 154 | // this tab (e.g. the OAuth2 consent page's in-place switch). |
| 155 | if (!workspaceSwitchListenerRegistered) { |
| 156 | workspaceSwitchListenerRegistered = true; |
| 157 | workspaceSwitchChannel.addEventListener("message", (event) => { |
| 158 | get().recordRecentVisit( |
| 159 | resolvePath(WORKSPACE_ROUTE_LANDING), |
| 160 | typeof event.data === "string" ? event.data : undefined |
| 161 | ); |
| 162 | window.location.href = "/"; |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | const unknownEnvironment = createUnknownEnvironment(); |
| 167 | const nullEnvironment = createNullEnvironment(); |
| 168 | const environmentFallbacksByName = new Map<string, Environment>(); |
| 169 | |
| 170 | const getEnvironmentFallback = (name: string, id: string) => { |
| 171 | const existing = environmentFallbacksByName.get(name); |
| 172 | if (existing) return existing; |
| 173 | const environment = { ...unknownEnvironment, id, name, title: id }; |
| 174 | environmentFallbacksByName.set(name, environment); |
| 175 | return environment; |
| 176 | }; |
| 177 | |
| 178 | // Persist the ENVIRONMENT setting and re-derive the cached environment list |
| 179 | // from the server's response (mirrors the legacy Pinia env store). |
| 180 | const writeEnvironmentSetting = async ( |
| 181 | environments: EnvironmentSetting_Environment[] |
| 182 | ): Promise<Environment[]> => { |
| 183 | const response = await get().upsertSetting({ |
| 184 | name: Setting_SettingName.ENVIRONMENT, |
| 185 | value: createProto(SettingValueSchema, { |
| 186 | value: { |
| 187 | case: "environment", |
| 188 | value: createProto(EnvironmentSettingSchema, { environments }), |
| 189 | }, |
| 190 | }), |
| 191 | }); |
| 192 | const next = |
| 193 | response.value?.value?.case === "environment" |
| 194 | ? convertEnvironmentList(response.value.value.value.environments) |
| 195 | : []; |
| 196 | set({ environmentList: next }); |
| 197 | return next; |
| 198 | }; |
| 199 | |
| 200 | return { |
| 201 | serverInfoTs: 0, |
| 202 | workspaceList: [], |
| 203 | environmentList: [], |
no test coverage detected