@geomak/ui 6.21.1 → 6.22.0

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.
package/dist/index.cjs CHANGED
@@ -5887,33 +5887,43 @@ function SecureLayout({
5887
5887
  canAccess,
5888
5888
  loadingFallback,
5889
5889
  fallback,
5890
+ onGranted,
5890
5891
  onDeny,
5891
5892
  className = ""
5892
5893
  }) {
5893
5894
  const reduced = framerMotion.useReducedMotion();
5894
- const [state, setState] = React26.useState("checking");
5895
5895
  const rolesKey = JSON.stringify(roles);
5896
5896
  const requiredRolesKey = JSON.stringify(requiredRoles);
5897
5897
  const permissionsKey = JSON.stringify(permissions);
5898
5898
  const requiredPermissionsKey = JSON.stringify(requiredPermissions);
5899
+ const passesSync = () => {
5900
+ let authed = isAuthenticated;
5901
+ if (authed === void 0 && token !== void 0) authed = tokenValid(token);
5902
+ if (authed === void 0) authed = true;
5903
+ if (!authed) return false;
5904
+ if (requiredRoles?.length && !has(roles, requiredRoles, requireAllRoles)) return false;
5905
+ if (requiredPermissions?.length && !has(permissions, requiredPermissions, requireAllPermissions)) return false;
5906
+ return true;
5907
+ };
5908
+ const [state, setState] = React26.useState(
5909
+ () => !passesSync() ? "denied" : canAccess ? "checking" : "granted"
5910
+ );
5899
5911
  React26.useEffect(() => {
5900
5912
  let cancelled = false;
5901
- setState("checking");
5902
- const evaluate = async () => {
5903
- let authed = isAuthenticated;
5904
- if (authed === void 0 && token !== void 0) authed = tokenValid(token);
5905
- if (authed === void 0) authed = true;
5906
- if (!authed) return false;
5907
- if (requiredRoles?.length && !has(roles, requiredRoles, requireAllRoles)) return false;
5908
- if (requiredPermissions?.length && !has(permissions, requiredPermissions, requireAllPermissions)) return false;
5909
- if (canAccess && !await canAccess()) return false;
5910
- return true;
5911
- };
5912
- evaluate().then((ok) => {
5913
+ const finish = (ok) => {
5913
5914
  if (cancelled) return;
5914
5915
  setState(ok ? "granted" : "denied");
5915
- if (!ok) onDeny?.();
5916
- });
5916
+ if (ok) onGranted?.();
5917
+ else onDeny?.();
5918
+ };
5919
+ if (!passesSync()) {
5920
+ finish(false);
5921
+ } else if (!canAccess) {
5922
+ finish(true);
5923
+ } else {
5924
+ setState("checking");
5925
+ Promise.resolve(canAccess()).then((ok) => finish(Boolean(ok)));
5926
+ }
5917
5927
  return () => {
5918
5928
  cancelled = true;
5919
5929
  };
@@ -5929,10 +5939,12 @@ function SecureLayout({
5929
5939
  requiredPermissionsKey
5930
5940
  ]);
5931
5941
  if (state === "checking") {
5932
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: ["flex min-h-[8rem] items-center justify-center", className].filter(Boolean).join(" "), children: loadingFallback ?? /* @__PURE__ */ jsxRuntime.jsx(Spinner2, {}) });
5942
+ if (loadingFallback === null) return null;
5943
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: ["flex min-h-[8rem] items-center justify-center", className].filter(Boolean).join(" "), children: loadingFallback !== void 0 ? loadingFallback : /* @__PURE__ */ jsxRuntime.jsx(Spinner2, {}) });
5933
5944
  }
5934
5945
  if (state === "denied") {
5935
- return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || void 0, children: fallback ?? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex min-h-[8rem] flex-col items-center justify-center gap-1 rounded-xl border border-border bg-surface p-8 text-center", children: [
5946
+ if (fallback === null) return null;
5947
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || void 0, children: fallback !== void 0 ? fallback : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex min-h-[8rem] flex-col items-center justify-center gap-1 rounded-xl border border-border bg-surface p-8 text-center", children: [
5936
5948
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-sm font-semibold text-foreground", children: "Access denied" }),
5937
5949
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-xs text-foreground-muted", children: "You don\u2019t have permission to view this content." })
5938
5950
  ] }) });