2023-03-17 20:40:29 +01:00
|
|
|
import { useReducer, useEffect } from "react";
|
2023-03-19 23:12:45 +01:00
|
|
|
import { headInsert } from "keycloakify/tools/headInsert";
|
|
|
|
import { pathJoin } from "keycloakify/bin/tools/pathJoin";
|
|
|
|
import { clsx } from "keycloakify/tools/clsx";
|
2023-03-17 20:40:29 +01:00
|
|
|
|
|
|
|
export function usePrepareTemplate(params: {
|
|
|
|
doFetchDefaultThemeResources: boolean;
|
|
|
|
stylesCommon?: string[];
|
|
|
|
styles?: string[];
|
|
|
|
scripts?: string[];
|
|
|
|
url: {
|
|
|
|
resourcesCommonPath: string;
|
|
|
|
resourcesPath: string;
|
|
|
|
};
|
2023-03-20 05:14:25 +01:00
|
|
|
htmlClassName: string | undefined;
|
|
|
|
bodyClassName: string | undefined;
|
2023-03-17 20:40:29 +01:00
|
|
|
}) {
|
2023-03-20 05:14:25 +01:00
|
|
|
const { doFetchDefaultThemeResources, stylesCommon, styles, url, scripts, htmlClassName, bodyClassName } = params;
|
2023-03-17 20:40:29 +01:00
|
|
|
|
|
|
|
const [isReady, setReady] = useReducer(() => true, !doFetchDefaultThemeResources);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!doFetchDefaultThemeResources) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let isUnmounted = false;
|
|
|
|
|
|
|
|
Promise.all(
|
|
|
|
[
|
|
|
|
...(stylesCommon ?? []).map(relativePath => pathJoin(url.resourcesCommonPath, relativePath)),
|
|
|
|
...(styles ?? []).map(relativePath => pathJoin(url.resourcesPath, relativePath))
|
|
|
|
]
|
|
|
|
.reverse()
|
|
|
|
.map(href =>
|
|
|
|
headInsert({
|
|
|
|
"type": "css",
|
|
|
|
href,
|
|
|
|
"position": "prepend"
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).then(() => {
|
|
|
|
if (isUnmounted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setReady();
|
|
|
|
});
|
|
|
|
|
|
|
|
(scripts ?? []).forEach(relativePath =>
|
|
|
|
headInsert({
|
|
|
|
"type": "javascript",
|
|
|
|
"src": pathJoin(url.resourcesPath, relativePath)
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
isUnmounted = true;
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2023-03-20 05:14:25 +01:00
|
|
|
useSetClassName({
|
|
|
|
"target": "html",
|
|
|
|
"className": htmlClassName
|
|
|
|
});
|
|
|
|
|
|
|
|
useSetClassName({
|
|
|
|
"target": "body",
|
|
|
|
"className": bodyClassName
|
|
|
|
});
|
|
|
|
|
|
|
|
return { isReady };
|
|
|
|
}
|
|
|
|
|
|
|
|
function useSetClassName(params: { target: "html" | "body"; className: string | undefined }) {
|
|
|
|
const { target, className } = params;
|
|
|
|
|
2023-03-17 20:40:29 +01:00
|
|
|
useEffect(() => {
|
2023-03-20 05:14:25 +01:00
|
|
|
if (className === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:40:29 +01:00
|
|
|
const htmlClassList = document.getElementsByTagName("html")[0].classList;
|
|
|
|
|
2023-03-20 05:14:25 +01:00
|
|
|
const tokens = clsx(target).split(" ");
|
2023-03-17 20:40:29 +01:00
|
|
|
|
|
|
|
htmlClassList.add(...tokens);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
htmlClassList.remove(...tokens);
|
|
|
|
};
|
2023-03-20 05:14:25 +01:00
|
|
|
}, [className]);
|
2023-03-17 20:40:29 +01:00
|
|
|
}
|