mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 23:54:25 +00:00
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:
@@ -0,0 +1,356 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import type { Annotation } from '@/ui/types'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { CompareControlBar } from './compare-control-bar'
|
||||
import { CompareImagePane } from './compare-image-pane'
|
||||
import { ComparePaneTopbar } from './compare-pane-topbar'
|
||||
import { CompareVideoPane } from './compare-video-pane'
|
||||
import { kindOf, isSameType } from './compare-utils'
|
||||
import type { ComparePaneHandle, CompareSide, PaneKind, PaneReportedState } from './types'
|
||||
|
||||
interface VersionItem {
|
||||
id: string
|
||||
version: number
|
||||
name?: string | null
|
||||
previewUrl?: string | null
|
||||
creator?: { id: string; name: string | null } | null
|
||||
}
|
||||
|
||||
interface CompareViewerProps {
|
||||
isPublic?: boolean
|
||||
shareId?: string
|
||||
versions: VersionItem[]
|
||||
leftId: string
|
||||
rightId: string
|
||||
activeSide: CompareSide
|
||||
annotations: Annotation[]
|
||||
/** Bumped nonce requests the active pane seek to `second` (comment navigation). */
|
||||
seekRequest?: { second: number; nonce: number }
|
||||
onActiveSideChange: (side: CompareSide) => void
|
||||
onSwitchVersion: (side: CompareSide, versionId: string) => void
|
||||
onExit: () => void
|
||||
onActiveAssetChange: (asset: AssetInfo | null) => void
|
||||
onPlay?: () => void
|
||||
onTimeUpdate?: (second: number) => void
|
||||
}
|
||||
|
||||
function useCompareAsset(id: string, isPublic: boolean, shareId?: string) {
|
||||
return useQuery({
|
||||
queryKey: isPublic
|
||||
? ['public-share-file', shareId, id, localStorage.getItem(`share_pwd_${shareId}`) || '']
|
||||
: ['files', id],
|
||||
queryFn: async () => {
|
||||
if (isPublic) {
|
||||
const password = localStorage.getItem(`share_pwd_${shareId}`) || ''
|
||||
const res = await client.api.shares[':shareId'].files[':fileId'].$get(
|
||||
{ param: { shareId: shareId!, fileId: id } },
|
||||
{ headers: { 'x-share-password': password } },
|
||||
)
|
||||
if (!res.ok) throw new Error('Failed to fetch version asset')
|
||||
return (await res.json()) as unknown as AssetInfo
|
||||
}
|
||||
const res = await client.api.files[':fileId'].$get({ param: { fileId: id } })
|
||||
if (!res.ok) throw new Error('Failed to fetch version asset')
|
||||
return (await res.json()) as unknown as AssetInfo
|
||||
},
|
||||
enabled: !!id && (!isPublic || !!shareId),
|
||||
})
|
||||
}
|
||||
|
||||
export function CompareViewer({
|
||||
isPublic = false,
|
||||
shareId,
|
||||
versions,
|
||||
leftId,
|
||||
rightId,
|
||||
activeSide,
|
||||
annotations,
|
||||
seekRequest,
|
||||
onActiveSideChange,
|
||||
onSwitchVersion,
|
||||
onExit,
|
||||
onActiveAssetChange,
|
||||
onPlay,
|
||||
onTimeUpdate,
|
||||
}: CompareViewerProps) {
|
||||
const rootRef = useRef<HTMLDivElement>(null)
|
||||
const leftRef = useRef<ComparePaneHandle | null>(null)
|
||||
const rightRef = useRef<ComparePaneHandle | null>(null)
|
||||
|
||||
const [leftState, setLeftState] = useState<PaneReportedState | null>(null)
|
||||
const [rightState, setRightState] = useState<PaneReportedState | null>(null)
|
||||
const [userVolume, setUserVolume] = useState(1)
|
||||
const [userMuted, setUserMuted] = useState(false)
|
||||
const [isFullScreen, setIsFullScreen] = useState(false)
|
||||
|
||||
const { data: leftAsset, isLoading: leftLoading } = useCompareAsset(leftId, isPublic, shareId)
|
||||
const { data: rightAsset, isLoading: rightLoading } = useCompareAsset(rightId, isPublic, shareId)
|
||||
|
||||
const leftKind = kindOf(leftAsset?.mediaType)
|
||||
const rightKind = kindOf(rightAsset?.mediaType)
|
||||
const sameType = isSameType(leftKind, rightKind)
|
||||
|
||||
const activeAsset = activeSide === 'left' ? leftAsset : rightAsset
|
||||
const activeState = activeSide === 'left' ? leftState : rightState
|
||||
|
||||
const getActive = useCallback(
|
||||
() => (activeSide === 'left' ? leftRef.current : rightRef.current),
|
||||
[activeSide],
|
||||
)
|
||||
const getOther = useCallback(
|
||||
() => (activeSide === 'left' ? rightRef.current : leftRef.current),
|
||||
[activeSide],
|
||||
)
|
||||
|
||||
// Bind right sidebar to active asset
|
||||
useEffect(() => {
|
||||
onActiveAssetChange(activeAsset ?? null)
|
||||
}, [activeAsset, onActiveAssetChange])
|
||||
|
||||
// Comment-driven seek on the active pane
|
||||
useEffect(() => {
|
||||
if (!seekRequest) return
|
||||
const active = getActive()
|
||||
active?.seekToSecond(seekRequest.second)
|
||||
active?.pause()
|
||||
if (sameType) {
|
||||
const other = getOther()
|
||||
other?.seekToSecond(seekRequest.second)
|
||||
other?.pause()
|
||||
}
|
||||
}, [seekRequest?.nonce])
|
||||
|
||||
// Fullscreen tracking
|
||||
useEffect(() => {
|
||||
const onFs = () => setIsFullScreen(!!document.fullscreenElement)
|
||||
document.addEventListener('fullscreenchange', onFs)
|
||||
return () => document.removeEventListener('fullscreenchange', onFs)
|
||||
}, [])
|
||||
|
||||
const handleStateChangeLeft = useCallback((s: PaneReportedState) => setLeftState(s), [])
|
||||
const handleStateChangeRight = useCallback((s: PaneReportedState) => setRightState(s), [])
|
||||
|
||||
const applyBoth = useCallback(
|
||||
(fn: (h: ComparePaneHandle) => void) => {
|
||||
const active = getActive()
|
||||
if (active) fn(active)
|
||||
if (sameType) {
|
||||
const other = getOther()
|
||||
if (other) fn(other)
|
||||
}
|
||||
},
|
||||
[getActive, getOther, sameType],
|
||||
)
|
||||
|
||||
// --- Control handlers ---
|
||||
const handleTogglePlay = useCallback(() => {
|
||||
const shouldPlay = !activeState?.video?.isPlaying
|
||||
applyBoth((h) => (shouldPlay ? h.play() : h.pause()))
|
||||
}, [activeState?.video?.isPlaying, applyBoth])
|
||||
|
||||
const handleSeek = useCallback(
|
||||
(frame: number) => applyBoth((h) => h.seekToFrame(frame)),
|
||||
[applyBoth],
|
||||
)
|
||||
|
||||
const handleToggleLoop = useCallback(() => getActive()?.toggleLoop(), [getActive])
|
||||
|
||||
const handleVolumeChange = useCallback((v: number) => {
|
||||
setUserVolume(v)
|
||||
setUserMuted(v === 0)
|
||||
}, [])
|
||||
|
||||
const handleToggleMute = useCallback(() => setUserMuted((m) => !m), [])
|
||||
|
||||
const handlePlaybackRate = useCallback(
|
||||
(rate: number) => applyBoth((h) => h.setPlaybackRate(rate)),
|
||||
[applyBoth],
|
||||
)
|
||||
|
||||
const handleChangeResolution = useCallback(
|
||||
(resolution: string) => getActive()?.changeResolution(resolution),
|
||||
[getActive],
|
||||
)
|
||||
|
||||
const handleZoomIn = useCallback(() => applyBoth((h) => h.zoomBy(1.2)), [applyBoth])
|
||||
const handleZoomOut = useCallback(() => applyBoth((h) => h.zoomBy(0.8)), [applyBoth])
|
||||
const handleFit = useCallback(() => applyBoth((h) => h.fit()), [applyBoth])
|
||||
const handleDownload = useCallback(
|
||||
(key?: string) => {
|
||||
getActive()?.download(key)
|
||||
},
|
||||
[getActive],
|
||||
)
|
||||
|
||||
const handleToggleFullScreen = useCallback(() => {
|
||||
if (!rootRef.current) return
|
||||
if (!document.fullscreenElement) {
|
||||
rootRef.current.requestFullscreen()
|
||||
} else {
|
||||
document.exitFullscreen()
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleUserPanLeft = useCallback(
|
||||
(dx: number, dy: number) => {
|
||||
if (sameType) rightRef.current?.panBy(dx, dy)
|
||||
},
|
||||
[sameType],
|
||||
)
|
||||
const handleUserPanRight = useCallback(
|
||||
(dx: number, dy: number) => {
|
||||
if (sameType) leftRef.current?.panBy(dx, dy)
|
||||
},
|
||||
[sameType],
|
||||
)
|
||||
|
||||
const handleRequestTogglePlay = useCallback(() => handleTogglePlay(), [handleTogglePlay])
|
||||
|
||||
const versionOf = useCallback(
|
||||
(id: string) => versions.find((v) => v.id === id)?.version,
|
||||
[versions],
|
||||
)
|
||||
|
||||
const activePreviewUrl = useMemo(
|
||||
() => activeAsset?.media?.videoPreview?.url,
|
||||
[activeAsset?.media?.videoPreview?.url],
|
||||
)
|
||||
|
||||
const renderPane = (side: CompareSide, asset: AssetInfo, kind: PaneKind) => {
|
||||
const isActive = activeSide === side
|
||||
const paneRef = side === 'left' ? leftRef : rightRef
|
||||
const onStateChange = side === 'left' ? handleStateChangeLeft : handleStateChangeRight
|
||||
const onUserPan = side === 'left' ? handleUserPanLeft : handleUserPanRight
|
||||
const paneAnnotations = isActive ? annotations : []
|
||||
|
||||
if (kind === 'video') {
|
||||
return (
|
||||
<CompareVideoPane
|
||||
key={asset.id}
|
||||
ref={paneRef}
|
||||
file={asset}
|
||||
shareId={shareId}
|
||||
isActive={isActive}
|
||||
muted={isActive ? userMuted : true}
|
||||
volume={userVolume}
|
||||
annotations={paneAnnotations}
|
||||
onStateChange={onStateChange}
|
||||
onActivate={() => onActiveSideChange(side)}
|
||||
onRequestTogglePlay={handleRequestTogglePlay}
|
||||
onTimeUpdate={onTimeUpdate}
|
||||
onPlay={onPlay}
|
||||
/>
|
||||
)
|
||||
}
|
||||
if (kind === 'image') {
|
||||
return (
|
||||
<CompareImagePane
|
||||
key={asset.id}
|
||||
ref={paneRef}
|
||||
file={asset}
|
||||
shareId={shareId}
|
||||
isActive={isActive}
|
||||
annotations={paneAnnotations}
|
||||
onStateChange={onStateChange}
|
||||
onUserPan={onUserPan}
|
||||
onActivate={() => onActiveSideChange(side)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<div className="flex flex-1 items-center justify-center bg-gray-100 dark:bg-gray-950">
|
||||
<p className="text-muted-foreground">Preview unavailable</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const bothLoaded = !!leftAsset && !!rightAsset
|
||||
const isLoading = leftLoading || rightLoading
|
||||
|
||||
return (
|
||||
<div ref={rootRef} className="flex h-full flex-1 flex-col bg-background">
|
||||
<div className="flex min-h-0 flex-1">
|
||||
{/* Left pane */}
|
||||
<div className="flex min-w-0 flex-1 flex-col border-r border-border">
|
||||
{leftAsset && (
|
||||
<ComparePaneTopbar
|
||||
side="left"
|
||||
fileName={leftAsset.name}
|
||||
version={versionOf(leftId)}
|
||||
activeVersionId={leftId}
|
||||
versions={versions}
|
||||
isActive={activeSide === 'left'}
|
||||
onActivate={() => onActiveSideChange('left')}
|
||||
onSwitchVersion={(vid) => onSwitchVersion('left', vid)}
|
||||
onExit={onExit}
|
||||
/>
|
||||
)}
|
||||
<div className="relative flex min-h-0 flex-1">
|
||||
{leftAsset ? (
|
||||
renderPane('left', leftAsset, leftKind)
|
||||
) : (
|
||||
<div className="flex flex-1 items-center justify-center">
|
||||
<Loader2 className="h-6 w-6 animate-spin text-foreground" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Right pane */}
|
||||
<div className="flex min-w-0 flex-1 flex-col">
|
||||
{rightAsset && (
|
||||
<ComparePaneTopbar
|
||||
side="right"
|
||||
fileName={rightAsset.name}
|
||||
version={versionOf(rightId)}
|
||||
activeVersionId={rightId}
|
||||
versions={versions}
|
||||
isActive={activeSide === 'right'}
|
||||
onActivate={() => onActiveSideChange('right')}
|
||||
onSwitchVersion={(vid) => onSwitchVersion('right', vid)}
|
||||
onExit={onExit}
|
||||
/>
|
||||
)}
|
||||
<div className="relative flex min-h-0 flex-1">
|
||||
{rightAsset ? (
|
||||
renderPane('right', rightAsset, rightKind)
|
||||
) : (
|
||||
<div className="flex flex-1 items-center justify-center">
|
||||
<Loader2 className="h-6 w-6 animate-spin text-foreground" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{isLoading && !bothLoaded && (
|
||||
<div className="absolute inset-0 z-10 flex items-center justify-center bg-background/30">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-foreground" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
<CompareControlBar
|
||||
activeState={activeState}
|
||||
previewUrl={activePreviewUrl}
|
||||
metadata={activeAsset?.media?.metadata}
|
||||
startTimecode={activeAsset?.media?.metadata?.startTimecode}
|
||||
isFullScreen={isFullScreen}
|
||||
onTogglePlay={handleTogglePlay}
|
||||
onToggleLoop={handleToggleLoop}
|
||||
onSeek={handleSeek}
|
||||
onVolumeChange={handleVolumeChange}
|
||||
onToggleMute={handleToggleMute}
|
||||
onChangePlaybackRate={handlePlaybackRate}
|
||||
onChangeResolution={handleChangeResolution}
|
||||
onZoomIn={handleZoomIn}
|
||||
onZoomOut={handleZoomOut}
|
||||
onFit={handleFit}
|
||||
onDownload={handleDownload}
|
||||
onToggleFullScreen={handleToggleFullScreen}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user