#20: Support advancedMsg

This commit is contained in:
garronej 2021-10-08 01:45:51 +02:00
parent 2c82a2332f
commit 2d03fbce79
2 changed files with 18 additions and 3 deletions

View File

@ -182,6 +182,7 @@
}
},
"msg": function(){ throw new Error("use import { useKcMessage } from 'keycloakify'"); },
"advancedMsg": function(){ throw new Error("use import { useKcMessage } from 'keycloakify'"); },
}
);

View File

@ -2,7 +2,6 @@
import { useCallback, useReducer } from "react";
import { useKcLanguageTag } from "./useKcLanguageTag";
import { kcMessages, evtTermsUpdated } from "./kcMessages/login";
import type { ReactNode } from "react";
import { useEvt } from "evt/hooks";
//NOTE for later: https://github.com/remarkjs/react-markdown/blob/236182ecf30bd89c1e5a7652acaf8d0bf81e6170/src/renderers.js#L7-L35
import ReactMarkdown from "react-markdown";
@ -45,7 +44,7 @@ export function useKcMessage() {
[kcLanguageTag, trigger]
);
const msg = useCallback<(...args: Parameters<typeof msgStr>) => ReactNode>(
const msg = useCallback<(...args: Parameters<typeof msgStr>) => JSX.Element>(
(key, ...args) =>
<ReactMarkdown allowDangerousHtml renderers={key === "termsText" ? undefined : { "paragraph": "span" }}>
{msgStr(key, ...args)}
@ -53,6 +52,21 @@ export function useKcMessage() {
[msgStr]
);
return { msg, msgStr };
const advancedMsg = useCallback(
(key: string): string => {
const match = key.match(/^\$\{([^{]+)\}$/);
if( match === null ){
return key;
}
return msgStr(match[1] as MessageKey);
},
[msgStr]
);
return { msg, msgStr, advancedMsg };
}