import { client } from '@/ui/api/client' import { useInfiniteQuery } from '@tanstack/react-query' import { Loader2, PanelLeftClose } from 'lucide-react' import type { FC } from 'react' import type { AssetInfoPaginatedList } from '@shumai/dtos' import { useEffect, useRef } from 'react' import { useInView } from 'react-intersection-observer' import { SidebarFileCard } from './sidebar-file-card' import { Button } from './ui/button' import { ScrollArea } from './ui/scroll-area' interface FileViewerLeftSidebarProps { projectId: string currentAssetId: string parentFolderId: string onCollapse: () => void } export const FileViewerLeftSidebar: FC = ({ projectId, currentAssetId, parentFolderId, onCollapse, }) => { const { ref, inView } = useInView({ threshold: 0, rootMargin: '200px', }) const activeItemRef = useRef(null) const hasScrolledRef = useRef(false) const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } = useInfiniteQuery({ queryKey: [ 'folders', parentFolderId, 'children', { assetType: 'file', }, ], queryFn: async ({ pageParam }) => { const res = await client.api.folders[':folderId'].children.$get({ param: { folderId: parentFolderId }, query: { assetType: 'file', after: pageParam, first: '20', }, }) if (!res.ok) throw new Error('failed to fetch folder children') return (await res.json()) as unknown as AssetInfoPaginatedList }, initialPageParam: '', getNextPageParam: (lastPage) => lastPage.pageInfo?.cursor || undefined, }) const files = data?.pages.flatMap((page) => page.data) ?? [] useEffect(() => { if (inView && hasNextPage && !isFetchingNextPage) { fetchNextPage() } }, [inView, hasNextPage, isFetchingNextPage, fetchNextPage]) // Reset scroll lock when current asset changes useEffect(() => { hasScrolledRef.current = false }, [currentAssetId]) // Scroll the active item into view useEffect(() => { const activeFileInView = files.some((f) => f?.id === currentAssetId) if (activeFileInView && activeItemRef.current && !hasScrolledRef.current) { activeItemRef.current.scrollIntoView({ block: 'center', }) hasScrolledRef.current = true } }, [files, currentAssetId]) return (

Current Folder

{isLoading && (
)} {files.map( (file) => file && ( ), )}
{isFetchingNextPage && (
)}
) }