keycloak-scim/src/sessions/SessionsSection.tsx

147 lines
4.4 KiB
TypeScript
Raw Normal View History

2021-07-05 15:58:19 +00:00
import React, { useState } from "react";
import { Link } from "react-router-dom";
2021-07-05 15:58:19 +00:00
import { useTranslation } from "react-i18next";
import moment from "moment";
2021-07-05 16:13:17 +00:00
import {
PageSection,
Select,
SelectOption,
SelectVariant,
} from "@patternfly/react-core";
import { FilterIcon } from "@patternfly/react-icons";
import type UserSessionRepresentation from "keycloak-admin/lib/defs/userSessionRepresentation";
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import { ViewHeader } from "../components/view-header/ViewHeader";
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
import { useAdminClient } from "../context/auth/AdminClient";
2021-07-06 08:17:04 +00:00
import "./SessionsSection.css";
const Clients = (row: UserSessionRepresentation) => {
return (
<>
{Object.values(row.clients!).map((client) => (
<Link key={client} to="" className="pf-u-mx-sm">
{client}
</Link>
))}
</>
);
};
export const SessionsSection = () => {
2021-07-05 15:58:19 +00:00
const { t } = useTranslation("sessions");
const adminClient = useAdminClient();
2021-07-05 15:58:19 +00:00
const [filterDropdownOpen, setFilterDropdownOpen] = useState(false);
2021-07-06 08:17:04 +00:00
const [filterType, setFilterType] = useState(
2021-07-06 08:57:29 +00:00
t("sessionsType.allSessions").toString()
2021-07-06 08:17:04 +00:00
);
2021-07-05 15:58:19 +00:00
const [key, setKey] = useState(0);
const refresh = () => {
setKey(new Date().getTime());
};
const loader = async () => {
const activeClients = await adminClient.sessions.find();
const clientSessions = (
await Promise.all(
activeClients.map((client) =>
adminClient.clients.listSessions({ id: client.id })
)
)
).flat();
const userIds = Array.from(
new Set(clientSessions.map((session) => session.userId))
);
const userSessions = (
await Promise.all(
userIds.map((userId) => adminClient.users.listSessions({ id: userId! }))
)
).flat();
return userSessions;
};
2021-07-05 16:13:17 +00:00
return (
<>
<ViewHeader titleKey="sessions:title" subKey="sessions:sessionExplain" />
<PageSection variant="light" className="pf-u-p-0">
<KeycloakDataTable
2021-07-06 08:17:04 +00:00
key={key}
loader={loader}
ariaLabelKey="session:title"
searchPlaceholderKey="sessions:searchForSession"
2021-07-05 15:58:19 +00:00
searchTypeComponent={
<Select
data-testid="filter-session-type-select"
isOpen={filterDropdownOpen}
2021-07-20 18:53:36 +00:00
className="kc-filter-session-type-select"
2021-07-05 15:58:19 +00:00
variant={SelectVariant.single}
2021-07-06 08:17:04 +00:00
onToggle={(isExpanded) => setFilterDropdownOpen(isExpanded)}
2021-07-05 15:58:19 +00:00
toggleIcon={<FilterIcon />}
onSelect={(_, value) => {
2021-07-06 08:17:04 +00:00
setFilterType(value.toString());
2021-07-05 15:58:19 +00:00
refresh();
setFilterDropdownOpen(false);
}}
selections={filterType}
>
2021-07-06 08:17:04 +00:00
<SelectOption
data-testid="all-sessions-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.allSessions")}
2021-07-06 08:17:04 +00:00
isPlaceholder
/>
<SelectOption
data-testid="regular-sso-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.regularSSO")}
2021-07-06 08:17:04 +00:00
/>
<SelectOption
data-testid="offline-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.offline")}
2021-07-06 08:17:04 +00:00
/>
<SelectOption
data-testid="direct-grant-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.directGrant")}
2021-07-06 08:17:04 +00:00
/>
<SelectOption
data-testid="service-account-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.serviceAccount")}
2021-07-06 08:17:04 +00:00
/>
2021-07-05 15:58:19 +00:00
</Select>
}
columns={[
{
name: "username",
displayKey: "sessions:subject",
},
{
name: "lastAccess",
displayKey: "sessions:lastAccess",
cellRenderer: (row) => moment(row.lastAccess).fromNow(),
},
{
name: "start",
displayKey: "sessions:startDate",
cellRenderer: (row) => moment(row.lastAccess).format("LLL"),
},
{
name: "clients",
displayKey: "sessions:accessedClients",
cellRenderer: Clients,
},
]}
emptyState={
<ListEmptyState
message={t("emptyTitle")}
instructions={t("emptyInstructions")}
/>
}
/>
</PageSection>
</>
);
};