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