uikit/dist/index.js
2026-06-13 18:21:22 +03:30

742 lines
20 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 _, useContext as P } from "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 O, IoChevronBackOutline as W } from "react-icons/io5";
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 = ({
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(
(r) => r >= 1 && r <= t
);
C(() => {
if (d !== null) {
e === d && (c(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 (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();
}, 1e3);
return () => clearTimeout(h);
}, [
u,
i,
d,
t,
e,
l
]);
const A = () => {
var m, w;
const r = parseInt(D(u), 10);
if (isNaN(r)) {
s(p(e)), o(!1), c(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();
};
return t === 0 || !n ? null : /* @__PURE__ */ I(
k,
{
justifyContent: "space-around",
backgroundColor: "white",
py: "10px",
px: "16px",
rounded: "lg",
border: "1px",
borderColor: "blue.200",
w: "100%",
children: [
/* @__PURE__ */ I(k, { spacing: 2, alignItems: "center", justifyContent: "center", children: [
/* @__PURE__ */ a(
K,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => l(e - 1),
isDisabled: e === 1,
icon: /* @__PURE__ */ a(O, {}),
"aria-label": "صفحه قبلی"
}
),
y.map((r) => /* @__PURE__ */ a(
R,
{
size: "md",
px: 2,
bg: e === r ? "#4B49AC" : "#e6e6f1",
color: e === r ? "#e6e6f1" : "#4B49AC",
_hover: { bg: e === r ? "#4B49AC" : "#cbcbe8" },
onClick: () => l(r),
variant: "soft",
children: p(r)
},
r
)),
/* @__PURE__ */ a(
K,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => l(e + 1),
isDisabled: e === t,
icon: /* @__PURE__ */ a(W, {}),
"aria-label": "صفحه بعدی"
}
),
/* @__PURE__ */ a(
L,
{
ref: b,
type: "text",
inputMode: "numeric",
placeholder: "صفحه",
w: "56px",
value: u,
onFocus: () => {
o(!0), c(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));
},
onKeyDown: (r) => {
r.key === "Enter" && A();
},
textAlign: "center",
color: "#1D2939",
px: 1,
minW: 0,
dir: "ltr"
}
)
] }),
/* @__PURE__ */ I(k, { spacing: 2, children: [
/* @__PURE__ */ a(S, { w: "auto", fontWeight: "bold", whiteSpace: "nowrap", children: "تعداد کل:" }),
/* @__PURE__ */ a(S, { children: p(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 = 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)));
return;
}
i || s(p(e));
}, [e, i, d]), C(() => {
if (!i || !u || d !== null) return;
const r = parseInt(D(u), 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();
}, 1e3);
return () => clearTimeout(h);
}, [
u,
i,
d,
t,
e,
l
]);
const A = () => {
var m, w;
const r = parseInt(D(u), 10);
if (isNaN(r)) {
s(p(e)), o(!1), c(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();
};
return t === 0 || !n ? null : /* @__PURE__ */ I(
k,
{
justifyContent: "space-around",
backgroundColor: "white",
py: "10px",
px: "16px",
rounded: "lg",
border: "1px",
borderColor: "blue.200",
w: "100%",
children: [
/* @__PURE__ */ I(k, { spacing: 2, whiteSpace: "nowrap", children: [
/* @__PURE__ */ a(S, { fontWeight: "bold", children: "صفحه" }),
/* @__PURE__ */ a(S, { children: p(e) }),
/* @__PURE__ */ a(S, { fontWeight: "bold", children: "از" }),
/* @__PURE__ */ a(S, { children: p(t) })
] }),
/* @__PURE__ */ I(k, { spacing: 2, alignItems: "center", justifyContent: "center", children: [
/* @__PURE__ */ a(
K,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => l(e - 1),
isDisabled: e === 1,
icon: /* @__PURE__ */ a(O, {}),
"aria-label": "صفحه قبلی"
}
),
y.map((r) => /* @__PURE__ */ a(
R,
{
size: "md",
bg: e === r ? "#4B49AC" : "#e6e6f1",
color: e === r ? "#e6e6f1" : "#4B49AC",
_hover: { bg: e === r ? "#4B49AC" : "#cbcbe8" },
onClick: () => l(r),
variant: "soft",
children: p(r)
},
r
)),
/* @__PURE__ */ a(
K,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => l(e + 1),
isDisabled: e === t,
icon: /* @__PURE__ */ a(W, {}),
"aria-label": "صفحه بعدی"
}
),
/* @__PURE__ */ a(
L,
{
ref: b,
type: "text",
inputMode: "numeric",
placeholder: "صفحه",
w: "100px",
value: u,
onFocus: () => {
o(!0), c(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));
},
onKeyDown: (r) => {
r.key === "Enter" && A();
},
textAlign: "center",
color: "#1D2939",
px: 1,
minW: 0,
dir: "ltr"
}
)
] }),
/* @__PURE__ */ I(k, { spacing: 2, whiteSpace: "nowrap", children: [
/* @__PURE__ */ a(S, { fontWeight: "bold", children: "تعداد کل:" }),
/* @__PURE__ */ a(S, { children: p(n) })
] })
]
}
);
}, Ne = ({
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(
(x) => {
const B = Math.min(Math.max(x, 1), t);
o(!1), s(""), c(null), B !== e && l(B);
},
[e, t, l]
), r = E(() => {
if (!u) {
o(!1), s(""), c(null);
return;
}
const x = parseInt(D(u), 10);
if (isNaN(x)) {
o(!1), s(""), c(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]);
C(() => {
var x;
i && ((x = b.current) == null || x.focus());
}, [i]), C(() => {
d !== null && e === d && c(null);
}, [e, d]), C(() => {
if (!i || !u) return;
const x = setTimeout(() => {
r();
}, 1e3);
return () => clearTimeout(x);
}, [u, i, r]);
const h = () => {
c(null), s(""), o(!0);
}, m = (x, B = !1) => /* @__PURE__ */ a(
R,
{
size: "md",
bg: B ? "#4B49AC" : "#e6e6f1",
color: B ? "#e6e6f1" : "#4B49AC",
_hover: { bg: B ? "#4B49AC" : "#cbcbe8" },
onClick: B ? h : () => A(x),
variant: "soft",
children: p(x)
},
x
);
if (t === 0 || !n) return null;
const w = t > 1 && y !== 1, g = t > 1 && y !== t;
return /* @__PURE__ */ a(
k,
{
justifyContent: "center",
backgroundColor: "white",
py: "10px",
px: "16px",
rounded: "lg",
border: "1px",
borderColor: "blue.200",
w: "100%",
children: /* @__PURE__ */ I(k, { spacing: 2, alignItems: "center", justifyContent: "center", children: [
/* @__PURE__ */ a(
K,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => A(y - 1),
isDisabled: y === 1,
icon: /* @__PURE__ */ a(O, {}),
"aria-label": "صفحه قبلی"
}
),
w && m(1),
i ? /* @__PURE__ */ a(
L,
{
ref: b,
type: "text",
inputMode: "numeric",
placeholder: "صفحه",
w: "48px",
value: u,
onChange: (x) => {
const B = x.target.value, X = D(B).replace(/\D/g, "");
s(p(X));
},
onBlur: () => {
u ? r() : (s(""), o(!1), c(null));
},
onKeyDown: (x) => {
x.key === "Enter" && r(), x.key === "Escape" && (s(""), o(!1), c(null));
},
textAlign: "center",
color: "#1D2939",
px: 1,
minW: 0,
dir: "ltr"
}
) : m(y, !0),
g && m(t),
/* @__PURE__ */ a(
K,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => A(y + 1),
isDisabled: y === t,
icon: /* @__PURE__ */ a(W, {}),
"aria-label": "صفحه بعدی"
}
)
] })
}
);
};
let V = null, f = null, F = null, j = !1;
const ie = $(null), oe = ({ url: e, realm: t, clientId: n }) => (f || (f = new te({
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 = {} } = {}) => {
const n = e ?? V;
if (!(n != null && n.url) || !(n != null && n.realm) || !(n != null && n.clientId))
throw new Error("Keycloak config is required.");
se(n);
const l = oe(n);
return F || (F = l.init({
onLoad: "login-required",
checkLoginIframe: !1,
flow: "implicit",
...t
}).then((u) => (j = !0, u)).catch((u) => {
throw j = !0, u;
})), F;
}, ce = ({
children: e,
config: 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));
}, []);
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));
}).catch(() => {
b.current && (o(!1), c(null), s(!0));
}), () => {
b.current = !1;
}), [t, n]);
const A = E(() => f == null ? void 0 : f.login(), []), r = E((w) => f == null ? void 0 : f.logout(w), []), h = E(
async (w = 30) => {
if (!f) return !1;
const g = await f.updateToken(w);
return y(), g;
},
[y]
), m = _(
() => ({
initialized: u,
authenticated: i,
token: d,
login: A,
logout: r,
updateToken: h
}),
[u, 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 = ({
user: e = null,
children: t,
config: n,
loading: l = /* @__PURE__ */ a("div", { children: "Loading permissions..." }),
errorFallback: u = /* @__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;
C(() => {
d && (de(s, d), o(d));
}, [d, s]);
const A = _(
() => fe(y),
[y]
), r = E(
(w = "") => pe(A, w),
[A]
), h = E((w = "") => !r(w), [r]), m = _(
() => ({
user: e,
can: r,
cannot: h,
permissions: y,
isLoading: c,
error: b
}),
[e, r, h, y, c, b]
);
return b && !y ? u : c && !y ? l : /* @__PURE__ */ a(Q.Provider, { value: m, children: t });
}, ae = (e = M) => {
if (!(typeof window > "u"))
try {
const t = localStorage.getItem(e);
return t ? JSON.parse(t) : void 0;
} catch {
return;
}
}, de = (e = M, t) => {
if (!(typeof window > "u"))
try {
localStorage.setItem(e, JSON.stringify(t));
} catch {
}
}, ze = (e = M) => {
if (!(typeof window > "u"))
try {
localStorage.removeItem(e);
} catch {
}
}, fe = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), pe = (e, t = "") => {
if (!t) return !1;
if (e.has(t) || [...e].some((l) => l.startsWith(t))) return !0;
let n = t;
for (; n.includes(":"); )
if (n = n.slice(0, n.lastIndexOf(":")), e.has(`${n}:*`)) return !0;
return !1;
}, Y = {
all: ["permissions"],
list: (e) => [...Y.all, e]
}, he = 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);
n.searchParams.set("client_id", e.clientId);
const l = await fetch(n.toString(), {
method: "GET",
headers: {
...t ? { Authorization: `Bearer ${t}` } : {}
}
});
if (!l.ok)
throw new Error("Failed to load permissions");
return l.json();
}, U = () => {
const e = P(Q);
if (!e)
throw new Error("usePermission must be used within PermissionProvider");
return e;
}, me = (e) => ne({
queryKey: Y.list(e == null ? void 0 : e.clientId),
queryFn: () => he(e),
enabled: !!(e != null && e.url) && !!(e != null && e.clientId) && G(),
staleTime: 1 / 0,
refetchOnWindowFocus: !1,
refetchOnReconnect: !1
}), Me = ({
perm: e,
children: t,
fallback: n = /* @__PURE__ */ a("div", { children: "You are not allowed to do this" })
}) => {
const { can: l } = U();
return l(e) ? t : n;
}, ge = ({ children: e }) => {
const { can: t, permissions: n } = U();
return n && t("admin") ? e : null;
}, Fe = ({ children: e }) => {
const { can: t, isLoading: n } = U();
return C(() => {
n || t("panel_access") || H();
}, [n, t]), e;
};
let z = null;
const be = (e) => {
const { api: t } = e, n = re.create({
baseURL: t.baseUrl
}), l = async () => {
(!le() || !G()) && await J();
}, u = async (i) => {
await l();
let o = T();
if (o) {
try {
await q(5), o = T();
} catch {
}
i.headers = {
...i.headers,
Authorization: `Bearer ${o}`
};
}
return i;
}, s = async (i) => {
var d;
const o = i.config;
if (((d = i == null ? void 0 : i.response) == null ? void 0 : d.status) === 401 && !(o != null && o._retry)) {
o._retry = !0;
try {
await q(30);
const c = T();
return o.headers = {
...o.headers,
Authorization: `Bearer ${c}`
}, n(o);
} catch {
H({ redirectUri: window.location.origin });
}
}
return Promise.reject(i);
};
return n.interceptors.request.use(u), n.interceptors.response.use(
(i) => i,
s
), {
api: n
};
}, we = (e) => (z = be(e), z), ye = () => {
if (!z)
throw new Error("api is not configured. Call configureApi(config) first.");
return z.api;
}, Ve = new Proxy(
{},
{
get(e, t) {
return ye()[t];
}
}
);
function xe() {
const [e, t] = v(!1), n = N(null);
return C(() => {
if (process.env.NODE_ENV !== "production") return;
let l;
const u = async () => {
const i = await fetch(`/version.json?t=${Date.now()}`, {
cache: "no-store"
});
if (!i.ok)
throw new Error(`Failed to fetch version: ${i.status}`);
const o = await i.json();
return String(o.version);
};
return (async () => {
try {
n.current = await u(), l = setInterval(async () => {
try {
const i = await u();
n.current && i !== n.current && (t(!0), clearInterval(l));
} catch (i) {
console.error("Update check failed:", i);
}
}, 3e4);
} catch (i) {
console.error("Initial version fetch failed:", i);
}
})(), () => {
l && clearInterval(l);
};
}, []), e ? /* @__PURE__ */ a(
"div",
{
style: {
position: "fixed",
bottom: "16px",
right: "16px",
background: "#111",
color: "#fff",
padding: "12px 16px",
borderRadius: "8px",
zIndex: 9999,
cursor: "pointer"
},
onClick: () => window.location.reload(),
children: "نسخه جدید سامانه در دسترس است"
}
) : null;
}
function je({
children: e,
apiBaseUrl: t,
keycloakClientId: n,
permissionClientId: l,
loading: u = /* @__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"
}) {
const b = _(
() => ({
api: {
baseUrl: t
},
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 {
ge as AdminGuard,
je as BiProvider,
Me as Can,
ce as KeycloakProvider,
De as LightPagination,
Ke as Pagination,
Fe as PanelAccessGuard,
_e as PermissionProvider,
Ne as SimplePagination,
Ve as api,
pe as checkPermission,
ze as clearCachedPermissions,
we as configureApi,
be as createApi,
fe as createPermissionSet,
ye as getApi,
ae as getCachedPermissions,
T as getKeycloakToken,
he as getPermissions,
J as initKeycloak,
G as isKeycloakAuthenticated,
le as isKeycloakInitialized,
Te as loginKeycloak,
H as logoutKeycloak,
Y as permissionKeys,
de as setCachedPermissions,
D as toEnDigits,
p as toFaDigits,
Ee as toFaNumber,
q as updateKeycloakToken,
U as usePermission,
me as usePermissionsQuery
};