update permission

This commit is contained in:
Mohamadzadeh 2026-06-13 11:51:07 +03:30
parent 6d1006f784
commit 818331917c
2 changed files with 348 additions and 365 deletions

644
dist/index.js vendored
View File

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

View File

@ -13,36 +13,30 @@ import {
logoutKeycloak, logoutKeycloak,
} from "./keycloak.jsx"; } from "./keycloak.jsx";
// contexts const PermissionContext = createContext(null);
const PermissionContext = createContext({ const DEFAULT_STORAGE_KEY = "permissions";
user: null,
can: () => false,
cannot: () => true,
permissions: undefined,
isLoading: false,
error: null,
});
// providers const getPermissionStorageKey = (config) => {
return config?.storageKey || DEFAULT_STORAGE_KEY;
};
// provider
export const PermissionProvider = ({ export const PermissionProvider = ({
user = null, user = null,
children, children,
clientId, config,
permissionsUrl,
storageKey = "permissions",
loading = <div>Loading permissions...</div>, loading = <div>Loading permissions...</div>,
errorFallback = <div>Error loading permissions</div>, errorFallback = <div>Error loading permissions</div>,
}) => { }) => {
const storageKey = getPermissionStorageKey(config);
const [cachedPermissions, setCachedPermissionsState] = useState(() => const [cachedPermissions, setCachedPermissionsState] = useState(() =>
getCachedPermissions(storageKey), getCachedPermissions(storageKey),
); );
const { data, isLoading, error } = usePermissionsQuery({ const { data, isLoading, error } = usePermissionsQuery(config);
clientId,
permissionsUrl,
});
const permissions = data ?? cachedPermissions; const permissions = data ?? cachedPermissions;
@ -77,13 +71,8 @@ export const PermissionProvider = ({
[user, can, cannot, permissions, isLoading, error], [user, can, cannot, permissions, isLoading, error],
); );
if (error && !permissions) { if (error && !permissions) return errorFallback;
return errorFallback; if (isLoading && !permissions) return loading;
}
if (isLoading && !permissions) {
return loading;
}
return ( return (
<PermissionContext.Provider value={value}> <PermissionContext.Provider value={value}>
@ -92,9 +81,9 @@ export const PermissionProvider = ({
); );
}; };
// storages // storage
export const getCachedPermissions = (storageKey = "permissions") => { export const getCachedPermissions = (storageKey = DEFAULT_STORAGE_KEY) => {
if (typeof window === "undefined") return undefined; if (typeof window === "undefined") return undefined;
try { try {
@ -106,7 +95,7 @@ export const getCachedPermissions = (storageKey = "permissions") => {
}; };
export const setCachedPermissions = ( export const setCachedPermissions = (
storageKey = "permissions", storageKey = DEFAULT_STORAGE_KEY,
permissions, permissions,
) => { ) => {
if (typeof window === "undefined") return; if (typeof window === "undefined") return;
@ -116,7 +105,7 @@ export const setCachedPermissions = (
} catch {} } catch {}
}; };
export const clearCachedPermissions = (storageKey = "permissions") => { export const clearCachedPermissions = (storageKey = DEFAULT_STORAGE_KEY) => {
if (typeof window === "undefined") return; if (typeof window === "undefined") return;
try { try {
@ -157,13 +146,17 @@ export const permissionKeys = {
list: (clientId) => [...permissionKeys.all, clientId], list: (clientId) => [...permissionKeys.all, clientId],
}; };
// apis // api
export const getPermissions = async (config) => {
if (!config?.url || !config?.clientId) {
throw new Error("Permission config requires url and clientId.");
}
export const getPermissions = async ({ permissionsUrl, clientId }) => {
const token = getKeycloakToken(); const token = getKeycloakToken();
const url = new URL(permissionsUrl); const url = new URL(config.url);
url.searchParams.set("client_id", clientId); url.searchParams.set("client_id", config.clientId);
const response = await fetch(url.toString(), { const response = await fetch(url.toString(), {
method: "GET", method: "GET",
@ -191,13 +184,13 @@ export const usePermission = () => {
return context; return context;
}; };
export const usePermissionsQuery = ({ clientId, permissionsUrl }) => { export const usePermissionsQuery = (config) => {
return useQuery({ return useQuery({
queryKey: permissionKeys.list(clientId), queryKey: permissionKeys.list(config?.clientId),
queryFn: () => getPermissions({ clientId, permissionsUrl }), queryFn: () => getPermissions(config),
enabled: enabled:
Boolean(clientId) && Boolean(config?.url) &&
Boolean(permissionsUrl) && Boolean(config?.clientId) &&
isKeycloakAuthenticated(), isKeycloakAuthenticated(),
staleTime: Infinity, staleTime: Infinity,
refetchOnWindowFocus: false, refetchOnWindowFocus: false,
@ -237,4 +230,4 @@ export const PanelAccessGuard = ({ children }) => {
}, [isLoading, can]); }, [isLoading, can]);
return children; return children;
}; };