"use client"; import { useState } from "react"; import { KBIndexType } from "@/types"; import { useKBDocuments } from "@/hooks/use-knowledge-base"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { KBIndexTypeBadge } from "./kb-index-type-badge"; import { FileCode, FolderOpen, Clock, ChevronLeft, ChevronRight } from "lucide-react"; import { formatDistanceToNow } from "date-fns"; const PAGE_SIZE = 25; interface KBCategoryViewProps { category: KBIndexType | null; } // Inner component that resets when category changes via key function KBCategoryViewInner({ category }: { category: KBIndexType }) { const [page, setPage] = useState(1); const offset = (page - 1) * PAGE_SIZE; const { data, isLoading } = useKBDocuments( category, { limit: PAGE_SIZE, offset }, true ); if (isLoading) { return (
{[1, 2, 3, 4, 5].map((i) => (
))}
); } const documents = data?.documents ?? []; const totalPages = Math.ceil((data?.total ?? 0) / PAGE_SIZE); return (
{/* Documents List */}
{documents.length === 0 ? (

No documents in this category yet.

) : ( documents.map((doc) => (

{doc.source}

{formatDistanceToNow(new Date(doc.indexed_at), { addSuffix: true })}
)) )}
{/* Pagination */} {totalPages > 1 && (

Page {page} of {totalPages} ({data?.total} documents)

)}
); } export function KBCategoryView({ category }: KBCategoryViewProps) { if (!category) { return (

Browse Categories

Select a category from the left to browse indexed documents.

); } // Key by category to reset state when category changes return ; }