feat(webui): always show right sidebar and implement left vertical carousel

This commit is contained in:
yiling
2026-06-15 15:37:10 +08:00
parent d8aaed5212
commit 68a1714e7f
5 changed files with 401 additions and 299 deletions
@@ -1,26 +1,40 @@
import { client } from '@/ui/api/client'
import { useInfiniteQuery } from '@tanstack/react-query'
import { Loader2, PanelLeftClose } from 'lucide-react'
import { File, Loader2 } from 'lucide-react'
import type { FC } from 'react'
import type { AssetInfoPaginatedList } from '@shumai/dtos'
import { useEffect, useRef } from 'react'
import type { AssetInfo, AssetInfoPaginatedList } from '@shumai/dtos'
import { useEffect, useRef, useState } 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'
import { Link } from '@tanstack/react-router'
import { cn } from '@/ui/lib/utils'
interface FileViewerLeftSidebarProps {
projectId: string
currentAssetId: string
parentFolderId: string
onCollapse: () => void
initialFiles: AssetInfo[]
initialNextCursor: string | undefined
}
export const CarouselFilePreview = ({ item }: { item: AssetInfo }) => {
if (item.preview?.thumbnailUrl) {
return (
<img src={item.preview.thumbnailUrl} alt="Preview" className="w-full h-full object-cover" />
)
}
return (
<div className="w-full h-full flex items-center justify-center bg-muted">
<File className="w-5 h-5 text-muted-foreground" />
</div>
)
}
export const FileViewerLeftSidebar: FC<FileViewerLeftSidebarProps> = ({
projectId,
currentAssetId,
parentFolderId,
onCollapse,
initialFiles,
initialNextCursor,
}) => {
const { ref, inView } = useInView({
threshold: 0,
@@ -29,38 +43,51 @@ export const FileViewerLeftSidebar: FC<FileViewerLeftSidebarProps> = ({
const activeItemRef = useRef<HTMLDivElement>(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) ?? []
const [files, setFiles] = useState<AssetInfo[]>(initialFiles)
const [nextCursor, setNextCursor] = useState<string | undefined>(initialNextCursor)
const [hasMore, setHasMore] = useState<boolean>(!!initialNextCursor)
const [isFetchingNext, setIsFetchingNext] = useState<boolean>(false)
// Sync state if initial files / cursor changes (due to parent folder change)
useEffect(() => {
if (inView && hasNextPage && !isFetchingNextPage) {
fetchNextPage()
setFiles(initialFiles)
setNextCursor(initialNextCursor)
setHasMore(!!initialNextCursor)
hasScrolledRef.current = false
}, [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)
}
}
}, [inView, hasNextPage, isFetchingNextPage, fetchNextPage])
fetchNextPage()
}, [inView, nextCursor, hasMore, isFetchingNext, parentFolderId])
// Reset scroll lock when current asset changes
useEffect(() => {
@@ -73,47 +100,48 @@ export const FileViewerLeftSidebar: FC<FileViewerLeftSidebarProps> = ({
if (activeFileInView && activeItemRef.current && !hasScrolledRef.current) {
activeItemRef.current.scrollIntoView({
block: 'center',
behavior: 'smooth',
})
hasScrolledRef.current = true
}
}, [files, currentAssetId])
return (
<div className="h-full w-full bg-card border-r border-border flex flex-col">
<div className="p-2 border-b flex items-center justify-between">
<h3 className="text-sm font-semibold px-2">Current Folder</h3>
<Button variant="ghost" size="icon-sm" onClick={onCollapse}>
<PanelLeftClose className="h-4 w-4" />
</Button>
</div>
<ScrollArea className="flex-1 [&>div>div]:block! px-2">
<div className="p-2 space-y-2">
{isLoading && (
<div className="flex justify-center items-center p-4">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
<div className="h-full w-24 bg-card border-r border-border flex flex-col flex-shrink-0 select-none">
<div className="flex-1 overflow-y-auto py-4 flex flex-col items-center gap-3 no-scrollbar">
{files.map((file) => {
if (!file) return null
const isActive = file.id === currentAssetId
return (
<div
key={file.id}
ref={file.id === currentAssetId ? activeItemRef : null}
className="flex-shrink-0"
>
<Link
to="/projects/$projectId/files/$fileId"
params={{ projectId, fileId: file.id }}
search={{ version: undefined }}
className={cn(
'w-[54px] h-[54px] rounded-lg border overflow-hidden flex items-center justify-center transition-all bg-muted/30 block',
isActive
? 'border-primary ring-2 ring-primary opacity-100 scale-105'
: 'border-border hover:border-primary/50 opacity-65 hover:opacity-100',
)}
>
<CarouselFilePreview item={file} />
</Link>
</div>
)}
{files.map(
(file) =>
file && (
<SidebarFileCard
ref={file.id === currentAssetId ? activeItemRef : null}
key={file.id}
projectId={projectId}
item={file}
isActive={file.id === currentAssetId}
/>
),
)}
<div ref={ref}>
{isFetchingNextPage && (
<div className="flex justify-center items-center p-4">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
)}
)
})}
{/* Infinite Scroll Trigger */}
{hasMore && (
<div ref={ref} className="py-2 flex justify-center items-center w-full">
{isFetchingNext && <Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />}
</div>
</div>
</ScrollArea>
)}
</div>
</div>
)
}