refactor: use saved lyrics
This commit is contained in:
3 files changed
+45
-21
No files matched your search
@@ -87,6 +87,7 @@
|
|||||||
position: relative;
|
position: relative;
|
||||||
margin-bottom: 2em;
|
margin-bottom: 2em;
|
||||||
border-radius: 20px;
|
border-radius: 20px;
|
||||||
|
overscroll-behavior: contain;
|
||||||
&-line {
|
&-line {
|
||||||
width: fit-content;
|
width: fit-content;
|
||||||
white-space: wrap;
|
white-space: wrap;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { useAudioPlayback } from "@/utils/store/audioPlayback";
|
|||||||
import useQueue, { setQueue } from "@/utils/store/queue";
|
import useQueue, { setQueue } from "@/utils/store/queue";
|
||||||
import { Fab, IconButton, Slider, Typography } from "@mui/material";
|
import { Fab, IconButton, Slider, Typography } from "@mui/material";
|
||||||
import { createFileRoute } from "@tanstack/react-router";
|
import { createFileRoute } from "@tanstack/react-router";
|
||||||
import React, { useEffect, useMemo, useRef, useState } from "react";
|
import React, { useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
import "./audio.scss";
|
import "./audio.scss";
|
||||||
import PlayNextButton from "@/components/buttons/playNextButton";
|
import PlayNextButton from "@/components/buttons/playNextButton";
|
||||||
@@ -49,11 +49,12 @@ function AudioPlayerRoute() {
|
|||||||
s.player.ref,
|
s.player.ref,
|
||||||
]);
|
]);
|
||||||
const [audioPlayer, setAudioPlayer] = useState<HTMLAudioElement | null>(
|
const [audioPlayer, setAudioPlayer] = useState<HTMLAudioElement | null>(
|
||||||
audioPlayerRef?.current,
|
audioPlayerRef?.current ? audioPlayerRef?.current : null,
|
||||||
);
|
);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log(audioPlayerRef);
|
if (audioPlayerRef?.current) {
|
||||||
setAudioPlayer(audioPlayerRef.current);
|
setAudioPlayer(audioPlayerRef?.current);
|
||||||
|
}
|
||||||
}, [audioPlayerRef?.current]);
|
}, [audioPlayerRef?.current]);
|
||||||
|
|
||||||
console.log(audioPlayer?.paused);
|
console.log(audioPlayer?.paused);
|
||||||
@@ -68,12 +69,16 @@ function AudioPlayerRoute() {
|
|||||||
|
|
||||||
const lyrics = useQuery({
|
const lyrics = useQuery({
|
||||||
queryKey: ["player", "audio", item?.Id, "lyrics"],
|
queryKey: ["player", "audio", item?.Id, "lyrics"],
|
||||||
queryFn: async () =>
|
queryFn: async () => {
|
||||||
(
|
if (!item?.Id || !api) {
|
||||||
await getLyricsApi(api).searchRemoteLyrics({
|
return null;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
await getLyricsApi(api).getLyrics({
|
||||||
itemId: item?.Id,
|
itemId: item?.Id,
|
||||||
})
|
})
|
||||||
).data,
|
).data;
|
||||||
|
},
|
||||||
enabled: Boolean(item?.Id),
|
enabled: Boolean(item?.Id),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -110,15 +115,31 @@ function AudioPlayerRoute() {
|
|||||||
const { active, over } = event;
|
const { active, over } = event;
|
||||||
if (active.id !== over?.id) {
|
if (active.id !== over?.id) {
|
||||||
const prevState = queue;
|
const prevState = queue;
|
||||||
const oldIndex = prevState?.map((item) => item.Id).indexOf(active.id);
|
const oldIndex = prevState
|
||||||
const newIndex = prevState?.map((item) => item.Id).indexOf(over.id);
|
?.map((item) => item.Id)
|
||||||
|
.indexOf(String(active.id));
|
||||||
|
const newIndex = prevState
|
||||||
|
?.map((item) => item.Id)
|
||||||
|
.indexOf(String(over?.id));
|
||||||
|
|
||||||
|
if (oldIndex === newIndex) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!oldIndex || !newIndex) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (oldIndex === -1 || newIndex === -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const newState = prevState
|
const newState = prevState
|
||||||
? arrayMove(prevState, oldIndex, newIndex)
|
? arrayMove(prevState, oldIndex, newIndex)
|
||||||
: prevState;
|
: prevState;
|
||||||
|
|
||||||
const currentTrack = newState?.map((item) => item.Id).indexOf(item?.Id);
|
const currentTrack = newState?.map((item) => item.Id).indexOf(item?.Id);
|
||||||
|
|
||||||
|
if (!currentTrack) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
setQueue(newState, currentTrack);
|
setQueue(newState, currentTrack);
|
||||||
// setQueue(newState);
|
// setQueue(newState);
|
||||||
}
|
}
|
||||||
@@ -139,13 +160,16 @@ function AudioPlayerRoute() {
|
|||||||
{item?.ImageTags?.Primary ? (
|
{item?.ImageTags?.Primary ? (
|
||||||
<img
|
<img
|
||||||
alt={item.Name ?? "Music"}
|
alt={item.Name ?? "Music"}
|
||||||
src={getImageUrlsApi(api).getItemImageUrlById(
|
src={
|
||||||
|
api &&
|
||||||
|
getImageUrlsApi(api).getItemImageUrlById(
|
||||||
item.Id ?? "",
|
item.Id ?? "",
|
||||||
"Primary",
|
"Primary",
|
||||||
{
|
{
|
||||||
tag: item.ImageTags.Primary,
|
tag: item.ImageTags.Primary,
|
||||||
},
|
},
|
||||||
)}
|
)
|
||||||
|
}
|
||||||
className="audio-info-image"
|
className="audio-info-image"
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
@@ -284,26 +308,24 @@ function AudioPlayerRoute() {
|
|||||||
{showLyrics && (
|
{showLyrics && (
|
||||||
<div
|
<div
|
||||||
className="audio-lyrics"
|
className="audio-lyrics"
|
||||||
data-has-synced-lyrics={Boolean(
|
data-has-synced-lyrics={Boolean(lyrics.data?.Metadata?.IsSynced)}
|
||||||
lyrics.data?.[0]?.Lyrics?.Metadata?.IsSynced,
|
|
||||||
)}
|
|
||||||
>
|
>
|
||||||
<div className="audio-lyrics-container" ref={lyricsContainer}>
|
<div className="audio-lyrics-container" ref={lyricsContainer}>
|
||||||
{lyrics.data?.[1]?.Lyrics?.Lyrics?.map((lyric, index) => (
|
{lyrics.data?.Lyrics?.map((lyric, index) => (
|
||||||
<div
|
<div
|
||||||
className="audio-lyrics-line"
|
className="audio-lyrics-line"
|
||||||
key={`${lyric.Text}${index}`}
|
key={`${lyric.Text}${index}`}
|
||||||
data-active-lyric={
|
data-active-lyric={
|
||||||
secToTicks(audioPlayer?.currentTime) >= (lyric.Start ?? 0) &&
|
secToTicks(audioPlayer?.currentTime) >= (lyric.Start ?? 0) &&
|
||||||
secToTicks(audioPlayer?.currentTime) <
|
secToTicks(audioPlayer?.currentTime) <
|
||||||
(lyrics.data?.[0]?.Lyrics?.Lyrics?.[index + 1]?.Start ?? 0)
|
(lyrics.data?.Lyrics?.[index + 1]?.Start ?? 0)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{lyric.Text}
|
{lyric.Text}
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
{!lyrics.data?.[0]?.Lyrics?.Metadata?.IsSynced && (
|
{!lyrics.data?.Metadata?.IsSynced && (
|
||||||
<Typography
|
<Typography
|
||||||
className="flex flex-align-center"
|
className="flex flex-align-center"
|
||||||
style={{
|
style={{
|
||||||
@@ -326,7 +348,7 @@ function AudioPlayerRoute() {
|
|||||||
onDragStart={(event) => {
|
onDragStart={(event) => {
|
||||||
const { active } = event;
|
const { active } = event;
|
||||||
setCurrentDraggingIndex(
|
setCurrentDraggingIndex(
|
||||||
queue?.map((item) => item.Id).indexOf(active.id),
|
queue?.map((item) => item.Id).indexOf(String(active.id) ?? "") ?? 0,
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
modifiers={[restrictToVerticalAxis]}
|
modifiers={[restrictToVerticalAxis]}
|
||||||
@@ -335,6 +357,7 @@ function AudioPlayerRoute() {
|
|||||||
<div className="audio-queue-container">
|
<div className="audio-queue-container">
|
||||||
<Typography variant="h5">Queue:</Typography>
|
<Typography variant="h5">Queue:</Typography>
|
||||||
<SortableContext
|
<SortableContext
|
||||||
|
//@ts-ignore
|
||||||
items={queue?.map((track) => track.Id)}
|
items={queue?.map((track) => track.Id)}
|
||||||
strategy={verticalListSortingStrategy}
|
strategy={verticalListSortingStrategy}
|
||||||
>
|
>
|
||||||
@@ -345,7 +368,7 @@ function AudioPlayerRoute() {
|
|||||||
</div>
|
</div>
|
||||||
</SortableContext>
|
</SortableContext>
|
||||||
<DragOverlay modifiers={[restrictToWindowEdges]}>
|
<DragOverlay modifiers={[restrictToWindowEdges]}>
|
||||||
{(currentDraggingIndex ?? -1) >= 0 && (
|
{currentDraggingIndex && currentDraggingIndex >= 0 && (
|
||||||
<div className="audio-queue-track dragging">
|
<div className="audio-queue-track dragging">
|
||||||
<span className="material-symbols-rounded">drag_handle</span>
|
<span className="material-symbols-rounded">drag_handle</span>
|
||||||
<div className="audio-queue-track-info">
|
<div className="audio-queue-track-info">
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type AudioPlaybackStore = {
|
|||||||
player: {
|
player: {
|
||||||
currentTick: number;
|
currentTick: number;
|
||||||
playing: boolean;
|
playing: boolean;
|
||||||
ref: React.Ref<HTMLAudioElement> | null;
|
ref: React.RefObject<HTMLAudioElement> | null;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
export const useAudioPlayback = createWithEqualityFn<AudioPlaybackStore>(
|
export const useAudioPlayback = createWithEqualityFn<AudioPlaybackStore>(
|
||||||
|
|||||||
Reference in new issue
Block a user