|
1 | 1 | /* eslint-disable @next/next/no-img-element */
|
2 | 2 | import { PlayCircle, PlayIcon, PlusIcon, VideoIcon } from 'lucide-react';
|
3 |
| -import { useState } from 'react'; |
| 3 | +import { useRef, useState } from 'react'; |
4 | 4 | import Lightbox, { GenericSlide, VideoSlide } from 'yet-another-react-lightbox';
|
5 | 5 | import 'yet-another-react-lightbox/styles.css';
|
6 | 6 | import { Message } from './ChatWindow';
|
@@ -35,6 +35,8 @@ const Searchvideos = ({
|
35 | 35 | const [loading, setLoading] = useState(false);
|
36 | 36 | const [open, setOpen] = useState(false);
|
37 | 37 | const [slides, setSlides] = useState<VideoSlide[]>([]);
|
| 38 | + const [currentIndex, setCurrentIndex] = useState(0); |
| 39 | + const videoRefs = useRef<(HTMLIFrameElement | null)[]>([]); |
38 | 40 |
|
39 | 41 | return (
|
40 | 42 | <>
|
@@ -182,18 +184,39 @@ const Searchvideos = ({
|
182 | 184 | open={open}
|
183 | 185 | close={() => setOpen(false)}
|
184 | 186 | slides={slides}
|
| 187 | + index={currentIndex} |
| 188 | + on={{ |
| 189 | + view: ({ index }) => { |
| 190 | + const previousIframe = videoRefs.current[currentIndex]; |
| 191 | + if (previousIframe?.contentWindow) { |
| 192 | + previousIframe.contentWindow.postMessage( |
| 193 | + '{"event":"command","func":"pauseVideo","args":""}', |
| 194 | + '*', |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + setCurrentIndex(index); |
| 199 | + }, |
| 200 | + }} |
185 | 201 | render={{
|
186 |
| - slide: ({ slide }) => |
187 |
| - slide.type === 'video-slide' ? ( |
| 202 | + slide: ({ slide }) => { |
| 203 | + const index = slides.findIndex((s) => s === slide); |
| 204 | + return slide.type === 'video-slide' ? ( |
188 | 205 | <div className="h-full w-full flex flex-row items-center justify-center">
|
189 | 206 | <iframe
|
190 |
| - src={slide.iframe_src} |
| 207 | + src={`${slide.iframe_src}${slide.iframe_src.includes('?') ? '&' : '?'}enablejsapi=1`} |
| 208 | + ref={(el) => { |
| 209 | + if (el) { |
| 210 | + videoRefs.current[index] = el; |
| 211 | + } |
| 212 | + }} |
191 | 213 | className="aspect-video max-h-[95vh] w-[95vw] rounded-2xl md:w-[80vw]"
|
192 | 214 | allowFullScreen
|
193 | 215 | allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
|
194 | 216 | />
|
195 | 217 | </div>
|
196 |
| - ) : null, |
| 218 | + ) : null; |
| 219 | + }, |
197 | 220 | }}
|
198 | 221 | />
|
199 | 222 | </>
|
|
0 commit comments