fix: player/audio route not able to interact with audioPlayer

This commit is contained in:
Prayag Prajapati committed 2024-11-17 11:50:16 +05:30
1 parent 60895ceecf
commit b0e08ce4bd
10 files changed
+338 -120

No files matched your search

+80 -60
View File
@@ -1,5 +1,9 @@
import { getRuntimeMusic } from "@/utils/date/time";
import useQueue, { setQueue } from "@/utils/store/queue";
import { useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
import { Typography } from "@mui/material";
import React from "react";
@@ -7,6 +11,8 @@ import { useCallback, useRef } from "react";
import { type XYCoord, useDrag, useDrop } from "react-dnd";
import type { Identifier } from "typescript";
type Props = {
track: BaseItemDto;
index: number;
@@ -17,85 +23,99 @@ const QueueTrack = (props: Props) => {
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 { attributes, listeners, setNodeRef, transform, transition } =
useSortable({
id: track.Id,
});
const dragIndex = hoverItem.index;
const dropIndex = index;
// 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],
// );
if (dragIndex === dropIndex) return;
// 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 hoverBoundingRect = trackRef.current?.getBoundingClientRect();
const hoverMiddleY =
(hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
// const dragIndex = hoverItem.index;
// const dropIndex = index;
const clientOffset = monitor.getClientOffset();
// if (dragIndex === dropIndex) return;
const hoverClientY = (clientOffset as XYCoord).y - hoverBoundingRect.top;
// const hoverBoundingRect = trackRef.current?.getBoundingClientRect();
// const hoverMiddleY =
// (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
if (dragIndex < dropIndex && hoverClientY < hoverMiddleY) {
return;
}
// const clientOffset = monitor.getClientOffset();
if (dragIndex > dropIndex && hoverClientY > hoverMiddleY) {
return;
}
// const hoverClientY = (clientOffset as XYCoord).y - hoverBoundingRect.top;
handleQueueUpdate(dragIndex, dropIndex);
hoverItem.index = dropIndex;
},
});
// if (dragIndex < dropIndex && hoverClientY < hoverMiddleY) {
// return;
// }
const [{ isDragging }, drag] = useDrag({
type: "Row",
item: () => {
const id = track.Id;
return { id, index };
},
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),
});
// if (dragIndex > dropIndex && hoverClientY > hoverMiddleY) {
// return;
// }
drag(drop(trackRef));
// 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));
const style = {
// opacity: isDragging ? 0.5 : 1,
transform: CSS.Transform.toString(transform),
transition,
};
return (
<div
ref={trackRef}
style={{ opacity: isDragging ? 0.5 : 1 }}
ref={setNodeRef}
style={style}
className={
currentItemIndex === index
? "audio-queue-track active"
: "audio-queue-track"
}
>
<span className="material-symbols-rounded">drag_handle</span>
<span {...attributes} {...listeners} className="material-symbols-rounded">
drag_handle
</span>
<div className="audio-queue-track-info">
<Typography className="audio-queue-track-info-name">
{track.Name}
@@ -105,7 +125,7 @@ const QueueTrack = (props: Props) => {
</Typography>
</div>
<Typography className="opacity-07">
{getRuntimeMusic(track.RunTimeTicks)}
{getRuntimeMusic(track.RunTimeTicks ?? 0)}
</Typography>
</div>
);