1- import { useEffect , useRef , useState } from 'react' ;
2- import { isPresent } from '../../../shared/utils/isPresent' ;
1+ import { useQuery } from '@tanstack/react-query' ;
32
43type Props = {
54 src : string ;
@@ -10,61 +9,21 @@ export const SlideVideo = ({ src, poster }: Props) => {
109 // Bundled assets are served without range request support, which makes video
1110 // playback stutter. Play it from an in-memory blob instead.
1211 // https://github.com/orgs/tauri-apps/discussions/7870
13- const [ blobSrc , setBlobSrc ] = useState < string > ( ) ;
14- const videoRef = useRef < HTMLVideoElement > ( null ) ;
15-
16- useEffect ( ( ) => {
17- let objectUrl : string | undefined ;
18- let cancelled = false ;
19-
20- void ( async ( ) => {
12+ const { data : blobSrc } = useQuery ( {
13+ queryKey : [ 'slide-video' , src ] as const ,
14+ queryFn : async ( ) => {
2115 const response = await fetch ( src ) ;
22- const blob = await response . blob ( ) ;
23- if ( cancelled ) return ;
24- objectUrl = URL . createObjectURL ( blob ) ;
25- setBlobSrc ( objectUrl ) ;
26- } ) ( ) ;
27-
28- return ( ) => {
29- cancelled = true ;
30- if ( isPresent ( objectUrl ) ) {
31- URL . revokeObjectURL ( objectUrl ) ;
32- }
33- } ;
34- } , [ src ] ) ;
35-
36- // The welcome window is pre-built hidden and only hidden again on close, so the webview
37- // outlives the window being on screen.
38- useEffect ( ( ) => {
39- const video = videoRef . current ;
40- if ( ! isPresent ( video ) || ! isPresent ( blobSrc ) ) return ;
41-
42- const syncPlayback = ( ) => {
43- if ( document . visibilityState === 'visible' ) {
44- void video . play ( ) . catch ( ( ) => {
45- // Autoplay can be refused while the window is not interactable yet.
46- } ) ;
47- } else {
48- video . pause ( ) ;
49- }
50- } ;
51-
52- syncPlayback ( ) ;
53- document . addEventListener ( 'visibilitychange' , syncPlayback ) ;
54-
55- return ( ) => {
56- document . removeEventListener ( 'visibilitychange' , syncPlayback ) ;
57- video . pause ( ) ;
58- } ;
59- } , [ blobSrc ] ) ;
16+ return URL . createObjectURL ( await response . blob ( ) ) ;
17+ } ,
18+ } ) ;
6019
6120 return (
6221 < video
63- ref = { videoRef }
6422 src = { blobSrc }
6523 poster = { poster }
6624 width = { '100%' }
6725 height = { 'auto' }
26+ autoPlay
6827 loop
6928 muted
7029 playsInline
0 commit comments