feat(transcode): add multi-resolution strategy with configurable ladders and fallback (#438)

* feat(transcode): add multi-resolution strategy with configurable ladders and fallback

* feat(settings): show toast on settings update and add manual verification plan to PR template

* fix(webui): preserve video resolution and fix 480p label in download dropdowns

* fix(webui): display standard short-side resolution for vertical videos in seekbar

* fix(webui): fix portrait transcode resolution labeling and serialize settings toggles
This commit is contained in:
Yiling-J
2026-10-01 13:27:50 +08:00
committed by GitHub
parent a84da8678e
commit e007054f47
35 changed files with 1260 additions and 123 deletions
@@ -1,6 +1,6 @@
// @vitest-environment happy-dom
import React from 'react'
import { cleanup, render, screen } from '@testing-library/react'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { BreadcrumbNav } from './breadcrumb-nav'
@@ -160,4 +160,107 @@ describe('BreadcrumbNav component', () => {
const triggerButton = screen.getByRole('button', { name: 'sample.mov' })
expect(triggerButton).toBeDefined()
})
it('displays 480p instead of 360p for 480p video transcode in download menu', () => {
render(
<QueryClientProvider client={queryClient}>
<BreadcrumbNav
{...baseProps}
fileId="file-1"
currentAsset={{ id: 'file-1', name: 'sample.mov', type: 'file', version: 1 }}
allowDownload={true}
downloadInfo={{
originalKey: 'raw.mov',
videoTranscodes: [
{
key: 'sample-480p.mp4',
width: 854,
height: 480,
hdr: false,
},
],
}}
/>
</QueryClientProvider>,
)
const triggerButton = screen.getByRole('button', { name: 'sample.mov' })
fireEvent.pointerDown(triggerButton, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('480p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('360p'))).toBe(false)
})
it('displays explicit resolution when provided in download menu', () => {
render(
<QueryClientProvider client={queryClient}>
<BreadcrumbNav
{...baseProps}
fileId="file-1"
currentAsset={{ id: 'file-1', name: 'sample.mov', type: 'file', version: 1 }}
allowDownload={true}
downloadInfo={{
originalKey: 'raw.mov',
videoTranscodes: [
{
key: 'sample-480p.mp4',
width: 854,
height: 480,
resolution: '480p',
hdr: false,
},
],
}}
/>
</QueryClientProvider>,
)
const triggerButton = screen.getByRole('button', { name: 'sample.mov' })
fireEvent.pointerDown(triggerButton, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('480p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('360p'))).toBe(false)
})
it('correctly labels legacy portrait video transcode in download menu using long side fallback', () => {
render(
<QueryClientProvider client={queryClient}>
<BreadcrumbNav
{...baseProps}
fileId="file-portrait"
currentAsset={{ id: 'file-portrait', name: 'portrait.mp4', type: 'file', version: 1 }}
allowDownload={true}
downloadInfo={{
originalKey: 'raw.mp4',
videoTranscodes: [
{
key: 'portrait-legacy.mp4',
width: 1080,
height: 1920,
hdr: false,
},
],
}}
/>
</QueryClientProvider>,
)
const triggerButton = screen.getByRole('button', { name: 'portrait.mp4' })
fireEvent.pointerDown(triggerButton, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('1080p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('1920p'))).toBe(false)
})
})
+3 -8
View File
@@ -48,6 +48,7 @@ import { useState } from 'react'
import { useDualSidebarStore } from '@/ui/stores/dual-sidebar'
import { ManageVersionsDialog } from './manage-versions-dialog'
import { AssetLinkedTasksDialog } from './kanban/asset-linked-tasks-dialog'
import { getVideoResolutionLabel } from '@/ui/lib/media'
interface BreadcrumbNavProps {
teamId: string
@@ -84,6 +85,7 @@ interface BreadcrumbNavProps {
key: string
width: number
height: number
resolution?: string
hdr?: boolean
}>
}
@@ -287,14 +289,7 @@ export function BreadcrumbNav({
<DropdownMenuSubContent className="w-48">
<DropdownMenuLabel>{m.download()}</DropdownMenuLabel>
{downloadInfo?.videoTranscodes?.map((t) => {
const longSide = Math.max(t.width, t.height)
let resolution = `${t.height}p`
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
const resolution = getVideoResolutionLabel(t)
const label = t.hdr ? `${resolution} (HDR)` : resolution
return (
<DropdownMenuItem
@@ -145,6 +145,7 @@ export function MobileFileDetail({
key: t.key,
width: t.width,
height: t.height,
resolution: t.resolution,
hdr: t.hdr,
})),
}
@@ -119,4 +119,69 @@ describe('MobileFileHeader', () => {
expect(menuBtn).toBeDefined()
fireEvent.click(menuBtn)
})
it('displays 480p instead of 360p for 480p video transcode in download menu', () => {
render(
<MobileFileHeader
fileName="scene1.mp4"
activeFileId="file-1"
allowDownload={true}
onBack={vi.fn()}
downloadInfo={{
originalKey: 'raw.mov',
videoTranscodes: [
{
key: 'scene1-480p.mp4',
width: 854,
height: 480,
resolution: '480p',
hdr: false,
},
],
}}
/>,
)
const menuBtn = screen.getByLabelText(/More options/i)
fireEvent.pointerDown(menuBtn, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('480p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('360p'))).toBe(false)
})
it('correctly labels legacy portrait video transcode in download menu using long side fallback', () => {
render(
<MobileFileHeader
fileName="portrait.mp4"
activeFileId="file-portrait"
allowDownload={true}
onBack={vi.fn()}
downloadInfo={{
originalKey: 'raw.mp4',
videoTranscodes: [
{
key: 'portrait-legacy.mp4',
width: 1080,
height: 1920,
hdr: false,
},
],
}}
/>,
)
const menuBtn = screen.getByLabelText(/More options/i)
fireEvent.pointerDown(menuBtn, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('1080p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('1920p'))).toBe(false)
})
})
@@ -26,6 +26,7 @@ import {
Trash2,
} from 'lucide-react'
import React from 'react'
import { getVideoResolutionLabel } from '@/ui/lib/media'
export interface MobileFileHeaderProps {
fileName: string
@@ -46,6 +47,7 @@ export interface MobileFileHeaderProps {
key: string
width: number
height: number
resolution?: string
hdr?: boolean
}>
}
@@ -238,14 +240,7 @@ export function MobileFileHeader({
<DropdownMenuSubContent className="w-48 z-50">
<DropdownMenuLabel className="text-xs">{m.download()}</DropdownMenuLabel>
{downloadInfo?.videoTranscodes?.map((t) => {
const longSide = Math.max(t.width, t.height)
let resolution = `${t.height}p`
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
const resolution = getVideoResolutionLabel(t)
const label = t.hdr ? `${resolution} (HDR)` : resolution
return (
<DropdownMenuItem
@@ -440,6 +440,7 @@ export function PublicShareManager({
key: t.key,
width: t.width,
height: t.height,
resolution: t.resolution,
hdr: t.hdr,
})),
}
@@ -214,4 +214,95 @@ describe('CompareVideoPane', () => {
vi.useRealTimers()
}
})
it('reports 2160p instead of 3840p for 4K vertical video when HLS switches level on Auto', () => {
mockHlsInstance.levels = [{ height: 3840, width: 2160, bitrate: 10000000 }]
const hlsVideo: AssetInfo = {
id: 'video-hls-compare-vertical-4k',
name: 'vertical-4k.m3u8',
proxyType: 'video',
media: {
isHls: true,
hls: {
key: 'hls-key',
url: 'https://cdn.example.com/master.m3u8',
resolutions: [{ width: 2160, height: 3840, resolution: '2160p' }],
},
metadata: {
originalWidth: 2160,
originalHeight: 3840,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const ref = createRef<ComparePaneHandle>()
const reportedStates: PaneReportedState[] = []
render(
<CompareVideoPane
ref={ref}
file={hlsVideo}
isActive={true}
annotations={[]}
onActivate={vi.fn()}
onStateChange={(s) => reportedStates.push(s)}
/>,
)
const levelSwitchedHandler = mockHlsInstance.on.mock.calls.find(
([event]) => event === 'hlsLevelSwitched',
)?.[1]
expect(levelSwitchedHandler).toBeDefined()
act(() => {
levelSwitchedHandler('hlsLevelSwitched', { level: 0 })
})
const lastState = reportedStates[reportedStates.length - 1]
expect(lastState?.video?.activeAutoResolution).toBe('2160p')
})
it('derives resolution from long side for legacy portrait MP4 transcode without explicit resolution', () => {
const legacyPortraitVideo: AssetInfo = {
id: 'legacy-portrait-compare-mp4',
name: 'portrait.mp4',
proxyType: 'video',
media: {
videoTranscodes: [
{
key: 'portrait-1080p.mp4',
url: 'https://cdn.example.com/portrait-1080p.mp4',
width: 1080,
height: 1920,
size: 5000000,
},
],
metadata: {
originalWidth: 1080,
originalHeight: 1920,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const reportedStates: PaneReportedState[] = []
render(
<CompareVideoPane
file={legacyPortraitVideo}
isActive={true}
annotations={[]}
onActivate={vi.fn()}
onStateChange={(s) => reportedStates.push(s)}
/>,
)
const lastState = reportedStates[reportedStates.length - 1]
expect(lastState?.video?.resolutions?.[0]?.resolution).toBe('1080p')
})
})
@@ -11,6 +11,7 @@ import { useFramePlayer } from './use-frame-player'
import { calculateFrameCenterTime, resolveTotalFrames } from './utils'
import { clampFrame as clampFrameUtil } from '../../compare/compare-utils'
import type { ComparePaneHandle, DisplayTranscode, PaneReportedState } from '../../compare/types'
import { getVideoResolutionLabel } from '@/ui/lib/media'
interface CompareVideoPaneProps {
file: AssetInfo
@@ -36,20 +37,10 @@ function computeResolutions(file: AssetInfo): DisplayTranscode[] {
? (file.media?.hls?.resolutions ?? [])
: (file.media?.videoTranscodes ?? [])
return baseTranscodes.map((t) => {
const longSide = Math.max(t.width, t.height)
const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined
let resolution = resCandidate || `${t.height}p`
if (!resCandidate) {
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
}
return { ...t, resolution }
})
return baseTranscodes.map((t) => ({
...t,
resolution: getVideoResolutionLabel(t),
}))
}
function getInitialResolution(resolutions: DisplayTranscode[]): DisplayTranscode | null {
@@ -216,7 +207,18 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => {
const level = hls?.levels[eventData.level]
if (level) {
setActiveAutoResolution(`${level.height}p`)
const matched = resolutions.find(
(r) => r.width === level.width && r.height === level.height,
)
if (matched?.resolution) {
setActiveAutoResolution(matched.resolution)
} else {
const shortSide =
level.width && level.height
? Math.min(level.width, level.height)
: level.height || level.width
setActiveAutoResolution(shortSide ? `${shortSide}p` : undefined)
}
}
})
@@ -617,7 +619,7 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
],
)
const displayAnnotations = [...annotations, ...(isActive ? draftAnnotations : [])]
const displayAnnotations = [...(annotations ?? []), ...(isActive ? draftAnnotations : [])]
const scale = zoom
const defaultPanX = (containerSize.width - vidW * scale) / 2
@@ -29,6 +29,7 @@ import {
import { Slider } from '../../ui/slider'
import ProgressBar from './progress-bar'
import { formatTime, formatTimecode } from './utils'
import { getVideoResolutionLabel } from '@/ui/lib/media'
export interface PlayerState {
isPlaying: boolean
@@ -431,8 +432,7 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
? data.media.videoTranscodes
: resolutions
).map((res) => {
const resLabel =
'resolution' in res && res.resolution ? res.resolution : `${res.height}p`
const resLabel = getVideoResolutionLabel(res)
const itemKey = res.key || `${resLabel}-${res.hdr ? 'hdr' : 'sdr'}`
return (
<DropdownMenuItem
@@ -495,4 +495,75 @@ describe('VideoViewer', () => {
vi.useRealTimers()
}
})
it('displays 2160p instead of 3840p for 4K vertical video when HLS switches level on Auto', () => {
mockHlsInstance.levels = [{ height: 3840, width: 2160, bitrate: 10000000 }]
const hlsVideo: AssetInfo = {
id: 'video-hls-vertical-4k',
name: 'vertical-4k.m3u8',
proxyType: 'video',
media: {
isHls: true,
hls: {
key: 'hls-key',
url: 'https://cdn.example.com/master.m3u8',
resolutions: [{ width: 2160, height: 3840, resolution: '2160p' }],
},
metadata: {
originalWidth: 2160,
originalHeight: 3840,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const { container } = render(<VideoViewer file={hlsVideo} />)
const levelSwitchedHandler = mockHlsInstance.on.mock.calls.find(
([event]) => event === 'hlsLevelSwitched',
)?.[1]
expect(levelSwitchedHandler).toBeDefined()
act(() => {
levelSwitchedHandler('hlsLevelSwitched', { level: 0 })
})
const settingsButton = container.querySelector('button:has(.lucide-settings)')
expect(settingsButton?.textContent).toContain('2160p')
expect(settingsButton?.textContent).not.toContain('3840p')
})
it('derives resolution from long side for legacy portrait MP4 transcode without explicit resolution', () => {
const legacyPortraitVideo: AssetInfo = {
id: 'legacy-portrait-mp4',
name: 'portrait.mp4',
proxyType: 'video',
media: {
videoTranscodes: [
{
key: 'portrait-1080p.mp4',
url: 'https://cdn.example.com/portrait-1080p.mp4',
width: 1080,
height: 1920,
size: 5000000,
},
],
metadata: {
originalWidth: 1080,
originalHeight: 1920,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const { container } = render(<VideoViewer file={legacyPortraitVideo} />)
const settingsButton = container.querySelector('button:has(.lucide-settings)')
expect(settingsButton?.textContent).toContain('1080p')
expect(settingsButton?.textContent).not.toContain('1920p')
})
})
@@ -10,6 +10,7 @@ import { MobileVideoControlBar } from './mobile-video-control-bar'
import { useIsMobile } from '@/ui/hooks/use-mobile'
import DrawingCanvas from '@/ui/components/drawing-canvas'
import { useAnnotationStore } from '@/ui/stores/annotation-store'
import { getVideoResolutionLabel } from '@/ui/lib/media'
import { FileViewerProps, MediaController } from '../types'
import { centeredPan, fitScale, zoomAtPoint } from '../pan-zoom'
import { usePanZoomGestures } from '../use-pan-zoom'
@@ -39,24 +40,10 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
? (data.media?.hls?.resolutions ?? [])
: (data.media?.videoTranscodes ?? [])
const resolutions: DisplayTranscode[] = baseTranscodes.map((t) => {
const longSide = Math.max(t.width, t.height)
const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined
let resolution = resCandidate || `${t.height}p`
if (!resCandidate) {
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
}
return {
...t,
resolution,
}
})
const resolutions: DisplayTranscode[] = baseTranscodes.map((t) => ({
...t,
resolution: getVideoResolutionLabel(t),
}))
// Only transcoded proxy versions are ever displayed; the raw original file
// is never used as a playback source.
const hasMedia = (isHls || resolutions.length > 0) && !!data.media?.metadata
@@ -365,8 +352,18 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => {
const level = hls?.levels[eventData.level]
if (level) {
const h = level.height || Math.min(level.width, level.height)
setActiveAutoResolution(h ? `${h}p` : undefined)
const matched = resolutions.find(
(r) => r.width === level.width && r.height === level.height,
)
if (matched?.resolution) {
setActiveAutoResolution(matched.resolution)
} else {
const shortSide =
level.width && level.height
? Math.min(level.width, level.height)
: level.height || level.width
setActiveAutoResolution(shortSide ? `${shortSide}p` : undefined)
}
}
})
+46 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { isImageFileName, getBestTranscode } from './media'
import { isImageFileName, getBestTranscode, getVideoResolutionLabel } from './media'
describe('isImageFileName', () => {
it('identifies web-supported image formats as images', () => {
@@ -49,3 +49,48 @@ describe('getBestTranscode', () => {
expect(getBestTranscode(transcodes, 1500)?.width).toBe(1200)
})
})
describe('getVideoResolutionLabel', () => {
it('preserves explicit resolution when present', () => {
expect(getVideoResolutionLabel({ width: 854, height: 480, resolution: '480p' })).toBe('480p')
expect(getVideoResolutionLabel({ width: 480, height: 854, resolution: '480p' })).toBe('480p')
expect(getVideoResolutionLabel({ width: 1920, height: 1080, resolution: '1080p' })).toBe(
'1080p',
)
})
it('derives resolution from long side for portrait legacy transcodes without explicit resolution', () => {
// 4K portrait
expect(getVideoResolutionLabel({ width: 2160, height: 3840 })).toBe('2160p')
// 2K / 1440p portrait
expect(getVideoResolutionLabel({ width: 1440, height: 2560 })).toBe('1440p')
// 1080p portrait
expect(getVideoResolutionLabel({ width: 1080, height: 1920 })).toBe('1080p')
// 720p portrait
expect(getVideoResolutionLabel({ width: 720, height: 1280 })).toBe('720p')
// 480p portrait
expect(getVideoResolutionLabel({ width: 480, height: 854 })).toBe('480p')
// 360p portrait
expect(getVideoResolutionLabel({ width: 360, height: 640 })).toBe('360p')
// 180p portrait
expect(getVideoResolutionLabel({ width: 180, height: 320 })).toBe('180p')
})
it('derives resolution from long side for landscape legacy transcodes without explicit resolution', () => {
expect(getVideoResolutionLabel({ width: 3840, height: 2160 })).toBe('2160p')
expect(getVideoResolutionLabel({ width: 2560, height: 1440 })).toBe('1440p')
expect(getVideoResolutionLabel({ width: 1920, height: 1080 })).toBe('1080p')
expect(getVideoResolutionLabel({ width: 1280, height: 720 })).toBe('720p')
expect(getVideoResolutionLabel({ width: 854, height: 480 })).toBe('480p')
expect(getVideoResolutionLabel({ width: 640, height: 360 })).toBe('360p')
expect(getVideoResolutionLabel({ width: 320, height: 180 })).toBe('180p')
})
it('falls back to short side or single dimension for non-standard sizes', () => {
expect(getVideoResolutionLabel({ width: 200, height: 100 })).toBe('100p')
expect(getVideoResolutionLabel({ height: 720 })).toBe('720p')
expect(getVideoResolutionLabel({ width: 500 })).toBe('500p')
expect(getVideoResolutionLabel(null)).toBe('')
expect(getVideoResolutionLabel(undefined)).toBe('')
})
})
+40
View File
@@ -40,3 +40,43 @@ export function getBestTranscode(
// If no candidate >= screenWidth, simply return the largest available (first in original sorted)
return sorted[0]
}
export function getVideoResolutionLabel(
transcode?: {
width?: number
height?: number
resolution?: string
} | null,
): string {
if (!transcode) return ''
if (transcode.resolution) {
return transcode.resolution
}
const width = transcode.width ?? 0
const height = transcode.height ?? 0
if (width > 0 && height > 0) {
const longSide = Math.max(width, height)
if (longSide >= 3840) return '2160p'
if (longSide >= 2560) return '1440p'
if (longSide >= 1920) return '1080p'
if (longSide >= 1280) return '720p'
if (longSide >= 854) return '480p'
if (longSide >= 640) return '360p'
if (longSide >= 320) return '180p'
const shortSide = Math.min(width, height)
return `${shortSide}p`
}
if (height > 0) {
return `${height}p`
}
if (width > 0) {
return `${width}p`
}
return ''
}
+4
View File
@@ -254,6 +254,10 @@
"video_strategy": "Video Strategy",
"best_match": "Best match",
"best_match_description": "Generates a single optimal resolution matching the source quality.",
"multi_resolutions": "Multi resolutions",
"multi_resolutions_description": "Generates selected resolutions up to the source quality.",
"video_resolution_ladders": "Resolution Ladders",
"video_resolution_ladders_description": "Select the target resolutions to generate for MP4 transcoding. Resolutions are only generated up to the source video resolution.",
"all_resolutions": "All resolutions",
"all_resolutions_description": "Generates all supported resolutions up to the source quality.",
"hardware_acceleration": "Hardware Acceleration",
+4
View File
@@ -254,6 +254,10 @@
"video_strategy": "视频策略",
"best_match": "最佳匹配",
"best_match_description": "生成与源质量匹配的单一最优分辨率。",
"multi_resolutions": "多分辨率",
"multi_resolutions_description": "生成选中的不超过源质量的分辨率。",
"video_resolution_ladders": "分辨率阶梯",
"video_resolution_ladders_description": "选择 MP4 转码的目标分辨率。仅生成不超过原始视频分辨率的分辨率阶梯。",
"all_resolutions": "所有分辨率",
"all_resolutions_description": "生成不超过源质量的所有支持分辨率。",
"hardware_acceleration": "硬件加速",
@@ -300,6 +300,7 @@ function FileViewPage() {
key: t.key,
width: t.width,
height: t.height,
resolution: t.resolution,
hdr: t.hdr,
})),
},
@@ -1,6 +1,8 @@
import { createLazyFileRoute } from '@tanstack/react-router'
import {
VideoTranscodeStrategy,
VideoResolutionLadder,
DEFAULT_VIDEO_RESOLUTIONS,
HardwareAcceleration,
UpdateTeamSettingsRequest,
TeamSettingsResponse,
@@ -97,7 +99,7 @@ function getTabFromHash(): SettingsTab {
: 'general'
}
function TeamSettingsPage() {
export function TeamSettingsPage() {
const { teamId } = Route.useParams()
const queryClient = useQueryClient()
const [activeTab, setActiveTabState] = useState<SettingsTab>(getTabFromHash)
@@ -297,6 +299,10 @@ function TeamSettingsPage() {
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.success(m.settings_updated())
},
onError: () => {
toast.error(m.failed_update_settings())
},
})
@@ -310,6 +316,102 @@ function TeamSettingsPage() {
})
}
const pendingVideoResolutionsRef = useRef<VideoResolutionLadder[] | null>(null)
const [optimisticVideoResolutions, setOptimisticVideoResolutions] = useState<
VideoResolutionLadder[] | null
>(null)
const videoResolutionsQueueRef = useRef<Promise<void>>(Promise.resolve())
const isVideoResolutionsUpdatingRef = useRef<boolean>(false)
const pendingHlsResolutionsRef = useRef<HlsResolutionLadder[] | null>(null)
const [optimisticHlsResolutions, setOptimisticHlsResolutions] = useState<
HlsResolutionLadder[] | null
>(null)
const hlsResolutionsQueueRef = useRef<Promise<void>>(Promise.resolve())
const isHlsResolutionsUpdatingRef = useRef<boolean>(false)
useEffect(() => {
if (!isVideoResolutionsUpdatingRef.current) {
pendingVideoResolutionsRef.current = null
setOptimisticVideoResolutions(null)
}
}, [settings])
useEffect(() => {
if (!isHlsResolutionsUpdatingRef.current) {
pendingHlsResolutionsRef.current = null
setOptimisticHlsResolutions(null)
}
}, [settings])
const handleVideoLadderToggle = (ladder: VideoResolutionLadder) => {
const current =
pendingVideoResolutionsRef.current ??
optimisticVideoResolutions ??
(settings as TeamSettingsResponse | undefined)?.transcode?.videoResolutions ??
DEFAULT_VIDEO_RESOLUTIONS
const updated = current.includes(ladder)
? current.filter((l) => l !== ladder)
: [...current, ladder]
if (updated.length === 0) return
pendingVideoResolutionsRef.current = updated
setOptimisticVideoResolutions(updated)
queryClient.setQueryData(
['teams', teamId, 'settings'],
(old: TeamSettingsResponse | undefined) => {
if (!old) return old
return {
...old,
transcode: {
...old.transcode,
videoResolutions: updated,
},
}
},
)
isVideoResolutionsUpdatingRef.current = true
videoResolutionsQueueRef.current = videoResolutionsQueueRef.current
.then(async () => {
const target = pendingVideoResolutionsRef.current
if (!target) return
const res = await client.api.teams[':teamId'].settings.$patch({
param: { teamId },
json: {
key: 'transcode.videoResolutions',
value: target,
},
})
if (!res.ok) throw new Error('Failed to update settings')
await res.json()
if (pendingVideoResolutionsRef.current === target) {
pendingVideoResolutionsRef.current = null
isVideoResolutionsUpdatingRef.current = false
setOptimisticVideoResolutions(null)
await queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.success(m.settings_updated())
}
})
.catch((err) => {
console.error(err)
pendingVideoResolutionsRef.current = null
isVideoResolutionsUpdatingRef.current = false
setOptimisticVideoResolutions(null)
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.error(m.failed_update_settings())
})
}
const handleHardwareAccelerationChange = (value: HardwareAcceleration) => {
updateSettings({
teamId,
@@ -349,6 +451,8 @@ function TeamSettingsPage() {
const handleHlsLadderToggle = (ladder: HlsResolutionLadder) => {
const current =
pendingHlsResolutionsRef.current ??
optimisticHlsResolutions ??
(settings as TeamSettingsResponse | undefined)?.transcode?.hlsResolutions ??
DEFAULT_HLS_RESOLUTIONS
const updated = current.includes(ladder)
@@ -357,13 +461,60 @@ function TeamSettingsPage() {
if (updated.length === 0) return
updateSettings({
teamId,
data: {
key: 'transcode.hlsResolutions',
value: updated,
pendingHlsResolutionsRef.current = updated
setOptimisticHlsResolutions(updated)
queryClient.setQueryData(
['teams', teamId, 'settings'],
(old: TeamSettingsResponse | undefined) => {
if (!old) return old
return {
...old,
transcode: {
...old.transcode,
hlsResolutions: updated,
},
}
},
})
)
isHlsResolutionsUpdatingRef.current = true
hlsResolutionsQueueRef.current = hlsResolutionsQueueRef.current
.then(async () => {
const target = pendingHlsResolutionsRef.current
if (!target) return
const res = await client.api.teams[':teamId'].settings.$patch({
param: { teamId },
json: {
key: 'transcode.hlsResolutions',
value: target,
},
})
if (!res.ok) throw new Error('Failed to update settings')
await res.json()
if (pendingHlsResolutionsRef.current === target) {
pendingHlsResolutionsRef.current = null
isHlsResolutionsUpdatingRef.current = false
setOptimisticHlsResolutions(null)
await queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.success(m.settings_updated())
}
})
.catch((err) => {
console.error(err)
pendingHlsResolutionsRef.current = null
isHlsResolutionsUpdatingRef.current = false
setOptimisticHlsResolutions(null)
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.error(m.failed_update_settings())
})
}
if (isSettingsLoading || isMeLoading) {
@@ -383,10 +534,20 @@ function TeamSettingsPage() {
(settings as any)?.transcode?.videoStrategy || VideoTranscodeStrategy.best_match
if (currentVideoStrategy === 'single' || currentVideoStrategy === 'disable') {
currentVideoStrategy = VideoTranscodeStrategy.best_match
} else if (currentVideoStrategy === 'full') {
currentVideoStrategy = VideoTranscodeStrategy.all
} else if (
currentVideoStrategy === 'full' ||
currentVideoStrategy === 'all' ||
currentVideoStrategy === 'multi'
) {
currentVideoStrategy = VideoTranscodeStrategy.multi
}
const currentVideoResolutions: VideoResolutionLadder[] =
optimisticVideoResolutions ??
(settings as TeamSettingsResponse | undefined)?.transcode?.videoResolutions ??
DEFAULT_VIDEO_RESOLUTIONS
const ALL_VIDEO_LADDERS: VideoResolutionLadder[] = ['480p', '720p', '1080p', '1440p', '2160p']
const currentHardwareAcceleration =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(settings as any)?.transcode?.hardwareAcceleration || HardwareAcceleration.off
@@ -395,6 +556,7 @@ function TeamSettingsPage() {
const currentHlsEnabled =
(settings as TeamSettingsResponse | undefined)?.transcode?.hlsEnabled ?? false
const currentHlsResolutions: HlsResolutionLadder[] =
optimisticHlsResolutions ??
(settings as TeamSettingsResponse | undefined)?.transcode?.hlsResolutions ??
DEFAULT_HLS_RESOLUTIONS
const ALL_HLS_LADDERS: HlsResolutionLadder[] = ['480p', '720p', '1080p', '1440p', '2160p']
@@ -842,17 +1004,47 @@ function TeamSettingsPage() {
<div
className={cn(
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
currentVideoStrategy === VideoTranscodeStrategy.all
currentVideoStrategy === VideoTranscodeStrategy.multi
? 'border-primary bg-primary/5'
: 'border-border',
)}
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.all)}
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.multi)}
>
<div className="font-semibold">{m.all_resolutions()}</div>
<div className="font-semibold">{m.multi_resolutions()}</div>
<div className="text-sm text-muted-foreground">
{m.all_resolutions_description()}
{m.multi_resolutions_description()}
</div>
</div>
{currentVideoStrategy === VideoTranscodeStrategy.multi && (
<div className="space-y-3 pt-2 pl-1">
<div className="space-y-1">
<h4 className="text-sm font-medium">
{m.video_resolution_ladders()}
</h4>
<p className="text-xs text-muted-foreground">
{m.video_resolution_ladders_description()}
</p>
</div>
<div className="flex flex-wrap gap-4 pt-1">
{ALL_VIDEO_LADDERS.map((ladder) => {
const checked = currentVideoResolutions.includes(ladder)
return (
<label
key={ladder}
className="flex items-center space-x-2 cursor-pointer text-sm"
>
<Checkbox
checked={checked}
onCheckedChange={() => handleVideoLadderToggle(ladder)}
/>
<span>{ladder}</span>
</label>
)
})}
</div>
</div>
)}
</div>
</div>
@@ -0,0 +1,241 @@
// @vitest-environment happy-dom
import { cleanup, render, screen, fireEvent, waitFor, act } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { TeamSettingsPage } from './settings.lazy'
import { toast } from 'sonner'
import { VideoTranscodeStrategy } from '@shumai/dtos'
interface PatchSettingsArgs {
param: { teamId: string }
json: { key: string; value: string[] }
}
const mockGetSettings = vi.fn()
const mockPatchSettings = vi.fn()
const mockGetMe = vi.fn()
vi.mock('@tanstack/react-router', () => ({
createLazyFileRoute: () => (opts: Record<string, unknown>) => ({
...opts,
useParams: () => ({ teamId: 'team-1' }),
}),
useNavigate: () => vi.fn(),
Link: ({ children, ...props }: React.AnchorHTMLAttributes<HTMLAnchorElement>) => (
<a {...props}>{children}</a>
),
}))
vi.mock('@/ui/api/client', () => ({
client: {
api: {
teams: {
':teamId': {
settings: {
$get: () => mockGetSettings(),
$patch: (args: unknown) => mockPatchSettings(args),
},
me: {
$get: () => mockGetMe(),
$patch: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
},
},
},
},
},
}))
vi.mock('sonner', () => ({
toast: {
success: vi.fn(),
error: vi.fn(),
},
}))
describe('TeamSettingsPage Transcode Settings', () => {
let queryClient: QueryClient
beforeEach(() => {
vi.clearAllMocks()
window.location.hash = '#transcode'
queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})
mockGetMe.mockResolvedValue({
ok: true,
json: async () => ({
id: 'user-1',
name: 'Owner',
role: 'owner',
}),
})
mockGetSettings.mockResolvedValue({
ok: true,
json: async () => ({
transcode: {
videoStrategy: VideoTranscodeStrategy.multi,
videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'],
hardwareAcceleration: 'off',
threads: 0,
hlsEnabled: true,
hlsResolutions: ['480p', '720p', '1080p', '1440p', '2160p'],
},
}),
})
})
afterEach(() => {
cleanup()
})
it('keeps pending selections optimistically and serializes consecutive toggles without submitting stale arrays', async () => {
let resolveFirstPatch!: (value: unknown) => void
const firstPatchPromise = new Promise((resolve) => {
resolveFirstPatch = resolve
})
const patchCalls: PatchSettingsArgs[] = []
mockPatchSettings.mockImplementation(async (args: PatchSettingsArgs) => {
patchCalls.push(args)
if (patchCalls.length === 1) {
await firstPatchPromise
}
return {
ok: true,
json: async () => ({
transcode: {
videoResolutions: args.json.value,
},
}),
}
})
render(
<QueryClientProvider client={queryClient}>
<TeamSettingsPage />
</QueryClientProvider>,
)
// Wait for settings to load
await waitFor(() => {
expect(screen.getAllByText('480p').length).toBeGreaterThan(0)
expect(screen.getAllByText('720p').length).toBeGreaterThan(0)
})
const label480p = screen.getAllByText('480p')[0].closest('label')
const checkbox480p = label480p?.querySelector('button[role="checkbox"]') as HTMLElement
expect(checkbox480p).toBeDefined()
expect(checkbox480p.getAttribute('aria-checked')).toBe('true')
const label720p = screen.getAllByText('720p')[0].closest('label')
const checkbox720p = label720p?.querySelector('button[role="checkbox"]') as HTMLElement
expect(checkbox720p).toBeDefined()
expect(checkbox720p.getAttribute('aria-checked')).toBe('true')
// Click 480p to uncheck it
act(() => {
fireEvent.click(checkbox480p)
})
// Optimistically 480p should be unchecked immediately
expect(checkbox480p.getAttribute('aria-checked')).toBe('false')
// Wait for the first PATCH request to be in-flight
await waitFor(() => {
expect(patchCalls.length).toBe(1)
})
expect(patchCalls[0].json.value).toEqual(['720p', '1080p', '1440p', '2160p'])
// Rapid consecutive click: Click 720p to uncheck it WHILE first patch is in-flight
act(() => {
fireEvent.click(checkbox720p)
})
// Optimistically 720p should also be unchecked immediately
expect(checkbox720p.getAttribute('aria-checked')).toBe('false')
// Resolve first patch so the queue advances to the second queued update
await act(async () => {
resolveFirstPatch({
ok: true,
json: async () => ({
transcode: {
videoResolutions: ['720p', '1080p', '1440p', '2160p'],
},
}),
})
})
// Wait for the second queued patch to complete
await waitFor(() => {
expect(patchCalls.length).toBe(2)
})
// The second patch MUST NOT submit stale array with 480p present!
// It must contain neither 480p nor 720p
expect(patchCalls[1].json.value).toEqual(['1080p', '1440p', '2160p'])
expect(patchCalls[1].json.value.includes('480p')).toBe(false)
expect(patchCalls[1].json.value.includes('720p')).toBe(false)
// Verify toast notification on success
expect(toast.success).toHaveBeenCalled()
})
it('coalesces rapid consecutive toggles before network dispatch into a single PATCH', async () => {
const patchCalls: PatchSettingsArgs[] = []
mockPatchSettings.mockImplementation(async (args: PatchSettingsArgs) => {
patchCalls.push(args)
return {
ok: true,
json: async () => ({
transcode: {
videoResolutions: args.json.value,
},
}),
}
})
render(
<QueryClientProvider client={queryClient}>
<TeamSettingsPage />
</QueryClientProvider>,
)
await waitFor(() => {
expect(screen.getAllByText('480p').length).toBeGreaterThan(0)
})
const label480p = screen.getAllByText('480p')[0].closest('label')
const checkbox480p = label480p?.querySelector('button[role="checkbox"]') as HTMLElement
const label720p = screen.getAllByText('720p')[0].closest('label')
const checkbox720p = label720p?.querySelector('button[role="checkbox"]') as HTMLElement
// Rapid synchronous clicks in the same event tick
act(() => {
fireEvent.click(checkbox480p)
fireEvent.click(checkbox720p)
})
// Both reflect optimistically
expect(checkbox480p.getAttribute('aria-checked')).toBe('false')
expect(checkbox720p.getAttribute('aria-checked')).toBe('false')
// Wait for the coalesced PATCH to complete
await waitFor(() => {
expect(patchCalls.length).toBeGreaterThanOrEqual(1)
})
// The submitted payload must reflect both toggles
const lastPatch = patchCalls[patchCalls.length - 1]
expect(lastPatch.json.value).toEqual(['1080p', '1440p', '2160p'])
})
})
+1
View File
@@ -23,6 +23,7 @@ export interface TopNavProjectState {
key: string
width: number
height: number
resolution?: string
hdr?: boolean
}>
}