// Copy pasted from: https://github.com/InseeFrLab/keycloakify/blob/main/src/lib/components/shared/Template.tsx import { useReducer, useEffect } from "react"; // You can replace all relative imports by cherry picking files from the keycloakify module. // For example, the following import: // import { headInsert } from "./tools/headInsert"; // becomes: import { headInsert } from "keycloakify/lib/tools/headInsert"; import { assert } from "keycloakify/lib/tools/assert"; import { clsx } from "keycloakify/lib/tools/clsx"; import { pathJoin } from "keycloakify/bin/tools/pathJoin"; import type { TemplateProps } from "keycloakify/lib/KcProps"; //import type { KcContextBase } from "keycloakify/lib/getKcContext"; import type { KcContext } from "./kcContext"; // Here Instead of KcContextBase.Common you should provide your own context import type { I18n } from "./i18n"; export default function Template(props: TemplateProps) { const { displayInfo = false, displayMessage = true, displayRequiredFields = false, displayWide = false, showAnotherWayIfPresent = true, headerNode, showUsernameNode = null, formNode, infoNode = null, kcContext, i18n, doFetchDefaultThemeResources } = props; const { msg, changeLocale, labelBySupportedLanguageTag, currentLanguageTag } = i18n; const { realm, locale, auth, url, message, isAppInitiatedAction } = kcContext; const [isExtraCssLoaded, setExtraCssLoaded] = useReducer(() => true, false); useEffect(() => { if (!doFetchDefaultThemeResources) { setExtraCssLoaded(); return; } let isUnmounted = false; const cleanups: (() => void)[] = []; const toArr = (x: string | readonly string[] | undefined) => (typeof x === "string" ? x.split(" ") : x ?? []); Promise.all( [ ...toArr(props.stylesCommon).map(relativePath => pathJoin(url.resourcesCommonPath, relativePath)), ...toArr(props.styles).map(relativePath => pathJoin(url.resourcesPath, relativePath)) ] .reverse() .map(href => headInsert({ "type": "css", href, "position": "prepend" }) ) ).then(() => { if (isUnmounted) { return; } setExtraCssLoaded(); }); toArr(props.scripts).forEach(relativePath => headInsert({ "type": "javascript", "src": pathJoin(url.resourcesPath, relativePath) }) ); if (props.kcHtmlClass !== undefined) { const htmlClassList = document.getElementsByTagName("html")[0].classList; const tokens = clsx(props.kcHtmlClass).split(" "); htmlClassList.add(...tokens); cleanups.push(() => htmlClassList.remove(...tokens)); } return () => { isUnmounted = true; cleanups.forEach(f => f()); }; }, [props.kcHtmlClass]); if (!isExtraCssLoaded) { return null; } return (
{msg("loginTitleHtml", realm.displayNameHtml)}
{realm.internationalizationEnabled && (assert(locale !== undefined), true) && locale.supported.length > 1 && (
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */} {labelBySupportedLanguageTag[currentLanguageTag]}
)} {!(auth !== undefined && auth.showUsername && !auth.showResetCredentials) ? ( displayRequiredFields ? (
* {msg("requiredFields")}

{headerNode}

) : (

{headerNode}

) ) : displayRequiredFields ? (
* {msg("requiredFields")}
{showUsernameNode}
{msg("restartLoginTooltip")}
) : ( <> {showUsernameNode}
{msg("restartLoginTooltip")}
)}
{/* App-initiated actions should not see warning messages about the need to complete the action during login. */} {displayMessage && message !== undefined && (message.type !== "warning" || !isAppInitiatedAction) && (
{message.type === "success" && } {message.type === "warning" && } {message.type === "error" && } {message.type === "info" && }
)} {formNode} {auth !== undefined && auth.showTryAnotherWayLink && showAnotherWayIfPresent && (
)} {displayInfo && (
{infoNode}
)}
); }