@keycloakify/keycloak-admin-ui
Version:
Repackaged Keycloak Admin UI
102 lines • 4.81 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _WhoAmI_me;
import { jsx as _jsx } from "react/jsx-runtime";
import { getI18n } from "react-i18next";
import { createNamedContext, useEnvironment, useFetch, useRequiredContext, } from "../../ui-shared";
import { useState } from "react";
import { useAdminClient } from "../../admin-client";
import { DEFAULT_LOCALE } from "../../i18n/i18n";
import { useRealm } from "../../context/realm-context/RealmContext";
// can be replaced with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getTextInfo
const RTL_LOCALES = [
"ar",
"dv",
"fa",
"ha",
"he",
"iw",
"ji",
"ps",
"sd",
"ug",
"ur",
"yi",
];
export class WhoAmI {
constructor(me) {
var _a;
_WhoAmI_me.set(this, void 0);
__classPrivateFieldSet(this, _WhoAmI_me, me, "f");
if ((_a = __classPrivateFieldGet(this, _WhoAmI_me, "f")) === null || _a === void 0 ? void 0 : _a.locale) {
getI18n().changeLanguage(__classPrivateFieldGet(this, _WhoAmI_me, "f").locale, (error) => {
var _a;
if (error) {
console.warn("Error(s) loading locale", (_a = __classPrivateFieldGet(this, _WhoAmI_me, "f")) === null || _a === void 0 ? void 0 : _a.locale, error);
}
});
if (RTL_LOCALES.includes(__classPrivateFieldGet(this, _WhoAmI_me, "f").locale)) {
document.getElementsByTagName("html")[0].setAttribute("dir", "rtl");
}
}
}
getDisplayName() {
if (__classPrivateFieldGet(this, _WhoAmI_me, "f") === undefined)
return "";
return __classPrivateFieldGet(this, _WhoAmI_me, "f").displayName;
}
getLocale() {
var _a, _b;
return (_b = (_a = __classPrivateFieldGet(this, _WhoAmI_me, "f")) === null || _a === void 0 ? void 0 : _a.locale) !== null && _b !== void 0 ? _b : DEFAULT_LOCALE;
}
getRealm() {
var _a, _b;
return (_b = (_a = __classPrivateFieldGet(this, _WhoAmI_me, "f")) === null || _a === void 0 ? void 0 : _a.realm) !== null && _b !== void 0 ? _b : "";
}
getUserId() {
if (__classPrivateFieldGet(this, _WhoAmI_me, "f") === undefined)
return "";
return __classPrivateFieldGet(this, _WhoAmI_me, "f").userId;
}
canCreateRealm() {
var _a;
return !!((_a = __classPrivateFieldGet(this, _WhoAmI_me, "f")) === null || _a === void 0 ? void 0 : _a.createRealm);
}
getRealmAccess() {
if (__classPrivateFieldGet(this, _WhoAmI_me, "f") === undefined)
return {};
return __classPrivateFieldGet(this, _WhoAmI_me, "f").realm_access;
}
isTemporary() {
var _a, _b;
return (_b = (_a = __classPrivateFieldGet(this, _WhoAmI_me, "f")) === null || _a === void 0 ? void 0 : _a.temporary) !== null && _b !== void 0 ? _b : false;
}
}
_WhoAmI_me = new WeakMap();
export const WhoAmIContext = createNamedContext("WhoAmIContext", undefined);
export const useWhoAmI = () => useRequiredContext(WhoAmIContext);
export const WhoAmIContextProvider = ({ children }) => {
const { adminClient } = useAdminClient();
const { environment } = useEnvironment();
const [whoAmI, setWhoAmI] = useState(new WhoAmI());
const { realm } = useRealm();
const [key, setKey] = useState(0);
useFetch(() => adminClient.whoAmI.find({
realm: environment.realm,
currentRealm: realm,
}), (me) => {
const whoAmI = new WhoAmI(me);
setWhoAmI(whoAmI);
}, [key, realm]);
return (_jsx(WhoAmIContext.Provider, { value: { refresh: () => setKey(key + 1), whoAmI }, children: children }));
};
//# sourceMappingURL=WhoAmI.js.map