Reneame useStylesAndScript to useInitialize
This commit is contained in:
parent
e5ab46727a
commit
f137960f96
@ -1,9 +1,8 @@
|
|||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { assert } from "keycloakify/tools/assert";
|
|
||||||
import { clsx } from "keycloakify/tools/clsx";
|
import { clsx } from "keycloakify/tools/clsx";
|
||||||
import { getKcClsx } from "keycloakify/account/lib/kcClsx";
|
import { getKcClsx } from "keycloakify/account/lib/kcClsx";
|
||||||
import { useInsertLinkTags } from "keycloakify/tools/useInsertLinkTags";
|
|
||||||
import { useSetClassName } from "keycloakify/tools/useSetClassName";
|
import { useSetClassName } from "keycloakify/tools/useSetClassName";
|
||||||
|
import { useInitialize } from "keycloakify/account/Template.useInitialize";
|
||||||
import type { TemplateProps } from "keycloakify/account/TemplateProps";
|
import type { TemplateProps } from "keycloakify/account/TemplateProps";
|
||||||
import type { I18n } from "./i18n";
|
import type { I18n } from "./i18n";
|
||||||
import type { KcContext } from "./KcContext";
|
import type { KcContext } from "./KcContext";
|
||||||
@ -13,9 +12,9 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
|
|
||||||
const { kcClsx } = getKcClsx({ doUseDefaultCss, classes });
|
const { kcClsx } = getKcClsx({ doUseDefaultCss, classes });
|
||||||
|
|
||||||
const { msg, msgStr, getChangeLocaleUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n;
|
const { msg, msgStr, currentLanguage, enabledLanguages } = i18n;
|
||||||
|
|
||||||
const { locale, url, features, realm, message, referrer } = kcContext;
|
const { url, features, realm, message, referrer } = kcContext;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
document.title = msgStr("accountManagementTitle");
|
document.title = msgStr("accountManagementTitle");
|
||||||
@ -31,30 +30,9 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
className: clsx("admin-console", "user", kcClsx("kcBodyClass"))
|
className: clsx("admin-console", "user", kcClsx("kcBodyClass"))
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
const { isReadyToRender } = useInitialize({ kcContext, doUseDefaultCss });
|
||||||
const { currentLanguageTag } = locale ?? {};
|
|
||||||
|
|
||||||
if (currentLanguageTag === undefined) {
|
if (!isReadyToRender) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const html = document.querySelector("html");
|
|
||||||
assert(html !== null);
|
|
||||||
html.lang = currentLanguageTag;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const { areAllStyleSheetsLoaded } = useInsertLinkTags({
|
|
||||||
componentOrHookName: "Template",
|
|
||||||
hrefs: !doUseDefaultCss
|
|
||||||
? []
|
|
||||||
: [
|
|
||||||
`${url.resourcesCommonPath}/node_modules/patternfly/dist/css/patternfly.min.css`,
|
|
||||||
`${url.resourcesCommonPath}/node_modules/patternfly/dist/css/patternfly-additions.min.css`,
|
|
||||||
`${url.resourcesPath}/css/account.css`
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!areAllStyleSheetsLoaded) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,16 +48,16 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
<div className="navbar-collapse navbar-collapse-1">
|
<div className="navbar-collapse navbar-collapse-1">
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<ul className="nav navbar-nav navbar-utility">
|
<ul className="nav navbar-nav navbar-utility">
|
||||||
{realm.internationalizationEnabled && (assert(locale !== undefined), true) && locale.supported.length > 1 && (
|
{enabledLanguages.length > 1 && (
|
||||||
<li>
|
<li>
|
||||||
<div className="kc-dropdown" id="kc-locale-dropdown">
|
<div className="kc-dropdown" id="kc-locale-dropdown">
|
||||||
<a href="#" id="kc-current-locale-link">
|
<a href="#" id="kc-current-locale-link">
|
||||||
{labelBySupportedLanguageTag[currentLanguageTag]}
|
{currentLanguage.label}
|
||||||
</a>
|
</a>
|
||||||
<ul>
|
<ul>
|
||||||
{locale.supported.map(({ languageTag }) => (
|
{enabledLanguages.map(({ languageTag, label, href }) => (
|
||||||
<li key={languageTag} className="kc-dropdown-item">
|
<li key={languageTag} className="kc-dropdown-item">
|
||||||
<a href={getChangeLocaleUrl(languageTag)}>{labelBySupportedLanguageTag[languageTag]}</a>
|
<a href={href}>{label}</a>
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
|
35
src/account/Template.useInitialize.ts
Normal file
35
src/account/Template.useInitialize.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { assert } from "keycloakify/tools/assert";
|
||||||
|
import { useInsertLinkTags } from "keycloakify/tools/useInsertLinkTags";
|
||||||
|
import type { KcContext } from "keycloakify/account/KcContext";
|
||||||
|
|
||||||
|
export type KcContextLike = {
|
||||||
|
url: {
|
||||||
|
resourcesCommonPath: string;
|
||||||
|
resourcesPath: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
assert<keyof KcContextLike extends keyof KcContext ? true : false>();
|
||||||
|
assert<KcContext extends KcContextLike ? true : false>();
|
||||||
|
|
||||||
|
export function useInitialize(params: {
|
||||||
|
kcContext: KcContextLike;
|
||||||
|
doUseDefaultCss: boolean;
|
||||||
|
}) {
|
||||||
|
const { kcContext, doUseDefaultCss } = params;
|
||||||
|
|
||||||
|
const { url } = kcContext;
|
||||||
|
|
||||||
|
const { areAllStyleSheetsLoaded } = useInsertLinkTags({
|
||||||
|
componentOrHookName: "Template",
|
||||||
|
hrefs: !doUseDefaultCss
|
||||||
|
? []
|
||||||
|
: [
|
||||||
|
`${url.resourcesCommonPath}/node_modules/patternfly/dist/css/patternfly.min.css`,
|
||||||
|
`${url.resourcesCommonPath}/node_modules/patternfly/dist/css/patternfly-additions.min.css`,
|
||||||
|
`${url.resourcesPath}/css/account.css`
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
return { isReadyToRender: areAllStyleSheetsLoaded };
|
||||||
|
}
|
@ -3,7 +3,7 @@ import { clsx } from "keycloakify/tools/clsx";
|
|||||||
import type { TemplateProps } from "keycloakify/login/TemplateProps";
|
import type { TemplateProps } from "keycloakify/login/TemplateProps";
|
||||||
import { getKcClsx } from "keycloakify/login/lib/kcClsx";
|
import { getKcClsx } from "keycloakify/login/lib/kcClsx";
|
||||||
import { useSetClassName } from "keycloakify/tools/useSetClassName";
|
import { useSetClassName } from "keycloakify/tools/useSetClassName";
|
||||||
import { useStylesAndScripts } from "keycloakify/login/Template.useStylesAndScripts";
|
import { useInitialize } from "keycloakify/login/Template.useInitialize";
|
||||||
import type { I18n } from "./i18n";
|
import type { I18n } from "./i18n";
|
||||||
import type { KcContext } from "./KcContext";
|
import type { KcContext } from "./KcContext";
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
className: bodyClassName ?? kcClsx("kcBodyClass")
|
className: bodyClassName ?? kcClsx("kcBodyClass")
|
||||||
});
|
});
|
||||||
|
|
||||||
const { isReadyToRender } = useStylesAndScripts({ kcContext, doUseDefaultCss });
|
const { isReadyToRender } = useInitialize({ kcContext, doUseDefaultCss });
|
||||||
|
|
||||||
if (!isReadyToRender) {
|
if (!isReadyToRender) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -2,7 +2,7 @@ import { useEffect } from "react";
|
|||||||
import { assert } from "keycloakify/tools/assert";
|
import { assert } from "keycloakify/tools/assert";
|
||||||
import { useInsertScriptTags } from "keycloakify/tools/useInsertScriptTags";
|
import { useInsertScriptTags } from "keycloakify/tools/useInsertScriptTags";
|
||||||
import { useInsertLinkTags } from "keycloakify/tools/useInsertLinkTags";
|
import { useInsertLinkTags } from "keycloakify/tools/useInsertLinkTags";
|
||||||
import { KcContext } from "keycloakify/login/KcContext/KcContext";
|
import type { KcContext } from "keycloakify/login/KcContext";
|
||||||
|
|
||||||
export type KcContextLike = {
|
export type KcContextLike = {
|
||||||
url: {
|
url: {
|
||||||
@ -16,7 +16,7 @@ export type KcContextLike = {
|
|||||||
assert<keyof KcContextLike extends keyof KcContext ? true : false>();
|
assert<keyof KcContextLike extends keyof KcContext ? true : false>();
|
||||||
assert<KcContext extends KcContextLike ? true : false>();
|
assert<KcContext extends KcContextLike ? true : false>();
|
||||||
|
|
||||||
export function useStylesAndScripts(params: {
|
export function useInitialize(params: {
|
||||||
kcContext: KcContextLike;
|
kcContext: KcContextLike;
|
||||||
doUseDefaultCss: boolean;
|
doUseDefaultCss: boolean;
|
||||||
}) {
|
}) {
|
||||||
@ -61,9 +61,7 @@ export function useStylesAndScripts(params: {
|
|||||||
textContent: `
|
textContent: `
|
||||||
import { checkCookiesAndSetTimer } from "${url.resourcesPath}/js/authChecker.js";
|
import { checkCookiesAndSetTimer } from "${url.resourcesPath}/js/authChecker.js";
|
||||||
|
|
||||||
checkCookiesAndSetTimer(
|
checkCookiesAndSetTimer("${url.ssoLoginInOtherTabsUrl}");
|
||||||
"${url.ssoLoginInOtherTabsUrl}"
|
|
||||||
);
|
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
]
|
]
|
Loading…
x
Reference in New Issue
Block a user