feat(webui): add compare versions view for version stacks

Add a Compare Versions mode to the file detail page (authenticated and
public share) that splits the viewer into two 50/50 panes showing two
versions side by side.

- Breadcrumb file menu gains a Compare Versions entry (version stacks
  with >=2 versions); the file segment is hidden in compare mode.
- URL-driven state (compare/cmpLeft/cmpRight/cmpActive) on both file
  routes so the view is refresh/share-safe.
- Per-side topbar with filename, version, version-switch dropdown, an
  active-side highlight, and an X exit button.
- Shared control bar reflects the active pane; for same-type media it
  coordinates both panes (play/pause, seek, frame-step, zoom, pan sync),
  seekbar length matches the active side while the shorter side holds its
  last frame; only the active side plays audio; resolution/download apply
  to the active side; fullscreen covers the whole compare view.
- Right sidebar (comments/fields) binds to the active side; comment
  selection seeks the active pane and annotations/comments can be created.
- Reuses useFramePlayer + DrawingCanvas without modifying the existing
  single-file video player.
- Unit tests for compare selection/clamp logic.
This commit is contained in:
yiling
2026-07-04 10:20:16 +08:00
parent e7273f1a02
commit 6c3d3ea2b3
25 changed files with 2056 additions and 38 deletions
@@ -1,5 +1,7 @@
import { client } from '@/ui/api/client'
import { FileViewer } from '@/ui/components/file-viewer'
import { CompareViewer } from '@/ui/components/compare/compare-viewer'
import { pickDefaultCompareVersions } from '@/ui/components/compare/compare-utils'
import { FileViewerLeftSidebar } from '@/ui/components/file-viewer-left-sidebar'
import { m } from '@/ui/paraglide/messages.js'
import { FileViewerRightSidebar } from '@/ui/components/file-viewer-right-sidebar'
@@ -25,6 +27,10 @@ function FileViewPage() {
const versionAssetId = search.version
const startTime = search.start
const activeFileId = versionAssetId || fileId
const compareLeftId = search.cmpLeft
const compareRightId = search.cmpRight
const compareActiveSide = search.cmpActive ?? 'left'
const isCompareMode = !!search.compare && !!compareLeftId && !!compareRightId
const videoRef = useRef<Player | null>(null)
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
@@ -32,6 +38,10 @@ function FileViewPage() {
const [annotations, setAnnotations] = useState<Annotation[]>([])
const [currentTime, setCurrentTime] = useState(0)
const [selectedCommentId, setSelectedCommentId] = useState<string | null>(null)
const [compareActiveAsset, setCompareActiveAsset] = useState<AssetInfo | null>(null)
const [seekRequest, setSeekRequest] = useState<{ second: number; nonce: number } | undefined>(
undefined,
)
const [carouselState, setCarouselState] = useState<{
show: boolean
files: AssetInfo[]
@@ -108,7 +118,7 @@ function FileViewPage() {
if (!res.ok) throw new Error('Failed to fetch versions')
return await res.json()
},
enabled: !!projectId && !!fileId && !!versionAssetId,
enabled: !!projectId && !!fileId && (!!versionAssetId || isCompareMode),
})
const { data: projectInfo } = useQuery({
@@ -158,6 +168,24 @@ function FileViewPage() {
})),
},
versions: versionsDataList,
compareMode: isCompareMode,
canCompareVersions: (versionsDataList?.length ?? 0) >= 2,
onCompareVersions: () => {
const pair = pickDefaultCompareVersions(versionsDataList)
if (!pair) return
navigate({
to: '/projects/$projectId/files/$fileId',
params: { projectId, fileId },
search: (p: Record<string, unknown>) => ({
...p,
version: undefined,
compare: true,
cmpLeft: pair.left.id,
cmpRight: pair.right.id,
cmpActive: 'left' as const,
}),
})
},
onFolderClick: (id: string) => {
navigate({
to: '/projects/$projectId/folders/$folderId',
@@ -176,6 +204,7 @@ function FileViewPage() {
versionsDataList,
activeFileId,
fileId,
isCompareMode,
setProjectState,
clearProjectState,
navigate,
@@ -270,6 +299,11 @@ function FileViewPage() {
setAnnotations([])
}, [fileData?.id])
useEffect(() => {
setSelectedCommentId(null)
setAnnotations([])
}, [compareActiveAsset?.id])
if (isLoading && !fileData) {
return <FileDetailSkeleton />
}
@@ -278,23 +312,45 @@ function FileViewPage() {
return <div>{m.file_not_found()}</div>
}
const sidebarFile = isCompareMode ? compareActiveAsset : (fileData as unknown as AssetInfo)
const sidebarFileId = sidebarFile?.id ?? activeFileId
const handleSaveField = (fieldId: string, value: unknown) => {
if (!teamId) return
patchMetadata(
{
param: { fileId: activeFileId },
param: { fileId: sidebarFileId },
json: [{ key: fieldId, value }] as InferRequestType<typeof $patchMetadata>['json'],
},
{
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ['files', activeFileId],
queryKey: ['files', sidebarFileId],
})
},
},
)
}
const updateCompareSearch = (patch: Record<string, unknown>) => {
navigate({
to: '/projects/$projectId/files/$fileId',
params: { projectId, fileId },
search: (p: Record<string, unknown>) => ({ ...p, ...patch }),
})
}
const handleCompareExit = () => {
const activeId = compareActiveSide === 'left' ? compareLeftId : compareRightId
updateCompareSearch({
compare: undefined,
cmpLeft: undefined,
cmpRight: undefined,
cmpActive: undefined,
version: activeId,
})
}
const handleCommentSelect = (comment: CommentInfo) => {
setSelectedCommentId(comment.id || null)
const newAnnotations = comment.annotations as Annotation[]
@@ -304,6 +360,13 @@ function FileViewPage() {
setAnnotations([])
}
if (isCompareMode) {
if (comment.second !== null && comment.second !== undefined) {
setSeekRequest({ second: comment.second, nonce: Date.now() })
}
return
}
if (comment.second !== null && comment.second !== undefined) {
if (videoRef.current) {
videoRef.current.currentTime(comment.second)
@@ -325,29 +388,50 @@ function FileViewPage() {
<div className="h-full flex flex-1 flex-col bg-background">
<div className="h-full flex flex-1 overflow-hidden">
<div className="flex-1 relative">
{isFetching && (
{isFetching && !isCompareMode && (
<div className="absolute inset-0 z-10 flex items-center justify-center bg-background/50">
<Loader2 className="h-8 w-8 animate-spin text-foreground" />
</div>
)}
<FileViewer
file={fileData as unknown as AssetInfo}
videoRef={videoRef}
onPlay={handlePlay}
onTimeUpdate={setCurrentTime}
annotations={annotations}
startTime={startTime}
>
{carouselState.show && (
<FileViewerLeftSidebar
projectId={projectId}
currentAssetId={activeFileId}
parentFolderId={parentFolderId}
initialFiles={carouselState.files}
initialNextCursor={carouselState.nextCursor}
/>
)}
</FileViewer>
{isCompareMode && compareLeftId && compareRightId ? (
<CompareViewer
versions={versionsDataList ?? []}
leftId={compareLeftId}
rightId={compareRightId}
activeSide={compareActiveSide}
annotations={annotations}
seekRequest={seekRequest}
onActiveSideChange={(side) => updateCompareSearch({ cmpActive: side })}
onSwitchVersion={(side, versionId) =>
updateCompareSearch(
side === 'left' ? { cmpLeft: versionId } : { cmpRight: versionId },
)
}
onExit={handleCompareExit}
onActiveAssetChange={setCompareActiveAsset}
onPlay={handlePlay}
onTimeUpdate={setCurrentTime}
/>
) : (
<FileViewer
file={fileData as unknown as AssetInfo}
videoRef={videoRef}
onPlay={handlePlay}
onTimeUpdate={setCurrentTime}
annotations={annotations}
startTime={startTime}
>
{carouselState.show && (
<FileViewerLeftSidebar
projectId={projectId}
currentAssetId={activeFileId}
parentFolderId={parentFolderId}
initialFiles={carouselState.files}
initialNextCursor={carouselState.nextCursor}
/>
)}
</FileViewer>
)}
</div>
<ResizeHandle
onResize={(delta) => {
@@ -359,12 +443,13 @@ function FileViewPage() {
<FileViewerRightSidebar
teamId={teamId}
projectId={projectId}
file={fileData as unknown as AssetInfo}
file={sidebarFile}
onSaveField={handleSaveField}
members={members}
onCommentSelect={handleCommentSelect}
currentTime={currentTime}
onTyping={() => {
if (isCompareMode) return
if (videoRef.current) {
videoRef.current.pause()
}
@@ -383,6 +468,17 @@ export const Route = createFileRoute('/projects/$projectId/files/$fileId')({
return {
version: (search.version as string) || undefined,
start: search.start ? Number(search.start) : undefined,
} as { version?: string; start?: number }
compare: search.compare === true || search.compare === 'true' || undefined,
cmpLeft: (search.cmpLeft as string) || undefined,
cmpRight: (search.cmpRight as string) || undefined,
cmpActive: search.cmpActive === 'right' ? 'right' : undefined,
} as {
version?: string
start?: number
compare?: boolean
cmpLeft?: string
cmpRight?: string
cmpActive?: 'left' | 'right'
}
},
})