Skip to content

Commit 9e8fff1

Browse files
committed
refactor: enhance MovieTrailer component by adding useAutoplay hook for video playback control
1 parent 6510bac commit 9e8fff1

File tree

7 files changed

+118
-14
lines changed

7 files changed

+118
-14
lines changed

exercises/01.start/01.problem.vite-plugin/app/movie-trailer.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Activity, useState } from 'react'
1+
import { Activity, useEffect, useState } from 'react'
22
import { type Movie } from '#app/movies-data.ts'
33

44
export function MovieTrailer({ movie }: { movie: Movie }) {
55
const [showTrailer, setShowTrailer] = useState(false)
6+
useAutoplay(showTrailer)
67

78
return (
89
<div className="mb-6">
@@ -18,7 +19,6 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
1819
src={movie.videoUrl}
1920
title={movie.title}
2021
autoPlay
21-
muted
2222
loop
2323
controls
2424
/>
@@ -27,3 +27,20 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
2727
</div>
2828
)
2929
}
30+
31+
function useAutoplay(showTrailer: boolean) {
32+
useEffect(() => {
33+
const video = document.querySelector('video')
34+
if (!(video instanceof HTMLVideoElement)) return
35+
if (!showTrailer) {
36+
void video.pause()
37+
return
38+
}
39+
40+
video.volume = 1
41+
void video.play()
42+
return () => {
43+
void video.pause()
44+
}
45+
}, [showTrailer])
46+
}

exercises/01.start/01.problem.vite-plugin/vite.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// import { unstable_reactRouterRSC as reactRouterRSC } from '@react-router/dev/vite'
2-
import { reactRouter } from '@react-router/dev/vite'
1+
import {
2+
reactRouter,
3+
// unstable_reactRouterRSC as reactRouterRSC,
4+
} from '@react-router/dev/vite'
35
import tailwindcss from '@tailwindcss/vite'
46
// import rsc from '@vitejs/plugin-rsc'
57
import { defineConfig } from 'vite'

exercises/01.start/01.solution.vite-plugin/app/movie-trailer.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Activity, useState } from 'react'
1+
import { Activity, useEffect, useState } from 'react'
22
import { type Movie } from '#app/movies-data.ts'
33

44
export function MovieTrailer({ movie }: { movie: Movie }) {
55
const [showTrailer, setShowTrailer] = useState(false)
6+
useAutoplay(showTrailer)
67

78
return (
89
<div className="mb-6">
@@ -18,7 +19,6 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
1819
src={movie.videoUrl}
1920
title={movie.title}
2021
autoPlay
21-
muted
2222
loop
2323
controls
2424
/>
@@ -27,3 +27,20 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
2727
</div>
2828
)
2929
}
30+
31+
function useAutoplay(showTrailer: boolean) {
32+
useEffect(() => {
33+
const video = document.querySelector('video')
34+
if (!(video instanceof HTMLVideoElement)) return
35+
if (!showTrailer) {
36+
void video.pause()
37+
return
38+
}
39+
40+
video.volume = 1
41+
void video.play()
42+
return () => {
43+
void video.pause()
44+
}
45+
}, [showTrailer])
46+
}

exercises/01.start/02.solution.loader-rsc/app/movie-trailer.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Activity, useState } from 'react'
1+
import { Activity, useEffect, useState } from 'react'
22
import { type Movie } from '#app/movies-data.ts'
33

44
export function MovieTrailer({ movie }: { movie: Movie }) {
55
const [showTrailer, setShowTrailer] = useState(false)
6+
useAutoplay(showTrailer)
67

78
return (
89
<div className="mb-6">
@@ -18,7 +19,6 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
1819
src={movie.videoUrl}
1920
title={movie.title}
2021
autoPlay
21-
muted
2222
loop
2323
controls
2424
/>
@@ -27,3 +27,20 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
2727
</div>
2828
)
2929
}
30+
31+
function useAutoplay(showTrailer: boolean) {
32+
useEffect(() => {
33+
const video = document.querySelector('video')
34+
if (!(video instanceof HTMLVideoElement)) return
35+
if (!showTrailer) {
36+
void video.pause()
37+
return
38+
}
39+
40+
video.volume = 1
41+
void video.play()
42+
return () => {
43+
void video.pause()
44+
}
45+
}, [showTrailer])
46+
}

exercises/01.start/03.solution.rsc-route/app/movie-trailer.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Activity, useState } from 'react'
1+
import { Activity, useEffect, useState } from 'react'
22
import { type Movie } from '#app/movies-data.ts'
33

44
export function MovieTrailer({ movie }: { movie: Movie }) {
55
const [showTrailer, setShowTrailer] = useState(false)
6+
useAutoplay(showTrailer)
67

78
return (
89
<div className="mb-6">
@@ -18,7 +19,6 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
1819
src={movie.videoUrl}
1920
title={movie.title}
2021
autoPlay
21-
muted
2222
loop
2323
controls
2424
/>
@@ -27,3 +27,20 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
2727
</div>
2828
)
2929
}
30+
31+
function useAutoplay(showTrailer: boolean) {
32+
useEffect(() => {
33+
const video = document.querySelector('video')
34+
if (!(video instanceof HTMLVideoElement)) return
35+
if (!showTrailer) {
36+
void video.pause()
37+
return
38+
}
39+
40+
video.volume = 1
41+
void video.play()
42+
return () => {
43+
void video.pause()
44+
}
45+
}, [showTrailer])
46+
}

exercises/01.start/04.solution.server-fns/app/movie-trailer.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Activity, useState } from 'react'
1+
import { Activity, useEffect, useState } from 'react'
22
import { type Movie } from '#app/movies-data.ts'
33

44
export function MovieTrailer({ movie }: { movie: Movie }) {
55
const [showTrailer, setShowTrailer] = useState(false)
6+
useAutoplay(showTrailer)
67

78
return (
89
<div className="mb-6">
@@ -18,7 +19,6 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
1819
src={movie.videoUrl}
1920
title={movie.title}
2021
autoPlay
21-
muted
2222
loop
2323
controls
2424
/>
@@ -27,3 +27,20 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
2727
</div>
2828
)
2929
}
30+
31+
function useAutoplay(showTrailer: boolean) {
32+
useEffect(() => {
33+
const video = document.querySelector('video')
34+
if (!(video instanceof HTMLVideoElement)) return
35+
if (!showTrailer) {
36+
void video.pause()
37+
return
38+
}
39+
40+
video.volume = 1
41+
void video.play()
42+
return () => {
43+
void video.pause()
44+
}
45+
}, [showTrailer])
46+
}

exercises/01.start/05.solution.use-client/app/movie-trailer.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use client'
22

3-
import { Activity, useState } from 'react'
3+
import { Activity, useEffect, useState } from 'react'
44
import { type Movie } from '#app/movies-data.ts'
55

66
export function MovieTrailer({ movie }: { movie: Movie }) {
77
const [showTrailer, setShowTrailer] = useState(false)
8+
useAutoplay(showTrailer)
89

910
return (
1011
<div className="mb-6">
@@ -20,7 +21,6 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
2021
src={movie.videoUrl}
2122
title={movie.title}
2223
autoPlay
23-
muted
2424
loop
2525
controls
2626
/>
@@ -29,3 +29,20 @@ export function MovieTrailer({ movie }: { movie: Movie }) {
2929
</div>
3030
)
3131
}
32+
33+
function useAutoplay(showTrailer: boolean) {
34+
useEffect(() => {
35+
const video = document.querySelector('video')
36+
if (!(video instanceof HTMLVideoElement)) return
37+
if (!showTrailer) {
38+
void video.pause()
39+
return
40+
}
41+
42+
video.volume = 1
43+
void video.play()
44+
return () => {
45+
void video.pause()
46+
}
47+
}, [showTrailer])
48+
}

0 commit comments

Comments
 (0)