2021-05-03 16:44:47 +00:00
|
|
|
import React, { useContext, useEffect, useState } from "react";
|
|
|
|
import _ from "lodash";
|
|
|
|
|
|
|
|
import RealmRepresentation from "keycloak-admin/lib/defs/realmRepresentation";
|
2021-04-08 19:20:35 +00:00
|
|
|
import { RecentUsed } from "../../components/realm-selector/recent-used";
|
2021-05-03 16:44:47 +00:00
|
|
|
import { useErrorHandler } from "react-error-boundary";
|
|
|
|
import { asyncStateFetch, useAdminClient } from "../auth/AdminClient";
|
|
|
|
import { WhoAmIContext } from "../whoami/WhoAmI";
|
2020-09-17 11:37:30 +00:00
|
|
|
|
2021-01-05 19:49:33 +00:00
|
|
|
type RealmContextType = {
|
|
|
|
realm: string;
|
|
|
|
setRealm: (realm: string) => void;
|
2021-05-03 16:44:47 +00:00
|
|
|
realms: RealmRepresentation[];
|
|
|
|
refresh: () => Promise<void>;
|
2021-01-05 19:49:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const RealmContext = React.createContext<RealmContextType>({
|
2020-10-02 06:47:32 +00:00
|
|
|
realm: "",
|
2021-01-05 19:49:33 +00:00
|
|
|
setRealm: () => {},
|
2021-05-03 16:44:47 +00:00
|
|
|
realms: [],
|
|
|
|
refresh: () => Promise.resolve(),
|
2020-09-17 11:37:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
type RealmContextProviderProps = { children: React.ReactNode };
|
|
|
|
|
|
|
|
export const RealmContextProvider = ({
|
|
|
|
children,
|
|
|
|
}: RealmContextProviderProps) => {
|
2021-05-03 16:44:47 +00:00
|
|
|
const { whoAmI } = useContext(WhoAmIContext);
|
|
|
|
const [realm, setRealm] = useState(whoAmI.getHomeRealm());
|
|
|
|
const [realms, setRealms] = useState<RealmRepresentation[]>([]);
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const errorHandler = useErrorHandler();
|
2021-04-08 19:20:35 +00:00
|
|
|
const recentUsed = new RecentUsed();
|
|
|
|
|
2021-05-03 16:44:47 +00:00
|
|
|
const updateRealmsList = (realms: RealmRepresentation[]) => {
|
|
|
|
setRealms(_.sortBy(realms, "realm"));
|
|
|
|
recentUsed.clean(realms.map((r) => r.realm!));
|
2021-04-08 19:20:35 +00:00
|
|
|
};
|
2020-09-17 11:37:30 +00:00
|
|
|
|
2021-05-03 16:44:47 +00:00
|
|
|
useEffect(
|
|
|
|
() =>
|
|
|
|
asyncStateFetch(
|
|
|
|
() => adminClient.realms.find(),
|
|
|
|
(realms) => updateRealmsList(realms),
|
|
|
|
errorHandler
|
|
|
|
),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const set = (realm: string) => {
|
|
|
|
if (
|
|
|
|
realms.length === 0 ||
|
|
|
|
realms.findIndex((r) => r.realm == realm) !== -1
|
|
|
|
) {
|
|
|
|
recentUsed.setRecentUsed(realm);
|
|
|
|
setRealm(realm);
|
|
|
|
adminClient.setConfig({
|
|
|
|
realmName: realm,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2020-09-17 11:37:30 +00:00
|
|
|
return (
|
2021-05-03 16:44:47 +00:00
|
|
|
<RealmContext.Provider
|
|
|
|
value={{
|
|
|
|
realm,
|
|
|
|
setRealm: set,
|
|
|
|
realms,
|
|
|
|
refresh: async () => {
|
|
|
|
const list = await adminClient.realms.find();
|
|
|
|
updateRealmsList(list);
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
>
|
2020-09-17 11:37:30 +00:00
|
|
|
{children}
|
|
|
|
</RealmContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
2021-01-05 19:49:33 +00:00
|
|
|
|
|
|
|
export const useRealm = () => useContext(RealmContext);
|