"use client"; import { Archive, ChevronLeft, ChevronRight, PanelRightOpen, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useThreads, ThreadWithTasks } from "@/providers/Thread"; import { useQueryState, parseAsString } from "nuqs"; import { useState, useCallback } from "react"; import { ThreadItem } from "./thread-item"; const THREADS_PER_PAGE = 10; interface TaskListSidebarProps { onCollapse?: () => void; } export default function TaskListSidebar({ onCollapse }: TaskListSidebarProps) { const [taskId, setTaskId] = useQueryState("taskId", parseAsString); const [threadId, setThreadId] = useQueryState("threadId", parseAsString); const [currentPage, setCurrentPage] = useState(0); const { threads, threadsLoading, handleThreadClick } = useThreads(); const onThreadClick = useCallback( (thread: ThreadWithTasks) => { handleThreadClick(thread, threadId, setThreadId); }, [handleThreadClick, threadId, setThreadId], ); // Sort threads by creation date (newest first) TODO use provider (already done there) const sortedThreads = threads; const totalThreads = sortedThreads.length; const totalPages = Math.ceil(totalThreads / THREADS_PER_PAGE); const startIndex = currentPage * THREADS_PER_PAGE; const endIndex = startIndex + THREADS_PER_PAGE; const paginatedThreads = sortedThreads.slice(startIndex, endIndex); return (

Threads

{totalThreads} total threads

{onCollapse && ( )}
{threadsLoading && threads.length === 0 ? (

Loading threads...

) : paginatedThreads.length > 0 ? (
{paginatedThreads.map((thread) => ( ))}
) : (

No threads found

)}
{totalPages > 1 && (
Page {currentPage + 1} of {totalPages}
)}
); }