diff --git a/dist/index.js b/dist/index.js index 62f7f0a..98a20d5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,54 +1,54 @@ -import { jsxs as k, jsx as c } from "react/jsx-runtime"; -import { useState as B, useRef as T, useEffect as g, useCallback as D, createContext as W, useMemo as I, useContext as G } from "react"; -import { HStack as S, IconButton as M, Button as _, Input as F, Text as A } from "@chakra-ui/react"; -import { IoChevronForwardOutline as V, IoChevronBackOutline as j } from "react-icons/io5"; -import J from "keycloak-js"; -import { useQuery as Q } from "@tanstack/react-query"; -const fe = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), d = (e) => String(e).replace(/\d/g, (n) => "۰۱۲۳۴۵۶۷۸۹"[Number(n)]), N = (e) => String(e).replace(/[۰-۹]/g, (r) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(r))), de = ({ +import { jsxs as E, jsx as l } from "react/jsx-runtime"; +import { useState as B, useRef as N, useEffect as S, useCallback as I, createContext as O, useMemo as _, useContext as Q } from "react"; +import { HStack as A, IconButton as K, Button as V, Input as j, Text as D } from "@chakra-ui/react"; +import { IoChevronForwardOutline as L, IoChevronBackOutline as R } from "react-icons/io5"; +import U from "keycloak-js"; +import { useQuery as Y } from "@tanstack/react-query"; +const pe = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), c = (e) => String(e).replace(/\d/g, (t) => "۰۱۲۳۴۵۶۷۸۹"[Number(t)]), k = (e) => String(e).replace(/[۰-۹]/g, (r) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(r))), me = ({ currentPage: e, - totalPages: n, + totalPages: t, totalCount: r, - onPageChange: s + onPageChange: o }) => { - const [l, i] = B(d(e)), [w, p] = B(!1), [m, o] = B(null), h = T(null), x = [.../* @__PURE__ */ new Set([1, e, n])].filter( - (t) => t >= 1 && t <= n + const [a, i] = B(c(e)), [y, d] = B(!1), [m, s] = B(null), b = N(null), w = [.../* @__PURE__ */ new Set([1, e, t])].filter( + (n) => n >= 1 && n <= t ); - g(() => { + S(() => { if (m !== null) { - e === m && (o(null), p(!1), i(d(e))); + e === m && (s(null), d(!1), i(c(e))); return; } - w || i(d(e)); - }, [e, w, m]), g(() => { - if (!w || !l || m !== null) return; - const t = parseInt(N(l), 10); - if (isNaN(t)) return; - const a = setTimeout(() => { - var b; - const f = Math.min(Math.max(t, 1), n); - f !== e ? (o(f), i(d(f)), s(f)) : (p(!1), i(d(e))), (b = h.current) == null || b.blur(); + y || i(c(e)); + }, [e, y, m]), S(() => { + if (!y || !a || m !== null) return; + const n = parseInt(k(a), 10); + if (isNaN(n)) return; + const f = setTimeout(() => { + var h; + const p = Math.min(Math.max(n, 1), t); + p !== e ? (s(p), i(c(p)), o(p)) : (d(!1), i(c(e))), (h = b.current) == null || h.blur(); }, 1e3); - return () => clearTimeout(a); + return () => clearTimeout(f); }, [ - l, - w, + a, + y, m, - n, + t, e, - s + o ]); const v = () => { - var f, b; - const t = parseInt(N(l), 10); - if (isNaN(t)) { - i(d(e)), p(!1), o(null), (f = h.current) == null || f.blur(); + var p, h; + const n = parseInt(k(a), 10); + if (isNaN(n)) { + i(c(e)), d(!1), s(null), (p = b.current) == null || p.blur(); return; } - const a = Math.min(Math.max(t, 1), n); - a !== e ? (o(a), i(d(a)), s(a)) : (i(d(e)), p(!1), o(null)), (b = h.current) == null || b.blur(); + const f = Math.min(Math.max(n, 1), t); + f !== e ? (s(f), i(c(f)), o(f)) : (i(c(e)), d(!1), s(null)), (h = b.current) == null || h.blur(); }; - return n === 0 || !r ? null : /* @__PURE__ */ k( - S, + return t === 0 || !r ? null : /* @__PURE__ */ E( + A, { justifyContent: "space-around", backgroundColor: "white", @@ -59,68 +59,68 @@ const fe = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), d = (e) => S borderColor: "blue.200", w: "100%", children: [ - /* @__PURE__ */ k(S, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ - /* @__PURE__ */ c( - M, + /* @__PURE__ */ E(A, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ + /* @__PURE__ */ l( + K, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, - onClick: () => s(e - 1), + onClick: () => o(e - 1), isDisabled: e === 1, - icon: /* @__PURE__ */ c(V, {}), + icon: /* @__PURE__ */ l(L, {}), "aria-label": "صفحه قبلی" } ), - x.map((t) => /* @__PURE__ */ c( - _, + w.map((n) => /* @__PURE__ */ l( + V, { size: "md", px: 2, - bg: e === t ? "#4B49AC" : "#e6e6f1", - color: e === t ? "#e6e6f1" : "#4B49AC", - _hover: { bg: e === t ? "#4B49AC" : "#cbcbe8" }, - onClick: () => s(t), + bg: e === n ? "#4B49AC" : "#e6e6f1", + color: e === n ? "#e6e6f1" : "#4B49AC", + _hover: { bg: e === n ? "#4B49AC" : "#cbcbe8" }, + onClick: () => o(n), variant: "soft", - children: d(t) + children: c(n) }, - t + n )), - /* @__PURE__ */ c( - M, + /* @__PURE__ */ l( + K, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, - onClick: () => s(e + 1), - isDisabled: e === n, - icon: /* @__PURE__ */ c(j, {}), + onClick: () => o(e + 1), + isDisabled: e === t, + icon: /* @__PURE__ */ l(R, {}), "aria-label": "صفحه بعدی" } ), - /* @__PURE__ */ c( - F, + /* @__PURE__ */ l( + j, { - ref: h, + ref: b, type: "text", inputMode: "numeric", placeholder: "صفحه", w: "56px", - value: l, + value: a, onFocus: () => { - p(!0), o(null), i(""); + d(!0), s(null), i(""); }, - onChange: (t) => { - const a = t.target.value, f = N(a).replace(/\D/g, ""); - i(d(f)); + onChange: (n) => { + const f = n.target.value, p = k(f).replace(/\D/g, ""); + i(c(p)); }, onBlur: () => { - l || (i(d(e)), p(!1), o(null)); + a || (i(c(e)), d(!1), s(null)); }, - onKeyDown: (t) => { - t.key === "Enter" && v(); + onKeyDown: (n) => { + n.key === "Enter" && v(); }, textAlign: "center", color: "#1D2939", @@ -130,58 +130,58 @@ const fe = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), d = (e) => S } ) ] }), - /* @__PURE__ */ k(S, { spacing: 2, children: [ - /* @__PURE__ */ c(A, { w: "auto", fontWeight: "bold", whiteSpace: "nowrap", children: "تعداد کل:" }), - /* @__PURE__ */ c(A, { children: d(r) }) + /* @__PURE__ */ E(A, { spacing: 2, children: [ + /* @__PURE__ */ l(D, { w: "auto", fontWeight: "bold", whiteSpace: "nowrap", children: "تعداد کل:" }), + /* @__PURE__ */ l(D, { children: c(r) }) ] }) ] } ); -}, pe = ({ +}, he = ({ currentPage: e, - totalPages: n, + totalPages: t, totalCount: r, - onPageChange: s + onPageChange: o }) => { - const [l, i] = B(d(e)), [w, p] = B(!1), [m, o] = B(null), h = T(null), x = Array.from( - /* @__PURE__ */ new Set([1, e, e + 1, n]) - ).filter((t) => t >= 1 && t <= n).sort((t, a) => t - a); - g(() => { + const [a, i] = B(c(e)), [y, d] = B(!1), [m, s] = B(null), b = N(null), w = Array.from( + /* @__PURE__ */ new Set([1, e, e + 1, t]) + ).filter((n) => n >= 1 && n <= t).sort((n, f) => n - f); + S(() => { if (m !== null) { - e === m && (o(null), p(!1), i(d(e))); + e === m && (s(null), d(!1), i(c(e))); return; } - w || i(d(e)); - }, [e, w, m]), g(() => { - if (!w || !l || m !== null) return; - const t = parseInt(N(l), 10); - if (isNaN(t)) return; - const a = setTimeout(() => { - var b; - const f = Math.min(Math.max(t, 1), n); - f !== e ? (o(f), i(d(f)), s(f)) : (p(!1), i(d(e))), (b = h.current) == null || b.blur(); + y || i(c(e)); + }, [e, y, m]), S(() => { + if (!y || !a || m !== null) return; + const n = parseInt(k(a), 10); + if (isNaN(n)) return; + const f = setTimeout(() => { + var h; + const p = Math.min(Math.max(n, 1), t); + p !== e ? (s(p), i(c(p)), o(p)) : (d(!1), i(c(e))), (h = b.current) == null || h.blur(); }, 1e3); - return () => clearTimeout(a); + return () => clearTimeout(f); }, [ - l, - w, + a, + y, m, - n, + t, e, - s + o ]); const v = () => { - var f, b; - const t = parseInt(N(l), 10); - if (isNaN(t)) { - i(d(e)), p(!1), o(null), (f = h.current) == null || f.blur(); + var p, h; + const n = parseInt(k(a), 10); + if (isNaN(n)) { + i(c(e)), d(!1), s(null), (p = b.current) == null || p.blur(); return; } - const a = Math.min(Math.max(t, 1), n); - a !== e ? (o(a), i(d(a)), s(a)) : (i(d(e)), p(!1), o(null)), (b = h.current) == null || b.blur(); + const f = Math.min(Math.max(n, 1), t); + f !== e ? (s(f), i(c(f)), o(f)) : (i(c(e)), d(!1), s(null)), (h = b.current) == null || h.blur(); }; - return n === 0 || !r ? null : /* @__PURE__ */ k( - S, + return t === 0 || !r ? null : /* @__PURE__ */ E( + A, { justifyContent: "space-around", backgroundColor: "white", @@ -192,73 +192,73 @@ const fe = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), d = (e) => S borderColor: "blue.200", w: "100%", children: [ - /* @__PURE__ */ k(S, { spacing: 2, whiteSpace: "nowrap", children: [ - /* @__PURE__ */ c(A, { fontWeight: "bold", children: "صفحه" }), - /* @__PURE__ */ c(A, { children: d(e) }), - /* @__PURE__ */ c(A, { fontWeight: "bold", children: "از" }), - /* @__PURE__ */ c(A, { children: d(n) }) + /* @__PURE__ */ E(A, { spacing: 2, whiteSpace: "nowrap", children: [ + /* @__PURE__ */ l(D, { fontWeight: "bold", children: "صفحه" }), + /* @__PURE__ */ l(D, { children: c(e) }), + /* @__PURE__ */ l(D, { fontWeight: "bold", children: "از" }), + /* @__PURE__ */ l(D, { children: c(t) }) ] }), - /* @__PURE__ */ k(S, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ - /* @__PURE__ */ c( - M, + /* @__PURE__ */ E(A, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ + /* @__PURE__ */ l( + K, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, - onClick: () => s(e - 1), + onClick: () => o(e - 1), isDisabled: e === 1, - icon: /* @__PURE__ */ c(V, {}), + icon: /* @__PURE__ */ l(L, {}), "aria-label": "صفحه قبلی" } ), - x.map((t) => /* @__PURE__ */ c( - _, + w.map((n) => /* @__PURE__ */ l( + V, { size: "md", - bg: e === t ? "#4B49AC" : "#e6e6f1", - color: e === t ? "#e6e6f1" : "#4B49AC", - _hover: { bg: e === t ? "#4B49AC" : "#cbcbe8" }, - onClick: () => s(t), + bg: e === n ? "#4B49AC" : "#e6e6f1", + color: e === n ? "#e6e6f1" : "#4B49AC", + _hover: { bg: e === n ? "#4B49AC" : "#cbcbe8" }, + onClick: () => o(n), variant: "soft", - children: d(t) + children: c(n) }, - t + n )), - /* @__PURE__ */ c( - M, + /* @__PURE__ */ l( + K, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, - onClick: () => s(e + 1), - isDisabled: e === n, - icon: /* @__PURE__ */ c(j, {}), + onClick: () => o(e + 1), + isDisabled: e === t, + icon: /* @__PURE__ */ l(R, {}), "aria-label": "صفحه بعدی" } ), - /* @__PURE__ */ c( - F, + /* @__PURE__ */ l( + j, { - ref: h, + ref: b, type: "text", inputMode: "numeric", placeholder: "صفحه", w: "100px", - value: l, + value: a, onFocus: () => { - p(!0), o(null), i(""); + d(!0), s(null), i(""); }, - onChange: (t) => { - const a = t.target.value, f = N(a).replace(/\D/g, ""); - i(d(f)); + onChange: (n) => { + const f = n.target.value, p = k(f).replace(/\D/g, ""); + i(c(p)); }, onBlur: () => { - l || (i(d(e)), p(!1), o(null)); + a || (i(c(e)), d(!1), s(null)); }, - onKeyDown: (t) => { - t.key === "Enter" && v(); + onKeyDown: (n) => { + n.key === "Enter" && v(); }, textAlign: "center", color: "#1D2939", @@ -268,69 +268,69 @@ const fe = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), d = (e) => S } ) ] }), - /* @__PURE__ */ k(S, { spacing: 2, whiteSpace: "nowrap", children: [ - /* @__PURE__ */ c(A, { fontWeight: "bold", children: "تعداد کل:" }), - /* @__PURE__ */ c(A, { children: d(r) }) + /* @__PURE__ */ E(A, { spacing: 2, whiteSpace: "nowrap", children: [ + /* @__PURE__ */ l(D, { fontWeight: "bold", children: "تعداد کل:" }), + /* @__PURE__ */ l(D, { children: c(r) }) ] }) ] } ); -}, me = ({ +}, be = ({ currentPage: e, - totalPages: n, + totalPages: t, totalCount: r, - onPageChange: s + onPageChange: o }) => { - const [l, i] = B(""), [w, p] = B(!1), [m, o] = B(null), h = T(null), x = m ?? e, v = D( - (y) => { - const C = Math.min(Math.max(y, 1), n); - p(!1), i(""), o(null), C !== e && s(C); + const [a, i] = B(""), [y, d] = B(!1), [m, s] = B(null), b = N(null), w = m ?? e, v = I( + (x) => { + const C = Math.min(Math.max(x, 1), t); + d(!1), i(""), s(null), C !== e && o(C); }, - [e, n, s] - ), t = D(() => { - if (!l) { - p(!1), i(""), o(null); + [e, t, o] + ), n = I(() => { + if (!a) { + d(!1), i(""), s(null); return; } - const y = parseInt(N(l), 10); - if (isNaN(y)) { - p(!1), i(""), o(null); + const x = parseInt(k(a), 10); + if (isNaN(x)) { + d(!1), i(""), s(null); return; } - const C = Math.min(Math.max(y, 1), n); - p(!1), i(""), C !== e ? (o(C), s(C)) : o(null); - }, [l, e, n, s]); - g(() => { - var y; - w && ((y = h.current) == null || y.focus()); - }, [w]), g(() => { - m !== null && e === m && o(null); - }, [e, m]), g(() => { - if (!w || !l) return; - const y = setTimeout(() => { - t(); + const C = Math.min(Math.max(x, 1), t); + d(!1), i(""), C !== e ? (s(C), o(C)) : s(null); + }, [a, e, t, o]); + S(() => { + var x; + y && ((x = b.current) == null || x.focus()); + }, [y]), S(() => { + m !== null && e === m && s(null); + }, [e, m]), S(() => { + if (!y || !a) return; + const x = setTimeout(() => { + n(); }, 1e3); - return () => clearTimeout(y); - }, [l, w, t]); - const a = () => { - o(null), i(""), p(!0); - }, f = (y, C = !1) => /* @__PURE__ */ c( - _, + return () => clearTimeout(x); + }, [a, y, n]); + const f = () => { + s(null), i(""), d(!0); + }, p = (x, C = !1) => /* @__PURE__ */ l( + V, { size: "md", bg: C ? "#4B49AC" : "#e6e6f1", color: C ? "#e6e6f1" : "#4B49AC", _hover: { bg: C ? "#4B49AC" : "#cbcbe8" }, - onClick: C ? a : () => v(y), + onClick: C ? f : () => v(x), variant: "soft", - children: d(y) + children: c(x) }, - y + x ); - if (n === 0 || !r) return null; - const b = n > 1 && x !== 1, E = n > 1 && x !== n; - return /* @__PURE__ */ c( - S, + if (t === 0 || !r) return null; + const h = t > 1 && w !== 1, M = t > 1 && w !== t; + return /* @__PURE__ */ l( + A, { justifyContent: "center", backgroundColor: "white", @@ -340,39 +340,39 @@ const fe = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), d = (e) => S border: "1px", borderColor: "blue.200", w: "100%", - children: /* @__PURE__ */ k(S, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ - /* @__PURE__ */ c( - M, + children: /* @__PURE__ */ E(A, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ + /* @__PURE__ */ l( + K, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, - onClick: () => v(x - 1), - isDisabled: x === 1, - icon: /* @__PURE__ */ c(V, {}), + onClick: () => v(w - 1), + isDisabled: w === 1, + icon: /* @__PURE__ */ l(L, {}), "aria-label": "صفحه قبلی" } ), - b && f(1), - w ? /* @__PURE__ */ c( - F, + h && p(1), + y ? /* @__PURE__ */ l( + j, { - ref: h, + ref: b, type: "text", inputMode: "numeric", placeholder: "صفحه", w: "48px", - value: l, - onChange: (y) => { - const C = y.target.value, q = N(C).replace(/\D/g, ""); - i(d(q)); + value: a, + onChange: (x) => { + const C = x.target.value, J = k(C).replace(/\D/g, ""); + i(c(J)); }, onBlur: () => { - l ? t() : (i(""), p(!1), o(null)); + a ? n() : (i(""), d(!1), s(null)); }, - onKeyDown: (y) => { - y.key === "Enter" && t(), y.key === "Escape" && (i(""), p(!1), o(null)); + onKeyDown: (x) => { + x.key === "Enter" && n(), x.key === "Escape" && (i(""), d(!1), s(null)); }, textAlign: "center", color: "#1D2939", @@ -380,18 +380,18 @@ const fe = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), d = (e) => S minW: 0, dir: "ltr" } - ) : f(x, !0), - E && f(n), - /* @__PURE__ */ c( - M, + ) : p(w, !0), + M && p(t), + /* @__PURE__ */ l( + K, { size: "md", bg: "#e6e6f1", color: "#4B49AC", _hover: { bg: "#cbcbe8" }, - onClick: () => v(x + 1), - isDisabled: x === n, - icon: /* @__PURE__ */ c(j, {}), + onClick: () => v(w + 1), + isDisabled: w === t, + icon: /* @__PURE__ */ l(R, {}), "aria-label": "صفحه بعدی" } ) @@ -399,195 +399,185 @@ const fe = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), d = (e) => S } ); }; -let u = null, z = null, K = !1; -const $ = W(null), H = ({ url: e, realm: n, clientId: r }) => (u || (u = new J({ +let u = null, z = null, F = !1; +const $ = O(null), g = ({ url: e, realm: t, clientId: r }) => (u || (u = new U({ url: e, - realm: n, + realm: t, clientId: r -})), u), Y = ({ config: e, initOptions: n = {} }) => { - const r = H(e); +})), u), H = ({ config: e, initOptions: t = {} }) => { + const r = g(e); return z || (z = r.init({ onLoad: "login-required", checkLoginIframe: !1, flow: "implicit", - ...n - }).then((s) => (K = !0, s)).catch((s) => { - throw K = !0, s; + ...t + }).then((o) => (F = !0, o)).catch((o) => { + throw F = !0, o; })), z; -}, he = () => K, U = () => !!(u != null && u.authenticated), X = () => (u == null ? void 0 : u.token) ?? null, be = async (e = 30) => u ? u.updateToken(e) : !1, Z = (e) => u == null ? void 0 : u.logout(e), we = (e) => u == null ? void 0 : u.login(e), xe = ({ +}, we = () => F, X = () => !!(u != null && u.authenticated), Z = () => (u == null ? void 0 : u.token) ?? null, ye = async (e = 30) => u ? u.updateToken(e) : !1, P = (e) => u == null ? void 0 : u.logout(e), xe = (e) => u == null ? void 0 : u.login(e), ve = ({ children: e, - config: n, + config: t, initOptions: r, - loading: s = null + loading: o = null }) => { - const [l, i] = B(!1), [w, p] = B(!1), [m, o] = B(null), h = T(!1), x = D(() => { - u && (p(!!u.authenticated), o(u.token ?? null)); + const [a, i] = B(!1), [y, d] = B(!1), [m, s] = B(null), b = N(!1), w = I(() => { + u && (d(!!u.authenticated), s(u.token ?? null)); }, []); - g(() => (h.current = !0, Y({ config: n, initOptions: r }).then((b) => { - h.current && (p(!!b), o((u == null ? void 0 : u.token) ?? null), i(!0)); + S(() => (b.current = !0, H({ config: t, initOptions: r }).then((h) => { + b.current && (d(!!h), s((u == null ? void 0 : u.token) ?? null), i(!0)); }).catch(() => { - h.current && (p(!1), o(null), i(!0)); + b.current && (d(!1), s(null), i(!0)); }), () => { - h.current = !1; - }), [n, r]); - const v = D(() => u == null ? void 0 : u.login(), []), t = D((b) => u == null ? void 0 : u.logout(b), []), a = D( - async (b = 30) => { + b.current = !1; + }), [t, r]); + const v = I(() => u == null ? void 0 : u.login(), []), n = I((h) => u == null ? void 0 : u.logout(h), []), f = I( + async (h = 30) => { if (!u) return !1; - const E = await u.updateToken(b); - return x(), E; + const M = await u.updateToken(h); + return w(), M; }, - [x] - ), f = I( + [w] + ), p = _( () => ({ - initialized: l, - authenticated: w, + initialized: a, + authenticated: y, token: m, login: v, - logout: t, - updateToken: a + logout: n, + updateToken: f }), - [l, w, m, v, t, a] + [a, y, m, v, n, f] ); - return l ? /* @__PURE__ */ c($.Provider, { value: f, children: e }) : s; -}, R = W({ - user: null, - can: () => !1, - cannot: () => !0, - permissions: void 0, - isLoading: !1, - error: null -}), ye = ({ + return a ? /* @__PURE__ */ l($.Provider, { value: p, children: e }) : o; +}, q = O(null), T = "permissions", ee = (e) => (e == null ? void 0 : e.storageKey) || T, Ce = ({ user: e = null, - children: n, - clientId: r, - permissionsUrl: s, - storageKey: l = "permissions", - loading: i = /* @__PURE__ */ c("div", { children: "Loading permissions..." }), - errorFallback: w = /* @__PURE__ */ c("div", { children: "Error loading permissions" }) + children: t, + config: r, + loading: o = /* @__PURE__ */ l("div", { children: "Loading permissions..." }), + errorFallback: a = /* @__PURE__ */ l("div", { children: "Error loading permissions" }) }) => { - const [p, m] = B( - () => P(l) - ), { data: o, isLoading: h, error: x } = oe({ - clientId: r, - permissionsUrl: s - }), v = o ?? p; - g(() => { - o && (ee(l, o), m(o)); - }, [o, l]); - const t = I( - () => ne(v), + const i = ee(r), [y, d] = B( + () => te(i) + ), { data: m, isLoading: s, error: b } = se(r), w = m ?? y; + S(() => { + m && (ne(i, m), d(m)); + }, [m, i]); + const v = _( + () => ie(w), + [w] + ), n = I( + (h = "") => re(v, h), [v] - ), a = D( - (E = "") => te(t, E), - [t] - ), f = D((E = "") => !a(E), [a]), b = I( + ), f = I((h = "") => !n(h), [n]), p = _( () => ({ user: e, - can: a, + can: n, cannot: f, - permissions: v, - isLoading: h, - error: x + permissions: w, + isLoading: s, + error: b }), - [e, a, f, v, h, x] + [e, n, f, w, s, b] ); - return x && !v ? w : h && !v ? i : /* @__PURE__ */ c(R.Provider, { value: b, children: n }); -}, P = (e = "permissions") => { + return b && !w ? a : s && !w ? o : /* @__PURE__ */ l(q.Provider, { value: p, children: t }); +}, te = (e = T) => { if (!(typeof window > "u")) try { - const n = localStorage.getItem(e); - return n ? JSON.parse(n) : void 0; + const t = localStorage.getItem(e); + return t ? JSON.parse(t) : void 0; } catch { return; } -}, ee = (e = "permissions", n) => { +}, ne = (e = T, t) => { if (!(typeof window > "u")) try { - localStorage.setItem(e, JSON.stringify(n)); + localStorage.setItem(e, JSON.stringify(t)); } catch { } -}, ve = (e = "permissions") => { +}, Be = (e = T) => { if (!(typeof window > "u")) try { localStorage.removeItem(e); } catch { } -}, ne = (e) => e ? new Set(e.map((n) => n.name)) : /* @__PURE__ */ new Set(), te = (e, n = "") => { - if (!n) return !1; - if (e.has(n) || [...e].some((s) => s.startsWith(n))) return !0; - let r = n; +}, ie = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), re = (e, t = "") => { + if (!t) return !1; + if (e.has(t) || [...e].some((o) => o.startsWith(t))) return !0; + let r = t; for (; r.includes(":"); ) if (r = r.slice(0, r.lastIndexOf(":")), e.has(`${r}:*`)) return !0; return !1; -}, O = { +}, G = { all: ["permissions"], - list: (e) => [...O.all, e] -}, ie = async ({ permissionsUrl: e, clientId: n }) => { - const r = X(), s = new URL(e); - s.searchParams.set("client_id", n); - const l = await fetch(s.toString(), { + list: (e) => [...G.all, e] +}, oe = async (e) => { + if (!(e != null && e.url) || !(e != null && e.clientId)) + throw new Error("Permission config requires url and clientId."); + const t = Z(), r = new URL(e.url); + r.searchParams.set("client_id", e.clientId); + const o = await fetch(r.toString(), { method: "GET", headers: { - ...r ? { Authorization: `Bearer ${r}` } : {} + ...t ? { Authorization: `Bearer ${t}` } : {} } }); - if (!l.ok) + if (!o.ok) throw new Error("Failed to load permissions"); - return l.json(); -}, L = () => { - const e = G(R); + return o.json(); +}, W = () => { + const e = Q(q); if (!e) throw new Error("usePermission must be used within PermissionProvider"); return e; -}, oe = ({ clientId: e, permissionsUrl: n }) => Q({ - queryKey: O.list(e), - queryFn: () => ie({ clientId: e, permissionsUrl: n }), - enabled: !!e && !!n && U(), +}, se = (e) => Y({ + queryKey: G.list(e == null ? void 0 : e.clientId), + queryFn: () => oe(e), + enabled: !!(e != null && e.url) && !!(e != null && e.clientId) && X(), staleTime: 1 / 0, refetchOnWindowFocus: !1, refetchOnReconnect: !1 -}), Ce = ({ +}), Se = ({ perm: e, - children: n, - fallback: r = /* @__PURE__ */ c("div", { children: "You are not allowed to do this" }) + children: t, + fallback: r = /* @__PURE__ */ l("div", { children: "You are not allowed to do this" }) }) => { - const { can: s } = L(); - return s(e) ? n : r; -}, Be = ({ children: e }) => { - const { can: n, permissions: r } = L(); - return r && n("admin") ? e : null; -}, ge = ({ children: e }) => { - const { can: n, isLoading: r } = L(); - return g(() => { - r || n("panel_access") || Z(); - }, [r, n]), e; + const { can: o } = W(); + return o(e) ? t : r; +}, Ae = ({ children: e }) => { + const { can: t, permissions: r } = W(); + return r && t("admin") ? e : null; +}, De = ({ children: e }) => { + const { can: t, isLoading: r } = W(); + return S(() => { + r || t("panel_access") || P(); + }, [r, t]), e; }; export { - Be as AdminGuard, - Ce as Can, - xe as KeycloakProvider, - de as LightPagination, - pe as Pagination, - ge as PanelAccessGuard, - ye as PermissionProvider, - me as SimplePagination, - te as checkPermission, - ve as clearCachedPermissions, - ne as createPermissionSet, - P as getCachedPermissions, - X as getKeycloakToken, - ie as getPermissions, - Y as initKeycloak, - U as isKeycloakAuthenticated, - he as isKeycloakInitialized, - we as loginKeycloak, - Z as logoutKeycloak, - O as permissionKeys, - ee as setCachedPermissions, - N as toEnDigits, - d as toFaDigits, - fe as toFaNumber, - be as updateKeycloakToken, - L as usePermission, - oe as usePermissionsQuery + Ae as AdminGuard, + Se as Can, + ve as KeycloakProvider, + me as LightPagination, + he as Pagination, + De as PanelAccessGuard, + Ce as PermissionProvider, + be as SimplePagination, + re as checkPermission, + Be as clearCachedPermissions, + ie as createPermissionSet, + te as getCachedPermissions, + Z as getKeycloakToken, + oe as getPermissions, + H as initKeycloak, + X as isKeycloakAuthenticated, + we as isKeycloakInitialized, + xe as loginKeycloak, + P as logoutKeycloak, + G as permissionKeys, + ne as setCachedPermissions, + k as toEnDigits, + c as toFaDigits, + pe as toFaNumber, + ye as updateKeycloakToken, + W as usePermission, + se as usePermissionsQuery }; diff --git a/src/core/permission.jsx b/src/core/permission.jsx index c66b185..b294c9b 100644 --- a/src/core/permission.jsx +++ b/src/core/permission.jsx @@ -13,36 +13,30 @@ import { logoutKeycloak, } from "./keycloak.jsx"; -// contexts +const PermissionContext = createContext(null); -const PermissionContext = createContext({ - user: null, - can: () => false, - cannot: () => true, - permissions: undefined, - isLoading: false, - error: null, -}); +const DEFAULT_STORAGE_KEY = "permissions"; -// providers +const getPermissionStorageKey = (config) => { + return config?.storageKey || DEFAULT_STORAGE_KEY; +}; + +// provider export const PermissionProvider = ({ user = null, children, - clientId, - permissionsUrl, - storageKey = "permissions", + config, loading =