'use client' import { DownloadIcon, ExternalLinkIcon, ScissorsIcon } from 'lucide-react' import { useRef, useState } from 'react' import { CampaignTranscriptSegmentButton } from '@/components/campaign/shared/CampaignTranscriptSegmentButton' import { SpeechCutDialog } from '@/components/campaign/speech/SpeechCutDialog' import { SpeechCutResultCard } from '@/components/campaign/speech/SpeechCutResultCard' import { WebSpeechPlatformPill } from '@/components/campaign/speech/WebSpeechPlatformPill' import { Button } from '@/components/ui/button' import type { SpeechCutViewModel } from '@/lib/speechCut' import { excerptSelectionDuration, extendRangeToSegment, initialExcerptRange, isExcerptSelectionAvailable, segmentInExcerpt, selectSegmentRange, type ExcerptRange, } from '@/lib/speechExcerptSelection' import type { WebSpeechDetailViewModel } from '@/utilities/speech/speechViewModels' type WebSpeechDetailPlayerProps = { speech: WebSpeechDetailViewModel /** `?t=` of the search hit; null when the detail was opened without a term. */ initialSeconds: number | null } /** * C216 — the web speech detail (design scenes 02/05): the mirrored private * media (native video or audio control — the artifact's audio variant), the * download, "Abrir na origem", the origin attribution and the clickable * transcript that seeks the player. Playback itself is browser territory. * * C217 — the transcript is also the cut picker: clicking a phrase seeks the * player and marks/extends the excerpt, and "Cortar trecho" opens the existing * cut dialog with the picked window (design scenes 01/06). */ export const WebSpeechDetailPlayer = ({ speech, initialSeconds }: WebSpeechDetailPlayerProps) => { const mediaRef = useRef(null) const [activeStart, setActiveStart] = useState( initialSeconds ?? speech.segments[0]?.startSeconds ?? null, ) // C217 — null until the first phrase click: nothing is marked on page load. const [selection, setSelection] = useState(null) const [cutDialogOpen, setCutDialogOpen] = useState(false) const [publishedCut, setPublishedCut] = useState(null) const selectionDuration = excerptSelectionDuration(speech.durationSeconds, speech.segments) // The server validates the window against the stored duration, so the picker // only exists when both the mirror and that duration do (no segment fallback // the action would refuse). const selectionAvailable = speech.fileHref !== null && speech.durationSeconds !== null && isExcerptSelectionAvailable(speech.durationSeconds, speech.segments) const seekTo = (seconds: number) => { const media = mediaRef.current if (!media) return media.currentTime = seconds setActiveStart(seconds) void media.play().catch(() => undefined) } const onTimeUpdate = () => { const media = mediaRef.current if (!media) return const current = media.currentTime const active = speech.segments.find( (segment) => current >= segment.startSeconds && current < segment.endSeconds, ) setActiveStart(active?.startSeconds ?? null) } const onLoadedMetadata = () => { const media = mediaRef.current if (media && initialSeconds !== null && initialSeconds > 0) { media.currentTime = initialSeconds setActiveStart(initialSeconds) } } /** C217 — one gesture: the click seeks (C216) and marks/extends the excerpt. */ const onSegmentActivate = (index: number) => { const segment = speech.segments[index] if (!segment) return seekTo(segment.startSeconds) if (!selectionAvailable || selectionDuration === null) return setSelection((current) => current ? extendRangeToSegment(current, speech.segments, index, selectionDuration) : selectSegmentRange(speech.segments, index, selectionDuration), ) } /** Opens the dialog on the picked window, defaulting to the first phrase. */ const openCutDialog = () => { if (!selectionAvailable || selectionDuration === null) return const range = selection ?? initialExcerptRange(speech.segments, selectionDuration) if (!range) return setSelection(range) setCutDialogOpen(true) } const setMediaRef = (element: HTMLVideoElement | HTMLAudioElement | null) => { mediaRef.current = element } const metaLine = [ speech.durationLabel, ...speech.topics.map((topic) => topic.label), ...speech.scopes.map((scope) => scope.label), ] .filter(Boolean) .join(' · ') const badge = ( Arquivo espelhado · privado ) // The mirrored file is the product: without it the page stays honest instead // of rendering a player that can only fail. const mediaBox = speech.fileHref ? ( speech.mediaKind === 'audio' ? (
{badge}
) : (
{badge}
) ) : (

O arquivo espelhado desta fala não está disponível.

) return ( <>
{mediaBox}
{selectionAvailable ? ( ) : null} {speech.downloadHref ? ( // The cut CTA leads when the picker exists; the download steps // back so the two primary-red controls never compete (scene 01). ) : null} {speech.sourceUrl ? ( ) : null}
{selectionAvailable && speech.segments.length > 0 ? (

Selecione na transcrição as frases do trecho.

) : null} {/* Mobile shows the attribution inline under the title (scene 05). */}

Origem

{[speech.platform.label, speech.channel].filter(Boolean).join(' · ')}

Publicado em {speech.dateLabel}

{speech.title}

{metaLine ? (

{metaLine}

) : null}

{[speech.platform.label, speech.channel, speech.dateLabel].filter(Boolean).join(' · ')}

{speech.segments.length > 0 ? (

Transcrição

{selectionAvailable ? ( <> Clique nas frases Toque para selecionar ) : ( 'Clique para posicionar' )}
    {speech.segments.map((segment, index) => { const inSelection = selection !== null && segmentInExcerpt(segment, selection) return (
  1. onSegmentActivate(index)} />
  2. ) })}
) : (

{speech.fileHref ? 'A transcrição desta fala está vazia — o arquivo continua disponível para download.' : 'A transcrição desta fala está vazia.'}

)}
{publishedCut ? : null} {selection ? ( ) : null} ) }