From f5412599a3a69b1744b7350ed1d12f7bf8abf4be Mon Sep 17 00:00:00 2001 From: Mohamadzadeh <7796733@gmail.com> Date: Sat, 13 Jun 2026 19:02:06 +0330 Subject: [PATCH] add PermissionProvider to BiProvider --- dist/index.js | 202 ++++++++++++++++++------------------ src/provider/BiProvider.jsx | 5 +- 2 files changed, 105 insertions(+), 102 deletions(-) diff --git a/dist/index.js b/dist/index.js index 07f7019..ea4cb95 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5,33 +5,33 @@ import { IoChevronForwardOutline as O, IoChevronBackOutline as W } from "react-i import te from "keycloak-js"; import { useQuery as ne } from "@tanstack/react-query"; import re from "axios"; -const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => String(e).replace(/\d/g, (t) => "۰۱۲۳۴۵۶۷۸۹"[Number(t)]), D = (e) => String(e).replace(/[۰-۹]/g, (n) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(n))), De = ({ +const De = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => String(e).replace(/\d/g, (t) => "۰۱۲۳۴۵۶۷۸۹"[Number(t)]), D = (e) => String(e).replace(/[۰-۹]/g, (n) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(n))), Ke = ({ currentPage: e, totalPages: t, totalCount: n, onPageChange: l }) => { - const [u, s] = v(p(e)), [i, o] = v(!1), [d, c] = v(null), b = N(null), y = [.../* @__PURE__ */ new Set([1, e, t])].filter( + const [c, s] = v(p(e)), [i, o] = v(!1), [d, u] = v(null), b = N(null), y = [.../* @__PURE__ */ new Set([1, e, t])].filter( (r) => r >= 1 && r <= t ); C(() => { if (d !== null) { - e === d && (c(null), o(!1), s(p(e))); + e === d && (u(null), o(!1), s(p(e))); return; } i || s(p(e)); }, [e, i, d]), C(() => { - if (!i || !u || d !== null) return; - const r = parseInt(D(u), 10); + if (!i || !c || d !== null) return; + const r = parseInt(D(c), 10); if (isNaN(r)) return; const h = setTimeout(() => { var w; const m = Math.min(Math.max(r, 1), t); - m !== e ? (c(m), s(p(m)), l(m)) : (o(!1), s(p(e))), (w = b.current) == null || w.blur(); + m !== e ? (u(m), s(p(m)), l(m)) : (o(!1), s(p(e))), (w = b.current) == null || w.blur(); }, 1e3); return () => clearTimeout(h); }, [ - u, + c, i, d, t, @@ -40,13 +40,13 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S ]); const A = () => { var m, w; - const r = parseInt(D(u), 10); + const r = parseInt(D(c), 10); if (isNaN(r)) { - s(p(e)), o(!1), c(null), (m = b.current) == null || m.blur(); + s(p(e)), o(!1), u(null), (m = b.current) == null || m.blur(); return; } const h = Math.min(Math.max(r, 1), t); - h !== e ? (c(h), s(p(h)), l(h)) : (s(p(e)), o(!1), c(null)), (w = b.current) == null || w.blur(); + h !== e ? (u(h), s(p(h)), l(h)) : (s(p(e)), o(!1), u(null)), (w = b.current) == null || w.blur(); }; return t === 0 || !n ? null : /* @__PURE__ */ I( k, @@ -109,16 +109,16 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S inputMode: "numeric", placeholder: "صفحه", w: "56px", - value: u, + value: c, onFocus: () => { - o(!0), c(null), s(""); + o(!0), u(null), s(""); }, onChange: (r) => { const h = r.target.value, m = D(h).replace(/\D/g, ""); s(p(m)); }, onBlur: () => { - u || (s(p(e)), o(!1), c(null)); + c || (s(p(e)), o(!1), u(null)); }, onKeyDown: (r) => { r.key === "Enter" && A(); @@ -138,33 +138,33 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S ] } ); -}, Ke = ({ +}, Ne = ({ currentPage: e, totalPages: t, totalCount: n, onPageChange: l }) => { - const [u, s] = v(p(e)), [i, o] = v(!1), [d, c] = v(null), b = N(null), y = Array.from( + const [c, s] = v(p(e)), [i, o] = v(!1), [d, u] = v(null), b = N(null), y = Array.from( /* @__PURE__ */ new Set([1, e, e + 1, t]) ).filter((r) => r >= 1 && r <= t).sort((r, h) => r - h); C(() => { if (d !== null) { - e === d && (c(null), o(!1), s(p(e))); + e === d && (u(null), o(!1), s(p(e))); return; } i || s(p(e)); }, [e, i, d]), C(() => { - if (!i || !u || d !== null) return; - const r = parseInt(D(u), 10); + if (!i || !c || d !== null) return; + const r = parseInt(D(c), 10); if (isNaN(r)) return; const h = setTimeout(() => { var w; const m = Math.min(Math.max(r, 1), t); - m !== e ? (c(m), s(p(m)), l(m)) : (o(!1), s(p(e))), (w = b.current) == null || w.blur(); + m !== e ? (u(m), s(p(m)), l(m)) : (o(!1), s(p(e))), (w = b.current) == null || w.blur(); }, 1e3); return () => clearTimeout(h); }, [ - u, + c, i, d, t, @@ -173,13 +173,13 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S ]); const A = () => { var m, w; - const r = parseInt(D(u), 10); + const r = parseInt(D(c), 10); if (isNaN(r)) { - s(p(e)), o(!1), c(null), (m = b.current) == null || m.blur(); + s(p(e)), o(!1), u(null), (m = b.current) == null || m.blur(); return; } const h = Math.min(Math.max(r, 1), t); - h !== e ? (c(h), s(p(h)), l(h)) : (s(p(e)), o(!1), c(null)), (w = b.current) == null || w.blur(); + h !== e ? (u(h), s(p(h)), l(h)) : (s(p(e)), o(!1), u(null)), (w = b.current) == null || w.blur(); }; return t === 0 || !n ? null : /* @__PURE__ */ I( k, @@ -247,16 +247,16 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S inputMode: "numeric", placeholder: "صفحه", w: "100px", - value: u, + value: c, onFocus: () => { - o(!0), c(null), s(""); + o(!0), u(null), s(""); }, onChange: (r) => { const h = r.target.value, m = D(h).replace(/\D/g, ""); s(p(m)); }, onBlur: () => { - u || (s(p(e)), o(!1), c(null)); + c || (s(p(e)), o(!1), u(null)); }, onKeyDown: (r) => { r.key === "Enter" && A(); @@ -276,45 +276,45 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S ] } ); -}, Ne = ({ +}, Te = ({ currentPage: e, totalPages: t, totalCount: n, onPageChange: l }) => { - const [u, s] = v(""), [i, o] = v(!1), [d, c] = v(null), b = N(null), y = d ?? e, A = E( + const [c, s] = v(""), [i, o] = v(!1), [d, u] = v(null), b = N(null), y = d ?? e, A = E( (x) => { const B = Math.min(Math.max(x, 1), t); - o(!1), s(""), c(null), B !== e && l(B); + o(!1), s(""), u(null), B !== e && l(B); }, [e, t, l] ), r = E(() => { - if (!u) { - o(!1), s(""), c(null); + if (!c) { + o(!1), s(""), u(null); return; } - const x = parseInt(D(u), 10); + const x = parseInt(D(c), 10); if (isNaN(x)) { - o(!1), s(""), c(null); + o(!1), s(""), u(null); return; } const B = Math.min(Math.max(x, 1), t); - o(!1), s(""), B !== e ? (c(B), l(B)) : c(null); - }, [u, e, t, l]); + o(!1), s(""), B !== e ? (u(B), l(B)) : u(null); + }, [c, e, t, l]); C(() => { var x; i && ((x = b.current) == null || x.focus()); }, [i]), C(() => { - d !== null && e === d && c(null); + d !== null && e === d && u(null); }, [e, d]), C(() => { - if (!i || !u) return; + if (!i || !c) return; const x = setTimeout(() => { r(); }, 1e3); return () => clearTimeout(x); - }, [u, i, r]); + }, [c, i, r]); const h = () => { - c(null), s(""), o(!0); + u(null), s(""), o(!0); }, m = (x, B = !1) => /* @__PURE__ */ a( R, { @@ -364,16 +364,16 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S inputMode: "numeric", placeholder: "صفحه", w: "48px", - value: u, + value: c, onChange: (x) => { const B = x.target.value, X = D(B).replace(/\D/g, ""); s(p(X)); }, onBlur: () => { - u ? r() : (s(""), o(!1), c(null)); + c ? r() : (s(""), o(!1), u(null)); }, onKeyDown: (x) => { - x.key === "Enter" && r(), x.key === "Escape" && (s(""), o(!1), c(null)); + x.key === "Enter" && r(), x.key === "Escape" && (s(""), o(!1), u(null)); }, textAlign: "center", color: "#1D2939", @@ -405,7 +405,7 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t url: e, realm: t, clientId: n -})), f), se = (e) => (V = e, V), le = () => j, G = () => !!(f != null && f.authenticated), T = () => (f == null ? void 0 : f.token) ?? null, q = async (e = 30) => f ? f.updateToken(e) : !1, H = (e) => f == null ? void 0 : f.logout(e), Te = (e) => f == null ? void 0 : f.login(e), J = ({ config: e, initOptions: t = {} } = {}) => { +})), f), se = (e) => (V = e, V), le = () => j, G = () => !!(f != null && f.authenticated), T = () => (f == null ? void 0 : f.token) ?? null, q = async (e = 30) => f ? f.updateToken(e) : !1, H = (e) => f == null ? void 0 : f.logout(e), _e = (e) => f == null ? void 0 : f.login(e), J = ({ config: e, initOptions: t = {} } = {}) => { const n = e ?? V; if (!(n != null && n.url) || !(n != null && n.realm) || !(n != null && n.clientId)) throw new Error("Keycloak config is required."); @@ -416,8 +416,8 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t checkLoginIframe: !1, flow: "implicit", ...t - }).then((u) => (j = !0, u)).catch((u) => { - throw j = !0, u; + }).then((c) => (j = !0, c)).catch((c) => { + throw j = !0, c; })), F; }, ce = ({ children: e, @@ -425,13 +425,13 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t initOptions: n, loading: l = null }) => { - const [u, s] = v(!1), [i, o] = v(!1), [d, c] = v(null), b = N(!1), y = E(() => { - f && (o(!!f.authenticated), c(f.token ?? null)); + const [c, s] = v(!1), [i, o] = v(!1), [d, u] = v(null), b = N(!1), y = E(() => { + f && (o(!!f.authenticated), u(f.token ?? null)); }, []); C(() => (b.current = !0, J({ config: t, initOptions: n }).then((w) => { - b.current && (o(!!w), c((f == null ? void 0 : f.token) ?? null), s(!0)); + b.current && (o(!!w), u((f == null ? void 0 : f.token) ?? null), s(!0)); }).catch(() => { - b.current && (o(!1), c(null), s(!0)); + b.current && (o(!1), u(null), s(!0)); }), () => { b.current = !1; }), [t, n]); @@ -444,34 +444,34 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t [y] ), m = _( () => ({ - initialized: u, + initialized: c, authenticated: i, token: d, login: A, logout: r, updateToken: h }), - [u, i, d, A, r, h] + [c, i, d, A, r, h] ); - return u ? /* @__PURE__ */ a(ie.Provider, { value: m, children: e }) : l; -}, Q = $(null), M = "permissions", ue = (e) => (e == null ? void 0 : e.storageKey) || M, _e = ({ + return c ? /* @__PURE__ */ a(ie.Provider, { value: m, children: e }) : l; +}, Q = $(null), M = "permissions", ue = (e) => (e == null ? void 0 : e.storageKey) || M, ae = ({ user: e = null, children: t, config: n, loading: l = /* @__PURE__ */ a("div", { children: "Loading permissions..." }), - errorFallback: u = /* @__PURE__ */ a("div", { children: "Error loading permissions" }) + errorFallback: c = /* @__PURE__ */ a("div", { children: "Error loading permissions" }) }) => { const s = ue(n), [i, o] = v( - () => ae(s) - ), { data: d, isLoading: c, error: b } = me(n), y = d ?? i; + () => de(s) + ), { data: d, isLoading: u, error: b } = be(n), y = d ?? i; C(() => { - d && (de(s, d), o(d)); + d && (fe(s, d), o(d)); }, [d, s]); const A = _( - () => fe(y), + () => pe(y), [y] ), r = E( - (w = "") => pe(A, w), + (w = "") => he(A, w), [A] ), h = E((w = "") => !r(w), [r]), m = _( () => ({ @@ -479,13 +479,13 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t can: r, cannot: h, permissions: y, - isLoading: c, + isLoading: u, error: b }), - [e, r, h, y, c, b] + [e, r, h, y, u, b] ); - return b && !y ? u : c && !y ? l : /* @__PURE__ */ a(Q.Provider, { value: m, children: t }); -}, ae = (e = M) => { + return b && !y ? c : u && !y ? l : /* @__PURE__ */ a(Q.Provider, { value: m, children: t }); +}, de = (e = M) => { if (!(typeof window > "u")) try { const t = localStorage.getItem(e); @@ -493,7 +493,7 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t } catch { return; } -}, de = (e = M, t) => { +}, fe = (e = M, t) => { if (!(typeof window > "u")) try { localStorage.setItem(e, JSON.stringify(t)); @@ -505,7 +505,7 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t localStorage.removeItem(e); } catch { } -}, fe = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), pe = (e, t = "") => { +}, pe = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), he = (e, t = "") => { if (!t) return !1; if (e.has(t) || [...e].some((l) => l.startsWith(t))) return !0; let n = t; @@ -515,7 +515,7 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t }, Y = { all: ["permissions"], list: (e) => [...Y.all, e] -}, he = async (e) => { +}, me = async (e) => { if (!(e != null && e.url) || !(e != null && e.clientId)) throw new Error("Permission config requires url and clientId."); const t = T(), n = new URL(e.url); @@ -534,9 +534,9 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t if (!e) throw new Error("usePermission must be used within PermissionProvider"); return e; -}, me = (e) => ne({ +}, be = (e) => ne({ queryKey: Y.list(e == null ? void 0 : e.clientId), - queryFn: () => he(e), + queryFn: () => me(e), enabled: !!(e != null && e.url) && !!(e != null && e.clientId) && G(), staleTime: 1 / 0, refetchOnWindowFocus: !1, @@ -558,12 +558,12 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new t }, [n, t]), e; }; let z = null; -const be = (e) => { +const we = (e) => { const { api: t } = e, n = re.create({ baseURL: t.baseUrl }), l = async () => { (!le() || !G()) && await J(); - }, u = async (i) => { + }, c = async (i) => { await l(); let o = T(); if (o) { @@ -584,10 +584,10 @@ const be = (e) => { o._retry = !0; try { await q(30); - const c = T(); + const u = T(); return o.headers = { ...o.headers, - Authorization: `Bearer ${c}` + Authorization: `Bearer ${u}` }, n(o); } catch { H({ redirectUri: window.location.origin }); @@ -595,13 +595,13 @@ const be = (e) => { } return Promise.reject(i); }; - return n.interceptors.request.use(u), n.interceptors.response.use( + return n.interceptors.request.use(c), n.interceptors.response.use( (i) => i, s ), { api: n }; -}, we = (e) => (z = be(e), z), ye = () => { +}, ye = (e) => (z = we(e), z), xe = () => { if (!z) throw new Error("api is not configured. Call configureApi(config) first."); return z.api; @@ -609,16 +609,16 @@ const be = (e) => { {}, { get(e, t) { - return ye()[t]; + return xe()[t]; } } ); -function xe() { +function ve() { const [e, t] = v(!1), n = N(null); return C(() => { if (process.env.NODE_ENV !== "production") return; let l; - const u = async () => { + const c = async () => { const i = await fetch(`/version.json?t=${Date.now()}`, { cache: "no-store" }); @@ -629,9 +629,9 @@ function xe() { }; return (async () => { try { - n.current = await u(), l = setInterval(async () => { + n.current = await c(), l = setInterval(async () => { try { - const i = await u(); + const i = await c(); n.current && i !== n.current && (t(!0), clearInterval(l)); } catch (i) { console.error("Update check failed:", i); @@ -667,12 +667,12 @@ function je({ apiBaseUrl: t, keycloakClientId: n, permissionClientId: l, - loading: u = /* @__PURE__ */ a(ee, {}), + loading: c = /* @__PURE__ */ a(ee, {}), updateChecker: s = !0, updateCheckerProps: i, keycloakUrl: o = "https://auth.ibagher.ir", keycloakRealm: d = "bi", - permissionUrl: c = "https://api.d.aiengines.ir/user_api/v1/permissions/list" + permissionUrl: u = "https://api.d.aiengines.ir/user_api/v1/permissions/list" }) { const b = _( () => ({ @@ -685,7 +685,7 @@ function je({ clientId: n }, permission: { - url: c, + url: u, clientId: l } }), @@ -695,14 +695,14 @@ function je({ l, o, d, - c + u ] ); return C(() => { - we(b); + ye(b); }, [b]), /* @__PURE__ */ I(Z, { children: [ - s && /* @__PURE__ */ a(xe, { ...i }), - /* @__PURE__ */ a(ce, { config: b.keycloak, loading: u, children: e }) + s && /* @__PURE__ */ a(ve, { ...i }), + /* @__PURE__ */ a(ce, { config: b.keycloak, loading: c, children: /* @__PURE__ */ a(ae, { config: b.permission, loading: c, children: e }) }) ] }); } export { @@ -710,32 +710,32 @@ export { je as BiProvider, Me as Can, ce as KeycloakProvider, - De as LightPagination, - Ke as Pagination, + Ke as LightPagination, + Ne as Pagination, Fe as PanelAccessGuard, - _e as PermissionProvider, - Ne as SimplePagination, + ae as PermissionProvider, + Te as SimplePagination, Ve as api, - pe as checkPermission, + he as checkPermission, ze as clearCachedPermissions, - we as configureApi, - be as createApi, - fe as createPermissionSet, - ye as getApi, - ae as getCachedPermissions, + ye as configureApi, + we as createApi, + pe as createPermissionSet, + xe as getApi, + de as getCachedPermissions, T as getKeycloakToken, - he as getPermissions, + me as getPermissions, J as initKeycloak, G as isKeycloakAuthenticated, le as isKeycloakInitialized, - Te as loginKeycloak, + _e as loginKeycloak, H as logoutKeycloak, Y as permissionKeys, - de as setCachedPermissions, + fe as setCachedPermissions, D as toEnDigits, p as toFaDigits, - Ee as toFaNumber, + De as toFaNumber, q as updateKeycloakToken, U as usePermission, - me as usePermissionsQuery + be as usePermissionsQuery }; diff --git a/src/provider/BiProvider.jsx b/src/provider/BiProvider.jsx index a85e591..130412c 100644 --- a/src/provider/BiProvider.jsx +++ b/src/provider/BiProvider.jsx @@ -1,6 +1,7 @@ import { useEffect, useMemo } from "react"; import { Spinner } from "@chakra-ui/react"; import { KeycloakProvider } from "../core/keycloak.jsx"; +import { PermissionProvider } from "../core/permission.jsx"; import { UpdateChecker } from "../components/UpdateChecker.jsx"; import { configureApi } from "../core/api.js"; @@ -50,7 +51,9 @@ export function BiProvider({ {updateChecker && } - {children} + + {children} + );