import { ComputerDesktopIcon, GlobeAltIcon } from "@heroicons/react/24/outline"; import { type AuthenticatedSessionsRequest, PageSize, useAuthenticatedSessionsQuery, useRevokeAuthenticationMutation } from "@hey/indexer"; import type { ApolloClientError } from "@hey/types/errors"; import dayjs from "dayjs"; import { memo, useCallback, useState } from "react"; import { toast } from "sonner"; import { WindowVirtualizer } from "virtua"; import Loader from "@/components/Shared/Loader"; import { Button, EmptyState, ErrorMessage } from "@/components/Shared/UI"; import errorToast from "@/helpers/errorToast"; import useLoadMoreOnIntersect from "@/hooks/useLoadMoreOnIntersect"; import { useAccountStore } from "@/store/persisted/useAccountStore"; const List = () => { const { currentAccount } = useAccountStore(); const [revoking, setRevoking] = useState(false); const [revokeingSessionId, setRevokeingSessionId] = useState( null ); const onError = useCallback((error: ApolloClientError) => { setRevoking(false); setRevokeingSessionId(null); errorToast(error); }, []); const onCompleted = () => { setRevoking(false); setRevokeingSessionId(null); toast.success("Session revoked"); }; const [revokeAuthentication] = useRevokeAuthenticationMutation({ onCompleted, onError, update: (cache) => { cache.evict({ id: "ROOT_QUERY" }); } }); const handleRevoke = async (authenticationId: string) => { setRevoking(true); setRevokeingSessionId(authenticationId); return await revokeAuthentication({ variables: { request: { authenticationId } } }); }; const request: AuthenticatedSessionsRequest = { pageSize: PageSize.Fifty }; const { data, error, fetchMore, loading } = useAuthenticatedSessionsQuery({ skip: !currentAccount?.address, variables: { request } }); const authenticatedSessions = data?.authenticatedSessions?.items; const pageInfo = data?.authenticatedSessions?.pageInfo; const hasMore = pageInfo?.next; const handleEndReached = useCallback(async () => { if (hasMore) { await fetchMore({ variables: { request: { ...request, cursor: pageInfo?.next } } }); } }, [fetchMore, hasMore, pageInfo?.next, request]); const loadMoreRef = useLoadMoreOnIntersect(handleEndReached); if (loading) { return ; } if (error) { return ( ); } if (!authenticatedSessions?.length) { return ( } message="You are not logged in on any other devices!" /> ); } return (
{authenticatedSessions.map((session) => (
{session.browser ? {session.browser} : null} {session.os ? - {session.os} : null}
{session.origin ? (
Origin - {session.origin}
) : null}
Registered -{" "} {dayjs(session.createdAt).format("MMM D, YYYY - h:mm:ss A")}
Last accessed -{" "} {dayjs(session.updatedAt).format("MMM D, YYYY - h:mm:ss A")}
Expires at -{" "} {dayjs(session.expiresAt).format("MMM D, YYYY - h:mm:ss A")}
))} {hasMore && }
); }; export default memo(List);