import { client } from '@/ui/api/client' import { cn } from '@/ui/lib/utils' import type { AssetInfo, AssetInfoPaginatedList } from '@shumai/dtos' import { Link } from '@tanstack/react-router' import { File, Loader2, AudioLines } from 'lucide-react' import type { FC } from 'react' import { useEffect, useRef, useState } from 'react' import { useInView } from 'react-intersection-observer' interface FileViewerLeftSidebarProps { projectId: string currentAssetId: string parentFolderId: string initialFiles: AssetInfo[] initialNextCursor: string | undefined } export const CarouselFilePreview = ({ item }: { item: AssetInfo }) => { if (item.preview?.thumbnailUrl) { return ( Preview ) } const isAudio = item.preview?.proxyType === 'audio' || item.proxyType === 'audio' if (isAudio) { return (
) } return (
) } export const FileViewerLeftSidebar: FC = ({ projectId, currentAssetId, parentFolderId, initialFiles, initialNextCursor, }) => { const { ref, inView } = useInView({ threshold: 0, rootMargin: '200px', }) const activeItemRef = useRef(null) const isInitialMountRef = useRef(true) const [files, setFiles] = useState(initialFiles) const [nextCursor, setNextCursor] = useState(initialNextCursor) const [hasMore, setHasMore] = useState(!!initialNextCursor) const [isFetchingNext, setIsFetchingNext] = useState(false) // Sync state if initial files / cursor changes (due to parent folder change) useEffect(() => { setFiles(initialFiles) setNextCursor(initialNextCursor) setHasMore(!!initialNextCursor) isInitialMountRef.current = true }, [initialFiles, initialNextCursor]) // Fetch next page when scrolling near bottom useEffect(() => { const fetchNextPage = async () => { if (!nextCursor || isFetchingNext || !inView || !hasMore) return setIsFetchingNext(true) try { const res = await client.api.folders[':folderId'].search.$post({ param: { folderId: parentFolderId }, json: { assetType: 'file', after: nextCursor, first: 200, recursively: false, conditions: [], }, }) if (!res.ok) throw new Error('Failed to fetch next page') const result = (await res.json()) as unknown as AssetInfoPaginatedList const batchFiles = (result.data || []) as AssetInfo[] setFiles((prev) => [...prev, ...batchFiles]) setNextCursor(result.pageInfo?.cursor || undefined) setHasMore(!!result.pageInfo?.cursor && batchFiles.length > 0) } catch (err) { console.error('Error fetching next page:', err) } finally { setIsFetchingNext(false) } } fetchNextPage() }, [inView, nextCursor, hasMore, isFetchingNext, parentFolderId]) // Scroll the active item into view useEffect(() => { const activeFileInView = files.some((f) => f?.id === currentAssetId) if (activeFileInView && activeItemRef.current) { if (isInitialMountRef.current) { activeItemRef.current.scrollIntoView({ block: 'center', inline: 'center', behavior: 'auto', }) isInitialMountRef.current = false } else { activeItemRef.current.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: 'smooth', }) } } }, [files, currentAssetId]) return (
{files.map((file) => { if (!file) return null const isActive = file.id === currentAssetId return (
{file.agent && ( AI )}
) })} {/* Infinite Scroll Trigger */} {hasMore && (
{isFetchingNext && }
)}
) }