import { Play } from "lucide-react"; import { useRef, useState } from "react"; import { useI18n } from "../../../i18n"; import type { Attachment, Post } from "../../../types/post"; import { AttachmentDownloadPill } from "../AttachmentDownloadPill"; import { useVideoPlayer } from "../overlays/VideoPlayer"; import { autolink } from "../utils/autolink"; import { postDisplayText } from "../utils/postText"; const MAX_VISIBLE = 4; function formatDuration(sec: number | undefined): string { if (!sec || sec <= 0) return ""; const m = Math.floor(sec / 60); const s = Math.floor(sec % 60); return `${m}:${s.toString().padStart(2, "0")}`; } function isVideoAttachment(att: Attachment): boolean { return att.kind === "video" || att.mime.startsWith("video/"); } function videoRatio(att: Attachment) { return att.width && att.height ? `${att.width} / ${att.height}` : "16 / 9"; } function VideoAttachmentCard({ postId, attachment, compact = false, overlayCount, }: { postId: string; attachment: Attachment; compact?: boolean; overlayCount?: number; }) { const { openVideo } = useVideoPlayer(); const [playing, setPlaying] = useState(false); const videoRef = useRef(null); const posterUrl = attachment.posterUrl ?? attachment.thumbnailUrl; const duration = formatDuration(attachment.durationSec); const previewVideoUrl = attachment.url.includes("#") ? attachment.url : `${attachment.url}#t=0.1`; return (
{ if (playing) { const v = videoRef.current; openVideo(attachment, v?.currentTime ?? 0); } }} > {playing && !compact ? (
); } export function VideoBubble({ post }: { post: Post }) { const { lang } = useI18n(); const videos = post.attachments.filter(isVideoAttachment); const text = postDisplayText(post, lang); const shouldMerge = videos.length > MAX_VISIBLE; if (!videos.length) return null; if (shouldMerge) { const visible = videos.slice(0, MAX_VISIBLE); const extra = videos.length - MAX_VISIBLE; return (
{visible.map((att, i) => { const isLastSlot = i === MAX_VISIBLE - 1 && extra > 0; return ( ); })}
{text ? (
{autolink(text)}
) : null}
); } return (
{videos.map((att) => ( ))} {text ? (
{autolink(text)}
) : null}
); }