@burdenoff/microfe-auth 2026.1013.2 → 2026.1014.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.
@@ -8,8 +8,8 @@ import { clearWorkspaceWideSelection as u, isWorkspaceWideSelection as d, select
8
8
  import { getUrlParam as p } from "@burdenoff/fe-libs/shared/providers/shell/ActiveContextStorage";
9
9
  import { useActiveContext as m } from "@burdenoff/fe-libs/shared/providers/shell/ActiveContextProvider";
10
10
  //#region src/auth/hooks/useContextSwitcherData.ts
11
- var h = "\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", g = "\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", _ = "\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", v = "\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";
12
- function y(e, t = 3e4) {
11
+ var h = "\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", g = "\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", ee = "\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", _ = "\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";
12
+ function v(e, t = 3e4) {
13
13
  try {
14
14
  let n = JSON.parse(atob(e.split(".")[1]));
15
15
  return n.exp ? n.exp * 1e3 < Date.now() + t : !1;
@@ -17,7 +17,7 @@ function y(e, t = 3e4) {
17
17
  return !0;
18
18
  }
19
19
  }
20
- function b(e) {
20
+ function y(e) {
21
21
  try {
22
22
  let t = JSON.parse(atob(e.split(".")[1]));
23
23
  return {
@@ -30,26 +30,26 @@ function b(e) {
30
30
  return null;
31
31
  }
32
32
  }
33
- function x(e, t) {
34
- if (!e || y(e)) return !1;
35
- let n = b(e);
33
+ function b(e, t) {
34
+ if (!e || v(e)) return !1;
35
+ let n = y(e);
36
36
  return !(!n || t.workspaceId && n.workspaceId !== t.workspaceId || t.organizationId && n.organizationId !== t.organizationId || t.workspaceWide && n.projectId || t.projectId && n.projectId !== t.projectId);
37
37
  }
38
- var S = class extends Error {
38
+ var x = class extends Error {
39
39
  constructor(e) {
40
40
  super(e), this.name = "AuthError";
41
41
  }
42
42
  };
43
- function C(e) {
43
+ function S(e) {
44
44
  return e.some((e) => e.extensions?.code === "UNAUTHENTICATED" || e.message?.toLowerCase().includes("unauthorized") || e.message?.toLowerCase().includes("not authenticated"));
45
45
  }
46
- function w() {
46
+ function C() {
47
47
  let t = e.getState().accessToken;
48
- if (t && !y(t)) return t;
48
+ if (t && !v(t)) return t;
49
49
  let n = c();
50
- return n && !y(n) ? n : t;
50
+ return n && !v(n) ? n : t;
51
51
  }
52
- function T(e) {
52
+ function w(e) {
53
53
  return Array.from(new Set(e.flatMap((e) => e.organizationId ? [e.organizationId] : []))).map((t, n) => ({
54
54
  id: t,
55
55
  name: e.find((e) => e.organizationId === t)?.name || `Organization ${n + 1}`,
@@ -57,69 +57,81 @@ function T(e) {
57
57
  tenantId: e.find((e) => e.organizationId === t)?.tenantId || ""
58
58
  }));
59
59
  }
60
- var E = new Set([
60
+ var T = new Set([
61
61
  "project.created",
62
62
  "project.updated",
63
63
  "project.deleted",
64
64
  "project.archived",
65
65
  "project.unarchived"
66
- ]), D = new Set([
66
+ ]), E = new Set([
67
67
  "workspace.created",
68
68
  "workspace.updated",
69
69
  "workspace.deleted",
70
70
  "workspace.archived",
71
71
  "workspace.reactivated"
72
72
  ]);
73
- function ee(e) {
73
+ function D(e) {
74
74
  if (e && typeof e == "object" && "workspaceId" in e) {
75
75
  let t = e.workspaceId;
76
76
  return typeof t == "string" ? t : void 0;
77
77
  }
78
78
  }
79
79
  function O(c) {
80
- let { organizationId: p, workspaceId: y, projectId: b, setContext: O } = m(), k = s(), 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), F = e((e) => e.workspaceToken), I = e((e) => e.isLoadingWorkspaces), L = e((e) => e.projects), R = e((e) => e.currentProjectId), z = e((e) => e.isLoadingProjects), [te, B] = a(null), [V, H] = a(0), U = i(0), W = i(null), G = i(/* @__PURE__ */ new Map()), K = i(y), q = i(p);
80
+ let { organizationId: p, workspaceId: v, projectId: y, tenantId: O, setContext: k } = m(), A = s(), j = e((e) => e.organizations), M = e((e) => e.currentOrganizationId), N = e((e) => e.isLoadingOrganizations), P = e((e) => e.workspaces), F = e((e) => e.currentWorkspaceId), I = e((e) => e.workspaceToken), L = e((e) => e.isLoadingWorkspaces), te = e((e) => e.projects), ne = e((e) => e.currentProjectId), re = e((e) => e.isLoadingProjects);
81
81
  r(() => {
82
- K.current = y, q.current = p;
82
+ let e = P.find((e) => e.id === v && e.organizationId === p), t = j.find((e) => e.id === p);
83
+ !e || !t || !/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(e.tenantId) || e.tenantId !== t.tenantId || O === e.tenantId || k({ tenantId: e.tenantId });
84
+ }, [
85
+ p,
86
+ v,
87
+ O,
88
+ j,
89
+ P,
90
+ k
91
+ ]);
92
+ let [ie, R] = a(null), [z, B] = a(0), V = i(0), H = i(null), U = i(/* @__PURE__ */ new Map()), W = i(v), G = i(p);
93
+ r(() => {
94
+ W.current = v, G.current = p;
83
95
  });
84
- let ne = t(), J = c?.globalBaseUrl ?? ne.globalBaseUrl, Y = c?.workspaceBaseUrl ?? ne.workspaceBaseUrl, re = i(null), ie = i(null), ae = i(null), oe = i(null);
96
+ let K = t(), q = c?.globalBaseUrl ?? K.globalBaseUrl, J = c?.workspaceBaseUrl ?? K.workspaceBaseUrl, ae = i(null), Y = i(null), oe = i(null), se = i(null);
85
97
  r(() => {
86
98
  let t = e.getState();
87
- if (p && p !== t.currentOrganizationId && p !== re.current && (re.current = p, t.setCurrentOrganizationId(p), t.workspaces.length > 0 && !t.workspaces.some((e) => e.organizationId === p) && t.setWorkspaces([])), y && y !== t.currentWorkspaceId && y !== ie.current && (ie.current = y, t.setCurrentWorkspaceId(y), t.projects.length > 0 && !t.projects.some((e) => e.workspaceId === y) && (t.setProjects([]), t.setCurrentProjectId(null))), d({
99
+ if (p && p !== t.currentOrganizationId && p !== ae.current && (ae.current = p, t.setCurrentOrganizationId(p), t.workspaces.length > 0 && !t.workspaces.some((e) => e.organizationId === p) && t.setWorkspaces([])), v && v !== t.currentWorkspaceId && v !== Y.current && (Y.current = v, t.setCurrentWorkspaceId(v), t.projects.length > 0 && !t.projects.some((e) => e.workspaceId === v) && (t.setProjects([]), t.setCurrentProjectId(null))), d({
88
100
  organizationId: p,
89
- workspaceId: y
90
- }) && !b && t.currentProjectId && (t.setCurrentProjectId(null), t.setWorkspaceToken(null)), b && b !== t.currentProjectId && b !== ae.current && (ae.current = b, t.setCurrentProjectId(b)), t.workspaceToken && !x(t.workspaceToken, {
91
- workspaceId: y,
101
+ workspaceId: v
102
+ }) && !y && t.currentProjectId && (t.setCurrentProjectId(null), t.setWorkspaceToken(null)), y && y !== t.currentProjectId && y !== oe.current && (oe.current = y, t.setCurrentProjectId(y)), t.workspaceToken && !b(t.workspaceToken, {
103
+ workspaceId: v,
92
104
  organizationId: p,
93
- projectId: b,
94
- workspaceWide: !b && d({
105
+ projectId: y,
106
+ workspaceWide: !y && d({
95
107
  organizationId: p,
96
- workspaceId: y
108
+ workspaceId: v
97
109
  })
98
110
  })) {
99
- let e = `${p ?? ""}|${y ?? ""}|${b ?? ""}`;
100
- oe.current !== e && (oe.current = e, t.setWorkspaceToken(null));
111
+ let e = `${p ?? ""}|${v ?? ""}|${y ?? ""}`;
112
+ se.current !== e && (se.current = e, t.setWorkspaceToken(null));
101
113
  }
102
114
  }, [
103
115
  p,
104
- y,
105
- b
116
+ v,
117
+ y
106
118
  ]);
107
119
  let X = n(async () => {
108
- if (W.current) return W.current;
109
- let t = e.getState(), n = w();
110
- return n ? (t.setLoadingOrganizations(!0), B(null), W.current = (async () => {
120
+ if (H.current) return H.current;
121
+ let t = e.getState(), n = C();
122
+ return n ? (t.setLoadingOrganizations(!0), R(null), H.current = (async () => {
111
123
  try {
112
- let r = q.current ?? t.currentOrganizationId, i = r ? await (async () => {
124
+ let r = G.current ?? t.currentOrganizationId, i = r ? await (async () => {
113
125
  let [e, t] = await Promise.all([l({
114
126
  gateway: "global",
115
127
  query: h,
116
128
  authToken: n,
117
- baseUrl: J
129
+ baseUrl: q
118
130
  }), l({
119
131
  gateway: "global",
120
132
  query: g,
121
133
  authToken: n,
122
- baseUrl: J
134
+ baseUrl: q
123
135
  })]);
124
136
  return {
125
137
  data: e.data?.myOrganizations ? {
@@ -132,16 +144,16 @@ function O(c) {
132
144
  gateway: "global",
133
145
  query: h,
134
146
  authToken: n,
135
- baseUrl: J
147
+ baseUrl: q
136
148
  });
137
149
  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 l({
138
150
  gateway: "global",
139
151
  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 ",
140
152
  authToken: n,
141
- baseUrl: J
153
+ baseUrl: q
142
154
  })), i.errors?.length) {
143
155
  let e = i.errors[0]?.message ?? "GraphQL error";
144
- throw C(i.errors) ? new S(e) : (t.workspaces.length === 0 && console.error("Failed to fetch organizations:", i.errors), Error(e));
156
+ throw S(i.errors) ? new x(e) : (t.workspaces.length === 0 && console.error("Failed to fetch organizations:", i.errors), Error(e));
145
157
  }
146
158
  let a = i.data?.myOrganizations?.items ?? [];
147
159
  if (r && a.length === 0 && t.workspaces.length === 0) {
@@ -149,7 +161,7 @@ function O(c) {
149
161
  gateway: "global",
150
162
  query: h,
151
163
  authToken: n,
152
- baseUrl: J
164
+ baseUrl: q
153
165
  });
154
166
  if (!e.errors?.length) a = e.data?.myOrganizations?.items ?? [], i = {
155
167
  ...e,
@@ -161,13 +173,13 @@ function O(c) {
161
173
  else {
162
174
  console.warn("[ContextSwitcher] Stale-context recovery failed:", e.errors);
163
175
  let t = e.errors?.[0]?.message ?? "Failed to fetch organizations";
164
- throw C(e.errors ?? []) ? new S(t) : Error(t);
176
+ throw S(e.errors ?? []) ? new x(t) : Error(t);
165
177
  }
166
178
  }
167
- let o = a.length === 0 && t.workspaces.length > 0 ? T(t.workspaces) : [], s = o.length > 0 ? o : a;
179
+ let o = a.length === 0 && t.workspaces.length > 0 ? w(t.workspaces) : [], s = o.length > 0 ? o : a;
168
180
  t.setOrganizations(s);
169
181
  let c = r, u = c && a.some((e) => e.id === c);
170
- if (c && !u && a.length > 0 && (c = a[0].id, O({
182
+ if (c && !u && a.length > 0 && (c = a[0].id, k({
171
183
  organizationId: c,
172
184
  workspaceId: null,
173
185
  tenantId: null,
@@ -179,11 +191,11 @@ function O(c) {
179
191
  gateway: "global",
180
192
  query: g,
181
193
  authToken: n,
182
- baseUrl: J
194
+ baseUrl: q
183
195
  }).then((e) => e.errors?.length ? null : (e.data?.workspaces?.items ?? []).filter((e) => e.organizationId === c));
184
- if (e || H((e) => e + 1), e) {
196
+ if (e || B((e) => e + 1), e) {
185
197
  t.setWorkspaces(e);
186
- let n = K.current ? e.find((e) => e.id === K.current) : null;
198
+ let n = W.current ? e.find((e) => e.id === W.current) : null;
187
199
  if (n) t.setCurrentWorkspaceId(n.id);
188
200
  else if (e.length > 0) {
189
201
  let n = {
@@ -192,85 +204,85 @@ function O(c) {
192
204
  tenantId: e[0].tenantId,
193
205
  projectId: null
194
206
  };
195
- K.current ? (t.setCurrentWorkspaceId(e[0].id), t.setWorkspaceToken(null), O(n)) : t.currentWorkspaceId || (t.setCurrentWorkspaceId(e[0].id), O(n));
207
+ W.current ? (t.setCurrentWorkspaceId(e[0].id), t.setWorkspaceToken(null), k(n)) : t.currentWorkspaceId || (t.setCurrentWorkspaceId(e[0].id), k(n));
196
208
  }
197
209
  }
198
210
  } catch {
199
- H((e) => e + 1);
211
+ B((e) => e + 1);
200
212
  } finally {
201
213
  t.setLoadingWorkspaces(!1);
202
214
  }
203
215
  }
204
- return U.current = 0, s;
216
+ return V.current = 0, s;
205
217
  } catch (e) {
206
- if (e instanceof S) return [];
218
+ if (e instanceof x) return [];
207
219
  let n = e instanceof Error ? e.message : "Failed to fetch organizations";
208
- if (t.workspaces.length === 0 && console.error("Failed to fetch organizations:", e), B(n), H((e) => e + 1), t.workspaces.length > 0) {
209
- let e = T(t.workspaces);
220
+ if (t.workspaces.length === 0 && console.error("Failed to fetch organizations:", e), R(n), B((e) => e + 1), t.workspaces.length > 0) {
221
+ let e = w(t.workspaces);
210
222
  if (e.length > 0) return t.setOrganizations(e), t.currentOrganizationId || t.setCurrentOrganizationId(e[0].id), e;
211
223
  }
212
224
  return [];
213
225
  } finally {
214
- t.setLoadingOrganizations(!1), W.current = null;
226
+ t.setLoadingOrganizations(!1), H.current = null;
215
227
  }
216
- })(), W.current) : [];
217
- }, [J, O]);
228
+ })(), H.current) : [];
229
+ }, [q, k]);
218
230
  r(() => {
219
- if (V === 0 || U.current >= 5 || !w()) return;
220
- U.current += 1;
221
- let e = Math.min(1500 * 2 ** (U.current - 1), 12e3), t = window.setTimeout(() => {
231
+ if (z === 0 || V.current >= 5 || !C()) return;
232
+ V.current += 1;
233
+ let e = Math.min(1500 * 2 ** (V.current - 1), 12e3), t = window.setTimeout(() => {
222
234
  X();
223
235
  }, e);
224
236
  return () => {
225
237
  window.clearTimeout(t);
226
238
  };
227
- }, [V, X]), r(() => {
239
+ }, [z, X]), r(() => {
228
240
  let t = () => {
229
- e.getState().organizations.length === 0 && w() && (U.current = 0, X());
241
+ e.getState().organizations.length === 0 && C() && (V.current = 0, X());
230
242
  };
231
243
  return window.addEventListener("online", t), () => {
232
244
  window.removeEventListener("online", t);
233
245
  };
234
246
  }, [X]);
235
247
  let Z = n(async (t) => {
236
- let n = e.getState(), r = w(), i = o(), a = n.user?.id, s = JSON.stringify([
248
+ let n = e.getState(), r = C(), i = o(), a = n.user?.id, s = JSON.stringify([
237
249
  i,
238
250
  a,
239
251
  r,
240
252
  t
241
- ]), c = G.current.get(s);
253
+ ]), c = U.current.get(s);
242
254
  if (c) return c;
243
255
  let u = () => {
244
256
  let n = e.getState();
245
- return o() === i && n.user?.id === a && w() === r && n.currentOrganizationId === t;
257
+ return o() === i && n.user?.id === a && C() === r && n.currentOrganizationId === t;
246
258
  };
247
259
  if (!r) return [];
248
- n.setLoadingWorkspaces(!0), B(null);
260
+ n.setLoadingWorkspaces(!0), R(null);
249
261
  let d = (async () => {
250
262
  try {
251
263
  let e = await l({
252
264
  gateway: "global",
253
265
  query: g,
254
266
  authToken: r,
255
- baseUrl: J
267
+ baseUrl: q
256
268
  });
257
269
  if (!u()) return [];
258
270
  if (e.data?.workspaces?.items && (e.data.workspaces.items = e.data.workspaces.items.filter((e) => e.organizationId === t)), e.errors?.length) {
259
271
  let t = e.errors[0]?.message ?? "GraphQL error";
260
- throw C(e.errors) ? new S(t) : (console.error("Failed to fetch workspaces:", e.errors), Error(t));
272
+ throw S(e.errors) ? new x(t) : (console.error("Failed to fetch workspaces:", e.errors), Error(t));
261
273
  }
262
274
  let i = (e.data?.workspaces?.items ?? []).filter((e) => e.organizationId === t);
263
275
  return n.setWorkspaces(i), i;
264
276
  } catch (e) {
265
- if (!u() || e instanceof S) return [];
277
+ if (!u() || e instanceof x) return [];
266
278
  let t = e instanceof Error ? e.message : "Failed to fetch workspaces";
267
- return console.error("Failed to fetch workspaces:", e), B(t), [];
279
+ return console.error("Failed to fetch workspaces:", e), R(t), [];
268
280
  } finally {
269
- u() && n.setLoadingWorkspaces(!1), G.current.delete(s);
281
+ u() && n.setLoadingWorkspaces(!1), U.current.delete(s);
270
282
  }
271
283
  })();
272
- return G.current.set(s, d), d;
273
- }, [J]), Q = n(async (t, n, r, i) => {
284
+ return U.current.set(s, d), d;
285
+ }, [q]), Q = n(async (t, n, r, i) => {
274
286
  let a = e.getState(), s = [
275
287
  a.currentOrganizationId,
276
288
  a.currentWorkspaceId,
@@ -278,28 +290,28 @@ function O(c) {
278
290
  ], c = d({
279
291
  organizationId: t,
280
292
  workspaceId: n ?? null
281
- }), u = w(), f = o(), p = a.user?.id;
293
+ }), u = C(), f = o(), p = a.user?.id;
282
294
  if (!u) return {
283
295
  success: !1,
284
296
  error: "No valid access token available"
285
297
  };
286
- B(null);
298
+ R(null);
287
299
  try {
288
300
  let m = await l({
289
301
  gateway: "workspace",
290
- query: _,
302
+ query: ee,
291
303
  variables: { input: {
292
304
  organizationId: t,
293
305
  ...n && { workspaceId: n },
294
306
  ...r && { projectId: r }
295
307
  } },
296
308
  authToken: u,
297
- baseUrl: Y,
309
+ baseUrl: J,
298
310
  tenantId: i
299
311
  });
300
312
  if (m.errors?.length) {
301
313
  let e = m.errors[0]?.message ?? "GraphQL error";
302
- return C(m.errors) || console.error("Failed to issue workspace token:", m.errors), {
314
+ return S(m.errors) || console.error("Failed to issue workspace token:", m.errors), {
303
315
  success: !1,
304
316
  error: e
305
317
  };
@@ -307,14 +319,14 @@ function O(c) {
307
319
  let h = m.data?.issueWorkspaceToken;
308
320
  if (h?.success && h.token) {
309
321
  let i = e.getState();
310
- if (o() !== f || e.getState().user?.id !== p || w() !== u || s.some((e, t) => e !== [
322
+ if (o() !== f || e.getState().user?.id !== p || C() !== u || s.some((e, t) => e !== [
311
323
  i.currentOrganizationId,
312
324
  i.currentWorkspaceId,
313
325
  i.currentProjectId
314
326
  ][t]) || d({
315
327
  organizationId: t,
316
328
  workspaceId: n ?? null
317
- }) !== c || !x(h.token, {
329
+ }) !== c || !b(h.token, {
318
330
  organizationId: t,
319
331
  workspaceId: n,
320
332
  projectId: r,
@@ -336,50 +348,50 @@ function O(c) {
336
348
  };
337
349
  } catch (e) {
338
350
  let t = e instanceof Error ? e.message : "Failed to issue workspace token";
339
- return console.error("Failed to issue workspace token:", e), B(t), {
351
+ return console.error("Failed to issue workspace token:", e), R(t), {
340
352
  success: !1,
341
353
  error: t
342
354
  };
343
355
  }
344
- }, [Y]), $ = n(async () => {
345
- let t = e.getState(), n = w(), r = t.workspaceToken, i = t.currentWorkspaceId, a = o(), s = t.user?.id, c = t.currentOrganizationId, u = t.currentProjectId, f = d({
356
+ }, [J]), $ = n(async () => {
357
+ let t = e.getState(), n = C(), r = t.workspaceToken, i = t.currentWorkspaceId, a = o(), s = t.user?.id, c = t.currentOrganizationId, u = t.currentProjectId, f = d({
346
358
  organizationId: c,
347
359
  workspaceId: i
348
360
  }), p = () => {
349
361
  let t = e.getState();
350
- return o() === a && t.user?.id === s && w() === n && t.workspaceToken === r && t.currentOrganizationId === c && t.currentWorkspaceId === i && t.currentProjectId === u && d({
362
+ return o() === a && t.user?.id === s && C() === n && t.workspaceToken === r && t.currentOrganizationId === c && t.currentWorkspaceId === i && t.currentProjectId === u && d({
351
363
  organizationId: c,
352
364
  workspaceId: i
353
365
  }) === f;
354
366
  };
355
367
  if (!n || !r || !i) return [];
356
- t.setLoadingProjects(!0), B(null);
368
+ t.setLoadingProjects(!0), R(null);
357
369
  try {
358
370
  let e = await l({
359
371
  gateway: "workspace",
360
- query: v,
372
+ query: _,
361
373
  authToken: n,
362
374
  workspaceToken: r,
363
- baseUrl: Y
375
+ baseUrl: J
364
376
  });
365
377
  if (!p()) return [];
366
378
  if (e.errors?.length) {
367
379
  let t = e.errors[0]?.message ?? "GraphQL error";
368
- throw C(e.errors) ? new S(t) : (console.error("Failed to fetch projects:", e.errors), Error(t));
380
+ throw S(e.errors) ? new x(t) : (console.error("Failed to fetch projects:", e.errors), Error(t));
369
381
  }
370
382
  let i = e.data?.projects?.items ?? [];
371
383
  return t.setProjects(i), i;
372
384
  } catch (e) {
373
- if (e instanceof S) return [];
385
+ if (e instanceof x) return [];
374
386
  let t = e instanceof Error ? e.message : "Failed to fetch projects";
375
- return console.error("Failed to fetch projects:", e), B(t), [];
387
+ return console.error("Failed to fetch projects:", e), R(t), [];
376
388
  } finally {
377
389
  t.setLoadingProjects(!1);
378
390
  }
379
- }, [Y]), se = n(async (t) => {
391
+ }, [J]), ce = n(async (t) => {
380
392
  let n = e.getState();
381
393
  return n.setCurrentWorkspaceId(null), n.setWorkspaceToken(null), n.setProjects([]), n.setCurrentProjectId(null), n.setCurrentOrganizationId(t.id), await Z(t.id);
382
- }, [Z]), ce = n(async (t) => {
394
+ }, [Z]), le = n(async (t) => {
383
395
  let n = e.getState();
384
396
  if (n.setCurrentProjectId(null), n.setProjects([]), !t.organizationId) return console.error("Workspace missing organizationId:", t), {
385
397
  success: !1,
@@ -390,12 +402,12 @@ function O(c) {
390
402
  success: !1,
391
403
  error: r.error
392
404
  };
393
- let i = e.getState(), a = o(), s = i.user?.id, c = w(), l = i.workspaceToken, u = i.currentProjectId, f = d({
405
+ let i = e.getState(), a = o(), s = i.user?.id, c = C(), l = i.workspaceToken, u = i.currentProjectId, f = d({
394
406
  organizationId: t.organizationId,
395
407
  workspaceId: t.id
396
408
  }), p = () => {
397
409
  let n = e.getState();
398
- return o() === a && n.user?.id === s && w() === c && n.workspaceToken === l && n.currentOrganizationId === t.organizationId && n.currentWorkspaceId === t.id && n.currentProjectId === u && d({
410
+ return o() === a && n.user?.id === s && C() === c && n.workspaceToken === l && n.currentOrganizationId === t.organizationId && n.currentWorkspaceId === t.id && n.currentProjectId === u && d({
399
411
  organizationId: t.organizationId,
400
412
  workspaceId: t.id
401
413
  }) === f;
@@ -410,25 +422,25 @@ function O(c) {
410
422
  success: !1,
411
423
  error: "Context changed during selection"
412
424
  };
413
- }, [Q, $]), le = n((t) => {
425
+ }, [Q, $]), ue = n((t) => {
414
426
  let n = e.getState();
415
427
  u({
416
428
  organizationId: n.currentOrganizationId,
417
429
  workspaceId: n.currentWorkspaceId
418
- }), n.setCurrentProjectId(t.id), n.workspaceToken && !x(n.workspaceToken, {
430
+ }), n.setCurrentProjectId(t.id), n.workspaceToken && !b(n.workspaceToken, {
419
431
  workspaceId: n.currentWorkspaceId,
420
432
  organizationId: n.currentOrganizationId,
421
433
  projectId: t.id
422
434
  }) && n.setWorkspaceToken(null);
423
- }, []), ue = n(() => {
435
+ }, []), de = n(() => {
424
436
  let t = e.getState(), n = {
425
- organizationId: q.current,
426
- workspaceId: K.current
437
+ organizationId: G.current,
438
+ workspaceId: W.current
427
439
  };
428
440
  if (!n.organizationId || !n.workspaceId || t.currentOrganizationId !== n.organizationId || t.currentWorkspaceId !== n.workspaceId) throw Error("Current workspace context required");
429
441
  if (!f(n)) throw Error("Workspace-wide preference could not be saved");
430
- t.setCurrentProjectId(null), t.setWorkspaceToken(null), O({ projectId: null });
431
- }, [O]);
442
+ t.setCurrentProjectId(null), t.setWorkspaceToken(null), k({ projectId: null });
443
+ }, [k]);
432
444
  return r(() => {
433
445
  let t = !1, n = (e) => {
434
446
  let n = !1, r = !1, i = () => {
@@ -446,47 +458,47 @@ function O(c) {
446
458
  }, r = n(() => $()), i = n(() => {
447
459
  let t = e.getState().currentOrganizationId;
448
460
  return t ? Z(t) : Promise.resolve();
449
- }), a = k.onAny((t) => {
461
+ }), a = A.onAny((t) => {
450
462
  let n = t.name;
451
- if (E.has(n)) {
452
- let n = ee(t.payload), { currentWorkspaceId: i } = e.getState();
463
+ if (T.has(n)) {
464
+ let n = D(t.payload), { currentWorkspaceId: i } = e.getState();
453
465
  if (n && i && n !== i) return;
454
466
  r();
455
467
  return;
456
468
  }
457
- D.has(n) && i();
469
+ E.has(n) && i();
458
470
  });
459
471
  return () => {
460
472
  t = !0, a();
461
473
  };
462
474
  }, [
463
- k,
475
+ A,
464
476
  $,
465
477
  Z
466
478
  ]), {
467
- organizations: A,
468
- workspaces: N,
469
- projects: L,
470
- currentOrganizationId: j,
471
- currentWorkspaceId: P,
472
- currentProjectId: R,
473
- currentOrganization: A.find((e) => e.id === j),
474
- currentWorkspace: N.find((e) => e.id === P),
475
- currentProject: L.find((e) => e.id === R),
476
- workspaceToken: F,
477
- isLoadingOrganizations: M,
478
- isLoadingWorkspaces: I,
479
- isLoadingProjects: z,
480
- isLoading: M || I || z,
481
- error: te,
479
+ organizations: j,
480
+ workspaces: P,
481
+ projects: te,
482
+ currentOrganizationId: M,
483
+ currentWorkspaceId: F,
484
+ currentProjectId: ne,
485
+ currentOrganization: j.find((e) => e.id === M),
486
+ currentWorkspace: P.find((e) => e.id === F),
487
+ currentProject: te.find((e) => e.id === ne),
488
+ workspaceToken: I,
489
+ isLoadingOrganizations: N,
490
+ isLoadingWorkspaces: L,
491
+ isLoadingProjects: re,
492
+ isLoading: N || L || re,
493
+ error: ie,
482
494
  fetchOrganizations: X,
483
495
  fetchWorkspacesForOrg: Z,
484
496
  issueWorkspaceToken: Q,
485
497
  fetchProjects: $,
486
- selectOrganization: se,
487
- selectWorkspace: ce,
488
- selectProject: le,
489
- selectWorkspaceWide: ue
498
+ selectOrganization: ce,
499
+ selectWorkspace: le,
500
+ selectProject: ue,
501
+ selectWorkspaceWide: de
490
502
  };
491
503
  }
492
504
  function k(n) {
@@ -520,9 +532,9 @@ function k(n) {
520
532
  ]);
521
533
  }
522
534
  function A(n) {
523
- let s = e((e) => e.currentWorkspaceId), c = e((e) => e.currentOrganizationId), f = e((e) => e.workspaces), m = e((e) => e.projects), h = e((e) => e.currentProjectId), g = e((e) => e.workspaceToken), b = e((e) => e.accessToken), S = t(), C = n?.workspaceBaseUrl ?? S.workspaceBaseUrl, w = n?.disableProjectSync ?? !1, T = n?.skipProjectFetch ?? !1, E = i(!1), [D, ee] = a(0), O = i(null), k = i(/* @__PURE__ */ new Map()), A = i(/* @__PURE__ */ new Map()), j = 3e4;
535
+ let s = e((e) => e.currentWorkspaceId), c = e((e) => e.currentOrganizationId), f = e((e) => e.workspaces), m = e((e) => e.projects), h = e((e) => e.currentProjectId), g = e((e) => e.workspaceToken), y = e((e) => e.accessToken), x = t(), S = n?.workspaceBaseUrl ?? x.workspaceBaseUrl, C = n?.disableProjectSync ?? !1, w = n?.skipProjectFetch ?? !1, T = i(!1), [E, D] = a(0), O = i(null), k = i(/* @__PURE__ */ new Map()), A = i(/* @__PURE__ */ new Map()), j = 3e4;
524
536
  r(() => {
525
- if (w || !s || !b || E.current) return;
537
+ if (C || !s || !y || T.current) return;
526
538
  let t = A.current.get(s);
527
539
  if (t && t.count >= 8 && Date.now() - t.lastFailureAt < j) return;
528
540
  t && Date.now() - t.lastFailureAt >= j && A.current.delete(s);
@@ -535,28 +547,28 @@ function A(n) {
535
547
  organizationId: r,
536
548
  workspaceId: s
537
549
  });
538
- let a = o(), S = i.user?.id, D = d({
550
+ let a = o(), x = i.user?.id, E = d({
539
551
  organizationId: r,
540
552
  workspaceId: s
541
553
  });
542
- if (D && i.currentProjectId) {
554
+ if (E && i.currentProjectId) {
543
555
  i.setCurrentProjectId(null), i.setWorkspaceToken(null);
544
556
  return;
545
557
  }
546
- let M = !g || y(g) || !x(g, {
558
+ let M = !g || v(g) || !b(g, {
547
559
  workspaceId: s,
548
560
  organizationId: c,
549
561
  projectId: h,
550
- workspaceWide: D
562
+ workspaceWide: E
551
563
  }), N = JSON.stringify([
552
564
  a,
553
- S,
554
- b,
565
+ x,
566
+ y,
555
567
  r,
556
568
  s
557
569
  ]), P = m.some((e) => e.workspaceId === s) || O.current?.scopeKey === N && O.current.projects === m;
558
570
  if (P && !M) {
559
- if (!i.currentProjectId && !D) {
571
+ if (!i.currentProjectId && !E) {
560
572
  let e = m.find((e) => e.workspaceId === s);
561
573
  e && i.setCurrentProjectId(e.id);
562
574
  }
@@ -564,45 +576,45 @@ function A(n) {
564
576
  }
565
577
  let F = () => {
566
578
  let t = e.getState();
567
- return t.accessToken === b && t.user?.id === S && o() === a && t.currentOrganizationId === r && t.currentWorkspaceId === s && t.currentProjectId === h && d({
579
+ return t.accessToken === y && t.user?.id === x && o() === a && t.currentOrganizationId === r && t.currentWorkspaceId === s && t.currentProjectId === h && d({
568
580
  organizationId: r,
569
581
  workspaceId: s
570
- }) === D;
582
+ }) === E;
571
583
  }, I = async () => {
572
- E.current = !0, i.setLoadingProjects(!0);
584
+ T.current = !0, i.setLoadingProjects(!0);
573
585
  let t = !1;
574
586
  try {
575
587
  let n = g;
576
588
  if (M) {
577
589
  let e = await l({
578
590
  gateway: "workspace",
579
- query: _,
591
+ query: ee,
580
592
  variables: { input: {
581
593
  organizationId: r,
582
594
  workspaceId: s,
583
595
  ...h ? { projectId: h } : {}
584
596
  } },
585
- authToken: b,
586
- baseUrl: C,
597
+ authToken: y,
598
+ baseUrl: S,
587
599
  tenantId: f.find((e) => e.id === s)?.tenantId
588
600
  });
589
601
  if (e.data?.issueWorkspaceToken?.success && e.data.issueWorkspaceToken.token) {
590
- if (n = e.data.issueWorkspaceToken.token, !F() || !x(n, {
602
+ if (n = e.data.issueWorkspaceToken.token, !F() || !b(n, {
591
603
  workspaceId: s,
592
604
  organizationId: r,
593
605
  projectId: h,
594
- workspaceWide: D
606
+ workspaceWide: E
595
607
  })) return;
596
608
  i.setWorkspaceToken(n);
597
609
  } else return;
598
610
  }
599
- if (n && !P && !T) {
611
+ if (n && !P && !w) {
600
612
  let t = await l({
601
613
  gateway: "workspace",
602
- query: v,
603
- authToken: b,
614
+ query: _,
615
+ authToken: y,
604
616
  workspaceToken: n,
605
- baseUrl: C
617
+ baseUrl: S
606
618
  });
607
619
  if (!F() || t.errors?.length) return;
608
620
  let a = t.data?.projects?.items ?? [];
@@ -616,9 +628,9 @@ function A(n) {
616
628
  }
617
629
  t = !0;
618
630
  } catch {} finally {
619
- i.setLoadingProjects(!1), E.current = !1;
631
+ i.setLoadingProjects(!1), T.current = !1;
620
632
  let n = F();
621
- n || ee((e) => e + 1);
633
+ n || D((e) => e + 1);
622
634
  let r = A.current;
623
635
  if (n && t) {
624
636
  r.delete(s);
@@ -635,7 +647,7 @@ function A(n) {
635
647
  let r = Math.min(1e3 * 2 ** (t - 1), 8e3), i = setTimeout(() => {
636
648
  k.current.delete(s);
637
649
  let t = e.getState();
638
- F() && t.currentWorkspaceId === s && !t.projects.some((e) => e.workspaceId === s) && !E.current && I();
650
+ F() && t.currentWorkspaceId === s && !t.projects.some((e) => e.workspaceId === s) && !T.current && I();
639
651
  }, r);
640
652
  k.current.set(s, i);
641
653
  }
@@ -644,17 +656,17 @@ function A(n) {
644
656
  };
645
657
  I();
646
658
  }, [
647
- D,
659
+ E,
648
660
  s,
649
661
  c,
650
662
  f,
651
663
  m,
652
664
  h,
653
665
  g,
654
- b,
666
+ y,
667
+ S,
655
668
  C,
656
- w,
657
- T
669
+ w
658
670
  ]), r(() => {
659
671
  let e = k.current;
660
672
  return () => {