-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·199 lines (186 loc) · 6.51 KB
/
test.js
File metadata and controls
executable file
·199 lines (186 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use client'
import React, { useState, useEffect, useRef } from "react";
import { Inter } from "next/font/google";
import {
Play,
Pause,
SkipForward,
SkipBack,
Maximize2,
Minimize2,
Home,
Search,
Library,
User,
ChevronLeft,
ChevronRight,
} from "lucide-react";
//import "../globals.css";
const inter = Inter({ subsets: ["latin"] });
export default function App() {
const [tracks, setTracks] = useState([]);
const [currentTrack, setCurrentTrack] = useState(null);
const [isPlayerExpanded, setIsPlayerExpanded] = useState(false);
const [isPlaying, setIsPlaying] = useState(false);
const [progress, setProgress] = useState(0);
const audioRef = useRef(null);
const scrollContainerRef = useRef(null);
useEffect(() => {
fetchTracks();
}, []);
useEffect(() => {
if (audioRef.current) {
if (isPlaying) {
audioRef.current.play();
} else {
audioRef.current.pause();
}
}
}, [isPlaying]);
const fetchTracks = async () => {
try {
const response = await fetch("https://a51.1tv.ng/src/api/");
const data = await response.json();
setTracks(data);
} catch (error) {
console.error("Error fetching tracks:", error);
}
};
const updatePlayCount = async (trackId) => {
try {
await fetch("https://a51.1tv.ng/src/api/?path=play", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: trackId }),
});
} catch (error) {
console.error("Error updating play count:", error);
}
};
const handlePlayTrack = (track) => {
setCurrentTrack(track);
updatePlayCount(track.id);
setIsPlaying(true);
};
const handleTimeUpdate = () => {
if (audioRef.current) {
const progress =
(audioRef.current.currentTime / audioRef.current.duration) * 100;
setProgress(progress);
}
};
const handleProgressClick = (e) => {
const bounds = e.currentTarget.getBoundingClientRect();
const percent = (e.clientX - bounds.left) / bounds.width;
if (audioRef.current) {
audioRef.current.currentTime = percent * audioRef.current.duration;
}
};
const scroll = (direction) => {
const container = scrollContainerRef.current;
if (container) {
const scrollAmount =
direction === "left" ? -container.offsetWidth : container.offsetWidth;
container.scrollBy({ left: scrollAmount, behavior: "smooth" });
}
};
return (
<html lang="en">
<body className={`${inter.className} bg-gray-900 text-white`}>
<div className="min-h-screen pb-24">
<main className="px-4 py-6">
<h1 className="text-3xl font-bold mb-8">Discover</h1>
<section className="mb-8">
<h2 className="text-xl font-semibold mb-4">Top Played</h2>
<div className="space-y-4">
{tracks
.sort((a, b) => b.no_plays - a.no_plays)
.slice(0, 5)
.map((track, index) => (
<div
key={track.id}
className="flex items-center gap-4 p-2 hover:bg-gray-800 rounded-lg cursor-pointer"
onClick={() => handlePlayTrack(track)}
>
<span className="text-2xl font-bold text-gray-400 w-8">
{index + 1}
</span>
<img
src={track.image}
alt={track.track_name}
className="w-12 h-12 object-cover rounded"
/>
<div className="flex-1">
<h3 className="font-medium">{track.track_name}</h3>
<p className="text-sm text-gray-400">{track.actor}</p>
</div>
<span className="text-sm text-gray-400">
{track.no_plays.toLocaleString()} plays
</span>
</div>
))}
</div>
</section>
</main>
{currentTrack && (
<div
className={`fixed bottom-0 left-0 right-0 bg-gray-800 transition-all duration-300 ${
isPlayerExpanded ? "h-full" : "h-20"
}`}
>
<audio
ref={audioRef}
src={currentTrack.audio}
onTimeUpdate={handleTimeUpdate}
onEnded={() => setIsPlaying(false)}
/>
<div className="flex items-center h-full p-4">
<div className={`flex ${isPlayerExpanded ? "flex-col" : ""} w-full`}>
<h3 className="font-semibold">{currentTrack.track_name}</h3>
<button
className="p-3 bg-green-500 rounded-full"
onClick={() => setIsPlaying(!isPlaying)}
>
{isPlaying ? <Pause /> : <Play />}
</button>
<button onClick={() => setIsPlayerExpanded(!isPlayerExpanded)}>
{isPlayerExpanded ? <Minimize2 /> : <Maximize2 />}
</button>
</div>
</div>
<div
className="absolute bottom-0 left-0 right-0 h-1 bg-gray-700 cursor-pointer"
onClick={handleProgressClick}
>
<div
className="h-full bg-green-500"
style={{ width: `${progress}%` }}
/>
</div>
</div>
)}
<nav className="fixed bottom-0 left-0 right-0 bg-gray-800 border-t border-gray-700">
<div className="flex justify-around py-4">
<button className="flex flex-col items-center">
<Home className="w-6 h-6" />
<span className="text-xs mt-1">Home</span>
</button>
<button className="flex flex-col items-center">
<Search className="w-6 h-6" />
<span className="text-xs mt-1">Search</span>
</button>
<button className="flex flex-col items-center">
<Library className="w-6 h-6" />
<span className="text-xs mt-1">Library</span>
</button>
<button className="flex flex-col items-center">
<User className="w-6 h-6" />
<span className="text-xs mt-1">Profile</span>
</button>
</div>
</nav>
</div>
</body>
</html>
);
}