Rework Terms
This commit is contained in:
parent
41739c8528
commit
aa9b7cccc7
@ -66,7 +66,7 @@
|
||||
"react": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"react-markdown": "^5.0.3",
|
||||
"react-markdown": "^9.0.1",
|
||||
"tsafe": "^1.6.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
@ -1,10 +1,8 @@
|
||||
import "keycloakify/tools/Object.fromEntries";
|
||||
import { useEffect, useState } from "react";
|
||||
import { assert } from "tsafe/assert";
|
||||
import messages_fallbackLanguage from "./baseMessages/en";
|
||||
import { getMessages } from "./baseMessages";
|
||||
import type { KcContext } from "../KcContext";
|
||||
import { Reflect } from "tsafe/Reflect";
|
||||
|
||||
export const fallbackLanguageTag = "en";
|
||||
|
||||
@ -88,7 +86,9 @@ export type GenericI18n<MessageKey extends string> = {
|
||||
isFetchingTranslations: boolean;
|
||||
};
|
||||
|
||||
function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: { [languageTag: string]: { [key in ExtraMessageKey]: string } }) {
|
||||
export function createGetI18n<ExtraMessageKey extends string = never>(messageBundle: {
|
||||
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||
}) {
|
||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||
|
||||
type Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined };
|
||||
@ -126,8 +126,8 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
|
||||
const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
|
||||
messages_fallbackLanguage,
|
||||
extraMessages_fallbackLanguage: extraMessages[fallbackLanguageTag],
|
||||
extraMessages: extraMessages[partialI18n.currentLanguageTag]
|
||||
messageBundle_fallbackLanguage: messageBundle[fallbackLanguageTag],
|
||||
messageBundle_currentLanguage: messageBundle[partialI18n.currentLanguageTag]
|
||||
});
|
||||
|
||||
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
|
||||
@ -135,17 +135,19 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
const result: Result = {
|
||||
i18n: {
|
||||
...partialI18n,
|
||||
...createI18nTranslationFunctions({ messages: undefined }),
|
||||
...createI18nTranslationFunctions({
|
||||
messages_currentLanguage: isCurrentLanguageFallbackLanguage ? messages_fallbackLanguage : undefined
|
||||
}),
|
||||
isFetchingTranslations: !isCurrentLanguageFallbackLanguage
|
||||
},
|
||||
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
|
||||
? undefined
|
||||
: (async () => {
|
||||
const messages = await getMessages(partialI18n.currentLanguageTag);
|
||||
const messages_currentLanguage = await getMessages(partialI18n.currentLanguageTag);
|
||||
|
||||
const i18n_currentLanguage: I18n = {
|
||||
...partialI18n,
|
||||
...createI18nTranslationFunctions({ messages }),
|
||||
...createI18nTranslationFunctions({ messages_currentLanguage }),
|
||||
isFetchingTranslations: false
|
||||
};
|
||||
|
||||
@ -168,66 +170,30 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
return { getI18n };
|
||||
}
|
||||
|
||||
export function createUseI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||
}) {
|
||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||
|
||||
const { getI18n } = createGetI18n(extraMessages);
|
||||
|
||||
function useI18n(params: { kcContext: KcContextLike }): { i18n: I18n } {
|
||||
const { kcContext } = params;
|
||||
|
||||
const { i18n, prI18n_currentLanguage } = getI18n({ kcContext });
|
||||
|
||||
const [i18n_toReturn, setI18n_toReturn] = useState<I18n>(i18n);
|
||||
|
||||
useEffect(() => {
|
||||
let isActive = true;
|
||||
|
||||
prI18n_currentLanguage?.then(i18n => {
|
||||
if (!isActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
setI18n_toReturn(i18n);
|
||||
});
|
||||
|
||||
return () => {
|
||||
isActive = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { i18n: i18n_toReturn };
|
||||
}
|
||||
|
||||
return { useI18n, ofTypeI18n: Reflect<I18n>() };
|
||||
}
|
||||
|
||||
function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraMessageKey extends string>(params: {
|
||||
messages_fallbackLanguage: Record<MessageKey, string>;
|
||||
extraMessages_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||
extraMessages: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||
messageBundle_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||
messageBundle_currentLanguage: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||
}) {
|
||||
const { extraMessages } = params;
|
||||
const { messageBundle_currentLanguage } = params;
|
||||
|
||||
const messages_fallbackLanguage = {
|
||||
...params.messages_fallbackLanguage,
|
||||
...params.extraMessages_fallbackLanguage
|
||||
...params.messageBundle_fallbackLanguage
|
||||
};
|
||||
|
||||
function createI18nTranslationFunctions(params: {
|
||||
messages: Partial<Record<MessageKey, string>> | undefined;
|
||||
messages_currentLanguage: Partial<Record<MessageKey, string>> | undefined;
|
||||
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
|
||||
const messages = {
|
||||
...params.messages,
|
||||
...extraMessages
|
||||
const messages_currentLanguage = {
|
||||
...params.messages_currentLanguage,
|
||||
...messageBundle_currentLanguage
|
||||
};
|
||||
|
||||
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
|
||||
const { key, args, doRenderAsHtml } = props;
|
||||
|
||||
const messageOrUndefined: string | undefined = (messages as any)[key] ?? (messages_fallbackLanguage as any)[key];
|
||||
const messageOrUndefined: string | undefined = (messages_currentLanguage as any)[key] ?? (messages_fallbackLanguage as any)[key];
|
||||
|
||||
if (messageOrUndefined === undefined) {
|
||||
return undefined;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
|
||||
export type { MessageKey, KcContextLike };
|
||||
export type I18n = GenericI18n<MessageKey>;
|
||||
export { createUseI18n } from "./i18n";
|
||||
export { createUseI18n } from "./useI18n";
|
||||
export { fallbackLanguageTag } from "./i18n";
|
||||
|
44
src/account/i18n/useI18n.ts
Normal file
44
src/account/i18n/useI18n.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import {
|
||||
createGetI18n,
|
||||
type GenericI18n,
|
||||
type MessageKey,
|
||||
type KcContextLike
|
||||
} from "./i18n";
|
||||
import { Reflect } from "tsafe/Reflect";
|
||||
|
||||
export function createUseI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||
}) {
|
||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||
|
||||
const { getI18n } = createGetI18n(extraMessages);
|
||||
|
||||
function useI18n(params: { kcContext: KcContextLike }): { i18n: I18n } {
|
||||
const { kcContext } = params;
|
||||
|
||||
const { i18n, prI18n_currentLanguage } = getI18n({ kcContext });
|
||||
|
||||
const [i18n_toReturn, setI18n_toReturn] = useState<I18n>(i18n);
|
||||
|
||||
useEffect(() => {
|
||||
let isActive = true;
|
||||
|
||||
prI18n_currentLanguage?.then(i18n => {
|
||||
if (!isActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
setI18n_toReturn(i18n);
|
||||
});
|
||||
|
||||
return () => {
|
||||
isActive = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { i18n: i18n_toReturn };
|
||||
}
|
||||
|
||||
return { useI18n, ofTypeI18n: Reflect<I18n>() };
|
||||
}
|
@ -8,8 +8,7 @@ import { assert } from "tsafe/assert";
|
||||
import {
|
||||
type ThemeType,
|
||||
basenameOfTheKeycloakifyResourcesDir,
|
||||
resources_common,
|
||||
nameOfTheLocalizationRealmOverridesUserProfileProperty
|
||||
resources_common
|
||||
} from "../../shared/constants";
|
||||
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
|
||||
|
||||
@ -119,10 +118,6 @@ export function generateFtlFilesCodeFactory(params: {
|
||||
.replace("KEYCLOAKIFY_THEME_TYPE_dExKd3xEdr", themeType)
|
||||
.replace("KEYCLOAKIFY_THEME_NAME_cXxKd3xEer", themeName)
|
||||
.replace("RESOURCES_COMMON_cLsLsMrtDkpVv", resources_common)
|
||||
.replace(
|
||||
"lOCALIZATION_REALM_OVERRIDES_USER_PROFILE_PROPERTY_KEY_aaGLsPgGIdeeX",
|
||||
nameOfTheLocalizationRealmOverridesUserProfileProperty
|
||||
)
|
||||
.replace(
|
||||
"USER_DEFINED_EXCLUSIONS_eKsaY4ZsZ4eMr2",
|
||||
buildContext.kcContextExclusionsFtlCode ?? ""
|
||||
|
@ -33,8 +33,9 @@ kcContext.pageId = "${pageId}";
|
||||
if( kcContext.url && kcContext.url.resourcesPath ){
|
||||
kcContext.url.resourcesCommonPath = kcContext.url.resourcesPath + "/" + "RESOURCES_COMMON_cLsLsMrtDkpVv";
|
||||
}
|
||||
kcContext["x-keycloakify"] = {};
|
||||
<#if profile?? && profile.attributes??>
|
||||
kcContext.lOCALIZATION_REALM_OVERRIDES_USER_PROFILE_PROPERTY_KEY_aaGLsPgGIdeeX = {
|
||||
kcContext["x-keycloakify"].realmMessageBundleUserProfile = {
|
||||
<#list profile.attributes as attribute>
|
||||
<#if attribute.annotations?? && attribute.displayName??>
|
||||
"${attribute.displayName}": decodeHtmlEntities("${advancedMsg(attribute.displayName)?js_string}"),
|
||||
@ -61,6 +62,9 @@ if( kcContext.url && kcContext.url.resourcesPath ){
|
||||
</#list>
|
||||
};
|
||||
</#if>
|
||||
<#if pageId == "terms.ftl" || termsAcceptanceRequired?? && termsAcceptanceRequired>
|
||||
kcContext["x-keycloakify"].realmMessageBundleTermsText= decodeHtmlEntities("${msg("termsText")?js_string}");
|
||||
</#if>
|
||||
attributes_to_attributesByName: {
|
||||
if( !kcContext.profile ){
|
||||
break attributes_to_attributesByName;
|
||||
@ -198,6 +202,9 @@ function decodeHtmlEntities(htmlStr){
|
||||
) || (
|
||||
key == "execution" &&
|
||||
are_same_path(path, [])
|
||||
) || (
|
||||
key == "entity" &&
|
||||
are_same_path(path, ["user"])
|
||||
)
|
||||
>
|
||||
<#-- <#local out_seq += ["/*" + path?join(".") + "." + key + " excluded*/"]> -->
|
||||
|
@ -1,5 +1,3 @@
|
||||
export const nameOfTheLocalizationRealmOverridesUserProfileProperty =
|
||||
"__localizationRealmOverridesUserProfile";
|
||||
export const keycloak_resources = "keycloak-resources";
|
||||
export const resources_common = "resources-common";
|
||||
export const lastKeycloakVersionWithAccountV1 = "21.1.2";
|
||||
|
@ -1,8 +1,4 @@
|
||||
import type {
|
||||
ThemeType,
|
||||
LoginThemePageId,
|
||||
nameOfTheLocalizationRealmOverridesUserProfileProperty
|
||||
} from "keycloakify/bin/shared/constants";
|
||||
import type { ThemeType, LoginThemePageId } from "keycloakify/bin/shared/constants";
|
||||
import type { ExtractAfterStartingWith } from "keycloakify/tools/ExtractAfterStartingWith";
|
||||
import type { ValueOf } from "keycloakify/tools/ValueOf";
|
||||
import { assert } from "tsafe/assert";
|
||||
@ -158,7 +154,10 @@ export declare namespace KcContext {
|
||||
ssoLoginInOtherTabsUrl: string;
|
||||
};
|
||||
properties: {};
|
||||
__localizationRealmOverridesUserProfile?: Record<string, string>;
|
||||
"x-keycloakify": {
|
||||
realmMessageBundleUserProfile: Record<string, string> | undefined;
|
||||
realmMessageBundleTermsText: string | undefined;
|
||||
};
|
||||
};
|
||||
|
||||
export type SamlPostForm = Common & {
|
||||
@ -276,6 +275,7 @@ export declare namespace KcContext {
|
||||
lastName?: string;
|
||||
markedForEviction?: boolean;
|
||||
};
|
||||
__localizationRealmOverridesTermsText?: string;
|
||||
};
|
||||
|
||||
export type LoginDeviceVerifyUserCode = Common & {
|
||||
@ -772,11 +772,3 @@ export type PasswordPolicies = {
|
||||
/** Whether the password can be the email address */
|
||||
notEmail?: boolean;
|
||||
};
|
||||
|
||||
assert<
|
||||
KcContext.Common extends Partial<
|
||||
Record<typeof nameOfTheLocalizationRealmOverridesUserProfileProperty, unknown>
|
||||
>
|
||||
? true
|
||||
: false
|
||||
>();
|
||||
|
@ -161,7 +161,10 @@ export const kcContextCommonMock: KcContext.Common = {
|
||||
scripts: [],
|
||||
isAppInitiatedAction: false,
|
||||
properties: {},
|
||||
__localizationRealmOverridesUserProfile: {}
|
||||
"x-keycloakify": {
|
||||
realmMessageBundleUserProfile: undefined,
|
||||
realmMessageBundleTermsText: undefined
|
||||
}
|
||||
};
|
||||
|
||||
const loginUrl = {
|
||||
|
@ -1,10 +1,8 @@
|
||||
import "keycloakify/tools/Object.fromEntries";
|
||||
import { useEffect, useState } from "react";
|
||||
import { assert } from "tsafe/assert";
|
||||
import messages_fallbackLanguage from "./baseMessages/en";
|
||||
import { getMessages } from "./baseMessages";
|
||||
import type { KcContext } from "../KcContext";
|
||||
import { Reflect } from "tsafe/Reflect";
|
||||
|
||||
export const fallbackLanguageTag = "en";
|
||||
|
||||
@ -13,7 +11,10 @@ export type KcContextLike = {
|
||||
currentLanguageTag: string;
|
||||
supported: { languageTag: string; url: string; label: string }[];
|
||||
};
|
||||
__localizationRealmOverridesUserProfile?: Record<string, string>;
|
||||
"x-keycloakify": {
|
||||
realmMessageBundleUserProfile: Record<string, string> | undefined;
|
||||
realmMessageBundleTermsText: string | undefined;
|
||||
};
|
||||
};
|
||||
|
||||
assert<KcContext extends KcContextLike ? true : false>();
|
||||
@ -89,7 +90,9 @@ export type GenericI18n<MessageKey extends string> = {
|
||||
isFetchingTranslations: boolean;
|
||||
};
|
||||
|
||||
function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: { [languageTag: string]: { [key in ExtraMessageKey]: string } }) {
|
||||
export function createGetI18n<ExtraMessageKey extends string = never>(messageBundle: {
|
||||
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||
}) {
|
||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||
|
||||
type Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined };
|
||||
@ -127,9 +130,10 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
|
||||
const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
|
||||
messages_fallbackLanguage,
|
||||
extraMessages_fallbackLanguage: extraMessages[fallbackLanguageTag],
|
||||
extraMessages: extraMessages[partialI18n.currentLanguageTag],
|
||||
__localizationRealmOverridesUserProfile: kcContext.__localizationRealmOverridesUserProfile
|
||||
messageBundle_fallbackLanguage: messageBundle[fallbackLanguageTag],
|
||||
messageBundle_currentLanguage: messageBundle[partialI18n.currentLanguageTag],
|
||||
realmMessageBundleUserProfile: kcContext["x-keycloakify"].realmMessageBundleUserProfile,
|
||||
realmMessageBundleTermsText: kcContext["x-keycloakify"].realmMessageBundleTermsText
|
||||
});
|
||||
|
||||
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
|
||||
@ -137,17 +141,19 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
const result: Result = {
|
||||
i18n: {
|
||||
...partialI18n,
|
||||
...createI18nTranslationFunctions({ messages: undefined }),
|
||||
...createI18nTranslationFunctions({
|
||||
messages_currentLanguage: isCurrentLanguageFallbackLanguage ? messages_fallbackLanguage : undefined
|
||||
}),
|
||||
isFetchingTranslations: !isCurrentLanguageFallbackLanguage
|
||||
},
|
||||
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
|
||||
? undefined
|
||||
: (async () => {
|
||||
const messages = await getMessages(partialI18n.currentLanguageTag);
|
||||
const messages_currentLanguage = await getMessages(partialI18n.currentLanguageTag);
|
||||
|
||||
const i18n_currentLanguage: I18n = {
|
||||
...partialI18n,
|
||||
...createI18nTranslationFunctions({ messages }),
|
||||
...createI18nTranslationFunctions({ messages_currentLanguage }),
|
||||
isFetchingTranslations: false
|
||||
};
|
||||
|
||||
@ -170,67 +176,48 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
return { getI18n };
|
||||
}
|
||||
|
||||
export function createUseI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||
}) {
|
||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||
|
||||
const { getI18n } = createGetI18n(extraMessages);
|
||||
|
||||
function useI18n(params: { kcContext: KcContextLike }): { i18n: I18n } {
|
||||
const { kcContext } = params;
|
||||
|
||||
const { i18n, prI18n_currentLanguage } = getI18n({ kcContext });
|
||||
|
||||
const [i18n_toReturn, setI18n_toReturn] = useState<I18n>(i18n);
|
||||
|
||||
useEffect(() => {
|
||||
let isActive = true;
|
||||
|
||||
prI18n_currentLanguage?.then(i18n => {
|
||||
if (!isActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
setI18n_toReturn(i18n);
|
||||
});
|
||||
|
||||
return () => {
|
||||
isActive = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { i18n: i18n_toReturn };
|
||||
}
|
||||
|
||||
return { useI18n, ofTypeI18n: Reflect<I18n>() };
|
||||
}
|
||||
|
||||
function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraMessageKey extends string>(params: {
|
||||
messages_fallbackLanguage: Record<MessageKey, string>;
|
||||
extraMessages_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||
extraMessages: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||
__localizationRealmOverridesUserProfile: Record<string, string> | undefined;
|
||||
messageBundle_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||
messageBundle_currentLanguage: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||
realmMessageBundleUserProfile: Record<string, string> | undefined;
|
||||
realmMessageBundleTermsText: string | undefined;
|
||||
}) {
|
||||
const { __localizationRealmOverridesUserProfile, extraMessages } = params;
|
||||
const { messageBundle_currentLanguage, realmMessageBundleUserProfile, realmMessageBundleTermsText } = params;
|
||||
|
||||
const messages_fallbackLanguage = {
|
||||
...params.messages_fallbackLanguage,
|
||||
...params.extraMessages_fallbackLanguage
|
||||
...params.messageBundle_fallbackLanguage
|
||||
};
|
||||
|
||||
function createI18nTranslationFunctions(params: {
|
||||
messages: Partial<Record<MessageKey, string>> | undefined;
|
||||
messages_currentLanguage: Partial<Record<MessageKey, string>> | undefined;
|
||||
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
|
||||
const messages = {
|
||||
...params.messages,
|
||||
...extraMessages
|
||||
const messages_currentLanguage = {
|
||||
...params.messages_currentLanguage,
|
||||
...messageBundle_currentLanguage
|
||||
};
|
||||
|
||||
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
|
||||
const { key, args, doRenderAsHtml } = props;
|
||||
|
||||
const messageOrUndefined: string | undefined = (messages as any)[key] ?? (messages_fallbackLanguage as any)[key];
|
||||
const messageOrUndefined: string | undefined = (() => {
|
||||
const messageOrUndefined = (messages_currentLanguage as any)[key] ?? (messages_fallbackLanguage as any)[key];
|
||||
|
||||
if (key === "termsText") {
|
||||
if (params.messages_currentLanguage === undefined) {
|
||||
return " ";
|
||||
}
|
||||
|
||||
if (realmMessageBundleTermsText !== messageOrUndefined) {
|
||||
return realmMessageBundleTermsText;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
return messageOrUndefined;
|
||||
})();
|
||||
|
||||
if (messageOrUndefined === undefined) {
|
||||
return undefined;
|
||||
@ -281,8 +268,8 @@ function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraM
|
||||
function resolveMsgAdvanced(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): JSX.Element | string {
|
||||
const { key, args, doRenderAsHtml } = props;
|
||||
|
||||
if (__localizationRealmOverridesUserProfile !== undefined && key in __localizationRealmOverridesUserProfile) {
|
||||
const resolvedMessage = __localizationRealmOverridesUserProfile[key];
|
||||
if (realmMessageBundleUserProfile !== undefined && key in realmMessageBundleUserProfile) {
|
||||
const resolvedMessage = realmMessageBundleUserProfile[key];
|
||||
|
||||
return doRenderAsHtml ? (
|
||||
<span
|
||||
|
@ -1,5 +1,5 @@
|
||||
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
|
||||
export type { MessageKey, KcContextLike };
|
||||
export type I18n = GenericI18n<MessageKey>;
|
||||
export { createUseI18n } from "./i18n";
|
||||
export { createUseI18n } from "./useI18n";
|
||||
export { fallbackLanguageTag } from "./i18n";
|
||||
|
44
src/login/i18n/useI18n.ts
Normal file
44
src/login/i18n/useI18n.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import {
|
||||
createGetI18n,
|
||||
type GenericI18n,
|
||||
type MessageKey,
|
||||
type KcContextLike
|
||||
} from "./i18n";
|
||||
import { Reflect } from "tsafe/Reflect";
|
||||
|
||||
export function createUseI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||
}) {
|
||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||
|
||||
const { getI18n } = createGetI18n(extraMessages);
|
||||
|
||||
function useI18n(params: { kcContext: KcContextLike }): { i18n: I18n } {
|
||||
const { kcContext } = params;
|
||||
|
||||
const { i18n, prI18n_currentLanguage } = getI18n({ kcContext });
|
||||
|
||||
const [i18n_toReturn, setI18n_toReturn] = useState<I18n>(i18n);
|
||||
|
||||
useEffect(() => {
|
||||
let isActive = true;
|
||||
|
||||
prI18n_currentLanguage?.then(i18n => {
|
||||
if (!isActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
setI18n_toReturn(i18n);
|
||||
});
|
||||
|
||||
return () => {
|
||||
isActive = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { i18n: i18n_toReturn };
|
||||
}
|
||||
|
||||
return { useI18n, ofTypeI18n: Reflect<I18n>() };
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
import { fallbackLanguageTag } from "keycloakify/login/i18n";
|
||||
import { assert } from "tsafe/assert";
|
||||
import {
|
||||
createStatefulObservable,
|
||||
useRerenderOnChange
|
||||
} from "keycloakify/tools/StatefulObservable";
|
||||
import { useOnFistMount } from "keycloakify/tools/useOnFirstMount";
|
||||
import { KcContext } from "../KcContext";
|
||||
|
||||
const obs = createStatefulObservable<
|
||||
| {
|
||||
termsMarkdown: string;
|
||||
termsLanguageTag: string | undefined;
|
||||
}
|
||||
| undefined
|
||||
>(() => undefined);
|
||||
|
||||
export type KcContextLike = {
|
||||
pageId: string;
|
||||
locale?: {
|
||||
currentLanguageTag: string;
|
||||
};
|
||||
termsAcceptanceRequired?: boolean;
|
||||
};
|
||||
|
||||
assert<KcContext extends KcContextLike ? true : false>();
|
||||
|
||||
/** Allow to avoid bundling the terms and download it on demand*/
|
||||
export function useDownloadTerms(params: {
|
||||
kcContext: KcContextLike;
|
||||
downloadTermsMarkdown: (params: {
|
||||
currentLanguageTag: string;
|
||||
}) => Promise<{ termsMarkdown: string; termsLanguageTag: string | undefined }>;
|
||||
}) {
|
||||
const { kcContext, downloadTermsMarkdown } = params;
|
||||
|
||||
useOnFistMount(async () => {
|
||||
if (kcContext.pageId === "terms.ftl" || kcContext.termsAcceptanceRequired) {
|
||||
obs.current = await downloadTermsMarkdown({
|
||||
currentLanguageTag:
|
||||
kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useTermsMarkdown() {
|
||||
useRerenderOnChange(obs);
|
||||
|
||||
if (obs.current === undefined) {
|
||||
return { isDownloadComplete: false as const };
|
||||
}
|
||||
|
||||
const { termsMarkdown, termsLanguageTag } = obs.current;
|
||||
|
||||
return { isDownloadComplete: true, termsMarkdown, termsLanguageTag };
|
||||
}
|
88
src/login/lib/useDownloadTerms.tsx
Normal file
88
src/login/lib/useDownloadTerms.tsx
Normal file
@ -0,0 +1,88 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { fallbackLanguageTag } from "keycloakify/login/i18n";
|
||||
import { assert } from "tsafe/assert";
|
||||
import { createStatefulObservable, useRerenderOnChange } from "keycloakify/tools/StatefulObservable";
|
||||
import { useOnFistMount } from "keycloakify/tools/useOnFirstMount";
|
||||
import { KcContext } from "../KcContext";
|
||||
import type { Options as ReactMarkdownOptions } from "../../tools/react-markdown";
|
||||
|
||||
const obs = createStatefulObservable<
|
||||
| {
|
||||
ReactMarkdown: (props: Readonly<ReactMarkdownOptions>) => JSX.Element;
|
||||
termsMarkdown: string;
|
||||
}
|
||||
| undefined
|
||||
>(() => undefined);
|
||||
|
||||
export type KcContextLike_useDownloadTerms = {
|
||||
pageId: string;
|
||||
locale?: {
|
||||
currentLanguageTag: string;
|
||||
};
|
||||
termsAcceptanceRequired?: boolean;
|
||||
};
|
||||
|
||||
assert<KcContext extends KcContextLike_useDownloadTerms ? true : false>();
|
||||
|
||||
/** Allow to avoid bundling the terms and download it on demand*/
|
||||
export function useDownloadTerms(params: {
|
||||
kcContext: KcContextLike_useDownloadTerms;
|
||||
downloadTermsMarkdown: (params: { currentLanguageTag: string }) => Promise<{ termsMarkdown: string; termsLanguageTag: string | undefined }>;
|
||||
}) {
|
||||
const { kcContext, downloadTermsMarkdown } = params;
|
||||
|
||||
useOnFistMount(async () => {
|
||||
if (kcContext.pageId === "terms.ftl" || kcContext.termsAcceptanceRequired) {
|
||||
const currentLanguageTag = kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag;
|
||||
|
||||
const [ReactMarkdown_base, { termsMarkdown, termsLanguageTag }] = await Promise.all([
|
||||
import("../../tools/react-markdown").then(_ => _.default),
|
||||
downloadTermsMarkdown({ currentLanguageTag })
|
||||
] as const);
|
||||
|
||||
const htmlLang = termsLanguageTag !== currentLanguageTag ? termsLanguageTag : undefined;
|
||||
|
||||
const ReactMarkdown: (props: Readonly<ReactMarkdownOptions>) => JSX.Element =
|
||||
htmlLang === undefined
|
||||
? ReactMarkdown_base
|
||||
: props => {
|
||||
const [anchor, setAnchor] = useState<HTMLDivElement | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (anchor === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const parent = anchor.parentElement;
|
||||
|
||||
assert(parent !== null);
|
||||
|
||||
parent.setAttribute("lang", htmlLang);
|
||||
|
||||
anchor.remove();
|
||||
}, [anchor]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<ReactMarkdown_base {...props} />
|
||||
<div ref={setAnchor} style={{ display: "none" }} aria-hidden />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
obs.current = { ReactMarkdown, termsMarkdown };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useTermsMarkdown() {
|
||||
useRerenderOnChange(obs);
|
||||
|
||||
if (obs.current === undefined) {
|
||||
return { isDownloadComplete: false as const };
|
||||
}
|
||||
|
||||
const { ReactMarkdown, termsMarkdown } = obs.current;
|
||||
|
||||
return { isDownloadComplete: true, ReactMarkdown, termsMarkdown };
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
import { useState } from "react";
|
||||
import { Markdown } from "keycloakify/tools/Markdown";
|
||||
import type { LazyOrNot } from "keycloakify/tools/LazyOrNot";
|
||||
import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
|
||||
import { getKcClsx, type KcClsx } from "keycloakify/login/lib/kcClsx";
|
||||
@ -78,23 +77,14 @@ export default function Register(props: RegisterProps) {
|
||||
function TermsAcceptance(props: { i18n: I18n; kcClsx: KcClsx; messagesPerField: Pick<KcContext["messagesPerField"], "existsError" | "get"> }) {
|
||||
const { i18n, kcClsx, messagesPerField } = props;
|
||||
|
||||
const { msg } = i18n;
|
||||
|
||||
// NOTE: Refer to https://docs.keycloakify.dev/terms-and-conditions to load your terms and conditions.
|
||||
const { termsMarkdown } = useTermsMarkdown();
|
||||
|
||||
if (termsMarkdown === undefined) {
|
||||
return null;
|
||||
}
|
||||
const { msg, msgStr } = i18n;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="form-group">
|
||||
<div className={kcClsx("kcInputWrapperClass")}>
|
||||
{msg("termsTitle")}
|
||||
<div id="kc-registration-terms-text">
|
||||
<Markdown>{termsMarkdown}</Markdown>
|
||||
</div>
|
||||
<div id="kc-registration-terms-text">{msgStr("termsText") ? msg("termsText") : <TermsMarkdown />}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
@ -121,3 +111,13 @@ function TermsAcceptance(props: { i18n: I18n; kcClsx: KcClsx; messagesPerField:
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function TermsMarkdown() {
|
||||
const { isDownloadComplete, termsMarkdown, ReactMarkdown } = useTermsMarkdown();
|
||||
|
||||
if (!isDownloadComplete) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return <ReactMarkdown>{termsMarkdown}</ReactMarkdown>;
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
import { Markdown } from "keycloakify/tools/Markdown";
|
||||
import { getKcClsx } from "keycloakify/login/lib/kcClsx";
|
||||
import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
|
||||
import type { PageProps } from "keycloakify/login/pages/PageProps";
|
||||
@ -15,13 +14,7 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
|
||||
|
||||
const { msg, msgStr } = i18n;
|
||||
|
||||
const { locale, url } = kcContext;
|
||||
|
||||
const { isDownloadComplete, termsMarkdown, termsLanguageTag } = useTermsMarkdown();
|
||||
|
||||
if (!isDownloadComplete) {
|
||||
return null;
|
||||
}
|
||||
const { url } = kcContext;
|
||||
|
||||
return (
|
||||
<Template
|
||||
@ -32,9 +25,7 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
|
||||
displayMessage={false}
|
||||
headerNode={msg("termsTitle")}
|
||||
>
|
||||
<div id="kc-terms-text" lang={termsLanguageTag !== locale?.currentLanguageTag ? termsLanguageTag : undefined}>
|
||||
<Markdown>{termsMarkdown}</Markdown>
|
||||
</div>
|
||||
<div id="kc-terms-text">{msgStr("termsText") ? msg("termsText") : <TermsMarkdown />}</div>
|
||||
<form className="form-actions" action={url.loginAction} method="POST">
|
||||
<input
|
||||
className={kcClsx("kcButtonClass", "kcButtonClass", "kcButtonClass", "kcButtonPrimaryClass", "kcButtonLargeClass")}
|
||||
@ -55,3 +46,13 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
|
||||
</Template>
|
||||
);
|
||||
}
|
||||
|
||||
function TermsMarkdown() {
|
||||
const { isDownloadComplete, termsMarkdown, ReactMarkdown } = useTermsMarkdown();
|
||||
|
||||
if (!isDownloadComplete) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return <ReactMarkdown>{termsMarkdown}</ReactMarkdown>;
|
||||
}
|
||||
|
@ -1,3 +0,0 @@
|
||||
import Markdown from "react-markdown";
|
||||
|
||||
export { Markdown };
|
3
src/tools/react-markdown.ts
Normal file
3
src/tools/react-markdown.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from "react-markdown";
|
||||
import Markdown from "react-markdown";
|
||||
export default Markdown;
|
Loading…
x
Reference in New Issue
Block a user