'use client' import { FC, useCallback, useEffect, useMemo, useRef, useState } from 'react' import { useQueryState } from 'nuqs' import { useStore } from '@/store' import useSessionLoader from '@/hooks/useSessionLoader' import SessionItem from './SessionItem' import SessionBlankState from './SessionBlankState' import { Skeleton } from '@/components/ui/skeleton' import { cn } from '@/lib/utils' interface SkeletonListProps { skeletonCount: number } const SkeletonList: FC = ({ skeletonCount }) => { const list = useMemo( () => Array.from({ length: skeletonCount }, (_, i) => i), [skeletonCount] ) return list.map((k, idx) => ( 0 && 'bg-background-secondary' )} /> )) } const Sessions = () => { const [agentId] = useQueryState('agent', { parse: (v) => v || undefined, history: 'push' }) const [teamId] = useQueryState('team') const [sessionId] = useQueryState('session') const [dbId] = useQueryState('db_id') const { selectedEndpoint, mode, isEndpointActive, isEndpointLoading, hydrated, sessionsData, setSessionsData, isSessionsLoading } = useStore() console.log({ sessionsData }) const [isScrolling, setIsScrolling] = useState(false) const [selectedSessionId, setSelectedSessionId] = useState( null ) const { getSessions, getSession } = useSessionLoader() const scrollTimeoutRef = useRef>(null) const handleScroll = () => { setIsScrolling(true) if (scrollTimeoutRef.current) { clearTimeout(scrollTimeoutRef.current) } scrollTimeoutRef.current = setTimeout(() => { setIsScrolling(false) }, 1500) } // Cleanup the scroll timeout when component unmounts useEffect(() => { return () => { if (scrollTimeoutRef.current) { clearTimeout(scrollTimeoutRef.current) } } }, []) useEffect(() => { if (hydrated && sessionId && selectedEndpoint && (agentId || teamId)) { const entityType = agentId ? 'agent' : 'team' getSession({ entityType, agentId, teamId, dbId }, sessionId) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [hydrated, sessionId, selectedEndpoint, agentId, teamId, dbId]) useEffect(() => { if (!selectedEndpoint || isEndpointLoading) return if (!(agentId || teamId || dbId)) { setSessionsData([]) return } setSessionsData([]) getSessions({ entityType: mode, agentId, teamId, dbId }) // eslint-disable-next-line react-hooks/exhaustive-deps }, [ selectedEndpoint, agentId, teamId, mode, isEndpointLoading, getSessions, dbId ]) useEffect(() => { if (sessionId) setSelectedSessionId(sessionId) }, [sessionId]) const handleSessionClick = useCallback( (id: string) => () => setSelectedSessionId(id), [] ) if (isSessionsLoading || isEndpointLoading) { return (
Sessions
) } return (
Sessions
setIsScrolling(true)} onMouseLeave={handleScroll} > {!isEndpointActive || (!isSessionsLoading && (!sessionsData || sessionsData?.length === 0)) ? ( ) : (
{sessionsData?.map((entry, idx) => ( ))}
)}
) } export default Sessions