@burdenoff/microfe-auth 2026.925.1 → 2026.1009.1

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