UNPKG

@keycloakify/keycloak-admin-ui

Version:
104 lines 4.55 kB
import { jsx as _jsx } from "react/jsx-runtime"; /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-namespace */ import { Suspense, useMemo } from "react"; import { assert } from "tsafe/assert"; import { is } from "tsafe/is"; import { joinPath } from "./utils/joinPath"; import defaultLogoSvgUrl from "@keycloakify/keycloak-admin-ui/public/logo.svg"; export function KcAdminUiLoader(props) { const { KcAdminUi, loadingFallback, ...paramsOfInit } = props; assert(is(KcAdminUi)); useMemo(() => init(paramsOfInit), []); return (_jsx(Suspense, { fallback: loadingFallback, children: (() => { const node = _jsx(KcAdminUi, {}); if (node === null) { return loadingFallback; } return node; })() })); } let previousRunParamsFingerprint = undefined; function init(params) { var _a; exit_condition: { const paramsFingerprint = JSON.stringify(params); if (previousRunParamsFingerprint === undefined) { previousRunParamsFingerprint = paramsFingerprint; break exit_condition; } if (paramsFingerprint !== previousRunParamsFingerprint) { window.location.reload(); return; } return; } const { kcContext } = params; const logoUrl = (() => { var _a, _b; if ((_a = params.logoUrl) === null || _a === void 0 ? void 0 : _a.startsWith("data:")) { const error = new Error([ `ERROR: The logo url can't be a data url.`, `Due to the fact your logo is very small it has been inlined in the bundle.`, `The logoUrl you passed is: ${params.logoUrl.substring(0, 25)}...`, `To fix this issue you can put the logo in the public directory and import it like:`, `logoUrl={\`\${${["import", "meta", "env", "BASE_URL"].join(".")}}assets/logo.svg\`} if you are using Vite or`, `logoUrl={\`\${process.env.PUBLIC_URL}/assets/logo.svg\`} if you are using Webpack (CRA).`, `If it's an SVG you can also pad it's size with random \`<!-- xxx -->\` comments to make it bigger that it passes the threshold of 4KB.`, ].join("\n")); alert(error.message); throw error; } const logoUrl_params = (_b = params.logoUrl) !== null && _b !== void 0 ? _b : defaultLogoSvgUrl; const url = new URL(logoUrl_params.startsWith("http") ? logoUrl_params : joinPath(window.location.origin, logoUrl_params)); return url.href.substring(url.origin.length); })(); const resourceUrl = kcContext.resourceUrl; if (!logoUrl.startsWith(resourceUrl)) { const error = new Error(`ERROR: The logo url can't be an external url.`); alert(error.message); throw error; } const referrerUrl = readQueryParamOrRestoreFromSessionStorage({ name: "referrer_uri", }); const environment = { serverBaseUrl: kcContext.serverBaseUrl, adminBaseUrl: kcContext.adminBaseUrl, authUrl: kcContext.authUrl, authServerUrl: kcContext.authServerUrl, realm: (_a = kcContext.loginRealm) !== null && _a !== void 0 ? _a : "master", clientId: kcContext.clientId, resourceUrl: kcContext.resourceUrl, logo: logoUrl.substring(resourceUrl.length), logoUrl: referrerUrl === undefined ? "/" : referrerUrl.replace("_hash_", "#"), consoleBaseUrl: kcContext.consoleBaseUrl, masterRealm: kcContext.masterRealm, resourceVersion: kcContext.resourceVersion, }; { assert(); const script = document.createElement("script"); script.id = "environment"; script.type = "application/json"; script.textContent = JSON.stringify(environment, null, 1); document.body.appendChild(script); } } function readQueryParamOrRestoreFromSessionStorage(params) { var _a; const { name } = params; const url = new URL(window.location.href); const value = url.searchParams.get(name); const PREFIX = "keycloakify:"; if (value !== null) { sessionStorage.setItem(`${PREFIX}${name}`, value); url.searchParams.delete(name); window.history.replaceState({}, "", url.toString()); return value; } return (_a = sessionStorage.getItem(`${PREFIX}${name}`)) !== null && _a !== void 0 ? _a : undefined; } //# sourceMappingURL=KcAdminUiLoader.js.map