Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

27.4 KB
Raw Blame
import { useEffect, useRef, useState } from 'react';
import { Link } from 'react-router';
import { Download, Images, Upload, X } from 'lucide-react';
import Drawer from '../Drawer';
import ProviderModelSelector from '../ProviderModelSelector';
import FilmStylePicker from '../codeAnimation/FilmStylePicker';
import FilePickerButton from '../ui/FilePickerButton';
import ListLoadError from '../ui/ListLoadError';
import GalleryImagePicker from '../imageGen/GalleryImagePicker';
import GalleryVideoPicker from '../videoGen/GalleryVideoPicker';
import useListResource from '../../hooks/useListResource';
import useRunWithPicker from '../../hooks/useRunWithPicker';
import { useAsyncAction } from '../../hooks/useAsyncAction';
import useUrlParams from '../../hooks/useUrlParams';
import { copyToClipboard } from '../../lib/clipboard';
import socket from '../../services/socket';
import { createAppLaunchVideo, getAppLaunchVideos, getMotionToolkit, installMotionSkills, publishAppLaunchVideo } from '../../services/apiApps';
import { listPipelineMusicLibrary } from '../../services/apiPipeline';
import { uploadFile } from '../../services/apiMedia';
import { trackAudioUrl } from '../../services/apiTracks';
import { formatBytes, formatDateTime, formatDurationSec, formatLoudness } from '../../utils/formatters';
import toast from '../ui/Toast';

const fetchTracks = () => listPipelineMusicLibrary({ silent: true }).then(result => result.tracks);
const fetchSkillPacks = () => getMotionToolkit({ silent: true }).then(result => result.skillPacks);
const inputClass = 'w-full rounded border border-port-border bg-port-bg p-2 text-port-text';
const buttonClass = 'rounded bg-port-accent px-3 py-2 text-white disabled:opacity-50';
const videoUrl = video => `/data/videos/${encodeURIComponent(video.filename)}`;
const posterUrl = video => `/data/video-thumbnails/${encodeURIComponent(video.thumbnail)}`;
// One run can render the same timeline in several frames (#8960); the order is
// the server's canonical render order.
const FORMATS = [
  ['landscape', 'Landscape 16:9'],
  ['vertical', 'Vertical 9:16'],
  ['square', 'Square 1:1'],
];
const formatLabel = format => FORMATS.find(([value]) => value === format)?.[1] ?? format;
// A style-reference upload is capped server-side to what `detectImageFormat`
// recognizes (PNG/JPEG/WEBP/GIF) or a video ffmpeg can decode — matching the
// picker's `accept` to that keeps the OS file dialog from offering a format
// (SVG, HEIC) that would upload fine and then 400 when the run is queued.
const REFERENCE_ACCEPT = 'image/png,image/jpeg,image/webp,image/gif,video/mp4,video/webm,video/quicktime,video/x-m4v,.png,.jpg,.jpeg,.webp,.gif,.mp4,.webm,.mov,.m4v';
const MOTION_STYLES = [
  ['walkthrough', 'Product walkthrough', 'A paced tour of the key flow, a cursor driving real actions on springs.'],
  ['showreel', 'Motion-graphics showreel', 'Beat-cut kinetic type, color-field swaps and generative shapes around the key flow.'],
  ['ui-morph', 'UI morph loop', 'One container that never cuts, morphing through the product flow on the beat and looping seamlessly.'],
];

// The run is an agent task, so it takes the same provider/model/effort pin as
// every other manual CoS dispatch. Drawer mounts it only while open, so the
// provider catalog and music library are fetched on demand.
function LaunchVideoForm({ appId, onQueued }) {
  const [tone, setTone] = useState('default');
  const [direction, setDirection] = useState('');
  const [formats, setFormats] = useState(['landscape']);
  const [duration, setDuration] = useState(20);
  const [motionStyle, setMotionStyle] = useState('walkthrough');
  const [styleGrammarId, setStyleGrammarId] = useState('');
  const [critiqueRounds, setCritiqueRounds] = useState(2);
  const [motionBlur, setMotionBlur] = useState('');
  const [motionSkills, setMotionSkills] = useState(false);
  const [installingSkills, setInstallingSkills] = useState(false);
  const { items: skillPacks, state: skillPacksState, error: skillPacksError, retry: retrySkillPacks, setItems: setSkillPacks } = useListResource(fetchSkillPacks);
  const [music, setMusic] = useState(false);
  const [generateMusic, setGenerateMusic] = useState(false);
  const [musicMethod, setMusicMethod] = useState('agent');
  const [musicTrack, setMusicTrack] = useState('');
  const { items: tracks, state: tracksState, error: tracksError, retry: retryTracks } = useListResource(fetchTracks);
  // A style reference (#8961): either an existing Media History image/video
  // (`source: 'gallery'`) or a fresh upload into the generic uploads store
  // (`source: 'upload'`) — kept out of the gallery so a one-off reference
  // doesn't clutter it. `label` and `previewUrl` are display-only.
  const [reference, setReference] = useState(null);
  const [imagePickerOpen, setImagePickerOpen] = useState(false);
  const [videoPickerOpen, setVideoPickerOpen] = useState(false);
  const [referenceUploading, setReferenceUploading] = useState(false);
  const submitting = useRef(false);
  const picker = useRunWithPicker();

  const installedSkills = skillPacks.filter(pack => pack.found.length);
  const consultSkills = motionSkills && installedSkills.length > 0;

  const handleInstallSkills = async () => {
    setInstallingSkills(true);
    const result = await installMotionSkills({ silent: true }).catch(err => { toast.error(err.message); return null; });
    setInstallingSkills(false);
    if (!result) return;
    setSkillPacks(result.skillPacks);
    setMotionSkills(true);
    toast.success('Motion skills installed');
  };

  const handleReferenceUpload = async event => {
    const file = event.target.files?.[0];
    if (!file) return;
    // Matches REFERENCE_ACCEPT: PNG/JPEG/WEBP/GIF for an image (what the
    // server's magic-byte sniff recognizes) or an MP4/WebM/QuickTime/M4V
    // container for a video. `accept` only filters the OS picker, not a
    // drag-drop, so this is the real gate against a format that would
    // otherwise upload fine and then 400 when the run is queued.
    const IMAGE_TYPES = ['image/png', 'image/jpeg', 'image/webp', 'image/gif'];
    const VIDEO_TYPES = ['video/mp4', 'video/webm', 'video/quicktime', 'video/x-m4v'];
    const kind = VIDEO_TYPES.includes(file.type) ? 'video' : IMAGE_TYPES.includes(file.type) ? 'image' : null;
    if (!kind) { toast.error('Choose a PNG, JPEG, WEBP, GIF, MP4, WebM, MOV or M4V file'); return; }
    setReferenceUploading(true);
    const saved = await uploadFile(file, file.name, { silent: true }).catch(err => { toast.error(err?.message || 'Upload failed'); return null; });
    setReferenceUploading(false);
    if (!saved?.filename) return;
    setReference({ kind, source: 'upload', filename: saved.filename, label: file.name, previewUrl: null });
  };

  const [submit, running] = useAsyncAction(async () => {
    if (submitting.current) return;
    submitting.current = true;
    await createAppLaunchVideo(appId, {
      // A single frame keeps the original `format` request shape.
      tone, direction, ...(formats.length === 1 ? { format: formats[0] } : { formats }), targetDurationSec: duration, motionStyle, critiqueRounds,
      ...(styleGrammarId ? { styleGrammarId } : {}),
      ...(motionBlur ? { motionBlur } : {}),
      ...(consultSkills ? { motionSkills: true } : {}),
      ...(music ? (generateMusic ? { generateMusic: true, musicMethod } : { musicTrack }) : {}),
      ...(reference ? { styleReference: { kind: reference.kind, source: reference.source, filename: reference.filename } } : {}),
      ...picker.pin,
    }, { silent: true }).then(onQueued).finally(() => { submitting.current = false; });
  });

  return <form className="space-y-4" onSubmit={event => { event.preventDefault(); submit(); }}>
    <section className="space-y-2">
      <div className="text-xs uppercase tracking-wide text-gray-500">Agent</div>
      <ProviderModelSelector {...picker.selectorProps} />
    </section>
    <div><label htmlFor="launch-tone">Tone</label><select id="launch-tone" className={inputClass} value={tone} onChange={event => setTone(event.target.value)}>{['default', 'polished', 'deadpan', 'cinematic', 'parody'].map(value => <option key={value} value={value}>{value}</option>)}</select></div>
    <div><label htmlFor="launch-direction">Direction (optional)</label><textarea id="launch-direction" className={inputClass} maxLength={2000} value={direction} onChange={event => setDirection(event.target.value)} /></div>
    <fieldset className="space-y-2">
      <legend>Style reference (optional)</legend>
      <p className="text-sm text-port-text-muted">A frame, a short clip, or a Media History image or video. The agent studies its palette, type, shot lengths, transitions, camera and texture before writing a style guide — never its content, logos or characters.</p>
      {reference ? (
        <div className="flex items-center gap-2 rounded border border-port-border p-2">
          {reference.previewUrl
            ? <img src={reference.previewUrl} alt="" className="h-12 w-12 rounded object-cover" />
            : <div className="flex h-12 w-12 items-center justify-center rounded bg-port-bg text-xs text-port-text-muted">{reference.kind === 'video' ? 'Video' : 'Image'}</div>}
          <span className="min-w-0 flex-1 truncate text-sm text-port-text">{reference.label}</span>
          <button type="button" aria-label="Clear style reference" onClick={() => setReference(null)} className="text-port-text-muted hover:text-port-error"><X size={16} /></button>
        </div>
      ) : (
        <div className="flex flex-wrap gap-2">
          <button type="button" className="inline-flex items-center gap-1.5 rounded border border-port-border px-2.5 py-1.5 text-xs text-port-text hover:border-port-accent" onClick={() => setImagePickerOpen(true)}><Images size={14} aria-hidden="true" /> Pick an image…</button>
          <button type="button" className="inline-flex items-center gap-1.5 rounded border border-port-border px-2.5 py-1.5 text-xs text-port-text hover:border-port-accent" onClick={() => setVideoPickerOpen(true)}><Images size={14} aria-hidden="true" /> Pick a video…</button>
          <FilePickerButton accept={REFERENCE_ACCEPT} onChange={handleReferenceUpload} disabled={referenceUploading} className="inline-flex items-center gap-1.5 rounded border border-port-border px-2.5 py-1.5 text-xs text-port-text hover:border-port-accent">
            <Upload size={14} aria-hidden="true" /> {referenceUploading ? 'Uploading…' : 'Upload a reference…'}
          </FilePickerButton>
        </div>
      )}
      <GalleryImagePicker open={imagePickerOpen} onClose={() => setImagePickerOpen(false)}
        onSelect={item => setReference({ kind: 'image', source: 'gallery', filename: item.filename, label: item.filename, previewUrl: item.thumbnailUrl || item.previewUrl })} />
      <GalleryVideoPicker open={videoPickerOpen} onClose={() => setVideoPickerOpen(false)}
        onSelect={item => setReference({ kind: 'video', source: 'gallery', filename: item.filename, label: item.filename, previewUrl: item.previewUrl })} />
    </fieldset>
    <fieldset>
      <legend>Formats</legend>
      <div className="flex flex-wrap gap-x-4 gap-y-1">
        {FORMATS.map(([value, label]) => <label key={value} htmlFor={`launch-format-${value}`}>
          <input id={`launch-format-${value}`} type="checkbox" checked={formats.includes(value)}
            onChange={event => setFormats(current => FORMATS.map(([name]) => name).filter(name => name === value ? event.target.checked : current.includes(name)))} /> {label}
        </label>)}
      </div>
      <p className="text-sm text-port-text-muted">Several formats render one film from a single timeline, recomposed for each frame.</p>
    </fieldset>
    <div><label htmlFor="launch-duration">Duration (15–120 seconds)</label><input id="launch-duration" type="number" min={15} max={120} step={1} required className={inputClass} value={duration} onChange={event => setDuration(event.target.value === '' ? '' : Number(event.target.value))} /></div>
    <div>
      <label htmlFor="launch-motion-style">Motion style</label>
      <select id="launch-motion-style" className={inputClass} value={motionStyle} onChange={event => setMotionStyle(event.target.value)}>
        {MOTION_STYLES.map(([value, label]) => <option key={value} value={value}>{label}</option>)}
      </select>
      <p className="text-sm text-port-text-muted">{MOTION_STYLES.find(([value]) => value === motionStyle)[2]}</p>
    </div>
    <FilmStylePicker id="launch-film-style" value={styleGrammarId} onChange={setStyleGrammarId} inputClass={inputClass}
      hint="optional motion, camera and sound rules; the app's palette and fonts still win" noneLabel="None (motion style only)" />
    <div>
      <label htmlFor="launch-critique-rounds">Critique rounds</label>
      <select id="launch-critique-rounds" className={inputClass} value={critiqueRounds} onChange={event => setCritiqueRounds(Number(event.target.value))}>
        {[0, 1, 2, 3, 4].map(value => <option key={value} value={value}>{value === 0 ? 'None (render directly)' : value}</option>)}
      </select>
      <p className="text-sm text-port-text-muted">The agent renders a contact sheet, scores its own frames and fixes the worst problems before the final render.</p>
    </div>
    <div>
      <label htmlFor="launch-motion-blur">Motion blur</label>
      <select id="launch-motion-blur" className={inputClass} value={motionBlur} onChange={event => setMotionBlur(event.target.value)}>
        <option value="">Composition default</option>
        <option value="off">Off</option>
        <option value="light">Light</option>
        <option value="film">Film</option>
      </select>
      <p className="text-sm text-port-text-muted">Overrides the composition's own blur at render time. Film uses a 180° shutter.</p>
    </div>
    <div>
      <label htmlFor="launch-motion-skills"><input id="launch-motion-skills" type="checkbox" disabled={!installedSkills.length} checked={consultSkills} onChange={event => setMotionSkills(event.target.checked)} /> Consult motion skills</label>
      {skillPacksState === 'error'
        ? <ListLoadError what="installed motion skills" error={skillPacksError} onRetry={retrySkillPacks} />
        : <p className="text-sm text-port-text-muted">{skillPacksState === 'loading' ? 'Checking installed skills…' : installedSkills.length
          ? `Technique guides from ${installedSkills.map(pack => pack.label).join(', ')}.`
          : 'HyperFrames, Remotion and Claude Animation skills are not installed.'}</p>}
      {skillPacksState === 'ready' && skillPacks.some(pack => !pack.installed) && (
        <button type="button" className="btn btn-secondary" disabled={installingSkills} onClick={handleInstallSkills}>
          {installingSkills ? 'Installing motion skills…' : installedSkills.length ? 'Install missing motion skills' : 'Install and enable motion skills'}
        </button>
      )}
    </div>
    <div><label htmlFor="launch-music"><input id="launch-music" type="checkbox" checked={music} onChange={event => setMusic(event.target.checked)} /> Include music</label></div>
    {music && <div><label htmlFor="launch-generate-music"><input id="launch-generate-music" type="checkbox" checked={generateMusic} onChange={event => setGenerateMusic(event.target.checked)} /> Generate original music</label><p className="text-sm text-port-text-muted">Choose how the original soundtrack is made.</p></div>}
    {music && generateMusic && <div>
      <label htmlFor="launch-music-method">Music creation</label>
      <select id="launch-music-method" className={inputClass} value={musicMethod} onChange={event => setMusicMethod(event.target.value)}>
        <option value="agent">Agent composition (no music model required)</option>
        <option value="service">Configured music service</option>
      </select>
      <p className="text-sm text-port-text-muted">{musicMethod === 'agent' ? 'The agent writes an instrumental score using available synthesis tools, such as Web Audio or Tone.js.' : 'Uses a ready PortOS music engine. Setup and provider charges may apply.'}</p>
    </div>}
    {music && !generateMusic && <fieldset className="space-y-2">
      <legend>Music-library track</legend>
      {tracksState === 'loading' && <p className="text-sm text-port-text-muted">Loading tracks…</p>}
      {tracksState === 'error' && <ListLoadError what="the music library" error={tracksError} onRetry={retryTracks} />}
      {tracksState === 'ready' && tracks.length === 0 && <p>Add a track to the Music library first.</p>}
      {tracksState === 'ready' && tracks.length > 0 && <ul className="max-h-80 space-y-2 overflow-y-auto">
        {tracks.map((track, index) => {
          const id = `launch-track-${index}`;
          const name = track.label || track.filename;
          return <li key={track.filename} className={`rounded border p-2 ${musicTrack === track.filename ? 'border-port-accent' : 'border-port-border'}`}>
            <label htmlFor={id} className="flex cursor-pointer items-start gap-2">
              <input id={id} type="radio" name="launch-track" required value={track.filename} checked={musicTrack === track.filename} onChange={() => setMusicTrack(track.filename)} className="mt-1" />
              <span className="min-w-0">
                <span className="block truncate text-port-text">{name}</span>
                <span className="block truncate font-mono text-xs text-gray-500">{track.filename} · {formatDateTime(track.updatedAt)} · {formatBytes(track.sizeBytes)}</span>
              </span>
            </label>
            <audio controls preload="none" className="mt-2 h-8 w-full" src={trackAudioUrl(track.filename)} aria-label={`Preview ${name}`} />
          </li>;
        })}
      </ul>}
    </fieldset>}
    <p className="text-sm text-port-text-muted">These options are submitted together. Follow and cancel the run in CoS agents.</p>
    <button type="submit" className={buttonClass} disabled={running || referenceUploading || !formats.length || duration === '' || (music && !generateMusic && !musicTrack)}>{running ? 'Queuing…' : 'Queue launch video'}</button>
  </form>;
}

function ReviseLaunchVideo({ appId, videoId }) {
  const [feedback, setFeedback] = useState('');
  const [taskId, setTaskId] = useState(null);
  const submitting = useRef(false);
  const picker = useRunWithPicker();
  const [revise, running] = useAsyncAction(async () => {
    if (submitting.current || !feedback.trim()) return;
    submitting.current = true;
    await createAppLaunchVideo(appId, { sourceVideoId: videoId, feedback: feedback.trim(), ...picker.pin }, { silent: true })
      .then(result => setTaskId(result.taskId))
      .finally(() => { submitting.current = false; });
  });
  return <form className="space-y-2 rounded border border-port-border p-3" onSubmit={event => { event.preventDefault(); revise(); }}>
    <h3 className="font-medium">Revise this version</h3>
    <p className="text-sm text-port-text-muted">Give feedback on the selected video. The agent edits a copy and saves a new version, preserving this take.</p>
    {taskId ? <p role="status">Revision queued. <Link className="text-port-accent" to="/cos/agents">Follow the revision in CoS agents</Link></p> : <>
      <label htmlFor="launch-feedback">Feedback</label>
      <textarea id="launch-feedback" className={inputClass} required maxLength={4000} value={feedback} disabled={running} onChange={event => setFeedback(event.target.value)} placeholder="For example: slow down the closing scene and make the headline larger." />
      <ProviderModelSelector {...picker.selectorProps} />
      <button type="submit" className={buttonClass} disabled={running || !feedback.trim()}>{running ? 'Queuing revision…' : 'Revise as new version'}</button>
    </>}
  </form>;
}

function PublishLaunchVideo({ appId, videoId }) {
  const [format, setFormat] = useState('mp4');
  const picker = useRunWithPicker();
  const submitting = useRef(false);
  const [taskId, setTaskId] = useState(null);
  const [publish, running] = useAsyncAction(async () => {
    if (submitting.current) return;
    submitting.current = true;
    await publishAppLaunchVideo(appId, { videoId, format, ...picker.pin }, { silent: true })
      .then(result => setTaskId(result.taskId))
      .finally(() => { submitting.current = false; });
  });
  return <div className="space-y-2 rounded border border-port-border p-3">
    <h3 className="font-medium">Publish to README</h3>
    <p className="text-sm text-port-text-muted">Publish an MP4 with sound and a clickable poster, or a silent looping GIF. MP4 playback or download opens from the poster; it is not an inline README player. The agent replaces any existing launch-video embed near the top of README.md, commits the selected assets, opens a PR and merges after review and checks pass. Assets inherit the repository’s visibility. Nothing is uploaded to YouTube or another video host.</p>
    {taskId ? <p role="status">README publication queued. <Link className="text-port-accent" to="/cos/agents">Follow the render and PR in CoS agents</Link></p> : <>
      <ProviderModelSelector {...picker.selectorProps} />
      <label htmlFor="launch-publish-format">README media format</label>
      <select id="launch-publish-format" className={inputClass} value={format} disabled={running} onChange={event => setFormat(event.target.value)}>
        <option value="mp4">MP4 with sound (clickable poster)</option>
        <option value="gif">GIF (silent, looping)</option>
      </select>
      <button type="button" className={buttonClass} disabled={running} onClick={publish}>{running ? 'Queuing publication…' : 'Publish to README and merge PR'}</button>
    </>}
  </div>;
}

export default function LaunchVideoPanel({ app }) {
  const [search, updateParams] = useUrlParams();
  const open = search.get('launchVideo') === 'true';
  const [videos, setVideos] = useState([]);
  const [error, setError] = useState('');
  const [queued, setQueued] = useState(false);
  // The previewed video lives in the URL so a specific take and format is linkable.
  const selected = videos.find(video => video.id === search.get('video')) ?? videos[0];
  // A multi-format run registers one video per format under one runId (#8960);
  // the tab shows it as a single take. Older entries without a runId stand alone.
  const takes = [...videos.reduce((groups, video) => {
    const key = video.runId ?? video.id;
    groups.set(key, [...(groups.get(key) ?? []), video]);
    return groups;
  }, new Map()).values()];
  const selectedTake = takes.find(take => take.includes(selected)) ?? [];
  const selectVideo = id => updateParams({ video: id }, { replace: true });

  useEffect(() => {
    let active = true;
    let generation = 0;
    const load = () => {
      const request = ++generation;
      getAppLaunchVideos(app.id, { silent: true }).then(result => {
        if (active && request === generation) { setVideos(result.videos); setError(''); }
      }).catch(err => { if (active && request === generation) setError(err.message); });
    };
    const completed = event => { if (event.appId === app.id) load(); };
    socket.on('video-gen:completed', completed);
    socket.on('connect', load);
    load();
    return () => { active = false; socket.off('video-gen:completed', completed); socket.off('connect', load); };
  }, [app.id]);

  return <section className="rounded-lg border border-port-border bg-port-card p-4 space-y-3">
    <div className="flex flex-wrap items-center justify-between gap-3">
      <h2 className="font-semibold">Launch videos</h2>
      <button type="button" className={buttonClass} onClick={() => { setQueued(false); updateParams({ launchVideo: 'true' }); }}>
        {videos.length ? 'Make another launch video' : 'Make launch video'}
      </button>
    </div>
    <p className="text-sm text-port-text-muted">Plan a short video using recreated screens and fictional content. Generating a video keeps it local until you choose to publish.</p>
    {error && <p role="alert" className="text-port-error">{error}</p>}
    {!videos.length && !error && <p className="text-sm text-port-text-muted">No launch videos yet.</p>}
    {selected && <div className="space-y-2">
      {selectedTake.length > 1 && <div role="group" aria-label="Formats in this take" className="flex flex-wrap gap-2">
        {selectedTake.map(video => <button key={video.id} type="button" aria-pressed={video.id === selected.id} onClick={() => selectVideo(video.id)}
          className={`rounded border px-2 py-1 text-sm ${video.id === selected.id ? 'border-port-accent text-port-text' : 'border-port-border text-port-text-muted'}`}>{formatLabel(video.format)}</button>)}
      </div>}
      <video key={selected.id} controls preload="metadata" className="max-h-[60vh] w-full bg-black" src={videoUrl(selected)} poster={posterUrl(selected)} aria-label="Selected launch video" />
      <div className="flex flex-wrap items-center gap-4 text-sm">
        <span className="text-port-text-muted">{formatDateTime(selected.createdAt)}{Number.isFinite(selected.durationSec) ? ` · ${formatDurationSec(selected.durationSec)}` : ''}</span>
        {selectedTake.length > 1
          ? selectedTake.map(video => <a key={video.id} className="inline-flex items-center gap-1 text-port-accent" href={videoUrl(video)} download={`${app.id}-launch-video-${video.id}.mp4`}><Download size={14} aria-hidden="true" />Download {formatLabel(video.format)}</a>)
          : <a className="inline-flex items-center gap-1 text-port-accent" href={videoUrl(selected)} download={`${app.id}-launch-video-${selected.id}.mp4`}><Download size={14} aria-hidden="true" />Download</a>}
        <button type="button" className="text-port-accent" onClick={() => copyToClipboard(selected.caption)}>Copy caption</button>
        <Link className="text-port-accent" to="/media/history">Media History</Link>
      </div>
      <p className="whitespace-pre-wrap">{selected.caption}</p>
      {selected.loudness && <p className="text-sm text-port-text-muted">Audio: {formatLoudness(selected.loudness)}</p>}
      {selected.sampleHistogram && <p className="text-sm text-port-text-muted">Motion blur samples per frame: {Object.entries(selected.sampleHistogram).map(([samples, frames]) => `${samples}× → ${frames} frames`).join(', ')}</p>}
      {selected.sourceVideoId && <p className="text-sm text-port-text-muted">Revised from version {selected.sourceVideoId}</p>}
      <ReviseLaunchVideo key={`revision:${app.id}:${selected.id}`} appId={app.id} videoId={selected.id} />
      <PublishLaunchVideo key={`${app.id}:${selected.id}`} appId={app.id} videoId={selected.id} />
    </div>}
    {takes.length > 1 && <ul aria-label="Launch video takes" className="grid grid-cols-2 gap-2 sm:grid-cols-4 lg:grid-cols-6">
      {takes.map(([video, ...others]) => {
        const current = selectedTake[0] === video;
        return <li key={video.id}>
          <button type="button" aria-pressed={current} onClick={() => selectVideo(video.id)} className={`w-full overflow-hidden rounded border text-left ${current ? 'border-port-accent' : 'border-port-border'}`}>
            <img src={posterUrl(video)} alt="" loading="lazy" className="aspect-video w-full object-cover" />
            <span className="block truncate p-1 text-xs text-port-text-muted">{formatDateTime(video.createdAt)}{others.length ? ` · ${others.length + 1} formats` : ''}</span>
          </button>
        </li>;
      })}
    </ul>}
    <Drawer open={open} onClose={() => updateParams({ launchVideo: null })} title="Make launch video" size="md">
      {queued ? <div className="space-y-3"><p>Launch video queued. Closing this drawer leaves the run active.</p><Link className="text-port-accent" to="/cos/agents">Open CoS agents to follow or cancel the run</Link></div>
        : <LaunchVideoForm appId={app.id} onQueued={() => setQueued(true)} />}
    </Drawer>
  </section>;
}