"use client"; import { Archive, ChevronLeft, ChevronRight } 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 = 5; export default function TaskList() { const [taskId, setTaskId] = useQueryState("taskId", parseAsString); const [threadId, setThreadId] = useQueryState("threadId", parseAsString); const [currentPage, setCurrentPage] = useState(0); const { threads, threadsLoading, handleThreadClick } = useThreads(); const isDashboardMode = !taskId; const onThreadClick = useCallback( (thread: ThreadWithTasks) => { handleThreadClick(thread, threadId, setThreadId); setTaskId(null); }, [handleThreadClick, threadId, setThreadId, setTaskId], ); if (!isDashboardMode) { return null; } // Threads are already sorted by creation date in ThreadProvider 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})

{threadsLoading ? (

Loading threads...

) : paginatedThreads.length > 0 ? (
{paginatedThreads.map((thread) => ( ))} {/* Pagination Controls */} {totalPages > 1 && (
Showing {startIndex + 1}-{Math.min(endIndex, totalThreads)} of{" "} {totalThreads} threads
)}
) : (

No threads found

)}
); }