uikit/dist/index.js
2026-06-13 14:00:57 +03:30

649 lines
18 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 } from "react/jsx-runtime";
import { useState as B, useRef as z, useEffect as C, useCallback as E, createContext as G, useMemo as R, useContext as Z } from "react";
import { HStack as k, IconButton as D, Button as V, Input as W, Text as S } from "@chakra-ui/react";
import { IoChevronForwardOutline as O, IoChevronBackOutline as q } from "react-icons/io5";
import P from "keycloak-js";
import { useQuery as ee } from "@tanstack/react-query";
import te from "axios";
const Be = (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, (r) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(r))), Ce = ({
currentPage: e,
totalPages: t,
totalCount: r,
onPageChange: l
}) => {
const [c, i] = B(p(e)), [u, o] = B(!1), [f, s] = B(null), w = z(null), y = [.../* @__PURE__ */ new Set([1, e, t])].filter(
(n) => n >= 1 && n <= t
);
C(() => {
if (f !== null) {
e === f && (s(null), o(!1), i(p(e)));
return;
}
u || i(p(e));
}, [e, u, f]), C(() => {
if (!u || !c || f !== null) return;
const n = parseInt(K(c), 10);
if (isNaN(n)) return;
const h = setTimeout(() => {
var b;
const m = Math.min(Math.max(n, 1), t);
m !== e ? (s(m), i(p(m)), l(m)) : (o(!1), i(p(e))), (b = w.current) == null || b.blur();
}, 1e3);
return () => clearTimeout(h);
}, [
c,
u,
f,
t,
e,
l
]);
const v = () => {
var m, b;
const n = parseInt(K(c), 10);
if (isNaN(n)) {
i(p(e)), o(!1), s(null), (m = w.current) == null || m.blur();
return;
}
const h = Math.min(Math.max(n, 1), t);
h !== e ? (s(h), i(p(h)), l(h)) : (i(p(e)), o(!1), s(null)), (b = w.current) == null || b.blur();
};
return t === 0 || !r ? 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(
D,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => l(e - 1),
isDisabled: e === 1,
icon: /* @__PURE__ */ a(O, {}),
"aria-label": "صفحه قبلی"
}
),
y.map((n) => /* @__PURE__ */ a(
V,
{
size: "md",
px: 2,
bg: e === n ? "#4B49AC" : "#e6e6f1",
color: e === n ? "#e6e6f1" : "#4B49AC",
_hover: { bg: e === n ? "#4B49AC" : "#cbcbe8" },
onClick: () => l(n),
variant: "soft",
children: p(n)
},
n
)),
/* @__PURE__ */ a(
D,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => l(e + 1),
isDisabled: e === t,
icon: /* @__PURE__ */ a(q, {}),
"aria-label": "صفحه بعدی"
}
),
/* @__PURE__ */ a(
W,
{
ref: w,
type: "text",
inputMode: "numeric",
placeholder: "صفحه",
w: "56px",
value: c,
onFocus: () => {
o(!0), s(null), i("");
},
onChange: (n) => {
const h = n.target.value, m = K(h).replace(/\D/g, "");
i(p(m));
},
onBlur: () => {
c || (i(p(e)), o(!1), s(null));
},
onKeyDown: (n) => {
n.key === "Enter" && v();
},
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(r) })
] })
]
}
);
}, ke = ({
currentPage: e,
totalPages: t,
totalCount: r,
onPageChange: l
}) => {
const [c, i] = B(p(e)), [u, o] = B(!1), [f, s] = B(null), w = z(null), y = Array.from(
/* @__PURE__ */ new Set([1, e, e + 1, t])
).filter((n) => n >= 1 && n <= t).sort((n, h) => n - h);
C(() => {
if (f !== null) {
e === f && (s(null), o(!1), i(p(e)));
return;
}
u || i(p(e));
}, [e, u, f]), C(() => {
if (!u || !c || f !== null) return;
const n = parseInt(K(c), 10);
if (isNaN(n)) return;
const h = setTimeout(() => {
var b;
const m = Math.min(Math.max(n, 1), t);
m !== e ? (s(m), i(p(m)), l(m)) : (o(!1), i(p(e))), (b = w.current) == null || b.blur();
}, 1e3);
return () => clearTimeout(h);
}, [
c,
u,
f,
t,
e,
l
]);
const v = () => {
var m, b;
const n = parseInt(K(c), 10);
if (isNaN(n)) {
i(p(e)), o(!1), s(null), (m = w.current) == null || m.blur();
return;
}
const h = Math.min(Math.max(n, 1), t);
h !== e ? (s(h), i(p(h)), l(h)) : (i(p(e)), o(!1), s(null)), (b = w.current) == null || b.blur();
};
return t === 0 || !r ? 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(
D,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => l(e - 1),
isDisabled: e === 1,
icon: /* @__PURE__ */ a(O, {}),
"aria-label": "صفحه قبلی"
}
),
y.map((n) => /* @__PURE__ */ a(
V,
{
size: "md",
bg: e === n ? "#4B49AC" : "#e6e6f1",
color: e === n ? "#e6e6f1" : "#4B49AC",
_hover: { bg: e === n ? "#4B49AC" : "#cbcbe8" },
onClick: () => l(n),
variant: "soft",
children: p(n)
},
n
)),
/* @__PURE__ */ a(
D,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => l(e + 1),
isDisabled: e === t,
icon: /* @__PURE__ */ a(q, {}),
"aria-label": "صفحه بعدی"
}
),
/* @__PURE__ */ a(
W,
{
ref: w,
type: "text",
inputMode: "numeric",
placeholder: "صفحه",
w: "100px",
value: c,
onFocus: () => {
o(!0), s(null), i("");
},
onChange: (n) => {
const h = n.target.value, m = K(h).replace(/\D/g, "");
i(p(m));
},
onBlur: () => {
c || (i(p(e)), o(!1), s(null));
},
onKeyDown: (n) => {
n.key === "Enter" && v();
},
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(r) })
] })
]
}
);
}, Se = ({
currentPage: e,
totalPages: t,
totalCount: r,
onPageChange: l
}) => {
const [c, i] = B(""), [u, o] = B(!1), [f, s] = B(null), w = z(null), y = f ?? e, v = E(
(x) => {
const A = Math.min(Math.max(x, 1), t);
o(!1), i(""), s(null), A !== e && l(A);
},
[e, t, l]
), n = E(() => {
if (!c) {
o(!1), i(""), s(null);
return;
}
const x = parseInt(K(c), 10);
if (isNaN(x)) {
o(!1), i(""), s(null);
return;
}
const A = Math.min(Math.max(x, 1), t);
o(!1), i(""), A !== e ? (s(A), l(A)) : s(null);
}, [c, e, t, l]);
C(() => {
var x;
u && ((x = w.current) == null || x.focus());
}, [u]), C(() => {
f !== null && e === f && s(null);
}, [e, f]), C(() => {
if (!u || !c) return;
const x = setTimeout(() => {
n();
}, 1e3);
return () => clearTimeout(x);
}, [c, u, n]);
const h = () => {
s(null), i(""), o(!0);
}, m = (x, A = !1) => /* @__PURE__ */ a(
V,
{
size: "md",
bg: A ? "#4B49AC" : "#e6e6f1",
color: A ? "#e6e6f1" : "#4B49AC",
_hover: { bg: A ? "#4B49AC" : "#cbcbe8" },
onClick: A ? h : () => v(x),
variant: "soft",
children: p(x)
},
x
);
if (t === 0 || !r) return null;
const b = t > 1 && y !== 1, _ = 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(
D,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => v(y - 1),
isDisabled: y === 1,
icon: /* @__PURE__ */ a(O, {}),
"aria-label": "صفحه قبلی"
}
),
b && m(1),
u ? /* @__PURE__ */ a(
W,
{
ref: w,
type: "text",
inputMode: "numeric",
placeholder: "صفحه",
w: "48px",
value: c,
onChange: (x) => {
const A = x.target.value, X = K(A).replace(/\D/g, "");
i(p(X));
},
onBlur: () => {
c ? n() : (i(""), o(!1), s(null));
},
onKeyDown: (x) => {
x.key === "Enter" && n(), x.key === "Escape" && (i(""), o(!1), s(null));
},
textAlign: "center",
color: "#1D2939",
px: 1,
minW: 0,
dir: "ltr"
}
) : m(y, !0),
_ && m(t),
/* @__PURE__ */ a(
D,
{
size: "md",
bg: "#e6e6f1",
color: "#4B49AC",
_hover: { bg: "#cbcbe8" },
onClick: () => v(y + 1),
isDisabled: y === t,
icon: /* @__PURE__ */ a(q, {}),
"aria-label": "صفحه بعدی"
}
)
] })
}
);
};
let j = null, d = null, F = null, L = !1;
const ne = G(null), re = ({ url: e, realm: t, clientId: r }) => (d || (d = new P({
url: e,
realm: t,
clientId: r
})), d), ie = (e) => (j = e, j), oe = () => L, $ = () => !!(d != null && d.authenticated), N = () => (d == null ? void 0 : d.token) ?? null, U = async (e = 30) => d ? d.updateToken(e) : !1, J = (e) => d == null ? void 0 : d.logout(e), Ie = (e) => d == null ? void 0 : d.login(e), Q = ({ config: e, initOptions: t = {} } = {}) => {
const r = e ?? j;
if (!(r != null && r.url) || !(r != null && r.realm) || !(r != null && r.clientId))
throw new Error("Keycloak config is required.");
ie(r);
const l = re(r);
return F || (F = l.init({
onLoad: "login-required",
checkLoginIframe: !1,
flow: "implicit",
...t
}).then((c) => (L = !0, c)).catch((c) => {
throw L = !0, c;
})), F;
}, Ee = ({
children: e,
config: t,
initOptions: r,
loading: l = null
}) => {
const [c, i] = B(!1), [u, o] = B(!1), [f, s] = B(null), w = z(!1), y = E(() => {
d && (o(!!d.authenticated), s(d.token ?? null));
}, []);
C(() => (w.current = !0, Q({ config: t, initOptions: r }).then((b) => {
w.current && (o(!!b), s((d == null ? void 0 : d.token) ?? null), i(!0));
}).catch(() => {
w.current && (o(!1), s(null), i(!0));
}), () => {
w.current = !1;
}), [t, r]);
const v = E(() => d == null ? void 0 : d.login(), []), n = E((b) => d == null ? void 0 : d.logout(b), []), h = E(
async (b = 30) => {
if (!d) return !1;
const _ = await d.updateToken(b);
return y(), _;
},
[y]
), m = R(
() => ({
initialized: c,
authenticated: u,
token: f,
login: v,
logout: n,
updateToken: h
}),
[c, u, f, v, n, h]
);
return c ? /* @__PURE__ */ a(ne.Provider, { value: m, children: e }) : l;
}, Y = G(null), M = "permissions", se = (e) => (e == null ? void 0 : e.storageKey) || M, Ke = ({
user: e = null,
children: t,
config: r,
loading: l = /* @__PURE__ */ a("div", { children: "Loading permissions..." }),
errorFallback: c = /* @__PURE__ */ a("div", { children: "Error loading permissions" })
}) => {
const i = se(r), [u, o] = B(
() => le(i)
), { data: f, isLoading: s, error: w } = fe(r), y = f ?? u;
C(() => {
f && (ce(i, f), o(f));
}, [f, i]);
const v = R(
() => ue(y),
[y]
), n = E(
(b = "") => ae(v, b),
[v]
), h = E((b = "") => !n(b), [n]), m = R(
() => ({
user: e,
can: n,
cannot: h,
permissions: y,
isLoading: s,
error: w
}),
[e, n, h, y, s, w]
);
return w && !y ? c : s && !y ? l : /* @__PURE__ */ a(Y.Provider, { value: m, children: t });
}, le = (e = M) => {
if (!(typeof window > "u"))
try {
const t = localStorage.getItem(e);
return t ? JSON.parse(t) : void 0;
} catch {
return;
}
}, ce = (e = M, t) => {
if (!(typeof window > "u"))
try {
localStorage.setItem(e, JSON.stringify(t));
} catch {
}
}, De = (e = M) => {
if (!(typeof window > "u"))
try {
localStorage.removeItem(e);
} catch {
}
}, ue = (e) => e ? new Set(e.map((t) => t.name)) : /* @__PURE__ */ new Set(), ae = (e, t = "") => {
if (!t) return !1;
if (e.has(t) || [...e].some((l) => l.startsWith(t))) return !0;
let r = t;
for (; r.includes(":"); )
if (r = r.slice(0, r.lastIndexOf(":")), e.has(`${r}:*`)) return !0;
return !1;
}, H = {
all: ["permissions"],
list: (e) => [...H.all, e]
}, de = async (e) => {
if (!(e != null && e.url) || !(e != null && e.clientId))
throw new Error("Permission config requires url and clientId.");
const t = N(), r = new URL(e.url);
r.searchParams.set("client_id", e.clientId);
const l = await fetch(r.toString(), {
method: "GET",
headers: {
...t ? { Authorization: `Bearer ${t}` } : {}
}
});
if (!l.ok)
throw new Error("Failed to load permissions");
return l.json();
}, g = () => {
const e = Z(Y);
if (!e)
throw new Error("usePermission must be used within PermissionProvider");
return e;
}, fe = (e) => ee({
queryKey: H.list(e == null ? void 0 : e.clientId),
queryFn: () => de(e),
enabled: !!(e != null && e.url) && !!(e != null && e.clientId) && $(),
staleTime: 1 / 0,
refetchOnWindowFocus: !1,
refetchOnReconnect: !1
}), Ne = ({
perm: e,
children: t,
fallback: r = /* @__PURE__ */ a("div", { children: "You are not allowed to do this" })
}) => {
const { can: l } = g();
return l(e) ? t : r;
}, Te = ({ children: e }) => {
const { can: t, permissions: r } = g();
return r && t("admin") ? e : null;
}, ze = ({ children: e }) => {
const { can: t, isLoading: r } = g();
return C(() => {
r || t("panel_access") || J();
}, [r, t]), e;
};
let T = null;
const pe = (e) => {
const { api: t } = e, r = te.create({
baseURL: t.baseUrl
}), l = async () => {
(!oe() || !$()) && await Q();
}, c = async (u) => {
await l();
let o = N();
if (o) {
try {
await U(5), o = N();
} catch {
}
u.headers = {
...u.headers,
Authorization: `Bearer ${o}`
};
}
return u;
}, i = async (u) => {
var f;
const o = u.config;
if (((f = u == null ? void 0 : u.response) == null ? void 0 : f.status) === 401 && !(o != null && o._retry)) {
o._retry = !0;
try {
await U(30);
const s = N();
return o.headers = {
...o.headers,
Authorization: `Bearer ${s}`
}, r(o);
} catch {
J({ redirectUri: window.location.origin });
}
}
return Promise.reject(u);
};
return r.interceptors.request.use(c), r.interceptors.response.use(
(u) => u,
i
), {
api: r
};
}, Me = (e) => (T = pe(e), T), he = () => {
if (!T)
throw new Error("api is not configured. Call configureApi(config) first.");
return T.api;
}, _e = new Proxy(
{},
{
get(e, t) {
return he()[t];
}
}
);
export {
Te as AdminGuard,
Ne as Can,
Ee as KeycloakProvider,
Ce as LightPagination,
ke as Pagination,
ze as PanelAccessGuard,
Ke as PermissionProvider,
Se as SimplePagination,
_e as api,
ae as checkPermission,
De as clearCachedPermissions,
Me as configureApi,
pe as createApi,
ue as createPermissionSet,
he as getApi,
le as getCachedPermissions,
N as getKeycloakToken,
de as getPermissions,
Q as initKeycloak,
$ as isKeycloakAuthenticated,
oe as isKeycloakInitialized,
Ie as loginKeycloak,
J as logoutKeycloak,
H as permissionKeys,
ce as setCachedPermissions,
K as toEnDigits,
p as toFaDigits,
Be as toFaNumber,
U as updateKeycloakToken,
g as usePermission,
fe as usePermissionsQuery
};