keycloak-scim/src/events/AdminEvents.tsx

193 lines
5 KiB
TypeScript
Raw Normal View History

2020-12-11 14:48:39 +00:00
import {
Button,
Modal,
ModalVariant,
ToolbarItem,
Tooltip,
2020-12-11 14:48:39 +00:00
} from "@patternfly/react-core";
import {
cellWidth,
2020-12-11 14:48:39 +00:00
Table,
TableBody,
TableHeader,
TableVariant,
} from "@patternfly/react-table";
import type AdminEventRepresentation from "keycloak-admin/lib/defs/adminEventRepresentation";
import moment from "moment";
import React, { ReactNode, useState } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
2020-12-11 14:48:39 +00:00
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
import { useAdminClient } from "../context/auth/AdminClient";
import { useRealm } from "../context/realm-context/RealmContext";
2020-12-11 14:48:39 +00:00
type DisplayDialogProps = {
titleKey: string;
onClose: () => void;
children: ReactNode;
};
const DisplayDialog = ({ titleKey, onClose, children }: DisplayDialogProps) => {
const { t } = useTranslation("events");
return (
<Modal
variant={ModalVariant.medium}
title={t(titleKey)}
isOpen={true}
onClose={onClose}
>
{children}
</Modal>
);
};
const MAX_TEXT_LENGTH = 38;
const Truncate = ({
text,
children,
}: {
text?: string;
children: (text: string) => any;
}) => {
const definedText = text || "";
const needsTruncation = definedText.length > MAX_TEXT_LENGTH;
const truncatedText = definedText.substr(0, MAX_TEXT_LENGTH);
return (
<>
{needsTruncation && (
<Tooltip content={text}>{children(truncatedText + "...")}</Tooltip>
)}
{!needsTruncation && <>{children(definedText)}</>}
</>
);
};
2020-12-11 14:48:39 +00:00
export const AdminEvents = () => {
const { t } = useTranslation("events");
const adminClient = useAdminClient();
const { realm } = useRealm();
2020-12-11 14:48:39 +00:00
const [key, setKey] = useState(0);
const refresh = () => setKey(new Date().getTime());
2020-12-11 14:48:39 +00:00
const [authEvent, setAuthEvent] = useState<AdminEventRepresentation>();
const [representationEvent, setRepresentationEvent] =
useState<AdminEventRepresentation>();
2020-12-11 14:48:39 +00:00
const loader = async (first?: number, max?: number, search?: string) => {
const params = {
first: first!,
max: max!,
realm,
};
if (search) {
console.log("how to search?", search);
}
return await adminClient.realms.findAdminEvents({ ...params });
};
const LinkResource = (row: AdminEventRepresentation) => (
<>
<Truncate text={row.resourcePath}>
{(text) => (
<>
{row.resourceType !== "COMPONENT" && (
<Link
to={`/${realm}/${row.resourcePath}${
row.resourceType !== "GROUP" ? "/settings" : ""
}`}
>
{text}
</Link>
)}
{row.resourceType === "COMPONENT" && <span>{text}</span>}
</>
)}
</Truncate>
</>
);
2020-12-11 14:48:39 +00:00
return (
<>
{authEvent && (
<DisplayDialog titleKey="auth" onClose={() => setAuthEvent(undefined)}>
<Table
aria-label="authData"
variant={TableVariant.compact}
cells={[t("attribute"), t("value")]}
rows={Object.entries(authEvent.authDetails!)}
>
<TableHeader />
<TableBody />
</Table>
</DisplayDialog>
)}
{representationEvent && (
<DisplayDialog
titleKey="representation"
onClose={() => setRepresentationEvent(undefined)}
>
some json from the changed values
</DisplayDialog>
)}
<KeycloakDataTable
2020-12-11 14:48:39 +00:00
key={key}
loader={loader}
isPaginated
2020-12-11 17:25:54 +00:00
ariaLabelKey="events:adminEvents"
2020-12-11 14:48:39 +00:00
searchPlaceholderKey="events:searchForEvent"
toolbarItem={
<>
<ToolbarItem>
<Button onClick={refresh}>{t("refresh")}</Button>
</ToolbarItem>
</>
}
actions={[
{
title: t("auth"),
onRowClick: (event) => setAuthEvent(event),
},
{
title: t("representation"),
onRowClick: (event) => setRepresentationEvent(event),
},
]}
columns={[
{
name: "time",
displayKey: "events:time",
cellRenderer: (row) => moment(row.time).format("LLL"),
2020-12-11 14:48:39 +00:00
},
{
name: "resourcePath",
displayKey: "events:resourcePath",
cellRenderer: LinkResource,
2020-12-11 14:48:39 +00:00
},
{
name: "resourceType",
displayKey: "events:resourceType",
},
{
name: "operationType",
displayKey: "events:operationType",
transforms: [cellWidth(10)],
2020-12-11 14:48:39 +00:00
},
{
name: "",
displayKey: "events:user",
cellRenderer: (event) => event.authDetails?.userId,
},
]}
emptyState={
<ListEmptyState
message={t("emptyEvents")}
instructions={t("emptyEventsInstructions")}
/>
}
/>
</>
);
};