import { useState, useEffect, useRef } from 'react' import { useQuery } from '@tanstack/react-query' import { apiFetch } from '@/lib/http' import { LoadingSkeleton } from '@/components/common/LoadingSkeleton' import { cn } from '@/lib/utils' interface HistoryItem { id: string query: string answer_brief: string created_at: string success: boolean duration_ms: number } interface HistoryResponse { items: HistoryItem[] total: number } async function fetchHistory(page: number): Promise { const res = await apiFetch(`/api/workspace/history?page=${page}&limit=20`) return res.json() } interface Props { onReplay?: (query: string) => void focusId?: string } export function QueryHistory({ onReplay, focusId }: Props) { const [page, setPage] = useState(1) const [expanded, setExpanded] = useState(focusId ?? null) const focusRef = useRef(null) const { data, isLoading } = useQuery({ queryKey: ['workspace-history', page], queryFn: () => fetchHistory(page), staleTime: 60_000, }) // Scroll the focused item into view once data loads useEffect(() => { if (focusId && focusRef.current) { focusRef.current.scrollIntoView({ behavior: 'smooth', block: 'center' }) } }, [focusId, data]) const totalPages = data ? Math.ceil(data.total / 20) : 1 return (

Query History

{data && (

{data.total} total queries

)}
{isLoading ? (
) : (data?.items ?? []).length === 0 ? (

No queries yet. Ask something to get started.

) : (
{(data?.items ?? []).map((item) => (
{expanded === item.id && (

{item.answer_brief}

{onReplay && ( )}
)}
))}
)} {/* Pagination */} {totalPages > 1 && (
Page {page} of {totalPages}
)}
) }