441 lines
11 KiB
JavaScript
441 lines
11 KiB
JavaScript
import { jsx as d, jsxs as z } from "react/jsx-runtime";
|
|
import G, { useState as x, useRef as F, useCallback as v, useEffect as g, useMemo as S, createContext as N } from "react";
|
|
import { createRoot as V } from "react-dom/client";
|
|
import { extendTheme as W, withDefaultColorScheme as Y, ChakraProvider as L, Spinner as H, Badge as J } from "@chakra-ui/react";
|
|
import { useQuery as X, QueryClient as Z, QueryClientProvider as ee } from "@tanstack/react-query";
|
|
import * as O from "next-query-params/pages";
|
|
import * as te from "use-query-params";
|
|
import re from "keycloak-js";
|
|
import { setKeycloakLogoutHandler as ne, usePermission as I, PermissionContext as oe } from "../core-runtime.js";
|
|
import se from "axios";
|
|
import { Global as ie } from "@emotion/react";
|
|
import { e as ae } from "./estedad-B1AoGIKy.js";
|
|
let T = null, o = null, B = null, C = !1;
|
|
const ce = N(null), le = ({ url: e, realm: r, clientId: t }) => (o || (o = new re({
|
|
url: e,
|
|
realm: r,
|
|
clientId: t
|
|
})), o), ue = (e) => (T = e, T), de = () => C, j = () => !!(o != null && o.authenticated), b = () => (o == null ? void 0 : o.token) ?? null, q = async (e = 30) => o ? o.updateToken(e) : !1, U = (e) => o == null ? void 0 : o.logout(e);
|
|
ne(U);
|
|
const Me = (e) => o == null ? void 0 : o.login(e), Q = ({ config: e, initOptions: r = {} } = {}) => {
|
|
const t = e ?? T;
|
|
if (!(t != null && t.url) || !(t != null && t.realm) || !(t != null && t.clientId))
|
|
throw new Error("Keycloak config is required.");
|
|
ue(t);
|
|
const n = le(t);
|
|
return B || (B = n.init({
|
|
onLoad: "login-required",
|
|
checkLoginIframe: !1,
|
|
flow: "implicit",
|
|
...r
|
|
}).then((a) => (C = !0, a)).catch((a) => {
|
|
throw C = !0, a;
|
|
})), B;
|
|
}, fe = ({
|
|
children: e,
|
|
config: r,
|
|
initOptions: t,
|
|
loading: n = null
|
|
}) => {
|
|
const [a, f] = x(!1), [c, s] = x(!1), [i, l] = x(null), u = F(!1), h = v(() => {
|
|
o && (s(!!o.authenticated), l(o.token ?? null));
|
|
}, []);
|
|
g(() => (u.current = !0, Q({ config: r, initOptions: t }).then((p) => {
|
|
u.current && (s(!!p), l((o == null ? void 0 : o.token) ?? null), f(!0));
|
|
}).catch(() => {
|
|
u.current && (s(!1), l(null), f(!0));
|
|
}), () => {
|
|
u.current = !1;
|
|
}), [r, t]);
|
|
const w = v(() => o == null ? void 0 : o.login(), []), m = v((p) => o == null ? void 0 : o.logout(p), []), y = v(
|
|
async (p = 30) => {
|
|
if (!o) return !1;
|
|
const K = await o.updateToken(p);
|
|
return h(), K;
|
|
},
|
|
[h]
|
|
), P = S(
|
|
() => ({
|
|
initialized: a,
|
|
authenticated: c,
|
|
token: i,
|
|
login: w,
|
|
logout: m,
|
|
updateToken: y
|
|
}),
|
|
[a, c, i, w, m, y]
|
|
);
|
|
return a ? /* @__PURE__ */ d(ce.Provider, { value: P, children: e }) : n;
|
|
};
|
|
let A = null;
|
|
const he = (e) => {
|
|
const { api: r } = e, t = se.create({
|
|
baseURL: r.baseUrl
|
|
}), n = async () => {
|
|
(!de() || !j()) && await Q();
|
|
}, a = !!e.keycloak, f = async (s) => {
|
|
if (!a) return s;
|
|
await n();
|
|
let i = b();
|
|
if (i) {
|
|
try {
|
|
await q(5), i = b();
|
|
} catch {
|
|
}
|
|
s.headers = {
|
|
...s.headers,
|
|
Authorization: `Bearer ${i}`
|
|
};
|
|
}
|
|
return s;
|
|
}, c = async (s) => {
|
|
var l;
|
|
const i = s.config;
|
|
if (((l = s == null ? void 0 : s.response) == null ? void 0 : l.status) === 401 && !(i != null && i._retry)) {
|
|
i._retry = !0;
|
|
try {
|
|
await q(30);
|
|
const u = b();
|
|
return i.headers = {
|
|
...i.headers,
|
|
Authorization: `Bearer ${u}`
|
|
}, t(i);
|
|
} catch {
|
|
U({ redirectUri: window.location.origin });
|
|
}
|
|
}
|
|
return Promise.reject(s);
|
|
};
|
|
return t.interceptors.request.use(f), t.interceptors.response.use(
|
|
(s) => s,
|
|
c
|
|
), {
|
|
api: t
|
|
};
|
|
}, pe = (e) => (A = he(e), A), me = () => {
|
|
if (!A)
|
|
throw new Error("api is not configured. Call configureApi(config) first.");
|
|
return A.api;
|
|
}, we = new Proxy(
|
|
{},
|
|
{
|
|
get(e, r) {
|
|
return me()[r];
|
|
}
|
|
}
|
|
), $e = async (e) => (await we.get(e)).data, R = "permissions", ye = (e) => (e == null ? void 0 : e.storageKey) || R, Pe = ({
|
|
user: e = null,
|
|
children: r,
|
|
config: t,
|
|
loading: n = /* @__PURE__ */ d("div", { children: "Loading permissions..." }),
|
|
errorFallback: a = /* @__PURE__ */ d("div", { children: "Error loading permissions" })
|
|
}) => {
|
|
const f = ye(t), [c, s] = x(
|
|
() => ve(f)
|
|
), { data: i, isLoading: l, error: u } = ke(t), h = i ?? c;
|
|
g(() => {
|
|
i && (xe(f, i), s(i));
|
|
}, [i, f]);
|
|
const w = S(
|
|
() => ge(h),
|
|
[h]
|
|
), m = v(
|
|
(p = "") => Ke(w, p),
|
|
[w]
|
|
), y = v((p = "") => !m(p), [m]), P = S(
|
|
() => ({
|
|
user: e,
|
|
can: m,
|
|
cannot: y,
|
|
permissions: h,
|
|
isLoading: l,
|
|
error: u
|
|
}),
|
|
[e, m, y, h, l, u]
|
|
);
|
|
return u && !h ? a : l && !h ? n : /* @__PURE__ */ d(oe.Provider, { value: P, children: r });
|
|
}, ve = (e = R) => {
|
|
if (!(typeof window > "u"))
|
|
try {
|
|
const r = localStorage.getItem(e);
|
|
return r ? JSON.parse(r) : void 0;
|
|
} catch {
|
|
return;
|
|
}
|
|
}, xe = (e = R, r) => {
|
|
if (!(typeof window > "u"))
|
|
try {
|
|
localStorage.setItem(e, JSON.stringify(r));
|
|
} catch {
|
|
}
|
|
}, Ge = (e = R) => {
|
|
if (!(typeof window > "u"))
|
|
try {
|
|
localStorage.removeItem(e);
|
|
} catch {
|
|
}
|
|
}, ge = (e) => e ? new Set(e.map((r) => r.name)) : /* @__PURE__ */ new Set(), Ke = (e, r = "") => {
|
|
if (!r) return !1;
|
|
if (e.has(r) || [...e].some((n) => n.startsWith(r))) return !0;
|
|
let t = r;
|
|
for (; t.includes(":"); )
|
|
if (t = t.slice(0, t.lastIndexOf(":")), e.has(`${t}:*`)) return !0;
|
|
return !1;
|
|
}, M = {
|
|
all: ["permissions"],
|
|
list: (e) => [...M.all, e]
|
|
}, Ee = async (e) => {
|
|
if (!(e != null && e.url) || !(e != null && e.clientId))
|
|
throw new Error("Permission config requires url and clientId.");
|
|
const r = b(), t = new URL(e.url);
|
|
t.searchParams.set("client_id", e.clientId);
|
|
const n = await fetch(t.toString(), {
|
|
method: "GET",
|
|
headers: {
|
|
...r ? { Authorization: `Bearer ${r}` } : {}
|
|
}
|
|
});
|
|
if (!n.ok)
|
|
throw new Error("Failed to load permissions");
|
|
return n.json();
|
|
}, ke = (e) => X({
|
|
queryKey: M.list(e == null ? void 0 : e.clientId),
|
|
queryFn: () => Ee(e),
|
|
enabled: !!(e != null && e.url) && !!(e != null && e.clientId) && j(),
|
|
staleTime: 1 / 0,
|
|
refetchOnWindowFocus: !1,
|
|
refetchOnReconnect: !1
|
|
}), Ne = ({
|
|
perm: e,
|
|
children: r,
|
|
fallback: t = /* @__PURE__ */ d("div", { children: "You are not allowed to do this" })
|
|
}) => {
|
|
const { can: n } = I();
|
|
return n(e) ? r : t;
|
|
}, Ve = ({ children: e }) => {
|
|
const { can: r, permissions: t } = I();
|
|
return t && r("admin") ? e : null;
|
|
}, We = ({ children: e, forceNoAccess: r = !1 }) => {
|
|
const { can: t, isLoading: n } = I();
|
|
return g(() => {
|
|
n || (r || !t("panel_access")) && U();
|
|
}, [n, t, r]), e;
|
|
};
|
|
function be() {
|
|
const [e, r] = x(!1), t = F(null);
|
|
return g(() => {
|
|
if (process.env.NODE_ENV !== "production") return;
|
|
let n;
|
|
const a = async () => {
|
|
const c = await fetch(`/version.json?t=${Date.now()}`, {
|
|
cache: "no-store"
|
|
});
|
|
if (!c.ok)
|
|
throw new Error(`Failed to fetch version: ${c.status}`);
|
|
const s = await c.json();
|
|
return String(s.version);
|
|
};
|
|
return (async () => {
|
|
try {
|
|
t.current = await a(), n = setInterval(async () => {
|
|
try {
|
|
const c = await a();
|
|
t.current && c !== t.current && (r(!0), clearInterval(n));
|
|
} catch (c) {
|
|
console.error("Update check failed:", c);
|
|
}
|
|
}, 3e4);
|
|
} catch (c) {
|
|
console.error("Initial version fetch failed:", c);
|
|
}
|
|
})(), () => {
|
|
n && clearInterval(n);
|
|
};
|
|
}, []), e ? /* @__PURE__ */ d(
|
|
"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;
|
|
}
|
|
const Se = new Z({
|
|
defaultOptions: {
|
|
queries: {
|
|
retry: 1,
|
|
refetchOnWindowFocus: !1
|
|
}
|
|
}
|
|
}), Ae = W(
|
|
{
|
|
direction: "rtl",
|
|
styles: {
|
|
global: {
|
|
html: {
|
|
fontSize: "14px",
|
|
direction: "rtl"
|
|
},
|
|
body: {
|
|
bg: "white",
|
|
direction: "rtl"
|
|
},
|
|
"*": {
|
|
boxSizing: "border-box"
|
|
}
|
|
}
|
|
},
|
|
fonts: {
|
|
heading: "Estedad",
|
|
body: "Estedad"
|
|
}
|
|
},
|
|
Y({ colorScheme: "facebook" })
|
|
), Re = () => /* @__PURE__ */ d(
|
|
ie,
|
|
{
|
|
styles: `
|
|
@font-face {
|
|
font-family: Estedad;
|
|
src: url("${ae}") format("woff2");
|
|
font-weight: 400;
|
|
font-style: normal;
|
|
font-display: swap;
|
|
}
|
|
`
|
|
}
|
|
), $ = (e, r) => {
|
|
let t = e;
|
|
for (let n = 0; n < 4 && t; n += 1) {
|
|
const a = Reflect.get(t, r);
|
|
if (a !== void 0) return a;
|
|
t = Reflect.get(t, "default");
|
|
}
|
|
}, _e = (e) => {
|
|
let r = e;
|
|
for (let t = 0; t < 4 && r; t += 1) {
|
|
const n = Reflect.get(r, "default");
|
|
if (n === void 0) break;
|
|
r = n;
|
|
}
|
|
return r;
|
|
}, Be = $(
|
|
te,
|
|
"QueryParamProvider"
|
|
), Te = $(O, "NextAdapterPages") ?? _e(O), D = "__UIKIT_REMOTE_DEPS__";
|
|
function Ce({ theme: e, chakraProviderProps: r } = {}) {
|
|
if (typeof window > "u") return;
|
|
const t = window[D] || {};
|
|
window[D] = {
|
|
...t,
|
|
React: t.React || G,
|
|
createRoot: t.createRoot || V,
|
|
ChakraProvider: t.ChakraProvider || L,
|
|
Badge: t.Badge || J,
|
|
theme: e ?? t.theme,
|
|
chakraProviderProps: {
|
|
...t.chakraProviderProps || {},
|
|
...r || {}
|
|
}
|
|
};
|
|
}
|
|
function Ye({
|
|
children: e,
|
|
apiBaseUrl: r,
|
|
keycloakClientId: t,
|
|
permissionClientId: n,
|
|
loading: a = /* @__PURE__ */ d(H, {}),
|
|
updateChecker: f = !1,
|
|
updateCheckerProps: c,
|
|
keycloakUrl: s = "https://auth.ibagher.ir",
|
|
keycloakRealm: i = "bi",
|
|
permissionUrl: l = "https://api.d.aiengines.ir/user_api/v1/permissions/list",
|
|
keycloakEnabled: u,
|
|
permissionEnabled: h,
|
|
theme: w = Ae,
|
|
remoteUiEnabled: m = !0,
|
|
remoteUiChakraProviderProps: y
|
|
}) {
|
|
const [P, p] = x(!1), K = u ?? !!t, _ = h ?? !!n, k = S(
|
|
() => ({
|
|
api: {
|
|
baseUrl: r || ""
|
|
},
|
|
keycloak: K ? {
|
|
url: s,
|
|
realm: i,
|
|
clientId: t
|
|
} : null,
|
|
permission: _ ? {
|
|
url: l,
|
|
clientId: n
|
|
} : null
|
|
}),
|
|
[
|
|
r,
|
|
K,
|
|
_,
|
|
s,
|
|
i,
|
|
t,
|
|
l,
|
|
n
|
|
]
|
|
);
|
|
if (g(() => {
|
|
p(!0);
|
|
}, []), g(() => {
|
|
P && pe(k);
|
|
}, [P, k]), !P)
|
|
return null;
|
|
m && Ce({
|
|
theme: w,
|
|
chakraProviderProps: y
|
|
});
|
|
let E = e;
|
|
return _ && (E = /* @__PURE__ */ d(Pe, { config: k.permission, loading: a, children: E })), K && (E = /* @__PURE__ */ d(fe, { config: k.keycloak, loading: a, children: E })), /* @__PURE__ */ z(L, { theme: w, children: [
|
|
/* @__PURE__ */ d(Re, {}),
|
|
/* @__PURE__ */ z(ee, { client: Se, children: [
|
|
f && /* @__PURE__ */ d(be, { ...c }),
|
|
/* @__PURE__ */ d(Be, { adapter: Te, children: E })
|
|
] })
|
|
] });
|
|
}
|
|
export {
|
|
Ve as A,
|
|
Ye as B,
|
|
Ne as C,
|
|
fe as K,
|
|
We as P,
|
|
we as a,
|
|
Pe as b,
|
|
Ke as c,
|
|
Ge as d,
|
|
pe as e,
|
|
$e as f,
|
|
ge as g,
|
|
me as h,
|
|
ve as i,
|
|
b as j,
|
|
Ee as k,
|
|
Q as l,
|
|
j as m,
|
|
de as n,
|
|
Me as o,
|
|
U as p,
|
|
M as q,
|
|
ke as r,
|
|
xe as s,
|
|
q as u
|
|
};
|