2021-09-29 08:51:47 +00:00
|
|
|
import React, { useState } from "react";
|
2021-11-16 11:10:10 +00:00
|
|
|
|
|
|
|
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
|
|
|
|
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
|
|
|
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
|
2021-04-29 06:28:59 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2021-01-13 19:59:45 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-09-02 20:14:43 +00:00
|
|
|
import { KEY_PROVIDER_TYPE } from "../util";
|
|
|
|
import { RealmSettingsTabs } from "./RealmSettingsTabs";
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2021-08-20 16:15:12 +00:00
|
|
|
const sortByPriority = (components: ComponentRepresentation[]) => {
|
|
|
|
const sortedComponents = [...components].sort((a, b) => {
|
|
|
|
const priorityA = Number(a.config?.priority);
|
|
|
|
const priorityB = Number(b.config?.priority);
|
|
|
|
|
|
|
|
return (
|
|
|
|
(!isNaN(priorityB) ? priorityB : 0) - (!isNaN(priorityA) ? priorityA : 0)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return sortedComponents;
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function RealmSettingsSection() {
|
2021-01-13 19:59:45 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-09-02 20:14:43 +00:00
|
|
|
const { realm: realmName } = useRealm();
|
2021-04-30 19:08:12 +00:00
|
|
|
const [realm, setRealm] = useState<RealmRepresentation>();
|
2021-07-05 11:24:10 +00:00
|
|
|
const [realmComponents, setRealmComponents] =
|
|
|
|
useState<ComponentRepresentation[]>();
|
2021-09-27 17:15:36 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
|
|
|
|
const refresh = () => {
|
|
|
|
setKey(key + 1);
|
|
|
|
};
|
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
useFetch(
|
2021-05-27 07:01:55 +00:00
|
|
|
async () => {
|
|
|
|
const realm = await adminClient.realms.findOne({ realm: realmName });
|
|
|
|
const realmComponents = await adminClient.components.find({
|
2021-08-20 16:15:12 +00:00
|
|
|
type: KEY_PROVIDER_TYPE,
|
2021-05-27 07:01:55 +00:00
|
|
|
realm: realmName,
|
|
|
|
});
|
|
|
|
|
2021-10-14 21:08:42 +00:00
|
|
|
return { realm, realmComponents };
|
2021-05-27 07:01:55 +00:00
|
|
|
},
|
2021-10-14 21:08:42 +00:00
|
|
|
({ realm, realmComponents }) => {
|
2021-08-20 16:15:12 +00:00
|
|
|
setRealmComponents(sortByPriority(realmComponents));
|
2021-07-09 14:23:49 +00:00
|
|
|
setRealm(realm);
|
2021-04-29 06:28:59 +00:00
|
|
|
},
|
2021-09-29 08:51:47 +00:00
|
|
|
[key]
|
2021-04-29 06:28:59 +00:00
|
|
|
);
|
2021-01-13 19:59:45 +00:00
|
|
|
|
2021-10-14 21:08:42 +00:00
|
|
|
if (!realm || !realmComponents) {
|
2021-11-16 11:10:10 +00:00
|
|
|
return <KeycloakSpinner />;
|
2021-08-23 19:00:42 +00:00
|
|
|
}
|
2021-01-13 19:59:45 +00:00
|
|
|
return (
|
2021-09-02 20:14:43 +00:00
|
|
|
<RealmSettingsTabs
|
|
|
|
realm={realm}
|
2021-09-27 17:15:36 +00:00
|
|
|
refresh={refresh}
|
2021-09-02 20:14:43 +00:00
|
|
|
realmComponents={realmComponents}
|
|
|
|
/>
|
2021-01-13 19:59:45 +00:00
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|