import { useParams } from 'common' import dayjs from 'dayjs' import { NotebookText, Plus } from 'lucide-react' import { AiIconAnimation, Badge, Button } from 'ui' import { Row } from 'ui-patterns/Row' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { HomeCard } from './HomeCard' import { useAnalyzeNotebook, useCreateNotebook } from '@/components/interfaces/Explorer/hooks' import { AlertError } from '@/components/ui/AlertError' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { useNotebooksInfiniteQuery, type NotebookRow, } from '@/data/content/notebooks/notebooks-infinite-query' const MAX_HOMEPAGE_NOTEBOOKS = 4 export const NotebooksSection = () => { const { ref: projectRef } = useParams() const { createNotebook } = useCreateNotebook() const { data, error, isPending, isError, isSuccess, refetch } = useNotebooksInfiniteQuery({ projectRef, limit: MAX_HOMEPAGE_NOTEBOOKS, sort: 'inserted_at', }) // The content endpoint can return more rows than `limit`, so cap the row here too const notebooks = (data?.pages[0]?.content ?? []).slice(0, MAX_HOMEPAGE_NOTEBOOKS) return (
{isPending && ( <>
)} {!isPending && (

Notebooks

)} {isError && ( refetch()}> Retry } /> )} {isSuccess && notebooks.length === 0 && ( createNotebook()} /> )} {isSuccess && notebooks.length > 0 && ( {notebooks.map((notebook) => ( ))} )}
) } function NotebookCard({ notebook, projectRef, }: { notebook: NotebookRow projectRef: string | undefined }) { const { analyzeNotebook, isCreating } = useAnalyzeNotebook() const cellCount = notebook.content?.cells?.length // Treat an unknown cell count as empty so Analyze is never offered on a notebook with no cells const isEmpty = !cellCount return ( } label="NOTEBOOK" title={notebook.name} description={notebook.description?.trim()} meta={`Created ${dayjs(notebook.inserted_at).fromNow()}`} actions={ <> {cellCount !== undefined && ( {cellCount} {cellCount === 1 ? 'CELL' : 'CELLS'} )} } disabled={isEmpty} onClick={() => analyzeNotebook({ id: notebook.id, name: notebook.name })} aria-label="Analyze" tooltip={{ content: { text: isEmpty ? 'Add a cell to the notebook to analyze it' : 'Analyze', }, }} /> } /> ) } function EmptyState({ onCreateNotebook }: { onCreateNotebook: () => void }) { return (

Create a notebook

Save queries and notes together to revisit and share

) }