Skip to content

Commit 0ba7acb

Browse files
committed
handle channel subscribe, handled mute, auto play,like dislike functionalities more efficiently,
1 parent b84e9db commit 0ba7acb

File tree

4 files changed

+74
-49
lines changed

4 files changed

+74
-49
lines changed

src/App.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ const carouselSettings = {
1919
const videos = Object.values(data);
2020

2121
function App() {
22-
const sliderRef = useRef(null);
2322
const [currentShortsIndex, setCurrentShortsIndex] = useState(0);
23+
const [isAudible, setIsAudible] = useState(false);
24+
const sliderRef = useRef(null);
25+
2426

2527
const handleCarouselChange = (newIndex) => {
2628
setCurrentShortsIndex(newIndex);
2729
};
2830

2931
return (
30-
<div className="flex overflow-hidden h-[100svh] w-screen items-center justify-center bg-slate-500">
32+
<div className="flex overflow-hidden h-[100svh] w-screen items-center justify-center bg-black">
3133
<Carousel
3234
{...carouselSettings}
3335
ref={sliderRef}
@@ -40,6 +42,8 @@ function App() {
4042
<Shorts
4143
{...video}
4244
i={i}
45+
isAudible={isAudible}
46+
setIsAudible={setIsAudible}
4347
currentShortsIndex={currentShortsIndex}
4448
/>
4549
</div>

src/components/shorts/index.js

+55-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useCallback, useEffect, useRef, useState } from "react";
2-
import { MdOutlineArrowRight } from "react-icons/md";
32
import {
43
IoIosShareAlt,
54
IoMdPlay as PlayIcon,
@@ -16,18 +15,29 @@ import { BsThreeDotsVertical } from "react-icons/bs";
1615
import { Slider } from "../ui/slider";
1716
import { debounce } from "../../lib/utils";
1817

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+
}) => {
2027
const [isPlaying, setIsPlaying] = useState(false);
21-
const [isAudible, setIsAudible] = useState(false);
2228
const [currentTime, setCurrentTime] = useState(0);
2329
const [isLiked, setIsLiked] = useState(false);
2430
const [isDisliked, setIsDisliked] = useState(false);
31+
const [isSubscribed, setIsSubscribed] = useState(false);
2532

2633
const videoRef = useRef(null);
2734

2835
const handlePlayPause = (action) => {
2936
try {
30-
if (videoRef.current.paused || action === "play") {
37+
if (
38+
(action !== "pause" && videoRef.current.paused) ||
39+
action === "play"
40+
) {
3141
videoRef.current.play();
3242
setIsPlaying(true);
3343
} else {
@@ -40,6 +50,20 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
4050
};
4151
const debouncedHandlePlayPause = debounce(handlePlayPause, 300);
4252

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+
4367
const handleTimeUpdate = () => {
4468
setCurrentTime(videoRef.current.currentTime);
4569
};
@@ -56,20 +80,6 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
5680
[currentShortsIndex, i, handlePlayPause]
5781
);
5882

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-
7383
useEffect(() => {
7484
const video = videoRef.current;
7585
if (!video) return;
@@ -81,6 +91,15 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
8191
};
8292
}, [handleKeyDown]);
8393

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+
84103
return (
85104
<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 ">
86105
<video
@@ -93,11 +112,13 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
93112
/>
94113

95114
<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>
101122
<button onClick={() => setIsAudible((prev) => !prev)}>
102123
{isAudible ? (
103124
<IoMdVolumeHigh className="h-6 w-6" />
@@ -117,18 +138,17 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
117138
}
118139
/>
119140
<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>
122148
</button>
123149
</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>
132152
</div>
133153
</div>
134154
<div className="absolute left-0 bottom-0 px-[1%] w-full z-20">
@@ -158,14 +178,14 @@ const Shorts = ({ channel, src, i, currentShortsIndex, description }) => {
158178
<button>
159179
<BiSolidCommentDetail className="md:bg-gray-100 md:bg-opacity-30 rounded-full p-1.5 h-11 w-11 " />
160180
</button>
161-
<button onClick={() => setIsDisliked((prev) => !prev)}>
181+
<button onClick={handleDislike}>
162182
<BiSolidDislike
163183
className={`md:bg-gray-100 md:bg-opacity-30 rounded-full h-11 w-11 p-1.5 ${
164184
isDisliked ? "text-blue-400" : ""
165185
}`}
166186
/>
167187
</button>
168-
<button onClick={() => setIsLiked((prev) => !prev)}>
188+
<button onClick={handleLike}>
169189
<BiSolidLike
170190
className={`${
171191
isLiked ? "text-blue-400" : ""

src/data.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "Channel1",
66
"image": "https://cdn.pixabay.com/photo/2012/06/19/10/32/owl-50267_640.jpg"
77
},
8-
"description": "Experience the grace and elegance of birds soaring through the sky, capturing the beauty of flight in stunning detai"
8+
"description": "Experience the grace and elegance of birds soaring through the sky, capturing the beauty of flight in stunning detail."
99
},
1010
"2": {
1111
"src": "https://res.cloudinary.com/ddh0reqyx/video/upload/v1708847395/sample5_eyv44r.mp4",
@@ -31,12 +31,12 @@
3131
},
3232
"description": "Immerse yourself in the soothing sounds of crashing waves and gentle sea breeze for ultimate relaxation."
3333
},
34-
"5":{
35-
"src": "https://res.cloudinary.com/ddh0reqyx/video/upload/v1708848265/sample7_kpsxer.mp4",
34+
"5": {
35+
"src": "https://res.cloudinary.com/ddh0reqyx/video/upload/v1708851377/reedumqlhj0w4vixkw5m.mp4",
3636
"channel": {
37-
"name": "Channel4",
38-
"image": "https://cdn.pixabay.com/photo/2016/11/08/05/20/sunset-1807524_640.jpg"
37+
"name": "Channel5",
38+
"image": "https://cdn.pixabay.com/photo/2017/02/07/16/47/kingfisher-2046453_640.jpg"
3939
},
40-
"description": "Immerse yourself in the soothing sounds of crashing waves and gentle sea breeze for ultimate relaxation."
40+
"description": "Only this video has audio, try turning on audio"
4141
}
4242
}

tsconfig.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
2-
"compilerOptions": {
3-
"baseUrl": ".",
4-
"paths": {
5-
"@/*": ["./*"]
6-
}
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"@/*": [
6+
"./*"
7+
]
78
}
89
}
9-
10+
}

0 commit comments

Comments
 (0)