import { useDebounce } from '@uidotdev/usehooks' import { useParams } from 'common' import { NotebookText } from 'lucide-react' import Link from 'next/link' import { useRouter } from 'next/router' import { useMemo, useState } from 'react' import { cn } from 'ui' import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader' import { ExplorerNavResourceWrapper, rowClassName } from './ExplorerLayout.constants' import { InfiniteListDefault, LoaderForIconMenuItems, type RowComponentBaseProps, } from '@/components/ui/InfiniteList' import { NotebookRow, useNotebooksInfiniteQuery, } from '@/data/content/notebooks/notebooks-infinite-query' const NOTEBOOK_ROW_HEIGHT = 28 type NotebookListItemProps = RowComponentBaseProps & { projectRef: string | undefined activeNotebookId: string | undefined } const NotebookListItem = ({ item: notebook, style, projectRef, activeNotebookId, }: NotebookListItemProps) => { const isActive = activeNotebookId === notebook.id return ( {notebook.name} ) } export const ExplorerNavNotebooks = ({ onBack }: { onBack: () => void }) => { const router = useRouter() const { ref, id } = useParams() const [search, setSearch] = useState('') const debouncedSearch = useDebounce(search, 500) const { data: notebooksData, isPending, hasNextPage, isFetchingNextPage, fetchNextPage, } = useNotebooksInfiniteQuery({ projectRef: ref, limit: 100, name: search.length === 0 ? search : debouncedSearch, }) const notebooks = useMemo(() => { const items = notebooksData?.pages.flatMap((page) => page.content) ?? [] return items }, [notebooksData?.pages]) const activeNotebookId = router.pathname.includes('/explorer/notebook/') ? id : undefined const itemProps = useMemo(() => ({ projectRef: ref, activeNotebookId }), [ref, activeNotebookId]) return (
{isPending ? ( ) : notebooks.length === 0 ? (

{search ? 'No notebooks found' : 'No notebooks created yet'}

) : ( notebooks[index]?.id ?? `notebook-${index}`} getItemSize={() => NOTEBOOK_ROW_HEIGHT} gap={1} hasNextPage={hasNextPage} isLoadingNextPage={isFetchingNextPage} onLoadNextPage={fetchNextPage} /> )}
) }