import { contentPieceMediaKind, type ContentPiecePublicItem } from '@/lib/contentPieceCatalog' import { cn } from '@/lib/utils' import { useCallback, useState } from 'react' /** * The media states read this subset of the public item, so the S39 home * projection (leaner, no search haystack) renders through the same component. */ type ContentPieceMediaItem = Pick< ContentPiecePublicItem, | 'isLink' | 'file' | 'framePath' | 'type' | 'origin' | 'originLabel' | 'title' | 'excerpt' | 'durationLabel' | 'typeLabel' > /** * S27 — the piece asset (artefato: cenas 01/02/03/07): a photo previews, a * text shows its opening, a video/audio only mounts the media element after * the voter taps play (`preload="none"`, nothing is fetched before the * gesture) and a link piece shows where it lives. The owner of the play state * is the host (catalogue keeps one at a time; the piece page keeps its own). */ const PlayIcon = () => ( ) const PlayButton = ({ label, onToggle, compact = false, }: { label: string onToggle: () => void /** C226 — the 124px home thumb (artefato cena 05) has room for a 44px play only. */ compact?: boolean }) => ( ) const DurationBadge = ({ label, compact = false }: { label: string | null; compact?: boolean }) => label ? ( {label} ) : null /** * C226 — the neutral marker of a piece with no still (artefato cena 04). The * piece type keeps reading on the card body tag, so nothing is lost. */ /** * C226 — the honest marker of a piece with no still (artefato cenas 04/05). The * `compact` form is the 124px home thumb, where the neutral surface itself is * the message and the marker would crowd the play; from `sm` up, where that * card becomes a full block, the complete marker returns (cena 05). */ const FrameUnavailableMarker = ({ detail = false, compact = false, }: { detail?: boolean compact?: boolean }) => ( Frame indisponível ) export const ContentPieceMedia = ({ item, playing, onToggle, onEnded, variant = 'card', className, }: { item: ContentPieceMediaItem playing: boolean onToggle: () => void onEnded: () => void variant?: 'card' | 'thumb' | 'detail' | 'home-thumb' className?: string }) => { const kind = contentPieceMediaKind(item) const base = cn('relative overflow-hidden bg-(--campaign-band)', className) // C226 — the still lives in the slot the play state shares, so the knowledge // that this piece has no frame is held by the host, not by the layer that // remounts on every play: `failed` is terminal for the card, never retried (a // broken image would be a lie and the retry would hammer the route). const [frameState, setFrameState] = useState<'loading' | 'ready' | 'failed'>('loading') // A still whose load settled BEFORE React attached the handlers (a warm // cache, an SSR page, a 404) fires NEITHER `load` nor `error` again — the // ref is the only place that can still tell a decoded image from a failed // one, and without it the fastest paths are exactly the broken ones. const settleLoadedFrame = useCallback((node: HTMLImageElement | null) => { if (!node?.complete) return setFrameState(node.naturalWidth > 0 ? 'ready' : 'failed') }, []) if (kind === null) { return (
) } if (kind === 'image') { return (
{/* eslint-disable-next-line @next/next/no-img-element -- private proxy path with on-demand headers */} {item.title}
) } if (kind === 'text') { return (

“{item.excerpt ?? item.title}”

) } if (kind === 'video' || kind === 'audio') { if (playing) { return (
{kind === 'video' ? (
) } return (
{kind === 'video' && item.framePath && frameState !== 'failed' && ( // eslint-disable-next-line @next/next/no-img-element -- private proxy path with on-demand headers ") and the title sits right below it. alt="" loading="lazy" decoding="async" ref={settleLoadedFrame} onLoad={() => setFrameState('ready')} onError={() => setFrameState('failed')} // Invisible until it decodes: a failed image would otherwise paint // the browser's broken-image glyph over the slot for a moment, and // the design's honest state is the neutral surface, never a glyph. className={cn( 'absolute inset-0 h-full w-full object-contain transition-opacity duration-200 motion-reduce:transition-none', frameState !== 'ready' && 'opacity-0', )} /> )} {/* A piece whose projection declares no frame, and one whose still the route could not produce: the same honest slot, no broken image. */} {kind === 'video' && (!item.framePath || frameState === 'failed') && ( )} {kind === 'audio' && ( {item.typeLabel} )}
) } return (

{item.file ? 'Baixe a peça para ver o arquivo.' : 'Sem arquivo para pré-visualizar.'}

) }