import React, { useMemo, SyntheticEvent, HTMLProps } from 'react'; import { IconButton } from 'react-file-utils'; import URLParse from 'url-parse'; import { OGAPIResponse } from 'getstream'; import { sanitizeURL } from '../utils'; import { CloseIcon } from './Icons'; export type VideoProps = { og: OGAPIResponse; handleClose?: (event: SyntheticEvent) => void; urlsThatAreGifs?: Array; }; export const Video = ({ og: { videos = [], images = [], url: ogURL, title, description, site_name: siteName }, handleClose, urlsThatAreGifs: gifHosts = ['i.giphy.com', 'i.imgur.com', 'media.giphy.com'], }: VideoProps) => { const video = useMemo(() => videos.find(({ type }) => type === 'text/html' || type === 'video/mp4'), [videos]); if (!video) return null; const videoURL = sanitizeURL(video.secure_url || video.video); const { host } = useMemo(() => new URLParse(videoURL ?? ''), [videoURL]); const isGif = useMemo(() => gifHosts.some((gifHost) => gifHost === host), [host, gifHosts]); const [image] = images; const videoProps: HTMLProps = isGif ? { controls: false, preload: 'auto', // load the video right away autoPlay: true, // display it like it's a gif muted: true, loop: true, playsInline: true, // don't open video in fullscreen on mobile // 'webkit-playsinline': 'webkit-playsinline', } : { controls: true, preload: 'metadata', // try fetching length of video etc. poster: image?.secure_url ?? image?.image, }; const closeButton = handleClose && ( ); if (video.type === 'text/html') { const newVideoURL = videoURL?.split('/watch?v=').join('/embed/'); return (
{closeButton}