@burdenoff/microfe-auth 2026.626.1 → 2026.627.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,15 @@
1
1
  import { useAuthStore as e } from "../store/authStore.js";
2
2
  import { resolveGatewayBaseUrls as t } from "../utils/gatewayBaseUrls.js";
3
3
  import { useCallback as n, useEffect as r, useRef as i, useState as a } from "react";
4
- import { getStoredAuthToken as o, graphqlFetch as s } from "@burdenoff/fe-libs/shared/graphql";
5
- import { useActiveContext as c } from "@burdenoff/fe-libs/shared/providers/shell/ActiveContextProvider";
4
+ import { useEventBus as o } from "@burdenoff/fe-libs/shared/events";
5
+ import { getStoredAuthToken as s, graphqlFetch as c } from "@burdenoff/fe-libs/shared/graphql";
6
+ import { useActiveContext as l } from "@burdenoff/fe-libs/shared/providers/shell/ActiveContextProvider";
6
7
  //#region src/auth/hooks/useContextSwitcherData.ts
7
- var l = "\n query MyOrganizations {\n myOrganizations {\n total\n items {\n id\n name\n slug\n tenantId\n description\n logo\n website\n status\n type\n createdAt\n updatedAt\n }\n }\n }\n", u = "\n query WorkspacesByOrg {\n workspaces: myWorkspaces(limit: 100) {\n total\n items {\n id\n name\n slug\n tenantId\n organizationId\n type\n status\n region\n createdAt\n updatedAt\n }\n }\n }\n", d = "\n query ContextSwitcherBootstrap {\n myOrganizations {\n total\n items {\n id\n name\n slug\n tenantId\n description\n logo\n website\n status\n type\n createdAt\n updatedAt\n }\n }\n workspaces: myWorkspaces(limit: 100) {\n total\n items {\n id\n name\n slug\n tenantId\n organizationId\n type\n status\n region\n createdAt\n updatedAt\n }\n }\n }\n", f = "\n mutation IssueWorkspaceToken($input: IssueWorkspaceTokenInput!) {\n issueWorkspaceToken(input: $input) {\n success\n token\n expiresAt\n organizationId\n workspaceId\n projectId\n scopes\n error {\n code\n message\n }\n }\n }\n", p = "\n query MyProjectsForContext {\n projects: myProjects {\n total\n items {\n id\n name\n key\n workspaceId\n status\n archived\n createdAt\n updatedAt\n }\n }\n }\n", m = "\n mutation CreateDefaultProject($input: CreateProjectInput!) {\n createProject(input: $input) {\n id\n name\n key\n workspaceId\n status\n archived\n createdAt\n updatedAt\n }\n }\n";
8
- async function h({ authToken: e, workspaceBaseUrl: t, workspaceToken: n }) {
9
- let r = await s({
8
+ var u = "\n query MyOrganizations {\n myOrganizations {\n total\n items {\n id\n name\n slug\n tenantId\n description\n logo\n website\n status\n type\n createdAt\n updatedAt\n }\n }\n }\n", d = "\n query WorkspacesByOrg {\n workspaces: myWorkspaces(limit: 100) {\n total\n items {\n id\n name\n slug\n tenantId\n organizationId\n type\n status\n region\n createdAt\n updatedAt\n }\n }\n }\n", f = "\n query ContextSwitcherBootstrap {\n myOrganizations {\n total\n items {\n id\n name\n slug\n tenantId\n description\n logo\n website\n status\n type\n createdAt\n updatedAt\n }\n }\n workspaces: myWorkspaces(limit: 100) {\n total\n items {\n id\n name\n slug\n tenantId\n organizationId\n type\n status\n region\n createdAt\n updatedAt\n }\n }\n }\n", p = "\n mutation IssueWorkspaceToken($input: IssueWorkspaceTokenInput!) {\n issueWorkspaceToken(input: $input) {\n success\n token\n expiresAt\n organizationId\n workspaceId\n projectId\n scopes\n error {\n code\n message\n }\n }\n }\n", m = "\n query MyProjectsForContext {\n projects: myProjects {\n total\n items {\n id\n name\n key\n workspaceId\n status\n archived\n createdAt\n updatedAt\n }\n }\n }\n", h = "\n mutation CreateDefaultProject($input: CreateProjectInput!) {\n createProject(input: $input) {\n id\n name\n key\n workspaceId\n status\n archived\n createdAt\n updatedAt\n }\n }\n";
9
+ async function g({ authToken: e, workspaceBaseUrl: t, workspaceToken: n }) {
10
+ let r = await c({
10
11
  gateway: "workspace",
11
- query: m,
12
+ query: h,
12
13
  variables: { input: { name: "Default Project" } },
13
14
  authToken: e,
14
15
  workspaceToken: n,
@@ -16,7 +17,7 @@ async function h({ authToken: e, workspaceBaseUrl: t, workspaceToken: n }) {
16
17
  });
17
18
  return r.errors?.length ? (console.warn("Failed to create default project:", r.errors), null) : r.data?.createProject ?? null;
18
19
  }
19
- function g(e, t = 3e4) {
20
+ function _(e, t = 3e4) {
20
21
  try {
21
22
  let n = JSON.parse(atob(e.split(".")[1]));
22
23
  return n.exp ? n.exp * 1e3 < Date.now() + t : !1;
@@ -24,7 +25,7 @@ function g(e, t = 3e4) {
24
25
  return !0;
25
26
  }
26
27
  }
27
- function _(e) {
28
+ function v(e) {
28
29
  try {
29
30
  let t = JSON.parse(atob(e.split(".")[1]));
30
31
  return {
@@ -37,26 +38,26 @@ function _(e) {
37
38
  return null;
38
39
  }
39
40
  }
40
- function v(e, t) {
41
- if (!e || g(e)) return !1;
42
- let n = _(e);
41
+ function y(e, t) {
42
+ if (!e || _(e)) return !1;
43
+ let n = v(e);
43
44
  return !(!n || t.workspaceId && n.workspaceId !== t.workspaceId || t.organizationId && n.organizationId !== t.organizationId || t.projectId && n.projectId !== t.projectId);
44
45
  }
45
- var y = class extends Error {
46
+ var b = class extends Error {
46
47
  constructor(e) {
47
48
  super(e), this.name = "AuthError";
48
49
  }
49
50
  };
50
- function b(e) {
51
+ function x(e) {
51
52
  return e.some((e) => e.extensions?.code === "UNAUTHENTICATED" || e.message?.toLowerCase().includes("unauthorized") || e.message?.toLowerCase().includes("not authenticated"));
52
53
  }
53
- function x() {
54
+ function S() {
54
55
  let t = e.getState().accessToken;
55
- if (t && !g(t)) return t;
56
- let n = o();
57
- return n && !g(n) ? n : t;
56
+ if (t && !_(t)) return t;
57
+ let n = s();
58
+ return n && !_(n) ? n : t;
58
59
  }
59
- function S(e) {
60
+ function C(e) {
60
61
  return Array.from(new Set(e.flatMap((e) => e.organizationId ? [e.organizationId] : []))).map((t, n) => ({
61
62
  id: t,
62
63
  name: e.find((e) => e.organizationId === t)?.name || `Organization ${n + 1}`,
@@ -64,55 +65,74 @@ function S(e) {
64
65
  tenantId: e.find((e) => e.organizationId === t)?.tenantId || ""
65
66
  }));
66
67
  }
67
- function C(o) {
68
- let { organizationId: m, workspaceId: _, projectId: C, setContext: w } = c(), T = e((e) => e.organizations), E = e((e) => e.currentOrganizationId), D = e((e) => e.isLoadingOrganizations), O = e((e) => e.workspaces), k = e((e) => e.currentWorkspaceId), A = e((e) => e.workspaceToken), j = e((e) => e.isLoadingWorkspaces), M = e((e) => e.projects), N = e((e) => e.currentProjectId), P = e((e) => e.isLoadingProjects), [F, I] = a(null), L = i(null), R = i(/* @__PURE__ */ new Map()), z = i(_), B = i(m);
68
+ var w = new Set([
69
+ "project.created",
70
+ "project.updated",
71
+ "project.deleted",
72
+ "project.archived",
73
+ "project.unarchived"
74
+ ]), T = new Set([
75
+ "workspace.created",
76
+ "workspace.updated",
77
+ "workspace.deleted",
78
+ "workspace.archived",
79
+ "workspace.reactivated"
80
+ ]);
81
+ function E(e) {
82
+ if (e && typeof e == "object" && "workspaceId" in e) {
83
+ let t = e.workspaceId;
84
+ return typeof t == "string" ? t : void 0;
85
+ }
86
+ }
87
+ function D(s) {
88
+ let { organizationId: h, workspaceId: v, projectId: D, setContext: O } = l(), k = o(), A = e((e) => e.organizations), j = e((e) => e.currentOrganizationId), M = e((e) => e.isLoadingOrganizations), N = e((e) => e.workspaces), P = e((e) => e.currentWorkspaceId), ee = e((e) => e.workspaceToken), F = e((e) => e.isLoadingWorkspaces), I = e((e) => e.projects), L = e((e) => e.currentProjectId), R = e((e) => e.isLoadingProjects), [te, z] = a(null), B = i(null), V = i(/* @__PURE__ */ new Map()), H = i(v), U = i(h);
69
89
  r(() => {
70
- z.current = _, B.current = m;
90
+ H.current = v, U.current = h;
71
91
  });
72
- let V = t(), H = o?.globalBaseUrl ?? V.globalBaseUrl, U = o?.workspaceBaseUrl ?? V.workspaceBaseUrl, W = i(null), G = i(null), K = i(null), q = i(null);
92
+ let W = t(), G = s?.globalBaseUrl ?? W.globalBaseUrl, K = s?.workspaceBaseUrl ?? W.workspaceBaseUrl, q = i(null), J = i(null), Y = i(null), X = i(null);
73
93
  r(() => {
74
94
  let t = e.getState();
75
- if (m && m !== t.currentOrganizationId && m !== W.current && (W.current = m, t.setCurrentOrganizationId(m), t.workspaces.length > 0 && !t.workspaces.some((e) => e.organizationId === m) && t.setWorkspaces([])), _ && _ !== t.currentWorkspaceId && _ !== G.current && (G.current = _, t.setCurrentWorkspaceId(_), t.projects.length > 0 && !t.projects.some((e) => e.workspaceId === _) && (t.setProjects([]), t.setCurrentProjectId(null))), C && C !== t.currentProjectId && C !== K.current && (K.current = C, t.setCurrentProjectId(C)), t.workspaceToken && !v(t.workspaceToken, {
76
- workspaceId: _,
77
- organizationId: m,
78
- projectId: C
95
+ if (h && h !== t.currentOrganizationId && h !== q.current && (q.current = h, t.setCurrentOrganizationId(h), t.workspaces.length > 0 && !t.workspaces.some((e) => e.organizationId === h) && t.setWorkspaces([])), v && v !== t.currentWorkspaceId && v !== J.current && (J.current = v, t.setCurrentWorkspaceId(v), t.projects.length > 0 && !t.projects.some((e) => e.workspaceId === v) && (t.setProjects([]), t.setCurrentProjectId(null))), D && D !== t.currentProjectId && D !== Y.current && (Y.current = D, t.setCurrentProjectId(D)), t.workspaceToken && !y(t.workspaceToken, {
96
+ workspaceId: v,
97
+ organizationId: h,
98
+ projectId: D
79
99
  })) {
80
- let e = `${m ?? ""}|${_ ?? ""}|${C ?? ""}`;
81
- q.current !== e && (q.current = e, t.setWorkspaceToken(null));
100
+ let e = `${h ?? ""}|${v ?? ""}|${D ?? ""}`;
101
+ X.current !== e && (X.current = e, t.setWorkspaceToken(null));
82
102
  }
83
103
  }, [
84
- m,
85
- _,
86
- C
104
+ h,
105
+ v,
106
+ D
87
107
  ]);
88
- let J = n(async () => {
89
- if (L.current) return L.current;
90
- let t = e.getState(), n = x();
91
- return !n || g(n) ? [] : (t.setLoadingOrganizations(!0), I(null), L.current = (async () => {
108
+ let ne = n(async () => {
109
+ if (B.current) return B.current;
110
+ let t = e.getState(), n = S();
111
+ return !n || _(n) ? [] : (t.setLoadingOrganizations(!0), z(null), B.current = (async () => {
92
112
  try {
93
- let r = B.current ?? t.currentOrganizationId, i = await s({
113
+ let r = U.current ?? t.currentOrganizationId, i = await c({
94
114
  gateway: "global",
95
- query: r ? d : l,
115
+ query: r ? f : u,
96
116
  variables: void 0,
97
117
  authToken: n,
98
- baseUrl: H
118
+ baseUrl: G
99
119
  });
100
- if (r && i.errors?.length && i.errors.some((e) => e?.extensions?.code === "FORBIDDEN" || /not a member of this organization/i.test(e?.message ?? "") || /Cannot return null for non-nullable field Query\.myOrganizations/i.test(e?.message ?? "")) && (i = await s({
120
+ if (r && i.errors?.length && i.errors.some((e) => e?.extensions?.code === "FORBIDDEN" || /not a member of this organization/i.test(e?.message ?? "") || /Cannot return null for non-nullable field Query\.myOrganizations/i.test(e?.message ?? "")) && (i = await c({
101
121
  gateway: "global",
102
122
  query: "\n query ContextSwitcherRecover {\n myOrganizations {\n total\n items { id name slug tenantId description logo website status type createdAt updatedAt }\n }\n workspaces: myWorkspaces {\n total\n items { id name slug tenantId organizationId type status region createdAt updatedAt }\n }\n }\n ",
103
123
  authToken: n,
104
- baseUrl: H
124
+ baseUrl: G
105
125
  })), i.errors?.length) {
106
126
  let e = i.errors[0]?.message ?? "GraphQL error";
107
- throw b(i.errors) ? new y(e) : (t.workspaces.length === 0 && console.error("Failed to fetch organizations:", i.errors), Error(e));
127
+ throw x(i.errors) ? new b(e) : (t.workspaces.length === 0 && console.error("Failed to fetch organizations:", i.errors), Error(e));
108
128
  }
109
129
  let a = i.data?.myOrganizations?.items ?? [];
110
130
  if (r && a.length === 0 && t.workspaces.length === 0) {
111
- let e = await s({
131
+ let e = await c({
112
132
  gateway: "global",
113
- query: l,
133
+ query: u,
114
134
  authToken: n,
115
- baseUrl: H
135
+ baseUrl: G
116
136
  });
117
137
  e.errors?.length ? console.warn("[ContextSwitcher] Stale-context recovery failed:", e.errors) : (a = e.data?.myOrganizations?.items ?? [], i = {
118
138
  ...e,
@@ -122,105 +142,105 @@ function C(o) {
122
142
  } }
123
143
  });
124
144
  }
125
- let o = a.length === 0 && t.workspaces.length > 0 ? S(t.workspaces) : [], c = o.length > 0 ? o : a;
126
- t.setOrganizations(c);
127
- let f = r, p = f && a.some((e) => e.id === f);
128
- if (f && !p && a.length > 0 && (f = a[0].id, w({
129
- organizationId: f,
145
+ let o = a.length === 0 && t.workspaces.length > 0 ? C(t.workspaces) : [], s = o.length > 0 ? o : a;
146
+ t.setOrganizations(s);
147
+ let l = r, p = l && a.some((e) => e.id === l);
148
+ if (l && !p && a.length > 0 && (l = a[0].id, O({
149
+ organizationId: l,
130
150
  workspaceId: null,
131
151
  tenantId: null,
132
152
  projectId: null
133
- }), t.setWorkspaces([]), t.setProjects([]), t.setCurrentWorkspaceId(null), t.setCurrentProjectId(null), t.setWorkspaceToken(null)), f && f !== t.currentOrganizationId && t.setCurrentOrganizationId(f), c.length > 0 && !f && (t.setCurrentOrganizationId(c[0].id), f = c[0].id), f && e.getState().workspaces.length === 0) {
153
+ }), t.setWorkspaces([]), t.setProjects([]), t.setCurrentWorkspaceId(null), t.setCurrentProjectId(null), t.setWorkspaceToken(null)), l && l !== t.currentOrganizationId && t.setCurrentOrganizationId(l), s.length > 0 && !l && (t.setCurrentOrganizationId(s[0].id), l = s[0].id), l && e.getState().workspaces.length === 0) {
134
154
  t.setLoadingWorkspaces(!0);
135
155
  try {
136
- let e = (r === f ? i.data?.workspaces?.items?.filter((e) => e.organizationId === f) : void 0) || await s({
156
+ let e = (r === l ? i.data?.workspaces?.items?.filter((e) => e.organizationId === l) : void 0) || await c({
137
157
  gateway: "global",
138
- query: u,
158
+ query: d,
139
159
  authToken: n,
140
- baseUrl: H
141
- }).then((e) => e.errors?.length ? null : (e.data?.workspaces?.items ?? []).filter((e) => e.organizationId === f));
160
+ baseUrl: G
161
+ }).then((e) => e.errors?.length ? null : (e.data?.workspaces?.items ?? []).filter((e) => e.organizationId === l));
142
162
  if (e) {
143
163
  t.setWorkspaces(e);
144
- let n = z.current ? e.find((e) => e.id === z.current) : null;
164
+ let n = H.current ? e.find((e) => e.id === H.current) : null;
145
165
  if (n) t.setCurrentWorkspaceId(n.id);
146
166
  else if (e.length > 0) {
147
167
  let n = {
148
- organizationId: f,
168
+ organizationId: l,
149
169
  workspaceId: e[0].id,
150
170
  tenantId: e[0].tenantId,
151
171
  projectId: null
152
172
  };
153
- z.current ? (t.setCurrentWorkspaceId(e[0].id), t.setWorkspaceToken(null), w(n)) : t.currentWorkspaceId || (t.setCurrentWorkspaceId(e[0].id), w(n));
173
+ H.current ? (t.setCurrentWorkspaceId(e[0].id), t.setWorkspaceToken(null), O(n)) : t.currentWorkspaceId || (t.setCurrentWorkspaceId(e[0].id), O(n));
154
174
  }
155
175
  }
156
176
  } catch {} finally {
157
177
  t.setLoadingWorkspaces(!1);
158
178
  }
159
179
  }
160
- return c;
180
+ return s;
161
181
  } catch (e) {
162
- if (e instanceof y) return [];
182
+ if (e instanceof b) return [];
163
183
  let n = e instanceof Error ? e.message : "Failed to fetch organizations";
164
- if (t.workspaces.length === 0 && console.error("Failed to fetch organizations:", e), I(n), t.workspaces.length > 0) {
165
- let e = S(t.workspaces);
184
+ if (t.workspaces.length === 0 && console.error("Failed to fetch organizations:", e), z(n), t.workspaces.length > 0) {
185
+ let e = C(t.workspaces);
166
186
  if (e.length > 0) return t.setOrganizations(e), t.currentOrganizationId || t.setCurrentOrganizationId(e[0].id), e;
167
187
  }
168
188
  return [];
169
189
  } finally {
170
- t.setLoadingOrganizations(!1), L.current = null;
190
+ t.setLoadingOrganizations(!1), B.current = null;
171
191
  }
172
- })(), L.current);
173
- }, [H, w]), Y = n(async (t) => {
174
- let n = R.current.get(t);
192
+ })(), B.current);
193
+ }, [G, O]), Z = n(async (t) => {
194
+ let n = V.current.get(t);
175
195
  if (n) return n;
176
- let r = e.getState(), i = x();
177
- if (!i || g(i)) return [];
178
- r.setLoadingWorkspaces(!0), I(null);
196
+ let r = e.getState(), i = S();
197
+ if (!i || _(i)) return [];
198
+ r.setLoadingWorkspaces(!0), z(null);
179
199
  let a = (async () => {
180
200
  try {
181
- let e = await s({
201
+ let e = await c({
182
202
  gateway: "global",
183
- query: u,
203
+ query: d,
184
204
  authToken: i,
185
- baseUrl: H
205
+ baseUrl: G
186
206
  });
187
207
  if (e.data?.workspaces?.items && (e.data.workspaces.items = e.data.workspaces.items.filter((e) => e.organizationId === t)), e.errors?.length) {
188
208
  let t = e.errors[0]?.message ?? "GraphQL error";
189
- throw b(e.errors) ? new y(t) : (console.error("Failed to fetch workspaces:", e.errors), Error(t));
209
+ throw x(e.errors) ? new b(t) : (console.error("Failed to fetch workspaces:", e.errors), Error(t));
190
210
  }
191
211
  let n = (e.data?.workspaces?.items ?? []).filter((e) => e.organizationId === t);
192
212
  return r.setWorkspaces(n), n;
193
213
  } catch (e) {
194
- if (e instanceof y) return [];
214
+ if (e instanceof b) return [];
195
215
  let t = e instanceof Error ? e.message : "Failed to fetch workspaces";
196
- return console.error("Failed to fetch workspaces:", e), I(t), [];
216
+ return console.error("Failed to fetch workspaces:", e), z(t), [];
197
217
  } finally {
198
- r.setLoadingWorkspaces(!1), R.current.delete(t);
218
+ r.setLoadingWorkspaces(!1), V.current.delete(t);
199
219
  }
200
220
  })();
201
- return R.current.set(t, a), a;
202
- }, [H]), X = n(async (t, n, r) => {
203
- let i = e.getState(), a = x();
204
- if (!a || g(a)) return {
221
+ return V.current.set(t, a), a;
222
+ }, [G]), Q = n(async (t, n, r) => {
223
+ let i = e.getState(), a = S();
224
+ if (!a || _(a)) return {
205
225
  success: !1,
206
226
  error: "No valid access token available"
207
227
  };
208
- I(null);
228
+ z(null);
209
229
  try {
210
- let e = await s({
230
+ let e = await c({
211
231
  gateway: "workspace",
212
- query: f,
232
+ query: p,
213
233
  variables: { input: {
214
234
  organizationId: t,
215
235
  ...n && { workspaceId: n },
216
236
  ...r && { projectId: r }
217
237
  } },
218
238
  authToken: a,
219
- baseUrl: U
239
+ baseUrl: K
220
240
  });
221
241
  if (e.errors?.length) {
222
242
  let t = e.errors[0]?.message ?? "GraphQL error";
223
- return b(e.errors) || console.error("Failed to issue workspace token:", e.errors), {
243
+ return x(e.errors) || console.error("Failed to issue workspace token:", e.errors), {
224
244
  success: !1,
225
245
  error: t
226
246
  };
@@ -240,57 +260,57 @@ function C(o) {
240
260
  };
241
261
  } catch (e) {
242
262
  let t = e instanceof Error ? e.message : "Failed to issue workspace token";
243
- return console.error("Failed to issue workspace token:", e), I(t), {
263
+ return console.error("Failed to issue workspace token:", e), z(t), {
244
264
  success: !1,
245
265
  error: t
246
266
  };
247
267
  }
248
- }, [U]), Z = n(async () => {
249
- let t = e.getState(), n = x(), r = t.workspaceToken, i = t.currentWorkspaceId;
250
- if (!n || g(n) || !r || !i) return [];
251
- t.setLoadingProjects(!0), I(null);
268
+ }, [K]), $ = n(async () => {
269
+ let t = e.getState(), n = S(), r = t.workspaceToken, i = t.currentWorkspaceId;
270
+ if (!n || _(n) || !r || !i) return [];
271
+ t.setLoadingProjects(!0), z(null);
252
272
  try {
253
- let e = await s({
273
+ let e = await c({
254
274
  gateway: "workspace",
255
- query: p,
275
+ query: m,
256
276
  authToken: n,
257
277
  workspaceToken: r,
258
- baseUrl: U
278
+ baseUrl: K
259
279
  });
260
280
  if (e.errors?.length) {
261
281
  let t = e.errors[0]?.message ?? "GraphQL error";
262
- throw b(e.errors) ? new y(t) : (console.error("Failed to fetch projects:", e.errors), Error(t));
282
+ throw x(e.errors) ? new b(t) : (console.error("Failed to fetch projects:", e.errors), Error(t));
263
283
  }
264
284
  let i = e.data?.projects?.items ?? [];
265
285
  return t.setProjects(i), i;
266
286
  } catch (e) {
267
- if (e instanceof y) return [];
287
+ if (e instanceof b) return [];
268
288
  let t = e instanceof Error ? e.message : "Failed to fetch projects";
269
- return console.error("Failed to fetch projects:", e), I(t), [];
289
+ return console.error("Failed to fetch projects:", e), z(t), [];
270
290
  } finally {
271
291
  t.setLoadingProjects(!1);
272
292
  }
273
- }, [U]), Q = n(async (t) => {
293
+ }, [K]), re = n(async (t) => {
274
294
  let n = e.getState();
275
- return n.setCurrentWorkspaceId(null), n.setWorkspaceToken(null), n.setProjects([]), n.setCurrentProjectId(null), n.setCurrentOrganizationId(t.id), await Y(t.id);
276
- }, [Y]), $ = n(async (t) => {
295
+ return n.setCurrentWorkspaceId(null), n.setWorkspaceToken(null), n.setProjects([]), n.setCurrentProjectId(null), n.setCurrentOrganizationId(t.id), await Z(t.id);
296
+ }, [Z]), ie = n(async (t) => {
277
297
  let n = e.getState();
278
298
  if (n.setCurrentProjectId(null), n.setProjects([]), !t.organizationId) return console.error("Workspace missing organizationId:", t), {
279
299
  success: !1,
280
300
  error: "Workspace is missing organizationId"
281
301
  };
282
- let r = await X(t.organizationId, t.id);
302
+ let r = await Q(t.organizationId, t.id);
283
303
  if (!r.success) return {
284
304
  success: !1,
285
305
  error: r.error
286
306
  };
287
- let i = await Z();
307
+ let i = await $();
288
308
  if (i.length === 0) {
289
- let t = x(), r = e.getState().workspaceToken;
309
+ let t = S(), r = e.getState().workspaceToken;
290
310
  if (t && r) {
291
- let e = await h({
311
+ let e = await g({
292
312
  authToken: t,
293
- workspaceBaseUrl: U,
313
+ workspaceBaseUrl: K,
294
314
  workspaceToken: r
295
315
  });
296
316
  e && (i = [e], n.setProjects(i));
@@ -301,54 +321,88 @@ function C(o) {
301
321
  projects: i
302
322
  };
303
323
  }, [
304
- X,
305
- Z,
306
- U
307
- ]), ee = n((t) => {
324
+ Q,
325
+ $,
326
+ K
327
+ ]), ae = n((t) => {
308
328
  let n = e.getState();
309
- n.setCurrentProjectId(t.id), n.workspaceToken && !v(n.workspaceToken, {
329
+ n.setCurrentProjectId(t.id), n.workspaceToken && !y(n.workspaceToken, {
310
330
  workspaceId: n.currentWorkspaceId,
311
331
  organizationId: n.currentOrganizationId,
312
332
  projectId: t.id
313
333
  }) && n.setWorkspaceToken(null);
314
334
  }, []);
315
- return {
316
- organizations: T,
317
- workspaces: O,
318
- projects: M,
319
- currentOrganizationId: E,
320
- currentWorkspaceId: k,
321
- currentProjectId: N,
322
- currentOrganization: T.find((e) => e.id === E),
323
- currentWorkspace: O.find((e) => e.id === k),
324
- currentProject: M.find((e) => e.id === N),
325
- workspaceToken: A,
326
- isLoadingOrganizations: D,
327
- isLoadingWorkspaces: j,
328
- isLoadingProjects: P,
329
- isLoading: D || j || P,
330
- error: F,
331
- fetchOrganizations: J,
332
- fetchWorkspacesForOrg: Y,
333
- issueWorkspaceToken: X,
334
- fetchProjects: Z,
335
- selectOrganization: Q,
336
- selectWorkspace: $,
337
- selectProject: ee
335
+ return r(() => {
336
+ let t = !1, n = (e) => {
337
+ let n = !1, r = !1, i = () => {
338
+ if (!t) {
339
+ if (n) {
340
+ r = !0;
341
+ return;
342
+ }
343
+ n = !0, e().finally(() => {
344
+ n = !1, !t && r && (r = !1, i());
345
+ });
346
+ }
347
+ };
348
+ return i;
349
+ }, r = n(() => $()), i = n(() => {
350
+ let t = e.getState().currentOrganizationId;
351
+ return t ? Z(t) : Promise.resolve();
352
+ }), a = k.onAny((t) => {
353
+ let n = t.name;
354
+ if (w.has(n)) {
355
+ let n = E(t.payload), { currentWorkspaceId: i } = e.getState();
356
+ if (n && i && n !== i) return;
357
+ r();
358
+ return;
359
+ }
360
+ T.has(n) && i();
361
+ });
362
+ return () => {
363
+ t = !0, a();
364
+ };
365
+ }, [
366
+ k,
367
+ $,
368
+ Z
369
+ ]), {
370
+ organizations: A,
371
+ workspaces: N,
372
+ projects: I,
373
+ currentOrganizationId: j,
374
+ currentWorkspaceId: P,
375
+ currentProjectId: L,
376
+ currentOrganization: A.find((e) => e.id === j),
377
+ currentWorkspace: N.find((e) => e.id === P),
378
+ currentProject: I.find((e) => e.id === L),
379
+ workspaceToken: ee,
380
+ isLoadingOrganizations: M,
381
+ isLoadingWorkspaces: F,
382
+ isLoadingProjects: R,
383
+ isLoading: M || F || R,
384
+ error: te,
385
+ fetchOrganizations: ne,
386
+ fetchWorkspacesForOrg: Z,
387
+ issueWorkspaceToken: Q,
388
+ fetchProjects: $,
389
+ selectOrganization: re,
390
+ selectWorkspace: ie,
391
+ selectProject: ae
338
392
  };
339
393
  }
340
- function w(n) {
341
- let i = e((e) => e.currentOrganizationId), a = e((e) => e.workspaces), o = e((e) => e.accessToken), c = e((e) => e.isLoadingWorkspaces), l = t(), d = n?.globalBaseUrl ?? l.globalBaseUrl, f = n?.disableWorkspaceSync ?? !1;
394
+ function O(n) {
395
+ let i = e((e) => e.currentOrganizationId), a = e((e) => e.workspaces), o = e((e) => e.accessToken), s = e((e) => e.isLoadingWorkspaces), l = t(), u = n?.globalBaseUrl ?? l.globalBaseUrl, f = n?.disableWorkspaceSync ?? !1;
342
396
  r(() => {
343
- if (f || !i || !o || c) return;
397
+ if (f || !i || !o || s) return;
344
398
  let t = a.some((e) => e.organizationId === i);
345
399
  if (a.length > 0 && !t) {
346
400
  let t = e.getState();
347
- t.setLoadingWorkspaces(!0), s({
401
+ t.setLoadingWorkspaces(!0), c({
348
402
  gateway: "global",
349
- query: u,
403
+ query: d,
350
404
  authToken: o,
351
- baseUrl: d
405
+ baseUrl: u
352
406
  }).then((e) => {
353
407
  if (e.data?.workspaces?.items && (e.data.workspaces.items = e.data.workspaces.items.filter((e) => e.organizationId === i)), !e.errors?.length) {
354
408
  let n = (e.data?.workspaces?.items ?? []).filter((e) => e.organizationId === i);
@@ -362,28 +416,28 @@ function w(n) {
362
416
  i,
363
417
  a,
364
418
  o,
365
- c,
366
- d,
419
+ s,
420
+ u,
367
421
  f
368
422
  ]);
369
423
  }
370
- function T(n) {
371
- let a = e((e) => e.currentWorkspaceId), o = e((e) => e.currentOrganizationId), c = e((e) => e.workspaces), l = e((e) => e.projects), u = e((e) => e.currentProjectId), d = e((e) => e.workspaceToken), m = e((e) => e.accessToken), _ = t(), y = n?.workspaceBaseUrl ?? _.workspaceBaseUrl, b = n?.disableProjectSync ?? !1, x = n?.skipProjectFetch ?? !1, S = i(!1), C = i(/* @__PURE__ */ new Map()), w = i(/* @__PURE__ */ new Map()), T = 3e4;
424
+ function k(n) {
425
+ let a = e((e) => e.currentWorkspaceId), o = e((e) => e.currentOrganizationId), s = e((e) => e.workspaces), l = e((e) => e.projects), u = e((e) => e.currentProjectId), d = e((e) => e.workspaceToken), f = e((e) => e.accessToken), h = t(), v = n?.workspaceBaseUrl ?? h.workspaceBaseUrl, b = n?.disableProjectSync ?? !1, x = n?.skipProjectFetch ?? !1, S = i(!1), C = i(/* @__PURE__ */ new Map()), w = i(/* @__PURE__ */ new Map()), T = 3e4;
372
426
  r(() => {
373
- if (b || !a || !m || S.current) return;
427
+ if (b || !a || !f || S.current) return;
374
428
  let t = w.current.get(a);
375
429
  if (t && t.count >= 8 && Date.now() - t.lastFailureAt < T) return;
376
430
  t && Date.now() - t.lastFailureAt >= T && w.current.delete(a);
377
- let n = c.find((e) => e.id === a);
431
+ let n = s.find((e) => e.id === a);
378
432
  if (n && o && n.organizationId !== o) return;
379
433
  let r = n?.organizationId ?? o;
380
434
  if (!r) return;
381
- let i = e.getState(), _ = !d || g(d) || !v(d, {
435
+ let i = e.getState(), h = !d || _(d) || !y(d, {
382
436
  workspaceId: a,
383
437
  organizationId: o,
384
438
  projectId: u
385
439
  }), E = l.some((e) => e.workspaceId === a);
386
- if (E && !_) {
440
+ if (E && !h) {
387
441
  if (!i.currentProjectId) {
388
442
  let e = l.find((e) => e.workspaceId === a);
389
443
  e && i.setCurrentProjectId(e.id);
@@ -395,35 +449,35 @@ function T(n) {
395
449
  let t = !1;
396
450
  try {
397
451
  let e = d;
398
- if (_) {
399
- let t = await s({
452
+ if (h) {
453
+ let t = await c({
400
454
  gateway: "workspace",
401
- query: f,
455
+ query: p,
402
456
  variables: { input: {
403
457
  organizationId: r,
404
458
  workspaceId: a,
405
459
  ...u ? { projectId: u } : {}
406
460
  } },
407
- authToken: m,
408
- baseUrl: y
461
+ authToken: f,
462
+ baseUrl: v
409
463
  });
410
464
  if (t.data?.issueWorkspaceToken?.success && t.data.issueWorkspaceToken.token) e = t.data.issueWorkspaceToken.token, i.setWorkspaceToken(e);
411
465
  else return;
412
466
  }
413
467
  if (e && !E && !x) {
414
- let t = await s({
468
+ let t = await c({
415
469
  gateway: "workspace",
416
- query: p,
417
- authToken: m,
470
+ query: m,
471
+ authToken: f,
418
472
  workspaceToken: e,
419
- baseUrl: y
473
+ baseUrl: v
420
474
  });
421
475
  if (t.errors?.length) return;
422
476
  let n = t.data?.projects?.items ?? [];
423
477
  if (n.length === 0) {
424
- let t = await h({
425
- authToken: m,
426
- workspaceBaseUrl: y,
478
+ let t = await g({
479
+ authToken: f,
480
+ workspaceBaseUrl: v,
427
481
  workspaceToken: e
428
482
  });
429
483
  if (t) n = [t];
@@ -461,12 +515,12 @@ function T(n) {
461
515
  }, [
462
516
  a,
463
517
  o,
464
- c,
518
+ s,
465
519
  l,
466
520
  u,
467
521
  d,
468
- m,
469
- y,
522
+ f,
523
+ v,
470
524
  b,
471
525
  x
472
526
  ]), r(() => {
@@ -477,6 +531,6 @@ function T(n) {
477
531
  }, []);
478
532
  }
479
533
  //#endregion
480
- export { C as useContextSwitcherData, T as useProjectSyncEffect, w as useWorkspaceSyncEffect };
534
+ export { D as useContextSwitcherData, k as useProjectSyncEffect, O as useWorkspaceSyncEffect };
481
535
 
482
536
  //# sourceMappingURL=useContextSwitcherData.js.map