import React, { useState } from "react";
import { Link } from "react-router-dom";
import { useTranslation } from "react-i18next";
import moment from "moment";
import { DropdownItem, PageSection } from "@patternfly/react-core";
import { CubesIcon } from "@patternfly/react-icons";
import type UserSessionRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userSessionRepresentation";
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
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";
import { RevocationModal } from "./RevocationModal";
import { LogoutAllSessionsModal } from "./LogoutAllSessionsModal";
import helpUrls from "../help-urls";
import { toClient } from "../clients/routes/Client";
import { useRealm } from "../context/realm-context/RealmContext";
import "./SessionsSection.css";
export default function SessionsSection() {
const { t } = useTranslation("sessions");
const adminClient = useAdminClient();
const { realm } = useRealm();
const [revocationModalOpen, setRevocationModalOpen] = useState(false);
const [logoutAllSessionsModalOpen, setLogoutAllSessionsModalOpen] =
useState(false);
const [activeClientDetails, setActiveClientDetails] = useState<
ClientRepresentation[]
>([]);
const [noSessions, setNoSessions] = useState(false);
const handleRevocationModalToggle = () => {
setRevocationModalOpen(!revocationModalOpen);
};
const handleLogoutAllSessionsModalToggle = () => {
setLogoutAllSessionsModalOpen(!logoutAllSessionsModalOpen);
};
const loader = async () => {
const activeClients = await adminClient.sessions.find();
const clientSessions = (
await Promise.all(
activeClients.map((client) =>
adminClient.clients.listSessions({ id: client.id })
)
)
).flat();
setNoSessions(clientSessions.length === 0);
const allClients = await adminClient.clients.find();
const getActiveClientDetails = allClients.filter((x) =>
activeClients.map((y) => y.id).includes(x.id)
);
setActiveClientDetails(getActiveClientDetails);
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;
};
const Clients = (row: UserSessionRepresentation) => (
<>
{Object.entries(row.clients!).map(([clientId, client]) => (
{client}
))}
>
);
const dropdownItems = [
handleRevocationModalToggle()}
>
{t("revocation")}
,
handleLogoutAllSessionsModalToggle()}
>
{t("signOutAllActiveSessions")}
,
];
return (
<>
{revocationModalOpen && (
{
handleRevocationModalToggle();
}}
/>
)}
{logoutAllSessionsModalOpen && (
)}
moment(row.lastAccess).fromNow(),
},
{
name: "start",
displayKey: "sessions:startDate",
cellRenderer: (row) => moment(row.lastAccess).format("LLL"),
},
{
name: "clients",
displayKey: "sessions:accessedClients",
cellRenderer: Clients,
},
]}
emptyState={
}
/>
>
);
}