feat: add new audioPlayer route and allow reordering queue

This commit is contained in:
Prayag Prajapati committed 2024-07-31 11:41:44 +05:30
1 parent d824823624
commit 7a2e4ae595
17 files changed
+629 -219

No files matched your search

-3
View File
@@ -19,8 +19,6 @@ import { useQuery, useQueryClient } from "@tanstack/react-query";
import { delUser } from "@/utils/storage/user";
import "./appBar.scss";
import { EventEmitter as event } from "@/eventEmitter";
import { getTypeIcon } from "../utils/iconsCollection";
import { useApiInContext } from "@/utils/store/api";
@@ -121,7 +119,6 @@ export const AppBar = () => {
await api.logout();
delUser();
sessionStorage.removeItem("accessToken");
event.emit("create-jellyfin-api", api.basePath);
queryClient.clear();
setAnchorEl(null);
navigate({ to: "/login" });
+1 -1
View File
@@ -86,7 +86,7 @@ const QueueButton = () => {
>
<Typography variant="subtitle2">
{item.Type === "Audio"
? item.IndexNumber ?? 0
? index + 1
: item.IndexNumberEnd
? `S${item.ParentIndexNumber}:E${item.IndexNumber} / ${item.IndexNumberEnd}`
: `S${item.ParentIndexNumber}:E${item.IndexNumber}`}
@@ -93,7 +93,7 @@ const AudioPlayer = () => {
});
setAudioRef(audioRef);
}
}, [url, currentTrack]);
}, [url, tracks[currentTrack]?.Id]);
const navigate = useNavigate();
@@ -288,6 +288,7 @@ const AudioPlayer = () => {
onTimeUpdate={(e) =>
setProgress(secToTicks(e.currentTarget.currentTime))
}
id="audio-player"
/>
<div className="audio-player-controls">
{controls}
@@ -317,7 +318,6 @@ const AudioPlayer = () => {
onChange={(e, value) => {
setIsScrubbing(true);
setSliderProgress(value);
console.log(value);
}}
onChangeCommitted={(e, value) => {
setIsScrubbing(false);
+114
View File
@@ -0,0 +1,114 @@
import { getRuntimeMusic } from "@/utils/date/time";
import useQueue, { setQueue } from "@/utils/store/queue";
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
import { Typography } from "@mui/material";
import React from "react";
import { useCallback, useRef } from "react";
import { type XYCoord, useDrag, useDrop } from "react-dnd";
import type { Identifier } from "typescript";
type Props = {
track: BaseItemDto;
index: number;
};
const QueueTrack = (props: Props) => {
const { track, index } = props;
const [queue, currentItemIndex] = useQueue((s) => [
s.tracks,
s.currentItemIndex,
]);
const trackRef = useRef<HTMLDivElement | null>(null);
const handleQueueUpdate = useCallback(
(dragIndex: number, dropIndex: number) => {
const prevState = queue;
prevState.splice(dropIndex, 0, prevState.splice(dragIndex, 1)[0]);
setQueue(prevState, dropIndex);
},
[queue],
);
const [{ handlerId }, drop] = useDrop<
{
index: number;
id: string;
type: string;
},
void,
{ handlerId: Identifier | null }
>({
accept: "Row",
collect(monitor) {
return {
handlerId: monitor.getHandlerId(),
};
},
hover(hoverItem, monitor) {
console.log(hoverItem);
if (!trackRef.current) return;
const dragIndex = hoverItem.index;
const dropIndex = index;
if (dragIndex === dropIndex) return;
const hoverBoundingRect = trackRef.current?.getBoundingClientRect();
const hoverMiddleY =
(hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
const clientOffset = monitor.getClientOffset();
const hoverClientY = (clientOffset as XYCoord).y - hoverBoundingRect.top;
if (dragIndex < dropIndex && hoverClientY < hoverMiddleY) {
return;
}
if (dragIndex > dropIndex && hoverClientY > hoverMiddleY) {
return;
}
handleQueueUpdate(dragIndex, dropIndex);
hoverItem.index = dropIndex;
},
});
const [{ isDragging }, drag] = useDrag({
type: "Row",
item: () => {
const id = track.Id;
return { id, index };
},
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),
});
drag(drop(trackRef));
return (
<div
ref={trackRef}
style={{ opacity: isDragging ? 0.5 : 1 }}
className={
currentItemIndex === index
? "audio-queue-track active"
: "audio-queue-track"
}
>
<span className="material-symbols-rounded">drag_handle</span>
<div className="audio-queue-track-info">
<Typography className="audio-queue-track-info-name">
{track.Name}
</Typography>
<Typography fontWeight={300} className="opacity-07" variant="subtitle2">
{track.Artists?.join(", ")}
</Typography>
</div>
<Typography className="opacity-07">
{getRuntimeMusic(track.RunTimeTicks)}
</Typography>
</div>
);
};
export default QueueTrack;
-3
View File
@@ -1,3 +0,0 @@
import { EventEmitter as eventEmitter } from "events";
export const EventEmitter = new eventEmitter();
+170 -164
View File
@@ -46,6 +46,8 @@ import "material-symbols/rounded.scss";
import type { Api, Jellyfin } from "@jellyfin/sdk";
import type { UserDto } from "@jellyfin/sdk/lib/generated-client";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
type ApiContext = {
api: Api;
@@ -94,175 +96,179 @@ export const Route = createRootRouteWithContext<ApiContext>()({
setBackdropLoading(true);
}, [backdropId]);
return (
<Suspense fallback={<h1>Loading...</h1>}>
<ThemeProvider theme={theme}>
<SnackbarProvider maxSnack={5}>
<CssBaseline />
<ScrollRestoration />
<Settings />
<EasterEgg />
<NProgress
key={
isQueryFetching || isMutating || routeIsLoading
? `show${(isQueryFetching ?? 1) * (isMutating ?? 1) * (routeIsLoading ?? 1)}`
: "hide"
}
isAnimating={isQueryFetching || isMutating || routeIsLoading}
/>
<DndProvider backend={HTML5Backend}>
<Suspense fallback={<h1>Loading...</h1>}>
<ThemeProvider theme={theme}>
<SnackbarProvider maxSnack={5}>
<CssBaseline />
<ScrollRestoration />
<Settings />
<EasterEgg />
<NProgress
key={
isQueryFetching || isMutating || routeIsLoading
? `show${(isQueryFetching ?? 1) * (isMutating ?? 1) * (routeIsLoading ?? 1)}`
: "hide"
}
isAnimating={isQueryFetching || isMutating || routeIsLoading}
/>
{routeIsLoading && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
style={{
width: "100vw",
height: "100vh",
display: "flex",
justifyContent: "center",
alignItems: "center",
background: "rgb(0 0 0 / 0.35)",
position: "fixed",
zIndex: 10000,
{routeIsLoading && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
style={{
width: "100vw",
height: "100vh",
display: "flex",
justifyContent: "center",
alignItems: "center",
background: "rgb(0 0 0 / 0.35)",
position: "fixed",
zIndex: 10000,
}}
>
<CircularProgress size={72} thickness={2} />
</motion.div>
)}
<Dialog
open={updateDialog}
fullWidth
PaperProps={{
style: {
borderRadius: "14px",
background: "rgb(0 0 0 / 0.5)",
backdropFilter: "blur(10px)",
border: "1px solid rgb(255 255 255 / 0.2)",
},
}}
sx={{
"& img": {
width: "100%",
},
}}
>
<CircularProgress size={72} thickness={2} />
</motion.div>
)}
<Dialog
open={updateDialog}
fullWidth
PaperProps={{
style: {
borderRadius: "14px",
background: "rgb(0 0 0 / 0.5)",
backdropFilter: "blur(10px)",
border: "1px solid rgb(255 255 255 / 0.2)",
},
}}
sx={{
"& img": {
width: "100%",
},
}}
>
{updateInfo !== undefined && (
<>
<DialogTitle
variant="h5"
style={{
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
gap: "0.4em",
}}
>
Update Available!
<Chip
icon={
<span className="material-symbols-rounded">update</span>
}
label={`v${updateInfo.version}`}
color="success"
/>
</DialogTitle>
<DialogContent dividers>
<DialogContentText>
<Markdown remarkPlugins={[remarkGfm]}>
{updateInfo.body}
</Markdown>
</DialogContentText>
</DialogContent>
<DialogActions
style={{
padding: "1em",
}}
>
<IconButton
disabled={updateDialogButton}
target="_blank"
href={`https://github.com/prayag17/JellyPlayer/releases/${updateInfo.version}`}
>
<SvgIcon>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
viewBox="0 0 16 16"
>
<title>Update</title>
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8" />
</svg>
</SvgIcon>
</IconButton>
<Button
size="large"
color="error"
variant="outlined"
disabled={updateDialogButton}
onClick={() => setUpdateDialog(false)}
>
close
</Button>
<LoadingButton
size="large"
color="success"
variant="contained"
loading={updateDialogButton}
loadingIndicator="Updating..."
onClick={async () => {
setUpdateDialogButton(true);
await updateInfo.downloadAndInstall();
enqueueSnackbar(
"Update has been installed! You need to relaunch JellyPlayer.",
{
variant: "success",
},
);
await relaunch();
{updateInfo !== undefined && (
<>
<DialogTitle
variant="h5"
style={{
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
gap: "0.4em",
}}
>
Update
</LoadingButton>
</DialogActions>
</>
)}
</Dialog>
<div className="app-backdrop-container">
<AnimatePresence>
<motion.img
key={backdropId}
src={backdropUrl}
alt=""
className="app-backdrop"
initial={{
opacity: 0,
}}
animate={{
opacity: backdropLoading ? 0 : 1,
}}
exit={{
opacity: 0,
}}
transition={{
opacity: {
duration: 1.2,
},
}}
onLoad={() => setBackdropLoading(false)}
loading="lazy"
/>
</AnimatePresence>
</div>
<AppBar />
<AudioPlayer />
<Outlet />
<ReactQueryDevtools />
{/* <TanStackRouterDevtools /> */}
</SnackbarProvider>
</ThemeProvider>
</Suspense>
Update Available!
<Chip
icon={
<span className="material-symbols-rounded">
update
</span>
}
label={`v${updateInfo.version}`}
color="success"
/>
</DialogTitle>
<DialogContent dividers>
<DialogContentText>
<Markdown remarkPlugins={[remarkGfm]}>
{updateInfo.body}
</Markdown>
</DialogContentText>
</DialogContent>
<DialogActions
style={{
padding: "1em",
}}
>
<IconButton
disabled={updateDialogButton}
target="_blank"
href={`https://github.com/prayag17/JellyPlayer/releases/${updateInfo.version}`}
>
<SvgIcon>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
viewBox="0 0 16 16"
>
<title>Update</title>
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8" />
</svg>
</SvgIcon>
</IconButton>
<Button
size="large"
color="error"
variant="outlined"
disabled={updateDialogButton}
onClick={() => setUpdateDialog(false)}
>
close
</Button>
<LoadingButton
size="large"
color="success"
variant="contained"
loading={updateDialogButton}
loadingIndicator="Updating..."
onClick={async () => {
setUpdateDialogButton(true);
await updateInfo.downloadAndInstall();
enqueueSnackbar(
"Update has been installed! You need to relaunch JellyPlayer.",
{
variant: "success",
},
);
await relaunch();
}}
>
Update
</LoadingButton>
</DialogActions>
</>
)}
</Dialog>
<div className="app-backdrop-container">
<AnimatePresence>
<motion.img
key={backdropId}
src={backdropUrl}
alt=""
className="app-backdrop"
initial={{
opacity: 0,
}}
animate={{
opacity: backdropLoading ? 0 : 1,
}}
exit={{
opacity: 0,
}}
transition={{
opacity: {
duration: 1.2,
},
}}
onLoad={() => setBackdropLoading(false)}
loading="lazy"
/>
</AnimatePresence>
</div>
<AppBar />
<AudioPlayer />
<Outlet />
<ReactQueryDevtools />
{/* <TanStackRouterDevtools /> */}
</SnackbarProvider>
</ThemeProvider>
</Suspense>
</DndProvider>
);
},
});
+2 -6
View File
@@ -250,7 +250,7 @@ function MusicAlbumTitlePage() {
<Typography variant="subtitle1">Title</Typography>
<Typography variant="subtitle1">Duration</Typography>
</div>
{musicTracks.data?.Items?.map((track) => (
{musicTracks.data?.Items?.map((track, index) => (
<div
className={
currentPlayingItem?.Id === track.Id
@@ -259,11 +259,7 @@ function MusicAlbumTitlePage() {
}
key={track.Id}
onClick={() =>
handlePlayback(
track.IndexNumber,
track,
musicTracks.data.Items,
)
handlePlayback(index, track, musicTracks.data.Items)
}
>
<div className="index-container">
-3
View File
@@ -97,9 +97,6 @@ $border-radius: 20px;
flex-direction: column;
gap: 0.1em
}
&:nth-last-of-type(even):not(.header) {
background: rgb(0 0 0 / 0.2);
}
&:hover {
background: rgb(255 255 255 / 0.1) !important;
.index-container {
+54 -15
View File
@@ -1,21 +1,60 @@
.audio-info {
&-container {
display: flex;
gap: 1em;
align-items: center;
justify-content: flex-start;
}
&-image {
.audio {
&-info {
width: 100%;
aspect-ratio: 1;
height: 100%;
object-fit: cover;
padding: 4em;
gap: 0.5em;
display: flex;
flex-direction: column;
&-container {
width: 25em;
display: flex;
gap: 0em;
align-items: center;
justify-content: flex-start;
}
&-image {
width: 100%;
aspect-ratio: 1;
position: relative;
border-radius: $border-radius-default;
overflow: hidden;
height: 100%;
object-fit: cover;
&-container {
width: 25em;
aspect-ratio: 1;
position: relative;
border-radius: $border-radius-default;
overflow: hidden;
}
}
&-controls {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
gap: 1em;
}
}
&-queue{
display: flex;
flex-direction: column;
width: 100%;
align-items: center;
gap: 1em;
margin-top: 2em;
&-track{
display: grid;
grid-template-columns: 3em 1fr 6em;
align-items: center;
justify-content: center;
width: 100%;
gap: 1em;
&-info-name{
transition: $transition-time-fast;
}
&.active{
.audio-queue-track-info-name{
color: $clr-accent-light;
font-weight: 500;
}
}
}
}
}
+112 -10
View File
@@ -1,25 +1,45 @@
import { useApiInContext } from "@/utils/store/api";
import { useAudioPlayback } from "@/utils/store/audioPlayback";
import useQueue from "@/utils/store/queue";
import { Typography } from "@mui/material";
import { Fab, IconButton, Slider, Typography } from "@mui/material";
import { createFileRoute } from "@tanstack/react-router";
import React from "react";
import React, { useCallback, useEffect, useRef, useState } from "react";
import "./audio.scss";
import LikeButton from "@/components/buttons/likeButton";
import PlayNextButton from "@/components/buttons/playNextButton";
import PlayPreviousButton from "@/components/buttons/playPreviousButtom";
import QueueTrack from "@/components/queueTrack";
import { getRuntimeMusic, secToTicks, ticksToSec } from "@/utils/date/time";
import { useDrop } from "react-dnd";
import { Identifier } from "typescript";
export const Route = createFileRoute("/_api/player/audio")({
component: AudioPlayerRoute,
});
const SEEK_AMOUNT = 10; // seconds to skip on fast_rewind or fast_forward button click
function AudioPlayerRoute() {
const item = useAudioPlayback((s) => s.item);
const api = useApiInContext((s) => s.api);
const [queue, currentTrack] = useQueue((s) => [
s.currentItemIndex,
s.currentItemIndex,
]);
const [queue, currentTrack] = useQueue((s) => [s.tracks, s.currentItemIndex]);
const [item] = useAudioPlayback((s) => [s.item]);
const audioPlayer: HTMLAudioElement | null =
document.querySelector("#audio-player");
const [isScrubbing, setIsScrubbing] = useState(false);
const [sliderProgress, setSliderProgress] = useState<number | number[]>(0); // this state hold the slider scrubbing value allowing user to scrub track without changing current time
const [progress, setProgress] = useState<number | number[]>(
audioPlayer?.currentTime ?? 0,
);
useEffect(() => {
audioPlayer?.addEventListener("timeupdate", () => {
setProgress(audioPlayer.currentTime);
});
}, [item?.Id]);
return (
<div className="scrollY padded-top">
<div className="scrollY padded-top flex flex-column" style={{ gap: "1em" }}>
<div className="audio-info-container">
<div className="audio-info-image-container">
{item?.ImageTags ? (
@@ -35,7 +55,7 @@ function AudioPlayerRoute() {
)}
</div>
<div className="audio-info">
<Typography variant="h4" fontWeight={300}>
<Typography variant="h4" fontWeight={400}>
{item?.Name}
</Typography>
<Typography
@@ -45,9 +65,91 @@ function AudioPlayerRoute() {
>
by {item?.Artists?.join(", ")}
</Typography>
<Slider
style={{
marginTop: "1.2em",
}}
value={isScrubbing ? sliderProgress : secToTicks(progress)}
max={item?.RunTimeTicks ?? 1}
step={1}
onChange={(_, newVal) => {
setIsScrubbing(true);
setSliderProgress(newVal);
}}
onChangeCommitted={(_, newVal) => {
setIsScrubbing(false);
Array.isArray(newVal)
? setProgress(ticksToSec(newVal[0]))
: setProgress(ticksToSec(newVal));
audioPlayer?.currentTime = Array.isArray(newVal)
? ticksToSec(newVal[0])
: ticksToSec(newVal);
}}
sx={{
"& .MuiSlider-thumb": {
width: 14,
height: 14,
transition: "0.1s ease-in-out",
opacity: 0,
"&.Mui-active": {
width: 20,
height: 20,
opacity: 1,
},
},
"&:hover .MuiSlider-thumb": {
opacity: 1,
},
"& .MuiSlider-rail": {
opacity: 0.28,
background: "white",
},
}}
/>
<div className="audio-info-controls">
<PlayPreviousButton />
<IconButton
onClick={() => {
audioPlayer?.currentTime -= SEEK_AMOUNT;
}}
>
<span className="material-symbols-rounded">fast_rewind</span>
</IconButton>
<Fab
color="white"
size="large"
onClick={() =>
audioPlayer?.paused ? audioPlayer.play() : audioPlayer?.pause()
}
>
<span
className="fill material-symbols-rounded"
style={{
fontSize: "2.4em",
}}
>
{audioPlayer?.paused ? "play_arrow" : "pause"}
</span>
</Fab>
<IconButton
onClick={() => {
audioPlayer?.currentTime += SEEK_AMOUNT;
}}
>
<span className="material-symbols-rounded">fast_forward</span>
</IconButton>
<PlayNextButton />
</div>
</div>
</div>
<div className="audio-queue-container">
<Typography variant="h5">Queue:</Typography>
<div className="audio-queue">
{queue.map((track, index) => (
<QueueTrack key={track.Id} track={track} index={index} />
))}
</div>
</div>
<div className="audio-lyrics-container"></div>
</div>
);
}
+1
View File
@@ -1,5 +1,6 @@
$clr-accent-default: hsl(337, 96%, 56%);
$clr-accent-light: hsl(337, 96%, 90%);
$clr-accent-dark: hsl(337, 96%, 20%);
$clr-secondary-default: hsl(273, 100%, 36%);
$clr-secondary-dark: hsl(273, 100%, 12%);