2020-10-08 11:38:25 +00:00
|
|
|
import React, { createContext, ReactNode, useContext } from "react";
|
2021-05-04 17:58:18 +00:00
|
|
|
import type { ServerInfoRepresentation } from "keycloak-admin/lib/defs/serverInfoRepesentation";
|
2020-11-12 12:55:52 +00:00
|
|
|
|
2020-11-02 20:15:09 +00:00
|
|
|
import { sortProviders } from "../../util";
|
2020-10-08 11:38:25 +00:00
|
|
|
import { DataLoader } from "../../components/data-loader/DataLoader";
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "../auth/AdminClient";
|
2020-10-08 11:38:25 +00:00
|
|
|
|
2020-10-12 17:59:15 +00:00
|
|
|
export const ServerInfoContext = createContext<ServerInfoRepresentation>(
|
2020-10-08 11:38:25 +00:00
|
|
|
{} as ServerInfoRepresentation
|
|
|
|
);
|
|
|
|
|
|
|
|
export const useServerInfo = () => useContext(ServerInfoContext);
|
|
|
|
|
|
|
|
export const useLoginProviders = () => {
|
2020-11-12 12:55:52 +00:00
|
|
|
return sortProviders(useServerInfo().providers!["login-protocol"].providers);
|
2020-10-08 11:38:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const ServerInfoProvider = ({ children }: { children: ReactNode }) => {
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2020-10-08 11:38:25 +00:00
|
|
|
const loader = async () => {
|
2020-11-12 12:55:52 +00:00
|
|
|
return await adminClient.serverInfo.find();
|
2020-10-08 11:38:25 +00:00
|
|
|
};
|
|
|
|
return (
|
|
|
|
<DataLoader loader={loader}>
|
|
|
|
{(serverInfo) => (
|
2021-01-12 12:39:37 +00:00
|
|
|
<ServerInfoContext.Provider value={serverInfo}>
|
2020-10-08 11:38:25 +00:00
|
|
|
{children}
|
|
|
|
</ServerInfoContext.Provider>
|
|
|
|
)}
|
|
|
|
</DataLoader>
|
|
|
|
);
|
|
|
|
};
|