Rework Terms

This commit is contained in:
Joseph Garrone 2024-06-21 02:01:32 +02:00
parent 41739c8528
commit aa9b7cccc7
19 changed files with 918 additions and 297 deletions

View File

@ -66,7 +66,7 @@
"react": "*" "react": "*"
}, },
"dependencies": { "dependencies": {
"react-markdown": "^5.0.3", "react-markdown": "^9.0.1",
"tsafe": "^1.6.6" "tsafe": "^1.6.6"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,10 +1,8 @@
import "keycloakify/tools/Object.fromEntries"; import "keycloakify/tools/Object.fromEntries";
import { useEffect, useState } from "react";
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
import messages_fallbackLanguage from "./baseMessages/en"; import messages_fallbackLanguage from "./baseMessages/en";
import { getMessages } from "./baseMessages"; import { getMessages } from "./baseMessages";
import type { KcContext } from "../KcContext"; import type { KcContext } from "../KcContext";
import { Reflect } from "tsafe/Reflect";
export const fallbackLanguageTag = "en"; export const fallbackLanguageTag = "en";
@ -88,7 +86,9 @@ export type GenericI18n<MessageKey extends string> = {
isFetchingTranslations: boolean; 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 I18n = GenericI18n<MessageKey | ExtraMessageKey>;
type Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined }; 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>({ const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
messages_fallbackLanguage, messages_fallbackLanguage,
extraMessages_fallbackLanguage: extraMessages[fallbackLanguageTag], messageBundle_fallbackLanguage: messageBundle[fallbackLanguageTag],
extraMessages: extraMessages[partialI18n.currentLanguageTag] messageBundle_currentLanguage: messageBundle[partialI18n.currentLanguageTag]
}); });
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag; const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
@ -135,17 +135,19 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
const result: Result = { const result: Result = {
i18n: { i18n: {
...partialI18n, ...partialI18n,
...createI18nTranslationFunctions({ messages: undefined }), ...createI18nTranslationFunctions({
messages_currentLanguage: isCurrentLanguageFallbackLanguage ? messages_fallbackLanguage : undefined
}),
isFetchingTranslations: !isCurrentLanguageFallbackLanguage isFetchingTranslations: !isCurrentLanguageFallbackLanguage
}, },
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
? undefined ? undefined
: (async () => { : (async () => {
const messages = await getMessages(partialI18n.currentLanguageTag); const messages_currentLanguage = await getMessages(partialI18n.currentLanguageTag);
const i18n_currentLanguage: I18n = { const i18n_currentLanguage: I18n = {
...partialI18n, ...partialI18n,
...createI18nTranslationFunctions({ messages }), ...createI18nTranslationFunctions({ messages_currentLanguage }),
isFetchingTranslations: false isFetchingTranslations: false
}; };
@ -168,66 +170,30 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
return { getI18n }; 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: { function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraMessageKey extends string>(params: {
messages_fallbackLanguage: Record<MessageKey, string>; messages_fallbackLanguage: Record<MessageKey, string>;
extraMessages_fallbackLanguage: Record<ExtraMessageKey, string> | undefined; messageBundle_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
extraMessages: Partial<Record<ExtraMessageKey, string>> | undefined; messageBundle_currentLanguage: Partial<Record<ExtraMessageKey, string>> | undefined;
}) { }) {
const { extraMessages } = params; const { messageBundle_currentLanguage } = params;
const messages_fallbackLanguage = { const messages_fallbackLanguage = {
...params.messages_fallbackLanguage, ...params.messages_fallbackLanguage,
...params.extraMessages_fallbackLanguage ...params.messageBundle_fallbackLanguage
}; };
function createI18nTranslationFunctions(params: { function createI18nTranslationFunctions(params: {
messages: Partial<Record<MessageKey, string>> | undefined; messages_currentLanguage: Partial<Record<MessageKey, string>> | undefined;
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> { }): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
const messages = { const messages_currentLanguage = {
...params.messages, ...params.messages_currentLanguage,
...extraMessages ...messageBundle_currentLanguage
}; };
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined { function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
const { key, args, doRenderAsHtml } = props; 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) { if (messageOrUndefined === undefined) {
return undefined; return undefined;

View File

@ -1,5 +1,5 @@
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n"; import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
export type { MessageKey, KcContextLike }; export type { MessageKey, KcContextLike };
export type I18n = GenericI18n<MessageKey>; export type I18n = GenericI18n<MessageKey>;
export { createUseI18n } from "./i18n"; export { createUseI18n } from "./useI18n";
export { fallbackLanguageTag } from "./i18n"; export { fallbackLanguageTag } from "./i18n";

View 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>() };
}

View File

@ -8,8 +8,7 @@ import { assert } from "tsafe/assert";
import { import {
type ThemeType, type ThemeType,
basenameOfTheKeycloakifyResourcesDir, basenameOfTheKeycloakifyResourcesDir,
resources_common, resources_common
nameOfTheLocalizationRealmOverridesUserProfileProperty
} from "../../shared/constants"; } from "../../shared/constants";
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath"; import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
@ -119,10 +118,6 @@ export function generateFtlFilesCodeFactory(params: {
.replace("KEYCLOAKIFY_THEME_TYPE_dExKd3xEdr", themeType) .replace("KEYCLOAKIFY_THEME_TYPE_dExKd3xEdr", themeType)
.replace("KEYCLOAKIFY_THEME_NAME_cXxKd3xEer", themeName) .replace("KEYCLOAKIFY_THEME_NAME_cXxKd3xEer", themeName)
.replace("RESOURCES_COMMON_cLsLsMrtDkpVv", resources_common) .replace("RESOURCES_COMMON_cLsLsMrtDkpVv", resources_common)
.replace(
"lOCALIZATION_REALM_OVERRIDES_USER_PROFILE_PROPERTY_KEY_aaGLsPgGIdeeX",
nameOfTheLocalizationRealmOverridesUserProfileProperty
)
.replace( .replace(
"USER_DEFINED_EXCLUSIONS_eKsaY4ZsZ4eMr2", "USER_DEFINED_EXCLUSIONS_eKsaY4ZsZ4eMr2",
buildContext.kcContextExclusionsFtlCode ?? "" buildContext.kcContextExclusionsFtlCode ?? ""

View File

@ -33,8 +33,9 @@ kcContext.pageId = "${pageId}";
if( kcContext.url && kcContext.url.resourcesPath ){ if( kcContext.url && kcContext.url.resourcesPath ){
kcContext.url.resourcesCommonPath = kcContext.url.resourcesPath + "/" + "RESOURCES_COMMON_cLsLsMrtDkpVv"; kcContext.url.resourcesCommonPath = kcContext.url.resourcesPath + "/" + "RESOURCES_COMMON_cLsLsMrtDkpVv";
} }
kcContext["x-keycloakify"] = {};
<#if profile?? && profile.attributes??> <#if profile?? && profile.attributes??>
kcContext.lOCALIZATION_REALM_OVERRIDES_USER_PROFILE_PROPERTY_KEY_aaGLsPgGIdeeX = { kcContext["x-keycloakify"].realmMessageBundleUserProfile = {
<#list profile.attributes as attribute> <#list profile.attributes as attribute>
<#if attribute.annotations?? && attribute.displayName??> <#if attribute.annotations?? && attribute.displayName??>
"${attribute.displayName}": decodeHtmlEntities("${advancedMsg(attribute.displayName)?js_string}"), "${attribute.displayName}": decodeHtmlEntities("${advancedMsg(attribute.displayName)?js_string}"),
@ -61,6 +62,9 @@ if( kcContext.url && kcContext.url.resourcesPath ){
</#list> </#list>
}; };
</#if> </#if>
<#if pageId == "terms.ftl" || termsAcceptanceRequired?? && termsAcceptanceRequired>
kcContext["x-keycloakify"].realmMessageBundleTermsText= decodeHtmlEntities("${msg("termsText")?js_string}");
</#if>
attributes_to_attributesByName: { attributes_to_attributesByName: {
if( !kcContext.profile ){ if( !kcContext.profile ){
break attributes_to_attributesByName; break attributes_to_attributesByName;
@ -198,6 +202,9 @@ function decodeHtmlEntities(htmlStr){
) || ( ) || (
key == "execution" && key == "execution" &&
are_same_path(path, []) are_same_path(path, [])
) || (
key == "entity" &&
are_same_path(path, ["user"])
) )
> >
<#-- <#local out_seq += ["/*" + path?join(".") + "." + key + " excluded*/"]> --> <#-- <#local out_seq += ["/*" + path?join(".") + "." + key + " excluded*/"]> -->

View File

@ -1,5 +1,3 @@
export const nameOfTheLocalizationRealmOverridesUserProfileProperty =
"__localizationRealmOverridesUserProfile";
export const keycloak_resources = "keycloak-resources"; export const keycloak_resources = "keycloak-resources";
export const resources_common = "resources-common"; export const resources_common = "resources-common";
export const lastKeycloakVersionWithAccountV1 = "21.1.2"; export const lastKeycloakVersionWithAccountV1 = "21.1.2";

View File

@ -1,8 +1,4 @@
import type { import type { ThemeType, LoginThemePageId } from "keycloakify/bin/shared/constants";
ThemeType,
LoginThemePageId,
nameOfTheLocalizationRealmOverridesUserProfileProperty
} from "keycloakify/bin/shared/constants";
import type { ExtractAfterStartingWith } from "keycloakify/tools/ExtractAfterStartingWith"; import type { ExtractAfterStartingWith } from "keycloakify/tools/ExtractAfterStartingWith";
import type { ValueOf } from "keycloakify/tools/ValueOf"; import type { ValueOf } from "keycloakify/tools/ValueOf";
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
@ -158,7 +154,10 @@ export declare namespace KcContext {
ssoLoginInOtherTabsUrl: string; ssoLoginInOtherTabsUrl: string;
}; };
properties: {}; properties: {};
__localizationRealmOverridesUserProfile?: Record<string, string>; "x-keycloakify": {
realmMessageBundleUserProfile: Record<string, string> | undefined;
realmMessageBundleTermsText: string | undefined;
};
}; };
export type SamlPostForm = Common & { export type SamlPostForm = Common & {
@ -276,6 +275,7 @@ export declare namespace KcContext {
lastName?: string; lastName?: string;
markedForEviction?: boolean; markedForEviction?: boolean;
}; };
__localizationRealmOverridesTermsText?: string;
}; };
export type LoginDeviceVerifyUserCode = Common & { export type LoginDeviceVerifyUserCode = Common & {
@ -772,11 +772,3 @@ export type PasswordPolicies = {
/** Whether the password can be the email address */ /** Whether the password can be the email address */
notEmail?: boolean; notEmail?: boolean;
}; };
assert<
KcContext.Common extends Partial<
Record<typeof nameOfTheLocalizationRealmOverridesUserProfileProperty, unknown>
>
? true
: false
>();

View File

@ -161,7 +161,10 @@ export const kcContextCommonMock: KcContext.Common = {
scripts: [], scripts: [],
isAppInitiatedAction: false, isAppInitiatedAction: false,
properties: {}, properties: {},
__localizationRealmOverridesUserProfile: {} "x-keycloakify": {
realmMessageBundleUserProfile: undefined,
realmMessageBundleTermsText: undefined
}
}; };
const loginUrl = { const loginUrl = {

View File

@ -1,10 +1,8 @@
import "keycloakify/tools/Object.fromEntries"; import "keycloakify/tools/Object.fromEntries";
import { useEffect, useState } from "react";
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
import messages_fallbackLanguage from "./baseMessages/en"; import messages_fallbackLanguage from "./baseMessages/en";
import { getMessages } from "./baseMessages"; import { getMessages } from "./baseMessages";
import type { KcContext } from "../KcContext"; import type { KcContext } from "../KcContext";
import { Reflect } from "tsafe/Reflect";
export const fallbackLanguageTag = "en"; export const fallbackLanguageTag = "en";
@ -13,7 +11,10 @@ export type KcContextLike = {
currentLanguageTag: string; currentLanguageTag: string;
supported: { languageTag: string; url: string; label: 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>(); assert<KcContext extends KcContextLike ? true : false>();
@ -89,7 +90,9 @@ export type GenericI18n<MessageKey extends string> = {
isFetchingTranslations: boolean; 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 I18n = GenericI18n<MessageKey | ExtraMessageKey>;
type Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined }; 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>({ const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
messages_fallbackLanguage, messages_fallbackLanguage,
extraMessages_fallbackLanguage: extraMessages[fallbackLanguageTag], messageBundle_fallbackLanguage: messageBundle[fallbackLanguageTag],
extraMessages: extraMessages[partialI18n.currentLanguageTag], messageBundle_currentLanguage: messageBundle[partialI18n.currentLanguageTag],
__localizationRealmOverridesUserProfile: kcContext.__localizationRealmOverridesUserProfile realmMessageBundleUserProfile: kcContext["x-keycloakify"].realmMessageBundleUserProfile,
realmMessageBundleTermsText: kcContext["x-keycloakify"].realmMessageBundleTermsText
}); });
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag; const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
@ -137,17 +141,19 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
const result: Result = { const result: Result = {
i18n: { i18n: {
...partialI18n, ...partialI18n,
...createI18nTranslationFunctions({ messages: undefined }), ...createI18nTranslationFunctions({
messages_currentLanguage: isCurrentLanguageFallbackLanguage ? messages_fallbackLanguage : undefined
}),
isFetchingTranslations: !isCurrentLanguageFallbackLanguage isFetchingTranslations: !isCurrentLanguageFallbackLanguage
}, },
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
? undefined ? undefined
: (async () => { : (async () => {
const messages = await getMessages(partialI18n.currentLanguageTag); const messages_currentLanguage = await getMessages(partialI18n.currentLanguageTag);
const i18n_currentLanguage: I18n = { const i18n_currentLanguage: I18n = {
...partialI18n, ...partialI18n,
...createI18nTranslationFunctions({ messages }), ...createI18nTranslationFunctions({ messages_currentLanguage }),
isFetchingTranslations: false isFetchingTranslations: false
}; };
@ -170,67 +176,48 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
return { getI18n }; 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: { function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraMessageKey extends string>(params: {
messages_fallbackLanguage: Record<MessageKey, string>; messages_fallbackLanguage: Record<MessageKey, string>;
extraMessages_fallbackLanguage: Record<ExtraMessageKey, string> | undefined; messageBundle_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
extraMessages: Partial<Record<ExtraMessageKey, string>> | undefined; messageBundle_currentLanguage: Partial<Record<ExtraMessageKey, string>> | undefined;
__localizationRealmOverridesUserProfile: Record<string, string> | undefined; realmMessageBundleUserProfile: Record<string, string> | undefined;
realmMessageBundleTermsText: string | undefined;
}) { }) {
const { __localizationRealmOverridesUserProfile, extraMessages } = params; const { messageBundle_currentLanguage, realmMessageBundleUserProfile, realmMessageBundleTermsText } = params;
const messages_fallbackLanguage = { const messages_fallbackLanguage = {
...params.messages_fallbackLanguage, ...params.messages_fallbackLanguage,
...params.extraMessages_fallbackLanguage ...params.messageBundle_fallbackLanguage
}; };
function createI18nTranslationFunctions(params: { function createI18nTranslationFunctions(params: {
messages: Partial<Record<MessageKey, string>> | undefined; messages_currentLanguage: Partial<Record<MessageKey, string>> | undefined;
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> { }): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
const messages = { const messages_currentLanguage = {
...params.messages, ...params.messages_currentLanguage,
...extraMessages ...messageBundle_currentLanguage
}; };
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined { function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
const { key, args, doRenderAsHtml } = props; 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) { if (messageOrUndefined === undefined) {
return 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 { function resolveMsgAdvanced(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): JSX.Element | string {
const { key, args, doRenderAsHtml } = props; const { key, args, doRenderAsHtml } = props;
if (__localizationRealmOverridesUserProfile !== undefined && key in __localizationRealmOverridesUserProfile) { if (realmMessageBundleUserProfile !== undefined && key in realmMessageBundleUserProfile) {
const resolvedMessage = __localizationRealmOverridesUserProfile[key]; const resolvedMessage = realmMessageBundleUserProfile[key];
return doRenderAsHtml ? ( return doRenderAsHtml ? (
<span <span

View File

@ -1,5 +1,5 @@
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n"; import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
export type { MessageKey, KcContextLike }; export type { MessageKey, KcContextLike };
export type I18n = GenericI18n<MessageKey>; export type I18n = GenericI18n<MessageKey>;
export { createUseI18n } from "./i18n"; export { createUseI18n } from "./useI18n";
export { fallbackLanguageTag } from "./i18n"; export { fallbackLanguageTag } from "./i18n";

44
src/login/i18n/useI18n.ts Normal file
View 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>() };
}

View File

@ -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 };
}

View 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 };
}

View File

@ -1,5 +1,4 @@
import { useState } from "react"; import { useState } from "react";
import { Markdown } from "keycloakify/tools/Markdown";
import type { LazyOrNot } from "keycloakify/tools/LazyOrNot"; import type { LazyOrNot } from "keycloakify/tools/LazyOrNot";
import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms"; import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
import { getKcClsx, type KcClsx } from "keycloakify/login/lib/kcClsx"; 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"> }) { function TermsAcceptance(props: { i18n: I18n; kcClsx: KcClsx; messagesPerField: Pick<KcContext["messagesPerField"], "existsError" | "get"> }) {
const { i18n, kcClsx, messagesPerField } = props; const { i18n, kcClsx, messagesPerField } = props;
const { msg } = i18n; const { msg, msgStr } = 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;
}
return ( return (
<> <>
<div className="form-group"> <div className="form-group">
<div className={kcClsx("kcInputWrapperClass")}> <div className={kcClsx("kcInputWrapperClass")}>
{msg("termsTitle")} {msg("termsTitle")}
<div id="kc-registration-terms-text"> <div id="kc-registration-terms-text">{msgStr("termsText") ? msg("termsText") : <TermsMarkdown />}</div>
<Markdown>{termsMarkdown}</Markdown>
</div>
</div> </div>
</div> </div>
<div className="form-group"> <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>;
}

View File

@ -1,4 +1,3 @@
import { Markdown } from "keycloakify/tools/Markdown";
import { getKcClsx } from "keycloakify/login/lib/kcClsx"; import { getKcClsx } from "keycloakify/login/lib/kcClsx";
import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms"; import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
import type { PageProps } from "keycloakify/login/pages/PageProps"; 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 { msg, msgStr } = i18n;
const { locale, url } = kcContext; const { url } = kcContext;
const { isDownloadComplete, termsMarkdown, termsLanguageTag } = useTermsMarkdown();
if (!isDownloadComplete) {
return null;
}
return ( return (
<Template <Template
@ -32,9 +25,7 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
displayMessage={false} displayMessage={false}
headerNode={msg("termsTitle")} headerNode={msg("termsTitle")}
> >
<div id="kc-terms-text" lang={termsLanguageTag !== locale?.currentLanguageTag ? termsLanguageTag : undefined}> <div id="kc-terms-text">{msgStr("termsText") ? msg("termsText") : <TermsMarkdown />}</div>
<Markdown>{termsMarkdown}</Markdown>
</div>
<form className="form-actions" action={url.loginAction} method="POST"> <form className="form-actions" action={url.loginAction} method="POST">
<input <input
className={kcClsx("kcButtonClass", "kcButtonClass", "kcButtonClass", "kcButtonPrimaryClass", "kcButtonLargeClass")} className={kcClsx("kcButtonClass", "kcButtonClass", "kcButtonClass", "kcButtonPrimaryClass", "kcButtonLargeClass")}
@ -55,3 +46,13 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
</Template> </Template>
); );
} }
function TermsMarkdown() {
const { isDownloadComplete, termsMarkdown, ReactMarkdown } = useTermsMarkdown();
if (!isDownloadComplete) {
return null;
}
return <ReactMarkdown>{termsMarkdown}</ReactMarkdown>;
}

View File

@ -1,3 +0,0 @@
import Markdown from "react-markdown";
export { Markdown };

View File

@ -0,0 +1,3 @@
export * from "react-markdown";
import Markdown from "react-markdown";
export default Markdown;

705
yarn.lock

File diff suppressed because it is too large Load Diff