import { useDebounce } from '@uidotdev/usehooks' import { useParams } from 'common' import { useRouter } from 'next/router' import { useMemo, useState } from 'react' import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader' import { ExplorerNavResourceWrapper } from './ExplorerLayout.constants' import { ExplorerNavItem } from './ExplorerNavItem' import { useExplorerDeleteItem } from './ExplorerProvider' import { InfiniteListDefault, LoaderForIconMenuItems, type RowComponentBaseProps, } from '@/components/ui/InfiniteList' import { NotebookRow, useNotebooksInfiniteQuery, } from '@/data/content/notebooks/notebooks-infinite-query' import { createTabId, useTabsStateSnapshot } from '@/state/tabs' const NOTEBOOK_ROW_HEIGHT = 28 type NotebookListItemProps = RowComponentBaseProps & { projectRef: string | undefined activeNotebookId: string | undefined onSelectDelete: (item: { id: string; type: 'notebook' | 'chat'; name: string }) => void } const NotebookListItem = ({ item: notebook, style, projectRef, activeNotebookId, onSelectDelete, }: NotebookListItemProps) => { const isActive = activeNotebookId === notebook.id const tabs = useTabsStateSnapshot() return ( tabs.makeTabPermanent(createTabId('notebook', { id: notebook.id }))} onSelectDelete={() => onSelectDelete({ id: notebook.id, type: 'notebook', name: notebook.name }) } /> ) } export const ExplorerNavNotebooks = () => { const router = useRouter() const { ref, id } = useParams() const { onSelectDelete } = useExplorerDeleteItem() 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, onSelectDelete }), [ref, activeNotebookId, onSelectDelete] ) 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} /> )}
) }