import React, { useState } from "react"; import { Link } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { PageSection, Select, SelectOption, SelectVariant, } from "@patternfly/react-core"; import moment from "moment"; import type UserSessionRepresentation from "keycloak-admin/lib/defs/userSessionRepresentation"; import { ViewHeader } from "../components/view-header/ViewHeader"; import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable"; import { useAdminClient } from "../context/auth/AdminClient"; import { FilterIcon } from "@patternfly/react-icons"; import "./SessionsSection.css"; const Clients = (row: UserSessionRepresentation) => { return ( <> {Object.values(row.clients!).map((client) => ( {client} ))} ); }; export const SessionsSection = () => { const { t } = useTranslation("sessions"); const adminClient = useAdminClient(); const [filterDropdownOpen, setFilterDropdownOpen] = useState(false); const [filterType, setFilterType] = useState( t("sessions:All session types").toString() ); 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; }; return ( <> setFilterDropdownOpen(isExpanded)} toggleIcon={} onSelect={(_, value) => { setFilterType(value.toString()); refresh(); setFilterDropdownOpen(false); }} selections={filterType} > } 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, }, ]} /> ); };