feat(versions): add version switching in stack detail page

This commit is contained in:
yiling
2026-05-23 10:49:56 +08:00
parent 190973b495
commit 308467f05d
8 changed files with 467 additions and 28 deletions
+95 -3
View File
@@ -5,6 +5,11 @@ import {
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
DropdownMenuSub,
DropdownMenuSubTrigger,
DropdownMenuSubContent,
DropdownMenuPortal,
DropdownMenuSeparator,
} from '@/ui/components/ui/dropdown-menu'
import {
DockToLeft,
@@ -12,8 +17,8 @@ import {
DockToRight,
DockToRightFilled,
} from '@/ui/components/ui/icons'
import { Link } from '@tanstack/react-router'
import { ChevronDown, LayoutGrid, List } from 'lucide-react'
import { Link, useNavigate } from '@tanstack/react-router'
import { ChevronDown, LayoutGrid, List, History, Check, FileIcon } from 'lucide-react'
import { cn } from '@/ui/lib/utils'
interface BreadcrumbNavProps {
@@ -36,6 +41,14 @@ interface BreadcrumbNavProps {
isPublic?: boolean
shareId?: string
onFolderClick?: (folderId: string) => void
fileId?: string
versions?: Array<{
id: string
version: number
name?: string | null
previewUrl?: string | null
creator?: { id: string; name: string | null } | null
}>
}
export function BreadcrumbNav({
@@ -53,7 +66,20 @@ export function BreadcrumbNav({
onRightSidebarToggle,
isPublic = false,
onFolderClick,
fileId,
versions,
}: BreadcrumbNavProps) {
const navigate = useNavigate()
const handleVersionClick = (versionId: string) => {
if (!projectId || !fileId) return
navigate({
to: '/projects/$projectId/files/$fileId',
params: { projectId, fileId },
search: (prev: Record<string, unknown>) => ({ ...prev, version: versionId }),
})
}
const breadcrumbs: { name: string; path?: string; id?: string; isMuted?: boolean }[] = isPublic
? [
{
@@ -125,12 +151,78 @@ export function BreadcrumbNav({
)}
<ChevronDown className="h-4 w-4" />
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuContent className="w-56">
<DropdownMenuItem onClick={() => console.log('Download')}>
Download
</DropdownMenuItem>
<DropdownMenuItem onClick={() => console.log('Rename')}>Rename</DropdownMenuItem>
<DropdownMenuItem onClick={() => console.log('Delete')}>Delete</DropdownMenuItem>
{versions && versions.length > 0 && (
<>
<DropdownMenuSeparator />
<DropdownMenuSub>
<DropdownMenuSubTrigger className="flex items-center gap-2">
<History className="h-4 w-4" />
<span>Versions</span>
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent className="w-80 p-1">
<div className="flex flex-col gap-1 max-h-96 overflow-y-auto">
{versions.map((v) => {
const isActive = currentAsset.version === v.version
return (
<DropdownMenuItem
key={v.id}
onClick={() => handleVersionClick(v.id)}
className="flex items-center gap-3 py-2 px-3 rounded-md cursor-pointer focus:bg-accent focus:text-accent-foreground"
>
<div className="flex-shrink-0 flex items-center justify-center bg-muted dark:bg-muted-foreground/20 text-muted-foreground dark:text-muted-foreground rounded-full px-2 py-0.5 text-xs font-semibold select-none min-w-[2rem]">
v{v.version}
</div>
<div className="h-10 w-16 flex-shrink-0 overflow-hidden rounded-md border border-border bg-muted flex items-center justify-center">
{v.previewUrl ? (
<img
src={v.previewUrl}
alt={v.name || undefined}
className="h-full w-full object-cover"
/>
) : (
<FileIcon className="h-4 w-4 text-muted-foreground" />
)}
</div>
<div className="flex flex-col flex-1 min-w-0 text-left">
<span className="truncate text-sm font-semibold text-foreground">
{v.name}
</span>
<span className="truncate text-xs text-muted-foreground">
{v.creator?.name || 'Unknown'}
</span>
</div>
{isActive && (
<Check className="h-4 w-4 text-primary ml-auto flex-shrink-0" />
)}
</DropdownMenuItem>
)
})}
</div>
<DropdownMenuSeparator />
<div className="p-1">
<button
className="w-full text-center text-xs font-medium py-2 px-3 rounded-md bg-muted/80 hover:bg-muted text-foreground transition-colors"
onClick={() => console.log('Manage versions')}
>
Manage versions...
</button>
</div>
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
</>
)}
</DropdownMenuContent>
</DropdownMenu>
)}
@@ -258,6 +258,7 @@ export default function FileSystemManager({
navigate({
to: '/projects/$projectId/files/$fileId',
params: { projectId, fileId: item.versionStack ? item.versionStack.id : item.id },
search: { version: undefined },
})
}
}
@@ -20,6 +20,10 @@ import type Player from 'video.js/dist/types/player'
function FileViewPage() {
const { projectId, fileId } = Route.useParams()
const search = Route.useSearch()
const versionAssetId = (search as { version?: string }).version
const activeFileId = versionAssetId || fileId
const videoRef = useRef<Player | null>(null)
const {
fileViewRightSidebarCollapsed: isRightSidebarCollapsed,
@@ -57,23 +61,53 @@ function FileViewPage() {
}, [teamId, fetchMembers])
const {
data: fileData,
isLoading,
isError,
isFetching,
data: stackData,
isLoading: isStackLoading,
isError: isStackError,
isFetching: isStackFetching,
} = useQuery({
queryKey: ['teams', teamId, 'files', fileId],
queryFn: async () => {
const res = await client.api.teams[':teamId'].files[':fileId'].$get({
param: { teamId: teamId!, fileId: fileId },
})
if (!res.ok) throw new Error('Failed to fetch file')
if (!res.ok) throw new Error('Failed to fetch stack')
return await res.json()
},
enabled: !!teamId,
placeholderData: keepPreviousData,
})
const {
data: versionData,
isLoading: isVersionLoading,
isError: isVersionError,
isFetching: isVersionFetching,
} = useQuery({
queryKey: ['teams', teamId, 'files', versionAssetId],
queryFn: async () => {
const res = await client.api.teams[':teamId'].files[':fileId'].$get({
param: { teamId: teamId!, fileId: versionAssetId! },
})
if (!res.ok) throw new Error('Failed to fetch version')
return await res.json()
},
enabled: !!teamId && !!versionAssetId,
placeholderData: keepPreviousData,
})
const { data: versionsList } = useQuery({
queryKey: ['projects', projectId, 'version_stacks', fileId, 'versions'],
queryFn: async () => {
const res = await client.api.projects[':projectId'].version_stacks[':stackId'].versions.$get({
param: { projectId: projectId, stackId: fileId },
})
if (!res.ok) throw new Error('Failed to fetch versions')
return await res.json()
},
enabled: !!projectId && !!fileId && !!versionAssetId,
})
const { data: projectInfo } = useQuery({
queryKey: ['projects', projectId],
queryFn: async () => {
@@ -85,6 +119,12 @@ function FileViewPage() {
},
})
const isLoading = isStackLoading || (!!versionAssetId && isVersionLoading)
const isError = isStackError || (!!versionAssetId && isVersionError)
const isFetching = isStackFetching || (!!versionAssetId && isVersionFetching)
const fileData = versionData || stackData
const versionsDataList = versionAssetId ? versionsList : stackData?.versionStack?.versions
if (isLoading && !fileData) {
return <div>Loading...</div>
}
@@ -97,13 +137,13 @@ function FileViewPage() {
if (!teamId) return
patchMetadata(
{
param: { teamId: teamId, fileId: fileId },
param: { teamId: teamId, fileId: activeFileId },
json: [{ key: fieldId, value }] as InferRequestType<typeof $patchMetadata>['json'],
},
{
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'files', fileId],
queryKey: ['teams', teamId, 'files', activeFileId],
})
},
},
@@ -136,9 +176,9 @@ function FileViewPage() {
currentAsset={{
name: fileData.name,
type: 'file',
version: fileData.versionStack
? (fileData.versionStack.versions.find((v) => v.id === fileData.id)?.version ??
fileData.versionStack.versions.length)
version: versionsDataList
? (versionsDataList.find((v) => v.id === activeFileId)?.version ??
versionsDataList.length)
: undefined,
}}
isRootFolder={false}
@@ -146,6 +186,8 @@ function FileViewPage() {
onLeftSidebarToggle={() => setIsLeftSidebarCollapsed(!isLeftSidebarCollapsed)}
isRightSidebarCollapsed={isRightSidebarCollapsed}
onRightSidebarToggle={() => setIsRightSidebarCollapsed(!isRightSidebarCollapsed)}
fileId={fileId}
versions={versionsDataList}
/>
<div className="flex flex-1 overflow-hidden">
{!isLeftSidebarCollapsed && (
@@ -154,7 +196,7 @@ function FileViewPage() {
<FileViewerLeftSidebar
teamId={teamId!}
projectId={projectId}
currentAssetId={fileId}
currentAssetId={activeFileId}
parentFolderId={
fileData.ancestorFolders?.[fileData.ancestorFolders.length - 1]?.id ??
projectInfo.rootFolder ??
@@ -211,4 +253,9 @@ function FileViewPage() {
export const Route = createFileRoute('/projects/$projectId/files/$fileId')({
component: FileViewPage,
validateSearch: (search: Record<string, unknown>) => {
return {
version: (search.version as string) || undefined,
}
},
})