1
1
import React , { useCallback , useEffect , useRef , useState } from "react" ;
2
- import { MdOutlineArrowRight } from "react-icons/md" ;
3
2
import {
4
3
IoIosShareAlt ,
5
4
IoMdPlay as PlayIcon ,
@@ -16,18 +15,29 @@ import { BsThreeDotsVertical } from "react-icons/bs";
16
15
import { Slider } from "../ui/slider" ;
17
16
import { debounce } from "../../lib/utils" ;
18
17
19
- const Shorts = ( { channel, src, i, currentShortsIndex, description } ) => {
18
+ const Shorts = ( {
19
+ channel,
20
+ src,
21
+ i,
22
+ currentShortsIndex,
23
+ description,
24
+ isAudible,
25
+ setIsAudible,
26
+ } ) => {
20
27
const [ isPlaying , setIsPlaying ] = useState ( false ) ;
21
- const [ isAudible , setIsAudible ] = useState ( false ) ;
22
28
const [ currentTime , setCurrentTime ] = useState ( 0 ) ;
23
29
const [ isLiked , setIsLiked ] = useState ( false ) ;
24
30
const [ isDisliked , setIsDisliked ] = useState ( false ) ;
31
+ const [ isSubscribed , setIsSubscribed ] = useState ( false ) ;
25
32
26
33
const videoRef = useRef ( null ) ;
27
34
28
35
const handlePlayPause = ( action ) => {
29
36
try {
30
- if ( videoRef . current . paused || action === "play" ) {
37
+ if (
38
+ ( action !== "pause" && videoRef . current . paused ) ||
39
+ action === "play"
40
+ ) {
31
41
videoRef . current . play ( ) ;
32
42
setIsPlaying ( true ) ;
33
43
} else {
@@ -40,6 +50,20 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
40
50
} ;
41
51
const debouncedHandlePlayPause = debounce ( handlePlayPause , 300 ) ;
42
52
53
+ useEffect ( ( ) => {
54
+ try {
55
+ if ( ! videoRef . current ) return ;
56
+ if ( currentShortsIndex === i ) {
57
+ debouncedHandlePlayPause ( "play" ) ;
58
+ } else {
59
+ debouncedHandlePlayPause ( "pause" ) ;
60
+ videoRef . current . currentTime = 0 ;
61
+ }
62
+ } catch ( e ) {
63
+ console . log ( "video error" ) ;
64
+ } // eslint-disable-next-line
65
+ } , [ currentShortsIndex , i ] ) ;
66
+
43
67
const handleTimeUpdate = ( ) => {
44
68
setCurrentTime ( videoRef . current . currentTime ) ;
45
69
} ;
@@ -56,20 +80,6 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
56
80
[ currentShortsIndex , i , handlePlayPause ]
57
81
) ;
58
82
59
- useEffect ( ( ) => {
60
- try {
61
- if ( ! videoRef . current ) return ;
62
- if ( currentShortsIndex === i ) {
63
- debouncedHandlePlayPause ( "play" ) ;
64
- } else {
65
- debouncedHandlePlayPause ( "pause" ) ;
66
- videoRef . current . currentTime = 0 ;
67
- }
68
- } catch ( e ) {
69
- console . log ( "video error" ) ;
70
- } // eslint-disable-next-line
71
- } , [ currentShortsIndex , i ] ) ;
72
-
73
83
useEffect ( ( ) => {
74
84
const video = videoRef . current ;
75
85
if ( ! video ) return ;
@@ -81,6 +91,15 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
81
91
} ;
82
92
} , [ handleKeyDown ] ) ;
83
93
94
+ const handleLike = ( ) => {
95
+ if ( isDisliked ) setIsDisliked ( false ) ;
96
+ setIsLiked ( ( prev ) => ! prev ) ;
97
+ } ;
98
+ const handleDislike = ( ) => {
99
+ if ( isLiked ) setIsLiked ( false ) ;
100
+ setIsDisliked ( ( prev ) => ! prev ) ;
101
+ } ;
102
+
84
103
return (
85
104
< div className = "w-screen xs:w-full md:w-[80%] text-white h-[100svh] xs:h-full xs:rounded-md flex flex-col relative p-2 " >
86
105
< video
@@ -93,11 +112,13 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
93
112
/>
94
113
95
114
< div className = "flex justify-between z-10 " >
96
- { isPlaying ? (
97
- < PauseIcon className = "h-6 w-6" />
98
- ) : (
99
- < PlayIcon className = "h-6 w-6" />
100
- ) }
115
+ < button onClick = { handlePlayPause } >
116
+ { isPlaying ? (
117
+ < PauseIcon className = "h-6 w-6" />
118
+ ) : (
119
+ < PlayIcon className = "h-6 w-6" />
120
+ ) }
121
+ </ button >
101
122
< button onClick = { ( ) => setIsAudible ( ( prev ) => ! prev ) } >
102
123
{ isAudible ? (
103
124
< IoMdVolumeHigh className = "h-6 w-6" />
@@ -117,18 +138,17 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
117
138
}
118
139
/>
119
140
< p > { channel . name || "@EpicPopcornReviews" } </ p >
120
- < button className = "rounded-2xl px-2 py-0.5 bg-white" >
121
- < span className = "opacity-80 text-black" > Subscribe</ span >
141
+ < button
142
+ onClick = { ( ) => setIsSubscribed ( ( prev ) => ! prev ) }
143
+ className = "rounded-2xl px-2 py-0.5 bg-white"
144
+ >
145
+ < span className = "opacity-80 text-black" >
146
+ { isSubscribed ? "Subscribed" : "Subscribe" }
147
+ </ span >
122
148
</ button >
123
149
</ div >
124
- < div className = "flex items-center gap-2 w-full font-semibold" >
125
- < MdOutlineArrowRight />
126
- < p > { "MCU 4/32 - He is SUCCESSFUL in" } </ p >
127
- </ div >
128
- < div className = "flex w-full font-semibold h-10 text-wrap max-w-[80%] " >
129
- < p className = "whitespace-normal truncate" >
130
- { description || "" }
131
- </ p >
150
+ < div className = "flex w-[80%] xs:w-full font-semibold h-28 xs:h-10 text-wrap max-w-[80%] " >
151
+ < p className = "whitespace-normal truncate" > { description || "" } </ p >
132
152
</ div >
133
153
</ div >
134
154
< div className = "absolute left-0 bottom-0 px-[1%] w-full z-20" >
@@ -158,14 +178,14 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
158
178
< button >
159
179
< BiSolidCommentDetail className = "md:bg-gray-100 md:bg-opacity-30 rounded-full p-1.5 h-11 w-11 " />
160
180
</ button >
161
- < button onClick = { ( ) => setIsDisliked ( ( prev ) => ! prev ) } >
181
+ < button onClick = { handleDislike } >
162
182
< BiSolidDislike
163
183
className = { `md:bg-gray-100 md:bg-opacity-30 rounded-full h-11 w-11 p-1.5 ${
164
184
isDisliked ? "text-blue-400" : ""
165
185
} `}
166
186
/>
167
187
</ button >
168
- < button onClick = { ( ) => setIsLiked ( ( prev ) => ! prev ) } >
188
+ < button onClick = { handleLike } >
169
189
< BiSolidLike
170
190
className = { `${
171
191
isLiked ? "text-blue-400" : ""
0 commit comments