add BiProvider

This commit is contained in:
Mohamadzadeh 2026-06-13 18:21:22 +03:30
parent 99c0c27f9a
commit 7fd56c4600
4 changed files with 298 additions and 241 deletions

452
dist/index.js vendored
View File

@ -1,55 +1,55 @@
import { jsxs as B, jsx as a, Fragment as Z } from "react/jsx-runtime"; import { jsxs as I, jsx as a, Fragment as Z } from "react/jsx-runtime";
import { useState as v, useRef as N, useEffect as C, useCallback as E, createContext as $, useMemo as z, useContext as P } from "react"; import { useState as v, useRef as N, useEffect as C, useCallback as E, createContext as $, useMemo as _, useContext as P } from "react";
import { HStack as I, IconButton as D, Button as L, Input as U, Text as S, Spinner as ee } from "@chakra-ui/react"; import { HStack as k, IconButton as K, Button as R, Input as L, Text as S, Spinner as ee } from "@chakra-ui/react";
import { IoChevronForwardOutline as g, IoChevronBackOutline as O } from "react-icons/io5"; import { IoChevronForwardOutline as O, IoChevronBackOutline as W } from "react-icons/io5";
import te from "keycloak-js"; import te from "keycloak-js";
import { useQuery as ne } from "@tanstack/react-query"; import { useQuery as ne } from "@tanstack/react-query";
import re from "axios"; 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)]), K = (e) => String(e).replace(/[۰-۹]/g, (n) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(n))), Ke = ({ 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 = ({
currentPage: e, currentPage: e,
totalPages: t, totalPages: t,
totalCount: n, totalCount: n,
onPageChange: s onPageChange: l
}) => { }) => {
const [c, l] = v(p(e)), [i, o] = v(!1), [f, u] = v(null), w = N(null), y = [.../* @__PURE__ */ new Set([1, e, t])].filter( 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(
(r) => r >= 1 && r <= t (r) => r >= 1 && r <= t
); );
C(() => { C(() => {
if (f !== null) { if (d !== null) {
e === f && (u(null), o(!1), l(p(e))); e === d && (c(null), o(!1), s(p(e)));
return; return;
} }
i || l(p(e)); i || s(p(e));
}, [e, i, f]), C(() => { }, [e, i, d]), C(() => {
if (!i || !c || f !== null) return; if (!i || !u || d !== null) return;
const r = parseInt(K(c), 10); const r = parseInt(D(u), 10);
if (isNaN(r)) return; if (isNaN(r)) return;
const h = setTimeout(() => { const h = setTimeout(() => {
var b; var w;
const m = Math.min(Math.max(r, 1), t); const m = Math.min(Math.max(r, 1), t);
m !== e ? (u(m), l(p(m)), s(m)) : (o(!1), l(p(e))), (b = w.current) == null || b.blur(); m !== e ? (c(m), s(p(m)), l(m)) : (o(!1), s(p(e))), (w = b.current) == null || w.blur();
}, 1e3); }, 1e3);
return () => clearTimeout(h); return () => clearTimeout(h);
}, [ }, [
c, u,
i, i,
f, d,
t, t,
e, e,
s l
]); ]);
const A = () => { const A = () => {
var m, b; var m, w;
const r = parseInt(K(c), 10); const r = parseInt(D(u), 10);
if (isNaN(r)) { if (isNaN(r)) {
l(p(e)), o(!1), u(null), (m = w.current) == null || m.blur(); s(p(e)), o(!1), c(null), (m = b.current) == null || m.blur();
return; return;
} }
const h = Math.min(Math.max(r, 1), t); const h = Math.min(Math.max(r, 1), t);
h !== e ? (u(h), l(p(h)), s(h)) : (l(p(e)), o(!1), u(null)), (b = w.current) == null || b.blur(); h !== e ? (c(h), s(p(h)), l(h)) : (s(p(e)), o(!1), c(null)), (w = b.current) == null || w.blur();
}; };
return t === 0 || !n ? null : /* @__PURE__ */ B( return t === 0 || !n ? null : /* @__PURE__ */ I(
I, k,
{ {
justifyContent: "space-around", justifyContent: "space-around",
backgroundColor: "white", backgroundColor: "white",
@ -60,65 +60,65 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
borderColor: "blue.200", borderColor: "blue.200",
w: "100%", w: "100%",
children: [ children: [
/* @__PURE__ */ B(I, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ /* @__PURE__ */ I(k, { spacing: 2, alignItems: "center", justifyContent: "center", children: [
/* @__PURE__ */ a( /* @__PURE__ */ a(
D, K,
{ {
size: "md", size: "md",
bg: "#e6e6f1", bg: "#e6e6f1",
color: "#4B49AC", color: "#4B49AC",
_hover: { bg: "#cbcbe8" }, _hover: { bg: "#cbcbe8" },
onClick: () => s(e - 1), onClick: () => l(e - 1),
isDisabled: e === 1, isDisabled: e === 1,
icon: /* @__PURE__ */ a(g, {}), icon: /* @__PURE__ */ a(O, {}),
"aria-label": "صفحه قبلی" "aria-label": "صفحه قبلی"
} }
), ),
y.map((r) => /* @__PURE__ */ a( y.map((r) => /* @__PURE__ */ a(
L, R,
{ {
size: "md", size: "md",
px: 2, px: 2,
bg: e === r ? "#4B49AC" : "#e6e6f1", bg: e === r ? "#4B49AC" : "#e6e6f1",
color: e === r ? "#e6e6f1" : "#4B49AC", color: e === r ? "#e6e6f1" : "#4B49AC",
_hover: { bg: e === r ? "#4B49AC" : "#cbcbe8" }, _hover: { bg: e === r ? "#4B49AC" : "#cbcbe8" },
onClick: () => s(r), onClick: () => l(r),
variant: "soft", variant: "soft",
children: p(r) children: p(r)
}, },
r r
)), )),
/* @__PURE__ */ a( /* @__PURE__ */ a(
D, K,
{ {
size: "md", size: "md",
bg: "#e6e6f1", bg: "#e6e6f1",
color: "#4B49AC", color: "#4B49AC",
_hover: { bg: "#cbcbe8" }, _hover: { bg: "#cbcbe8" },
onClick: () => s(e + 1), onClick: () => l(e + 1),
isDisabled: e === t, isDisabled: e === t,
icon: /* @__PURE__ */ a(O, {}), icon: /* @__PURE__ */ a(W, {}),
"aria-label": "صفحه بعدی" "aria-label": "صفحه بعدی"
} }
), ),
/* @__PURE__ */ a( /* @__PURE__ */ a(
U, L,
{ {
ref: w, ref: b,
type: "text", type: "text",
inputMode: "numeric", inputMode: "numeric",
placeholder: "صفحه", placeholder: "صفحه",
w: "56px", w: "56px",
value: c, value: u,
onFocus: () => { onFocus: () => {
o(!0), u(null), l(""); o(!0), c(null), s("");
}, },
onChange: (r) => { onChange: (r) => {
const h = r.target.value, m = K(h).replace(/\D/g, ""); const h = r.target.value, m = D(h).replace(/\D/g, "");
l(p(m)); s(p(m));
}, },
onBlur: () => { onBlur: () => {
c || (l(p(e)), o(!1), u(null)); u || (s(p(e)), o(!1), c(null));
}, },
onKeyDown: (r) => { onKeyDown: (r) => {
r.key === "Enter" && A(); r.key === "Enter" && A();
@ -131,58 +131,58 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
} }
) )
] }), ] }),
/* @__PURE__ */ B(I, { spacing: 2, children: [ /* @__PURE__ */ I(k, { spacing: 2, children: [
/* @__PURE__ */ a(S, { w: "auto", fontWeight: "bold", whiteSpace: "nowrap", children: "تعداد کل:" }), /* @__PURE__ */ a(S, { w: "auto", fontWeight: "bold", whiteSpace: "nowrap", children: "تعداد کل:" }),
/* @__PURE__ */ a(S, { children: p(n) }) /* @__PURE__ */ a(S, { children: p(n) })
] }) ] })
] ]
} }
); );
}, De = ({ }, Ke = ({
currentPage: e, currentPage: e,
totalPages: t, totalPages: t,
totalCount: n, totalCount: n,
onPageChange: s onPageChange: l
}) => { }) => {
const [c, l] = v(p(e)), [i, o] = v(!1), [f, u] = v(null), w = N(null), y = Array.from( const [u, s] = v(p(e)), [i, o] = v(!1), [d, c] = v(null), b = N(null), y = Array.from(
/* @__PURE__ */ new Set([1, e, e + 1, t]) /* @__PURE__ */ new Set([1, e, e + 1, t])
).filter((r) => r >= 1 && r <= t).sort((r, h) => r - h); ).filter((r) => r >= 1 && r <= t).sort((r, h) => r - h);
C(() => { C(() => {
if (f !== null) { if (d !== null) {
e === f && (u(null), o(!1), l(p(e))); e === d && (c(null), o(!1), s(p(e)));
return; return;
} }
i || l(p(e)); i || s(p(e));
}, [e, i, f]), C(() => { }, [e, i, d]), C(() => {
if (!i || !c || f !== null) return; if (!i || !u || d !== null) return;
const r = parseInt(K(c), 10); const r = parseInt(D(u), 10);
if (isNaN(r)) return; if (isNaN(r)) return;
const h = setTimeout(() => { const h = setTimeout(() => {
var b; var w;
const m = Math.min(Math.max(r, 1), t); const m = Math.min(Math.max(r, 1), t);
m !== e ? (u(m), l(p(m)), s(m)) : (o(!1), l(p(e))), (b = w.current) == null || b.blur(); m !== e ? (c(m), s(p(m)), l(m)) : (o(!1), s(p(e))), (w = b.current) == null || w.blur();
}, 1e3); }, 1e3);
return () => clearTimeout(h); return () => clearTimeout(h);
}, [ }, [
c, u,
i, i,
f, d,
t, t,
e, e,
s l
]); ]);
const A = () => { const A = () => {
var m, b; var m, w;
const r = parseInt(K(c), 10); const r = parseInt(D(u), 10);
if (isNaN(r)) { if (isNaN(r)) {
l(p(e)), o(!1), u(null), (m = w.current) == null || m.blur(); s(p(e)), o(!1), c(null), (m = b.current) == null || m.blur();
return; return;
} }
const h = Math.min(Math.max(r, 1), t); const h = Math.min(Math.max(r, 1), t);
h !== e ? (u(h), l(p(h)), s(h)) : (l(p(e)), o(!1), u(null)), (b = w.current) == null || b.blur(); h !== e ? (c(h), s(p(h)), l(h)) : (s(p(e)), o(!1), c(null)), (w = b.current) == null || w.blur();
}; };
return t === 0 || !n ? null : /* @__PURE__ */ B( return t === 0 || !n ? null : /* @__PURE__ */ I(
I, k,
{ {
justifyContent: "space-around", justifyContent: "space-around",
backgroundColor: "white", backgroundColor: "white",
@ -193,70 +193,70 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
borderColor: "blue.200", borderColor: "blue.200",
w: "100%", w: "100%",
children: [ children: [
/* @__PURE__ */ B(I, { spacing: 2, whiteSpace: "nowrap", children: [ /* @__PURE__ */ I(k, { spacing: 2, whiteSpace: "nowrap", children: [
/* @__PURE__ */ a(S, { fontWeight: "bold", children: "صفحه" }), /* @__PURE__ */ a(S, { fontWeight: "bold", children: "صفحه" }),
/* @__PURE__ */ a(S, { children: p(e) }), /* @__PURE__ */ a(S, { children: p(e) }),
/* @__PURE__ */ a(S, { fontWeight: "bold", children: "از" }), /* @__PURE__ */ a(S, { fontWeight: "bold", children: "از" }),
/* @__PURE__ */ a(S, { children: p(t) }) /* @__PURE__ */ a(S, { children: p(t) })
] }), ] }),
/* @__PURE__ */ B(I, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ /* @__PURE__ */ I(k, { spacing: 2, alignItems: "center", justifyContent: "center", children: [
/* @__PURE__ */ a( /* @__PURE__ */ a(
D, K,
{ {
size: "md", size: "md",
bg: "#e6e6f1", bg: "#e6e6f1",
color: "#4B49AC", color: "#4B49AC",
_hover: { bg: "#cbcbe8" }, _hover: { bg: "#cbcbe8" },
onClick: () => s(e - 1), onClick: () => l(e - 1),
isDisabled: e === 1, isDisabled: e === 1,
icon: /* @__PURE__ */ a(g, {}), icon: /* @__PURE__ */ a(O, {}),
"aria-label": "صفحه قبلی" "aria-label": "صفحه قبلی"
} }
), ),
y.map((r) => /* @__PURE__ */ a( y.map((r) => /* @__PURE__ */ a(
L, R,
{ {
size: "md", size: "md",
bg: e === r ? "#4B49AC" : "#e6e6f1", bg: e === r ? "#4B49AC" : "#e6e6f1",
color: e === r ? "#e6e6f1" : "#4B49AC", color: e === r ? "#e6e6f1" : "#4B49AC",
_hover: { bg: e === r ? "#4B49AC" : "#cbcbe8" }, _hover: { bg: e === r ? "#4B49AC" : "#cbcbe8" },
onClick: () => s(r), onClick: () => l(r),
variant: "soft", variant: "soft",
children: p(r) children: p(r)
}, },
r r
)), )),
/* @__PURE__ */ a( /* @__PURE__ */ a(
D, K,
{ {
size: "md", size: "md",
bg: "#e6e6f1", bg: "#e6e6f1",
color: "#4B49AC", color: "#4B49AC",
_hover: { bg: "#cbcbe8" }, _hover: { bg: "#cbcbe8" },
onClick: () => s(e + 1), onClick: () => l(e + 1),
isDisabled: e === t, isDisabled: e === t,
icon: /* @__PURE__ */ a(O, {}), icon: /* @__PURE__ */ a(W, {}),
"aria-label": "صفحه بعدی" "aria-label": "صفحه بعدی"
} }
), ),
/* @__PURE__ */ a( /* @__PURE__ */ a(
U, L,
{ {
ref: w, ref: b,
type: "text", type: "text",
inputMode: "numeric", inputMode: "numeric",
placeholder: "صفحه", placeholder: "صفحه",
w: "100px", w: "100px",
value: c, value: u,
onFocus: () => { onFocus: () => {
o(!0), u(null), l(""); o(!0), c(null), s("");
}, },
onChange: (r) => { onChange: (r) => {
const h = r.target.value, m = K(h).replace(/\D/g, ""); const h = r.target.value, m = D(h).replace(/\D/g, "");
l(p(m)); s(p(m));
}, },
onBlur: () => { onBlur: () => {
c || (l(p(e)), o(!1), u(null)); u || (s(p(e)), o(!1), c(null));
}, },
onKeyDown: (r) => { onKeyDown: (r) => {
r.key === "Enter" && A(); r.key === "Enter" && A();
@ -269,7 +269,7 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
} }
) )
] }), ] }),
/* @__PURE__ */ B(I, { spacing: 2, whiteSpace: "nowrap", children: [ /* @__PURE__ */ I(k, { spacing: 2, whiteSpace: "nowrap", children: [
/* @__PURE__ */ a(S, { fontWeight: "bold", children: "تعداد کل:" }), /* @__PURE__ */ a(S, { fontWeight: "bold", children: "تعداد کل:" }),
/* @__PURE__ */ a(S, { children: p(n) }) /* @__PURE__ */ a(S, { children: p(n) })
] }) ] })
@ -280,58 +280,58 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
currentPage: e, currentPage: e,
totalPages: t, totalPages: t,
totalCount: n, totalCount: n,
onPageChange: s onPageChange: l
}) => { }) => {
const [c, l] = v(""), [i, o] = v(!1), [f, u] = v(null), w = N(null), y = f ?? e, A = E( const [u, s] = v(""), [i, o] = v(!1), [d, c] = v(null), b = N(null), y = d ?? e, A = E(
(x) => { (x) => {
const k = Math.min(Math.max(x, 1), t); const B = Math.min(Math.max(x, 1), t);
o(!1), l(""), u(null), k !== e && s(k); o(!1), s(""), c(null), B !== e && l(B);
}, },
[e, t, s] [e, t, l]
), r = E(() => { ), r = E(() => {
if (!c) { if (!u) {
o(!1), l(""), u(null); o(!1), s(""), c(null);
return; return;
} }
const x = parseInt(K(c), 10); const x = parseInt(D(u), 10);
if (isNaN(x)) { if (isNaN(x)) {
o(!1), l(""), u(null); o(!1), s(""), c(null);
return; return;
} }
const k = Math.min(Math.max(x, 1), t); const B = Math.min(Math.max(x, 1), t);
o(!1), l(""), k !== e ? (u(k), s(k)) : u(null); o(!1), s(""), B !== e ? (c(B), l(B)) : c(null);
}, [c, e, t, s]); }, [u, e, t, l]);
C(() => { C(() => {
var x; var x;
i && ((x = w.current) == null || x.focus()); i && ((x = b.current) == null || x.focus());
}, [i]), C(() => { }, [i]), C(() => {
f !== null && e === f && u(null); d !== null && e === d && c(null);
}, [e, f]), C(() => { }, [e, d]), C(() => {
if (!i || !c) return; if (!i || !u) return;
const x = setTimeout(() => { const x = setTimeout(() => {
r(); r();
}, 1e3); }, 1e3);
return () => clearTimeout(x); return () => clearTimeout(x);
}, [c, i, r]); }, [u, i, r]);
const h = () => { const h = () => {
u(null), l(""), o(!0); c(null), s(""), o(!0);
}, m = (x, k = !1) => /* @__PURE__ */ a( }, m = (x, B = !1) => /* @__PURE__ */ a(
L, R,
{ {
size: "md", size: "md",
bg: k ? "#4B49AC" : "#e6e6f1", bg: B ? "#4B49AC" : "#e6e6f1",
color: k ? "#e6e6f1" : "#4B49AC", color: B ? "#e6e6f1" : "#4B49AC",
_hover: { bg: k ? "#4B49AC" : "#cbcbe8" }, _hover: { bg: B ? "#4B49AC" : "#cbcbe8" },
onClick: k ? h : () => A(x), onClick: B ? h : () => A(x),
variant: "soft", variant: "soft",
children: p(x) children: p(x)
}, },
x x
); );
if (t === 0 || !n) return null; if (t === 0 || !n) return null;
const b = t > 1 && y !== 1, F = t > 1 && y !== t; const w = t > 1 && y !== 1, g = t > 1 && y !== t;
return /* @__PURE__ */ a( return /* @__PURE__ */ a(
I, k,
{ {
justifyContent: "center", justifyContent: "center",
backgroundColor: "white", backgroundColor: "white",
@ -341,9 +341,9 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
border: "1px", border: "1px",
borderColor: "blue.200", borderColor: "blue.200",
w: "100%", w: "100%",
children: /* @__PURE__ */ B(I, { spacing: 2, alignItems: "center", justifyContent: "center", children: [ children: /* @__PURE__ */ I(k, { spacing: 2, alignItems: "center", justifyContent: "center", children: [
/* @__PURE__ */ a( /* @__PURE__ */ a(
D, K,
{ {
size: "md", size: "md",
bg: "#e6e6f1", bg: "#e6e6f1",
@ -351,29 +351,29 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
_hover: { bg: "#cbcbe8" }, _hover: { bg: "#cbcbe8" },
onClick: () => A(y - 1), onClick: () => A(y - 1),
isDisabled: y === 1, isDisabled: y === 1,
icon: /* @__PURE__ */ a(g, {}), icon: /* @__PURE__ */ a(O, {}),
"aria-label": "صفحه قبلی" "aria-label": "صفحه قبلی"
} }
), ),
b && m(1), w && m(1),
i ? /* @__PURE__ */ a( i ? /* @__PURE__ */ a(
U, L,
{ {
ref: w, ref: b,
type: "text", type: "text",
inputMode: "numeric", inputMode: "numeric",
placeholder: "صفحه", placeholder: "صفحه",
w: "48px", w: "48px",
value: c, value: u,
onChange: (x) => { onChange: (x) => {
const k = x.target.value, X = K(k).replace(/\D/g, ""); const B = x.target.value, X = D(B).replace(/\D/g, "");
l(p(X)); s(p(X));
}, },
onBlur: () => { onBlur: () => {
c ? r() : (l(""), o(!1), u(null)); u ? r() : (s(""), o(!1), c(null));
}, },
onKeyDown: (x) => { onKeyDown: (x) => {
x.key === "Enter" && r(), x.key === "Escape" && (l(""), o(!1), u(null)); x.key === "Enter" && r(), x.key === "Escape" && (s(""), o(!1), c(null));
}, },
textAlign: "center", textAlign: "center",
color: "#1D2939", color: "#1D2939",
@ -382,9 +382,9 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
dir: "ltr" dir: "ltr"
} }
) : m(y, !0), ) : m(y, !0),
F && m(t), g && m(t),
/* @__PURE__ */ a( /* @__PURE__ */ a(
D, K,
{ {
size: "md", size: "md",
bg: "#e6e6f1", bg: "#e6e6f1",
@ -392,7 +392,7 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
_hover: { bg: "#cbcbe8" }, _hover: { bg: "#cbcbe8" },
onClick: () => A(y + 1), onClick: () => A(y + 1),
isDisabled: y === t, isDisabled: y === t,
icon: /* @__PURE__ */ a(O, {}), icon: /* @__PURE__ */ a(W, {}),
"aria-label": "صفحه بعدی" "aria-label": "صفحه بعدی"
} }
) )
@ -400,91 +400,91 @@ const Ee = (e) => new Intl.NumberFormat("fa-IR").format(Number(e)), p = (e) => S
} }
); );
}; };
let R = null, d = null, V = null, j = !1; let V = null, f = null, F = null, j = !1;
const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (d || (d = new te({ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new te({
url: e, url: e,
realm: t, realm: t,
clientId: n clientId: n
})), d), se = (e) => (R = e, R), le = () => j, G = () => !!(d != null && d.authenticated), T = () => (d == null ? void 0 : d.token) ?? null, q = async (e = 30) => d ? d.updateToken(e) : !1, H = (e) => d == null ? void 0 : d.logout(e), Te = (e) => d == null ? void 0 : d.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), Te = (e) => f == null ? void 0 : f.login(e), J = ({ config: e, initOptions: t = {} } = {}) => {
const n = e ?? R; const n = e ?? V;
if (!(n != null && n.url) || !(n != null && n.realm) || !(n != null && n.clientId)) if (!(n != null && n.url) || !(n != null && n.realm) || !(n != null && n.clientId))
throw new Error("Keycloak config is required."); throw new Error("Keycloak config is required.");
se(n); se(n);
const s = oe(n); const l = oe(n);
return V || (V = s.init({ return F || (F = l.init({
onLoad: "login-required", onLoad: "login-required",
checkLoginIframe: !1, checkLoginIframe: !1,
flow: "implicit", flow: "implicit",
...t ...t
}).then((c) => (j = !0, c)).catch((c) => { }).then((u) => (j = !0, u)).catch((u) => {
throw j = !0, c; throw j = !0, u;
})), V; })), F;
}, ce = ({ }, ce = ({
children: e, children: e,
config: t, config: t,
initOptions: n, initOptions: n,
loading: s = null loading: l = null
}) => { }) => {
const [c, l] = v(!1), [i, o] = v(!1), [f, u] = v(null), w = N(!1), y = E(() => { const [u, s] = v(!1), [i, o] = v(!1), [d, c] = v(null), b = N(!1), y = E(() => {
d && (o(!!d.authenticated), u(d.token ?? null)); f && (o(!!f.authenticated), c(f.token ?? null));
}, []); }, []);
C(() => (w.current = !0, J({ config: t, initOptions: n }).then((b) => { C(() => (b.current = !0, J({ config: t, initOptions: n }).then((w) => {
w.current && (o(!!b), u((d == null ? void 0 : d.token) ?? null), l(!0)); b.current && (o(!!w), c((f == null ? void 0 : f.token) ?? null), s(!0));
}).catch(() => { }).catch(() => {
w.current && (o(!1), u(null), l(!0)); b.current && (o(!1), c(null), s(!0));
}), () => { }), () => {
w.current = !1; b.current = !1;
}), [t, n]); }), [t, n]);
const A = E(() => d == null ? void 0 : d.login(), []), r = E((b) => d == null ? void 0 : d.logout(b), []), h = E( const A = E(() => f == null ? void 0 : f.login(), []), r = E((w) => f == null ? void 0 : f.logout(w), []), h = E(
async (b = 30) => { async (w = 30) => {
if (!d) return !1; if (!f) return !1;
const F = await d.updateToken(b); const g = await f.updateToken(w);
return y(), F; return y(), g;
}, },
[y] [y]
), m = z( ), m = _(
() => ({ () => ({
initialized: c, initialized: u,
authenticated: i, authenticated: i,
token: f, token: d,
login: A, login: A,
logout: r, logout: r,
updateToken: h updateToken: h
}), }),
[c, i, f, A, r, h] [u, i, d, A, r, h]
); );
return c ? /* @__PURE__ */ a(ie.Provider, { value: m, children: e }) : s; return u ? /* @__PURE__ */ a(ie.Provider, { value: m, children: e }) : l;
}, Q = $(null), M = "permissions", ue = (e) => (e == null ? void 0 : e.storageKey) || M, ze = ({ }, Q = $(null), M = "permissions", ue = (e) => (e == null ? void 0 : e.storageKey) || M, _e = ({
user: e = null, user: e = null,
children: t, children: t,
config: n, config: n,
loading: s = /* @__PURE__ */ a("div", { children: "Loading permissions..." }), loading: l = /* @__PURE__ */ a("div", { children: "Loading permissions..." }),
errorFallback: c = /* @__PURE__ */ a("div", { children: "Error loading permissions" }) errorFallback: u = /* @__PURE__ */ a("div", { children: "Error loading permissions" })
}) => { }) => {
const l = ue(n), [i, o] = v( const s = ue(n), [i, o] = v(
() => ae(l) () => ae(s)
), { data: f, isLoading: u, error: w } = me(n), y = f ?? i; ), { data: d, isLoading: c, error: b } = me(n), y = d ?? i;
C(() => { C(() => {
f && (de(l, f), o(f)); d && (de(s, d), o(d));
}, [f, l]); }, [d, s]);
const A = z( const A = _(
() => fe(y), () => fe(y),
[y] [y]
), r = E( ), r = E(
(b = "") => pe(A, b), (w = "") => pe(A, w),
[A] [A]
), h = E((b = "") => !r(b), [r]), m = z( ), h = E((w = "") => !r(w), [r]), m = _(
() => ({ () => ({
user: e, user: e,
can: r, can: r,
cannot: h, cannot: h,
permissions: y, permissions: y,
isLoading: u, isLoading: c,
error: w error: b
}), }),
[e, r, h, y, u, w] [e, r, h, y, c, b]
); );
return w && !y ? c : u && !y ? s : /* @__PURE__ */ a(Q.Provider, { value: m, children: t }); return b && !y ? u : c && !y ? l : /* @__PURE__ */ a(Q.Provider, { value: m, children: t });
}, ae = (e = M) => { }, ae = (e = M) => {
if (!(typeof window > "u")) if (!(typeof window > "u"))
try { try {
@ -499,7 +499,7 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (d || (d = new t
localStorage.setItem(e, JSON.stringify(t)); localStorage.setItem(e, JSON.stringify(t));
} catch { } catch {
} }
}, _e = (e = M) => { }, ze = (e = M) => {
if (!(typeof window > "u")) if (!(typeof window > "u"))
try { try {
localStorage.removeItem(e); localStorage.removeItem(e);
@ -507,7 +507,7 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (d || (d = new t
} }
}, fe = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), pe = (e, t = "") => { }, fe = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), pe = (e, t = "") => {
if (!t) return !1; if (!t) return !1;
if (e.has(t) || [...e].some((s) => s.startsWith(t))) return !0; if (e.has(t) || [...e].some((l) => l.startsWith(t))) return !0;
let n = t; let n = t;
for (; n.includes(":"); ) for (; n.includes(":"); )
if (n = n.slice(0, n.lastIndexOf(":")), e.has(`${n}:*`)) return !0; if (n = n.slice(0, n.lastIndexOf(":")), e.has(`${n}:*`)) return !0;
@ -520,16 +520,16 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (d || (d = new t
throw new Error("Permission config requires url and clientId."); throw new Error("Permission config requires url and clientId.");
const t = T(), n = new URL(e.url); const t = T(), n = new URL(e.url);
n.searchParams.set("client_id", e.clientId); n.searchParams.set("client_id", e.clientId);
const s = await fetch(n.toString(), { const l = await fetch(n.toString(), {
method: "GET", method: "GET",
headers: { headers: {
...t ? { Authorization: `Bearer ${t}` } : {} ...t ? { Authorization: `Bearer ${t}` } : {}
} }
}); });
if (!s.ok) if (!l.ok)
throw new Error("Failed to load permissions"); throw new Error("Failed to load permissions");
return s.json(); return l.json();
}, W = () => { }, U = () => {
const e = P(Q); const e = P(Q);
if (!e) if (!e)
throw new Error("usePermission must be used within PermissionProvider"); throw new Error("usePermission must be used within PermissionProvider");
@ -546,25 +546,25 @@ const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (d || (d = new t
children: t, children: t,
fallback: n = /* @__PURE__ */ a("div", { children: "You are not allowed to do this" }) fallback: n = /* @__PURE__ */ a("div", { children: "You are not allowed to do this" })
}) => { }) => {
const { can: s } = W(); const { can: l } = U();
return s(e) ? t : n; return l(e) ? t : n;
}, Fe = ({ children: e }) => { }, ge = ({ children: e }) => {
const { can: t, permissions: n } = W(); const { can: t, permissions: n } = U();
return n && t("admin") ? e : null; return n && t("admin") ? e : null;
}, Ve = ({ children: e }) => { }, Fe = ({ children: e }) => {
const { can: t, isLoading: n } = W(); const { can: t, isLoading: n } = U();
return C(() => { return C(() => {
n || t("panel_access") || H(); n || t("panel_access") || H();
}, [n, t]), e; }, [n, t]), e;
}; };
let _ = null; let z = null;
const be = (e) => { const be = (e) => {
const { api: t } = e, n = re.create({ const { api: t } = e, n = re.create({
baseURL: t.baseUrl baseURL: t.baseUrl
}), s = async () => { }), l = async () => {
(!le() || !G()) && await J(); (!le() || !G()) && await J();
}, c = async (i) => { }, u = async (i) => {
await s(); await l();
let o = T(); let o = T();
if (o) { if (o) {
try { try {
@ -577,17 +577,17 @@ const be = (e) => {
}; };
} }
return i; return i;
}, l = async (i) => { }, s = async (i) => {
var f; var d;
const o = i.config; const o = i.config;
if (((f = i == null ? void 0 : i.response) == null ? void 0 : f.status) === 401 && !(o != null && o._retry)) { if (((d = i == null ? void 0 : i.response) == null ? void 0 : d.status) === 401 && !(o != null && o._retry)) {
o._retry = !0; o._retry = !0;
try { try {
await q(30); await q(30);
const u = T(); const c = T();
return o.headers = { return o.headers = {
...o.headers, ...o.headers,
Authorization: `Bearer ${u}` Authorization: `Bearer ${c}`
}, n(o); }, n(o);
} catch { } catch {
H({ redirectUri: window.location.origin }); H({ redirectUri: window.location.origin });
@ -595,17 +595,17 @@ const be = (e) => {
} }
return Promise.reject(i); return Promise.reject(i);
}; };
return n.interceptors.request.use(c), n.interceptors.response.use( return n.interceptors.request.use(u), n.interceptors.response.use(
(i) => i, (i) => i,
l s
), { ), {
api: n api: n
}; };
}, we = (e) => (_ = be(e), _), ye = () => { }, we = (e) => (z = be(e), z), ye = () => {
if (!_) if (!z)
throw new Error("api is not configured. Call configureApi(config) first."); throw new Error("api is not configured. Call configureApi(config) first.");
return _.api; return z.api;
}, Re = new Proxy( }, Ve = new Proxy(
{}, {},
{ {
get(e, t) { get(e, t) {
@ -617,8 +617,8 @@ function xe() {
const [e, t] = v(!1), n = N(null); const [e, t] = v(!1), n = N(null);
return C(() => { return C(() => {
if (process.env.NODE_ENV !== "production") return; if (process.env.NODE_ENV !== "production") return;
let s; let l;
const c = async () => { const u = async () => {
const i = await fetch(`/version.json?t=${Date.now()}`, { const i = await fetch(`/version.json?t=${Date.now()}`, {
cache: "no-store" cache: "no-store"
}); });
@ -629,10 +629,10 @@ function xe() {
}; };
return (async () => { return (async () => {
try { try {
n.current = await c(), s = setInterval(async () => { n.current = await u(), l = setInterval(async () => {
try { try {
const i = await c(); const i = await u();
n.current && i !== n.current && (t(!0), clearInterval(s)); n.current && i !== n.current && (t(!0), clearInterval(l));
} catch (i) { } catch (i) {
console.error("Update check failed:", i); console.error("Update check failed:", i);
} }
@ -641,7 +641,7 @@ function xe() {
console.error("Initial version fetch failed:", i); console.error("Initial version fetch failed:", i);
} }
})(), () => { })(), () => {
s && clearInterval(s); l && clearInterval(l);
}; };
}, []), e ? /* @__PURE__ */ a( }, []), e ? /* @__PURE__ */ a(
"div", "div",
@ -664,32 +664,60 @@ function xe() {
} }
function je({ function je({
children: e, children: e,
config: t, apiBaseUrl: t,
loading: n = /* @__PURE__ */ a(ee, {}), keycloakClientId: n,
permissionClientId: l,
loading: u = /* @__PURE__ */ a(ee, {}),
updateChecker: s = !0, updateChecker: s = !0,
updateCheckerProps: c updateCheckerProps: i,
keycloakUrl: o = "https://auth.ibagher.ir",
keycloakRealm: d = "bi",
permissionUrl: c = "https://api.d.aiengines.ir/user_api/v1/permissions/list"
}) { }) {
return z(() => { const b = _(
we(t); () => ({
}, [t]), /* @__PURE__ */ B(Z, { children: [ api: {
s && /* @__PURE__ */ a(xe, { ...c }), baseUrl: t
/* @__PURE__ */ a(ce, { config: t.keycloak, loading: n, children: e }) },
keycloak: {
url: o,
realm: d,
clientId: n
},
permission: {
url: c,
clientId: l
}
}),
[
t,
n,
l,
o,
d,
c
]
);
return C(() => {
we(b);
}, [b]), /* @__PURE__ */ I(Z, { children: [
s && /* @__PURE__ */ a(xe, { ...i }),
/* @__PURE__ */ a(ce, { config: b.keycloak, loading: u, children: e })
] }); ] });
} }
export { export {
Fe as AdminGuard, ge as AdminGuard,
je as BiProvider,
Me as Can, Me as Can,
ce as KeycloakProvider, ce as KeycloakProvider,
Ke as LightPagination, De as LightPagination,
De as Pagination, Ke as Pagination,
Ve as PanelAccessGuard, Fe as PanelAccessGuard,
ze as PermissionProvider, _e as PermissionProvider,
Ne as SimplePagination, Ne as SimplePagination,
je as UiKitProvider, Ve as api,
xe as UpdateChecker,
Re as api,
pe as checkPermission, pe as checkPermission,
_e as clearCachedPermissions, ze as clearCachedPermissions,
we as configureApi, we as configureApi,
be as createApi, be as createApi,
fe as createPermissionSet, fe as createPermissionSet,
@ -704,10 +732,10 @@ export {
H as logoutKeycloak, H as logoutKeycloak,
Y as permissionKeys, Y as permissionKeys,
de as setCachedPermissions, de as setCachedPermissions,
K as toEnDigits, D as toEnDigits,
p as toFaDigits, p as toFaDigits,
Ee as toFaNumber, Ee as toFaNumber,
q as updateKeycloakToken, q as updateKeycloakToken,
W as usePermission, U as usePermission,
me as usePermissionsQuery me as usePermissionsQuery
}; };

View File

@ -1,27 +0,0 @@
import { useMemo } from "react";
import { Spinner } from "@chakra-ui/react";
import { KeycloakProvider } from "./keycloak.jsx";
import { UpdateChecker } from "../components/UpdateChecker.jsx";
import { configureApi } from "./api.js";
export function UiKitProvider({
children,
config,
loading = <Spinner />,
updateChecker = true,
updateCheckerProps,
}) {
useMemo(() => {
configureApi(config);
}, [config]);
return (
<>
{updateChecker && <UpdateChecker {...updateCheckerProps} />}
<KeycloakProvider config={config.keycloak} loading={loading}>
{children}
</KeycloakProvider>
</>
);
}

View File

@ -33,5 +33,4 @@ export {
export { createApi, configureApi, getApi, api } from "./core/api.js"; export { createApi, configureApi, getApi, api } from "./core/api.js";
export { UiKitProvider } from "./core/UiKitProvider.jsx"; export { BiProvider } from "./provider/BiProvider.jsx";
export { UpdateChecker } from "./components/UpdateChecker.jsx";

View File

@ -0,0 +1,57 @@
import { useEffect, useMemo } from "react";
import { Spinner } from "@chakra-ui/react";
import { KeycloakProvider } from "../core/keycloak.jsx";
import { UpdateChecker } from "../components/UpdateChecker.jsx";
import { configureApi } from "../core/api.js";
export function BiProvider({
children,
apiBaseUrl,
keycloakClientId,
permissionClientId,
loading = <Spinner />,
updateChecker = true,
updateCheckerProps,
keycloakUrl = "https://auth.ibagher.ir",
keycloakRealm = "bi",
permissionUrl = "https://api.d.aiengines.ir/user_api/v1/permissions/list",
}) {
const config = useMemo(
() => ({
api: {
baseUrl: apiBaseUrl,
},
keycloak: {
url: keycloakUrl,
realm: keycloakRealm,
clientId: keycloakClientId,
},
permission: {
url: permissionUrl,
clientId: permissionClientId,
},
}),
[
apiBaseUrl,
keycloakClientId,
permissionClientId,
keycloakUrl,
keycloakRealm,
permissionUrl,
],
);
useEffect(() => {
configureApi(config);
}, [config]);
return (
<>
{updateChecker && <UpdateChecker {...updateCheckerProps} />}
<KeycloakProvider config={config.keycloak} loading={loading}>
{children}
</KeycloakProvider>
</>
);
}