UNPKG

@keycloakify/keycloak-admin-ui

Version:
34 lines 1.75 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { getI18n } from "react-i18next"; import { createNamedContext, useEnvironment, useFetch, useRequiredContext, } from "../../ui-shared"; import { useEffect, useState } from "react"; import { useMatch } from "react-router-dom"; import { useAdminClient } from "../../admin-client"; import { DashboardRouteWithRealm } from "../../dashboard/routes/Dashboard"; export const RealmContext = createNamedContext("RealmContext", undefined); export const RealmContextProvider = ({ children }) => { var _a; const { adminClient } = useAdminClient(); const { environment } = useEnvironment(); const [key, setKey] = useState(0); const refresh = () => setKey(key + 1); const [realmRepresentation, setRealmRepresentation] = useState(); const routeMatch = useMatch({ path: DashboardRouteWithRealm.path, end: false, }); const realm = (_a = routeMatch === null || routeMatch === void 0 ? void 0 : routeMatch.params.realm) !== null && _a !== void 0 ? _a : environment.realm; // Configure admin client to use selected realm when it changes. useEffect(() => { (async () => { adminClient.setConfig({ realmName: realm }); const namespace = encodeURIComponent(realm); await getI18n().loadNamespaces(namespace); getI18n().setDefaultNamespace(namespace); })(); }, [realm]); useFetch(() => adminClient.realms.findOne({ realm }), setRealmRepresentation, [realm, key]); return (_jsx(RealmContext.Provider, { value: { realm, realmRepresentation, refresh }, children: children })); }; export const useRealm = () => useRequiredContext(RealmContext); //# sourceMappingURL=RealmContext.js.map