import { useState } from "react"; import { Button, Divider, Group, Skeleton, Stack, Table, Text, } from "@mantine/core"; import { IconDevices } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; import { useGetSessionsQuery, useRevokeSessionMutation, useRevokeAllSessionsMutation, } from "@/features/session/queries/session-query"; import { formattedDate } from "@/lib/time"; const PAGE_SIZE = 5; export default function SessionList() { const { t } = useTranslation(); const { data: sessions, isLoading } = useGetSessionsQuery(); const revokeSessionMutation = useRevokeSessionMutation(); const revokeAllSessionsMutation = useRevokeAllSessionsMutation(); const [visibleCount, setVisibleCount] = useState(PAGE_SIZE); const otherSessions = sessions?.filter((s) => !s?.isCurrentDevice) ?? []; const visibleSessions = sessions?.slice(0, visibleCount) ?? []; const hasMore = sessions && visibleCount < sessions.length; if (isLoading) { return ( {t("Device Name")} {t("Last Active")} {[1, 2, 3].map((i) => ( ))}
); } return ( {otherSessions.length > 0 && ( <>
{t("Log out of all devices")} {t( "Log out of all sessions except this device", )}
)} {t("Device Name")} {t("Last Active")} {otherSessions.length > 0 && } {visibleSessions.map((session) => (
{session.deviceName || t("Unknown device")} {session?.isCurrentDevice && ( {t("This Device")} )}
{session?.isCurrentDevice ? t("Now") : formattedDate(new Date(session.lastActiveAt))} {otherSessions.length > 0 && ( {!session?.isCurrentDevice && ( )} )}
))}
{hasMore && ( )} {(!sessions || sessions.length === 0) && ( {t("No active sessions")} )}
); }