import { useEffect, useMemo, useReducer, useRef, useState } from 'react'
import { Loader2, Play, Radio, SquareTerminal } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { TooltipProvider } from '@/components/ui/tooltip'
import { MultiTrackWidget } from '@/components/widgets/MultiTrackWidget'
import type { TrackData } from '@/types/multitrack'
import {
benchApp,
benchState,
connectBenchSocket,
createBenchNode,
createBenchWidget,
fetchBenchConfig,
onBenchStateChange,
queueBenchRender,
type BenchOutputInfo,
} from './bench-runtime'
const RESOLUTION_PRESETS: Array<{ label: string; width: number; height: number }> = [
{ label: '1920 x 1080 (16:9)', width: 1920, height: 1080 },
{ label: '1088 x 1920 (9:16)', width: 1088, height: 1920 },
{ label: '1280 x 720 (16:9)', width: 1280, height: 720 },
{ label: '720 x 1280 (9:16)', width: 720, height: 1280 },
{ label: '832 x 480 (16:9)', width: 832, height: 480 },
{ label: '480 x 832 (9:16)', width: 480, height: 832 },
{ label: '1024 x 1024 (1:1)', width: 1024, height: 1024 },
]
function useBenchTick(): number {
const [, force] = useReducer((x: number) => x + 1, 0)
useEffect(() => onBenchStateChange(force), [force])
return 0
}
function OutputCard({ label, out }: { label: string; out: BenchOutputInfo }) {
return (
{label}
{out.width}x{out.height} · {out.fps ?? '?'} fps · {Math.round((out.bytes ?? 0) / 1024)} KB
)
}
export function BenchApp() {
useBenchTick()
const [trackData, setTrackData] = useState(null)
const [config, setConfig] = useState>>(null)
const [backend, setBackend] = useState('auto')
const [resolution, setResolution] = useState('1920 x 1080 (16:9)')
const [steps, setSteps] = useState(4)
const [seed, setSeed] = useState(803100406447983)
const [projectName, setProjectName] = useState('MM-girlwaving')
const [queueing, setQueueing] = useState(false)
const node = useMemo(createBenchNode, [])
const widget = useMemo(createBenchWidget, [])
const consoleRef = useRef(null)
useEffect(() => {
connectBenchSocket()
void fetchBenchConfig().then(setConfig)
fetch('/api/workflow')
.then((r) => r.json())
.then((body: { track_data: string }) => {
try {
const parsed = JSON.parse(body.track_data) as TrackData
setTrackData(parsed)
setProjectName((current) => current || 'MM-girlwaving')
} catch {
/* server sent unusable track data; keep the empty editor */
}
})
.catch(() => undefined)
}, [])
useEffect(() => {
if (consoleRef.current) consoleRef.current.scrollTop = consoleRef.current.scrollHeight
})
const preset = RESOLUTION_PRESETS.find((p) => p.label === resolution)
const queue = async () => {
if (!trackData || queueing) return
setQueueing(true)
try {
await queueBenchRender({
track_data: trackData,
backend: backend === 'auto' ? undefined : backend,
overrides: {
width: preset?.width ?? 1920,
height: preset?.height ?? 1080,
steps,
seed,
project_name: projectName,
},
})
} catch (error) {
benchState.error = error instanceof Error ? error.message : String(error)
} finally {
setQueueing(false)
}
}
const busy = benchState.jobStatus === 'queued' || benchState.jobStatus === 'running'
const outputs = benchState.outputs ?? {}
return (
Moxie Multimedia Bench
{benchState.connected ? 'server online' : 'offline'}
backend: {config?.backend ?? benchState.backend}
v{config?.version ?? '?'}
{trackData ? (
setTrackData(next)}
inputName="track_data"
node={node}
widget={widget as never}
app={benchApp}
/>
) : (
loading timeline…
)}
)
}