keycloak-scim/apps/admin-ui/src/clients/ClientsSection.tsx

251 lines
7.8 KiB
TypeScript
Raw Normal View History

import {
AlertVariant,
Badge,
Button,
2021-02-24 07:59:12 +00:00
ButtonVariant,
PageSection,
2021-03-03 07:02:40 +00:00
Tab,
TabTitleText,
ToolbarItem,
} from "@patternfly/react-core";
import { cellWidth, IRowData, TableText } from "@patternfly/react-table";
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
import type { ClientQuery } from "@keycloak/keycloak-admin-client/lib/resources/clients";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom-v5-compat";
import { useAlerts } from "../components/alert/Alerts";
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
import { formattedLinkTableCell } from "../components/external-link/FormattedLink";
import {
Action,
KeycloakDataTable,
} from "../components/table-toolbar/KeycloakDataTable";
import { ViewHeader } from "../components/view-header/ViewHeader";
import { useAdminClient } from "../context/auth/AdminClient";
import { useRealm } from "../context/realm-context/RealmContext";
import { emptyFormatter, exportClient } from "../util";
import { convertClientToUrl } from "../utils/client-url";
2021-03-04 08:20:37 +00:00
import { InitialAccessTokenList } from "./initial-access/InitialAccessTokenList";
import { toAddClient } from "./routes/AddClient";
import { toClient } from "./routes/Client";
import { toImportClient } from "./routes/ImportClient";
import { isRealmClient, getProtocolName } from "./utils";
import helpUrls from "../help-urls";
import { useAccess } from "../context/access/Access";
2022-06-22 11:35:10 +00:00
import {
RoutableTabs,
useRoutableTab,
2022-06-22 11:35:10 +00:00
} from "../components/routable-tabs/RoutableTabs";
import { ClientsTab, toClients } from "./routes/Clients";
2021-10-29 16:11:06 +00:00
export default function ClientsSection() {
const { t } = useTranslation("clients");
const { addAlert, addError } = useAlerts();
const { adminClient } = useAdminClient();
2021-03-05 10:18:47 +00:00
const { realm } = useRealm();
2021-02-24 07:59:12 +00:00
const [key, setKey] = useState(0);
const refresh = () => setKey(new Date().getTime());
const [selectedClient, setSelectedClient] = useState<ClientRepresentation>();
const { hasAccess } = useAccess();
const isManager = hasAccess("manage-clients");
const loader = async (first?: number, max?: number, search?: string) => {
const params: ClientQuery = {
first: first!,
max: max!,
};
if (search) {
params.clientId = search;
params.search = true;
}
return await adminClient.clients.find({ ...params });
};
const useTab = (tab: ClientsTab) => useRoutableTab(toClients({ realm, tab }));
const listTab = useTab("list");
const initialAccessTokenTab = useTab("initial-access-token");
2021-02-24 07:59:12 +00:00
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
titleKey: t("clientDelete", { clientId: selectedClient?.clientId }),
messageKey: "clients:clientDeleteConfirm",
continueButtonLabel: "common:delete",
continueButtonVariant: ButtonVariant.danger,
onConfirm: async () => {
try {
await adminClient.clients.del({
id: selectedClient!.id!,
});
addAlert(t("clientDeletedSuccess"), AlertVariant.success);
refresh();
2021-02-24 07:59:12 +00:00
} catch (error) {
2021-09-27 14:54:01 +00:00
addError("clients:clientDeleteError", error);
2021-02-24 07:59:12 +00:00
}
},
});
const ClientDetailLink = (client: ClientRepresentation) => (
2021-08-26 12:15:28 +00:00
<Link
key={client.id}
to={toClient({ realm, clientId: client.id!, tab: "settings" })}
>
{client.clientId}
{!client.enabled && (
<Badge key={`${client.id}-disabled`} isRead className="pf-u-ml-sm">
{t("common:disabled")}
</Badge>
)}
</Link>
);
const ClientName = (client: ClientRepresentation) => (
<TableText wrapModifier="truncate">
{emptyFormatter()(client.name)}
</TableText>
);
const ClientDescription = (client: ClientRepresentation) => (
2021-08-26 12:15:28 +00:00
<TableText wrapModifier="truncate">
{emptyFormatter()(client.description)}
</TableText>
);
const ToolbarItems = () => {
if (!isManager) return <span />;
return (
<>
<ToolbarItem>
<Button
component={(props) => (
<Link {...props} to={toAddClient({ realm })} />
)}
>
{t("createClient")}
</Button>
</ToolbarItem>
<ToolbarItem>
<Button
component={(props) => (
<Link {...props} to={toImportClient({ realm })} />
)}
variant="link"
2022-10-03 11:00:26 +00:00
data-testid="importClient"
>
{t("importClient")}
</Button>
</ToolbarItem>
</>
);
};
return (
<>
<ViewHeader
titleKey="clients:clientList"
subKey="clients:clientsExplain"
helpUrl={helpUrls.clientsUrl}
2021-03-03 07:02:40 +00:00
divider={false}
/>
<PageSection variant="light" className="pf-u-p-0">
2022-06-22 11:35:10 +00:00
<RoutableTabs
mountOnEnter
isBox
defaultLocation={toClients({
realm,
tab: "list",
})}
>
2021-03-03 07:02:40 +00:00
<Tab
data-testid="list"
title={<TabTitleText>{t("clientsList")}</TabTitleText>}
{...listTab}
2021-03-03 07:02:40 +00:00
>
<DeleteConfirm />
<KeycloakDataTable
key={key}
loader={loader}
isPaginated
ariaLabelKey="clients:clientList"
searchPlaceholderKey="clients:searchForClient"
toolbarItem={<ToolbarItems />}
actionResolver={(rowData: IRowData) => {
const client: ClientRepresentation = rowData.data;
const actions: Action<ClientRepresentation>[] = [
{
title: t("common:export"),
onClick() {
exportClient(client);
},
2021-03-03 07:02:40 +00:00
},
];
2022-05-30 09:23:24 +00:00
if (
!isRealmClient(client) &&
(isManager || client.access?.configure)
) {
actions.push({
title: t("common:delete"),
onClick() {
setSelectedClient(client);
toggleDeleteDialog();
},
});
}
return actions;
}}
2021-03-03 07:02:40 +00:00
columns={[
{
name: "clientId",
2021-04-06 17:39:27 +00:00
displayKey: "common:clientId",
transforms: [cellWidth(20)],
2021-03-03 07:02:40 +00:00
cellRenderer: ClientDetailLink,
},
{
name: "clientName",
displayKey: "common:clientName",
transforms: [cellWidth(20)],
cellRenderer: ClientName,
},
{
name: "protocol",
displayKey: "common:type",
transforms: [cellWidth(10)],
cellRenderer: (client) =>
getProtocolName(t, client.protocol ?? "openid-connect"),
},
2021-03-03 07:02:40 +00:00
{
name: "description",
displayKey: "common:description",
transforms: [cellWidth(30)],
cellRenderer: ClientDescription,
2021-03-03 07:02:40 +00:00
},
{
name: "baseUrl",
displayKey: "clients:homeURL",
transforms: [cellWidth(20)],
2021-03-03 07:02:40 +00:00
cellFormatters: [formattedLinkTableCell(), emptyFormatter()],
cellRenderer: (c) =>
convertClientToUrl(c, adminClient.baseUrl),
2021-03-03 07:02:40 +00:00
},
]}
/>
</Tab>
<Tab
data-testid="initialAccessToken"
title={<TabTitleText>{t("initialAccessToken")}</TabTitleText>}
{...initialAccessTokenTab}
2021-03-03 07:02:40 +00:00
>
<InitialAccessTokenList />
</Tab>
2022-06-22 11:35:10 +00:00
</RoutableTabs>
</PageSection>
</>
);
2021-10-29 16:11:06 +00:00
}