refactor(musik): integrate music player library functions and fix build errors
- Integrate togglePlayPause, getNextIndex, getPrevIndex, handleRepeatOrNext, seekTo, toggleShuffle, setAudioVolume, toggleMute library functions - Fix ESLint warnings: remove unused eslint-disable, add missing useEffect dependencies - Fix ESLint error in useMusicPlayer.ts togglePlayPause function - Add force-dynamic export to root layout to prevent prerendering errors - Improve seek slider with preview/commit functionality - Add isSeeking state to prevent UI flickering during seek Fixes: Build PageNotFoundError for admin/darmasaba pages Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -19,7 +19,6 @@ const nextConfig: NextConfig = {
|
||||
},
|
||||
];
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
export default nextConfig;
|
||||
|
||||
BIN
public/mp3-logo.png
Normal file
BIN
public/mp3-logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 82 KiB |
32
src/app/darmasaba/(pages)/musik/lib/nextPrev.ts
Normal file
32
src/app/darmasaba/(pages)/musik/lib/nextPrev.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
export function getNextIndex(
|
||||
currentIndex: number,
|
||||
total: number,
|
||||
isShuffle: boolean
|
||||
) {
|
||||
if (total === 0) return -1;
|
||||
|
||||
if (isShuffle) {
|
||||
return Math.floor(Math.random() * total);
|
||||
}
|
||||
|
||||
return (currentIndex + 1) % total;
|
||||
}
|
||||
|
||||
export function getPrevIndex(
|
||||
currentIndex: number,
|
||||
total: number,
|
||||
isShuffle: boolean
|
||||
) {
|
||||
if (total === 0) return -1;
|
||||
|
||||
if (isShuffle) {
|
||||
return Math.floor(Math.random() * total);
|
||||
}
|
||||
|
||||
return currentIndex - 1 < 0 ? total - 1 : currentIndex - 1;
|
||||
}
|
||||
|
||||
//pakai di ui
|
||||
|
||||
// const next = getNextIndex(currentSongIndex, filteredMusik.length, isShuffle);
|
||||
// playSong(next);
|
||||
24
src/app/darmasaba/(pages)/musik/lib/playPause.ts
Normal file
24
src/app/darmasaba/(pages)/musik/lib/playPause.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { RefObject } from "react";
|
||||
|
||||
export function togglePlayPause(
|
||||
audioRef: RefObject<HTMLAudioElement | null>,
|
||||
isPlaying: boolean,
|
||||
setIsPlaying: (v: boolean) => void
|
||||
) {
|
||||
if (!audioRef.current) return;
|
||||
|
||||
if (isPlaying) {
|
||||
audioRef.current.pause();
|
||||
setIsPlaying(false);
|
||||
} else {
|
||||
audioRef.current
|
||||
.play()
|
||||
.then(() => setIsPlaying(true))
|
||||
.catch(console.error);
|
||||
}
|
||||
}
|
||||
|
||||
// pakai di ui
|
||||
// onClick={() =>
|
||||
// togglePlayPause(audioRef, isPlaying, setIsPlaying)
|
||||
// }
|
||||
22
src/app/darmasaba/(pages)/musik/lib/repeat.ts
Normal file
22
src/app/darmasaba/(pages)/musik/lib/repeat.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { RefObject } from "react";
|
||||
|
||||
export function handleRepeatOrNext(
|
||||
audioRef: RefObject<HTMLAudioElement | null>,
|
||||
isRepeat: boolean,
|
||||
playNext: () => void
|
||||
) {
|
||||
if (!audioRef.current) return;
|
||||
|
||||
if (isRepeat) {
|
||||
audioRef.current.currentTime = 0;
|
||||
audioRef.current.play();
|
||||
} else {
|
||||
playNext();
|
||||
}
|
||||
}
|
||||
|
||||
//dipakai di ui
|
||||
|
||||
// onEnded={() =>
|
||||
// handleRepeatOrNext(audioRef, isRepeat, playNext)
|
||||
// }
|
||||
25
src/app/darmasaba/(pages)/musik/lib/seek.ts
Normal file
25
src/app/darmasaba/(pages)/musik/lib/seek.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export function seekTo(
|
||||
audioRef: React.RefObject<HTMLAudioElement | null>,
|
||||
time: number
|
||||
) {
|
||||
if (!audioRef.current) return;
|
||||
audioRef.current.currentTime = time;
|
||||
}
|
||||
|
||||
|
||||
// import { RefObject } from "react";
|
||||
|
||||
// export function seekTo(
|
||||
// audioRef: RefObject<HTMLAudioElement | null>,
|
||||
// time: number,
|
||||
// setCurrentTime: (v: number) => void
|
||||
// ) {
|
||||
// if (!audioRef.current) return;
|
||||
|
||||
// audioRef.current.currentTime = time;
|
||||
// setCurrentTime(time);
|
||||
// }
|
||||
|
||||
// //pakai di ui
|
||||
|
||||
// // onChange={(v) => seekTo(audioRef, v, setCurrentTime)}
|
||||
6
src/app/darmasaba/(pages)/musik/lib/shuffle.ts
Normal file
6
src/app/darmasaba/(pages)/musik/lib/shuffle.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export function toggleShuffle(
|
||||
isShuffle: boolean,
|
||||
setIsShuffle: (v: boolean) => void
|
||||
) {
|
||||
setIsShuffle(!isShuffle);
|
||||
}
|
||||
29
src/app/darmasaba/(pages)/musik/lib/volume.ts
Normal file
29
src/app/darmasaba/(pages)/musik/lib/volume.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { RefObject } from "react";
|
||||
|
||||
export function setAudioVolume(
|
||||
audioRef: RefObject<HTMLAudioElement | null>,
|
||||
volume: number,
|
||||
setVolume: (v: number) => void,
|
||||
setIsMuted: (v: boolean) => void
|
||||
) {
|
||||
if (!audioRef.current) return;
|
||||
|
||||
audioRef.current.volume = volume / 100;
|
||||
setVolume(volume);
|
||||
|
||||
if (volume > 0) {
|
||||
setIsMuted(false);
|
||||
}
|
||||
}
|
||||
|
||||
export function toggleMute(
|
||||
audioRef: RefObject<HTMLAudioElement | null>,
|
||||
isMuted: boolean,
|
||||
setIsMuted: (v: boolean) => void
|
||||
) {
|
||||
if (!audioRef.current) return;
|
||||
|
||||
const muted = !isMuted;
|
||||
audioRef.current.muted = muted;
|
||||
setIsMuted(muted);
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
'use client'
|
||||
import { ActionIcon, Avatar, Badge, Box, Card, Flex, Grid, Group, Paper, ScrollArea, Slider, Stack, Text, TextInput } from '@mantine/core';
|
||||
import { IconArrowsShuffle, IconPlayerPauseFilled, IconPlayerPlayFilled, IconPlayerSkipBackFilled, IconPlayerSkipForwardFilled, IconRepeat, IconRepeatOff, IconSearch, IconVolume, IconVolumeOff, IconX } from '@tabler/icons-react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import BackButton from '../../desa/layanan/_com/BackButto';
|
||||
import { togglePlayPause } from '../lib/playPause';
|
||||
import { getNextIndex, getPrevIndex } from '../lib/nextPrev';
|
||||
import { handleRepeatOrNext } from '../lib/repeat';
|
||||
import { seekTo } from '../lib/seek';
|
||||
import { toggleShuffle } from '../lib/shuffle';
|
||||
import { setAudioVolume, toggleMute as toggleMuteUtil } from '../lib/volume';
|
||||
|
||||
interface MusicFile {
|
||||
id: string;
|
||||
@@ -39,9 +44,8 @@ const MusicPlayer = () => {
|
||||
const [musikData, setMusikData] = useState<Musik[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [currentSongIndex, setCurrentSongIndex] = useState(-1);
|
||||
|
||||
const [isSeeking, setIsSeeking] = useState(false);
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||
const progressInterval = useRef<number | null>(null);
|
||||
|
||||
// Fetch musik data from API
|
||||
useEffect(() => {
|
||||
@@ -71,30 +75,30 @@ const MusicPlayer = () => {
|
||||
(musik.genre && musik.genre.toLowerCase().includes(search.toLowerCase()))
|
||||
);
|
||||
|
||||
const currentSong = currentSongIndex >= 0 && currentSongIndex < filteredMusik.length
|
||||
? filteredMusik[currentSongIndex]
|
||||
const currentSong = currentSongIndex >= 0 && currentSongIndex < filteredMusik.length
|
||||
? filteredMusik[currentSongIndex]
|
||||
: null;
|
||||
|
||||
// Update progress bar
|
||||
useEffect(() => {
|
||||
if (isPlaying && audioRef.current) {
|
||||
progressInterval.current = window.setInterval(() => {
|
||||
if (audioRef.current) {
|
||||
setCurrentTime(Math.floor(audioRef.current.currentTime));
|
||||
}
|
||||
}, 1000);
|
||||
} else {
|
||||
if (progressInterval.current) {
|
||||
clearInterval(progressInterval.current);
|
||||
}
|
||||
}
|
||||
// // Update progress bar
|
||||
// useEffect(() => {
|
||||
// if (isPlaying && audioRef.current) {
|
||||
// progressInterval.current = window.setInterval(() => {
|
||||
// if (audioRef.current) {
|
||||
// setCurrentTime(Math.floor(audioRef.current.currentTime));
|
||||
// }
|
||||
// }, 1000);
|
||||
// } else {
|
||||
// if (progressInterval.current) {
|
||||
// clearInterval(progressInterval.current);
|
||||
// }
|
||||
// }
|
||||
|
||||
return () => {
|
||||
if (progressInterval.current) {
|
||||
clearInterval(progressInterval.current);
|
||||
}
|
||||
};
|
||||
}, [isPlaying]);
|
||||
// return () => {
|
||||
// if (progressInterval.current) {
|
||||
// clearInterval(progressInterval.current);
|
||||
// }
|
||||
// };
|
||||
// }, [isPlaying]);
|
||||
|
||||
// Update duration when song changes
|
||||
useEffect(() => {
|
||||
@@ -110,7 +114,7 @@ const MusicPlayer = () => {
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [currentSongIndex]);
|
||||
}, [currentSongIndex, currentSong, isPlaying]);
|
||||
|
||||
const formatTime = (seconds: number) => {
|
||||
const mins = Math.floor(seconds / 60);
|
||||
@@ -120,84 +124,64 @@ const MusicPlayer = () => {
|
||||
|
||||
const playSong = (index: number) => {
|
||||
if (index < 0 || index >= filteredMusik.length) return;
|
||||
|
||||
|
||||
setCurrentSongIndex(index);
|
||||
setIsPlaying(true);
|
||||
};
|
||||
|
||||
const handleSongEnd = () => {
|
||||
if (isRepeat) {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.currentTime = 0;
|
||||
audioRef.current.play();
|
||||
}
|
||||
} else {
|
||||
// Play next song
|
||||
const playNext = () => {
|
||||
let nextIndex: number;
|
||||
if (isShuffle) {
|
||||
nextIndex = Math.floor(Math.random() * filteredMusik.length);
|
||||
} else {
|
||||
nextIndex = (currentSongIndex + 1) % filteredMusik.length;
|
||||
}
|
||||
|
||||
|
||||
if (filteredMusik.length > 1) {
|
||||
playSong(nextIndex);
|
||||
} else {
|
||||
setIsPlaying(false);
|
||||
setCurrentTime(0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
handleRepeatOrNext(audioRef, isRepeat, playNext);
|
||||
};
|
||||
|
||||
const handleSeek = (value: number) => {
|
||||
setCurrentTime(value);
|
||||
if (audioRef.current) {
|
||||
audioRef.current.currentTime = value;
|
||||
}
|
||||
seekTo(audioRef, value);
|
||||
};
|
||||
|
||||
const toggleMute = () => {
|
||||
const newMuted = !isMuted;
|
||||
setIsMuted(newMuted);
|
||||
if (audioRef.current) {
|
||||
audioRef.current.muted = newMuted;
|
||||
}
|
||||
toggleMuteUtil(audioRef, isMuted, setIsMuted);
|
||||
};
|
||||
|
||||
const handleVolumeChange = (val: number) => {
|
||||
setVolume(val);
|
||||
if (audioRef.current) {
|
||||
audioRef.current.volume = val / 100;
|
||||
}
|
||||
if (val > 0 && isMuted) {
|
||||
setIsMuted(false);
|
||||
}
|
||||
setAudioVolume(audioRef, val, setVolume, setIsMuted);
|
||||
};
|
||||
|
||||
const skipBack = () => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.currentTime = Math.max(0, audioRef.current.currentTime - 10);
|
||||
const prevIndex = getPrevIndex(currentSongIndex, filteredMusik.length, isShuffle);
|
||||
if (prevIndex >= 0) {
|
||||
playSong(prevIndex);
|
||||
}
|
||||
};
|
||||
|
||||
const skipForward = () => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.currentTime = Math.min(duration, audioRef.current.currentTime + 10);
|
||||
const nextIndex = getNextIndex(currentSongIndex, filteredMusik.length, isShuffle);
|
||||
if (nextIndex >= 0) {
|
||||
playSong(nextIndex);
|
||||
}
|
||||
};
|
||||
|
||||
const togglePlayPause = () => {
|
||||
const toggleShuffleHandler = () => {
|
||||
toggleShuffle(isShuffle, setIsShuffle);
|
||||
};
|
||||
|
||||
const togglePlayPauseHandler = () => {
|
||||
if (!currentSong) return;
|
||||
|
||||
if (isPlaying) {
|
||||
audioRef.current?.pause();
|
||||
setIsPlaying(false);
|
||||
} else {
|
||||
audioRef.current?.play().catch(err => {
|
||||
console.error('Error playing audio:', err);
|
||||
});
|
||||
setIsPlaying(true);
|
||||
}
|
||||
togglePlayPause(audioRef, isPlaying, setIsPlaying);
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
@@ -217,13 +201,16 @@ const MusicPlayer = () => {
|
||||
<audio
|
||||
ref={audioRef}
|
||||
src={currentSong.audioFile.link}
|
||||
onEnded={handleSongEnd}
|
||||
onLoadedMetadata={() => {
|
||||
if (audioRef.current) {
|
||||
setDuration(Math.floor(audioRef.current.duration));
|
||||
}
|
||||
}}
|
||||
muted={isMuted}
|
||||
onLoadedMetadata={() => {
|
||||
if (!audioRef.current) return;
|
||||
setDuration(Math.floor(audioRef.current.duration));
|
||||
}}
|
||||
onTimeUpdate={() => {
|
||||
if (!audioRef.current || isSeeking) return;
|
||||
setCurrentTime(Math.floor(audioRef.current.currentTime));
|
||||
}}
|
||||
onEnded={handleSongEnd}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -262,10 +249,10 @@ const MusicPlayer = () => {
|
||||
{currentSong ? (
|
||||
<Card radius="md" p="xl" shadow="md">
|
||||
<Group align="center" gap="xl">
|
||||
<Avatar
|
||||
src={currentSong.coverImage?.link || 'https://images.unsplash.com/photo-1470225620780-dba8ba36b745?w=400&h=400&fit=crop'}
|
||||
size={180}
|
||||
radius="md"
|
||||
<Avatar
|
||||
src={currentSong.coverImage?.link || 'https://images.unsplash.com/photo-1470225620780-dba8ba36b745?w=400&h=400&fit=crop'}
|
||||
size={180}
|
||||
radius="md"
|
||||
/>
|
||||
<Stack gap="md" style={{ flex: 1 }}>
|
||||
<div>
|
||||
@@ -319,10 +306,10 @@ const MusicPlayer = () => {
|
||||
onClick={() => playSong(index)}
|
||||
>
|
||||
<Group gap="md" align="center">
|
||||
<Avatar
|
||||
src={song.coverImage?.link || 'https://images.unsplash.com/photo-1470225620780-dba8ba36b745?w=400&h=400&fit=crop'}
|
||||
size={64}
|
||||
radius="md"
|
||||
<Avatar
|
||||
src={song.coverImage?.link || 'https://images.unsplash.com/photo-1470225620780-dba8ba36b745?w=400&h=400&fit=crop'}
|
||||
size={64}
|
||||
radius="md"
|
||||
/>
|
||||
<Stack gap={4} style={{ flex: 1, minWidth: 0 }}>
|
||||
<Text size="sm" fw={600} c="#0B4F78" truncate>{song.judul}</Text>
|
||||
@@ -359,10 +346,10 @@ const MusicPlayer = () => {
|
||||
>
|
||||
<Flex align="center" justify="space-between" gap="xl" h="100%">
|
||||
<Group gap="md" style={{ flex: 1 }}>
|
||||
<Avatar
|
||||
src={currentSong?.coverImage?.link || 'https://images.unsplash.com/photo-1470225620780-dba8ba36b745?w=400&h=400&fit=crop'}
|
||||
size={56}
|
||||
radius="md"
|
||||
<Avatar
|
||||
src={currentSong?.coverImage?.link || 'https://images.unsplash.com/photo-1470225620780-dba8ba36b745?w=400&h=400&fit=crop'}
|
||||
size={56}
|
||||
radius="md"
|
||||
/>
|
||||
<div style={{ flex: 1, minWidth: 0 }}>
|
||||
{currentSong ? (
|
||||
@@ -381,7 +368,7 @@ const MusicPlayer = () => {
|
||||
<ActionIcon
|
||||
variant={isShuffle ? 'filled' : 'subtle'}
|
||||
color="#0B4F78"
|
||||
onClick={() => setIsShuffle(!isShuffle)}
|
||||
onClick={toggleShuffleHandler}
|
||||
radius="xl"
|
||||
>
|
||||
{isShuffle ? <IconArrowsShuffle size={18} /> : <IconX size={18} />}
|
||||
@@ -394,7 +381,7 @@ const MusicPlayer = () => {
|
||||
color="#0B4F78"
|
||||
size={56}
|
||||
radius="xl"
|
||||
onClick={togglePlayPause}
|
||||
onClick={togglePlayPauseHandler}
|
||||
>
|
||||
{isPlaying ? <IconPlayerPauseFilled size={26} /> : <IconPlayerPlayFilled size={26} />}
|
||||
</ActionIcon>
|
||||
@@ -415,7 +402,14 @@ const MusicPlayer = () => {
|
||||
<Slider
|
||||
value={currentTime}
|
||||
max={duration}
|
||||
onChange={handleSeek}
|
||||
onChange={(v) => {
|
||||
setIsSeeking(true);
|
||||
setCurrentTime(v); // preview
|
||||
}}
|
||||
onChangeEnd={(v) => {
|
||||
seekTo(audioRef, v); // commit
|
||||
setIsSeeking(false);
|
||||
}}
|
||||
color="#0B4F78"
|
||||
size="xs"
|
||||
style={{ flex: 1 }}
|
||||
|
||||
@@ -12,6 +12,9 @@ import { Metadata, Viewport } from "next";
|
||||
import { ViewTransitions } from "next-view-transitions";
|
||||
import { ToastContainer } from "react-toastify";
|
||||
|
||||
// Force dynamic rendering untuk menghindari error prerendering
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
// ✅ Pisahkan viewport ke export tersendiri
|
||||
export const viewport: Viewport = {
|
||||
width: "device-width",
|
||||
|
||||
Reference in New Issue
Block a user