Update components/playlist/PlaylistMenu.tsx
Browse files
components/playlist/PlaylistMenu.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import { FC, useEffect, useRef, useState } from "react"
|
| 2 |
import { MediaElement, Playlist, RoomState } from "../../lib/types"
|
| 3 |
-
import { DragDropContext, Droppable } from "
|
| 4 |
import classNames from "classnames"
|
| 5 |
import { Socket } from "socket.io-client"
|
| 6 |
import {
|
|
@@ -13,6 +13,10 @@ import IconChevron from "../icon/IconChevron"
|
|
| 13 |
import PlaylistItem from "./PlaylistItem"
|
| 14 |
import InputUrl from "../input/InputUrl"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
interface Props {
|
| 17 |
socket: Socket<ServerToClientEvents, ClientToServerEvents>
|
| 18 |
}
|
|
@@ -105,56 +109,70 @@ const PlaylistMenu: FC<Props> = ({ socket }) => {
|
|
| 105 |
)
|
| 106 |
|
| 107 |
if (
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
) {
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
if (newPlaylist.currentIndex === movedFrom) {
|
| 115 |
-
newPlaylist.currentIndex = movedTo
|
| 116 |
-
} else if (
|
| 117 |
-
movedFrom < newPlaylist.currentIndex &&
|
| 118 |
-
movedTo >= newPlaylist.currentIndex
|
| 119 |
-
) {
|
| 120 |
-
newPlaylist.currentIndex -= 1
|
| 121 |
-
} else if (
|
| 122 |
-
movedFrom > newPlaylist.currentIndex &&
|
| 123 |
-
movedTo <= newPlaylist.currentIndex
|
| 124 |
-
) {
|
| 125 |
-
newPlaylist.currentIndex += 1
|
| 126 |
-
}
|
| 127 |
}
|
| 128 |
|
|
|
|
| 129 |
socket.emit("updatePlaylist", newPlaylist)
|
| 130 |
}}
|
| 131 |
>
|
| 132 |
<Droppable droppableId={"playlistMenu"}>
|
| 133 |
-
{(provided) => (
|
| 134 |
-
<div
|
| 135 |
-
{
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
</div>
|
| 159 |
)}
|
| 160 |
</Droppable>
|
|
|
|
| 1 |
import { FC, useEffect, useRef, useState } from "react"
|
| 2 |
import { MediaElement, Playlist, RoomState } from "../../lib/types"
|
| 3 |
+
import { DragDropContext as _DragDropContext, Droppable as _Droppable, DragDropContextProps, DroppableProps } from "react-beautiful-dnd"
|
| 4 |
import classNames from "classnames"
|
| 5 |
import { Socket } from "socket.io-client"
|
| 6 |
import {
|
|
|
|
| 13 |
import PlaylistItem from "./PlaylistItem"
|
| 14 |
import InputUrl from "../input/InputUrl"
|
| 15 |
|
| 16 |
+
// HACK: this fixes type incompatibility
|
| 17 |
+
const DragDropContext = _DragDropContext as unknown as FC<DragDropContextProps>
|
| 18 |
+
const Droppable = _Droppable as unknown as FC<DroppableProps>
|
| 19 |
+
|
| 20 |
interface Props {
|
| 21 |
socket: Socket<ServerToClientEvents, ClientToServerEvents>
|
| 22 |
}
|
|
|
|
| 109 |
)
|
| 110 |
|
| 111 |
if (
|
| 112 |
+
playlist.currentIndex > result.source.index &&
|
| 113 |
+
playlist.currentIndex < result.destination.index
|
| 114 |
+
) {
|
| 115 |
+
newPlaylist.currentIndex--
|
| 116 |
+
} else if (
|
| 117 |
+
playlist.currentIndex < result.source.index &&
|
| 118 |
+
playlist.currentIndex > result.destination.index
|
| 119 |
) {
|
| 120 |
+
newPlaylist.currentIndex++
|
| 121 |
+
} else if (playlist.currentIndex === result.source.index) {
|
| 122 |
+
newPlaylist.currentIndex = result.destination.index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
}
|
| 124 |
|
| 125 |
+
console.log("Playlist updated to:", newPlaylist)
|
| 126 |
socket.emit("updatePlaylist", newPlaylist)
|
| 127 |
}}
|
| 128 |
>
|
| 129 |
<Droppable droppableId={"playlistMenu"}>
|
| 130 |
+
{(provided, snapshot) => (
|
| 131 |
+
<div
|
| 132 |
+
{...provided.droppableProps}
|
| 133 |
+
ref={provided.innerRef}
|
| 134 |
+
className={classNames(
|
| 135 |
+
"flex flex-col rounded gap-1",
|
| 136 |
+
snapshot.isDraggingOver && "bg-dark-800"
|
| 137 |
+
)}
|
| 138 |
+
>
|
| 139 |
+
<>
|
| 140 |
+
{playlist.items.map((item, index) => (
|
| 141 |
+
<PlaylistItem
|
| 142 |
+
key={item.src[0].src + "-" + index}
|
| 143 |
+
playing={playlist.currentIndex === index}
|
| 144 |
+
item={item}
|
| 145 |
+
index={index}
|
| 146 |
+
deleteItem={(index) => {
|
| 147 |
+
if (index < 0 || index >= playlist.items.length) {
|
| 148 |
+
return
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
const newPlaylist: Playlist = JSON.parse(
|
| 152 |
+
JSON.stringify(playlist)
|
| 153 |
+
)
|
| 154 |
+
newPlaylist.items.splice(index, 1)
|
| 155 |
+
if (newPlaylist.currentIndex === index) {
|
| 156 |
+
newPlaylist.currentIndex = -1
|
| 157 |
+
} else if (newPlaylist.currentIndex > index) {
|
| 158 |
+
newPlaylist.currentIndex--
|
| 159 |
+
}
|
| 160 |
+
socket.emit("updatePlaylist", newPlaylist)
|
| 161 |
+
}}
|
| 162 |
+
updateTitle={(newTitle) => {
|
| 163 |
+
const newPlaylist: Playlist = JSON.parse(
|
| 164 |
+
JSON.stringify(playlist)
|
| 165 |
+
)
|
| 166 |
+
newPlaylist.items[index].title = newTitle
|
| 167 |
+
socket.emit("updatePlaylist", newPlaylist)
|
| 168 |
+
}}
|
| 169 |
+
play={() => {
|
| 170 |
+
playItemFromPlaylist(socket, playlist, index)
|
| 171 |
+
}}
|
| 172 |
+
/>
|
| 173 |
+
))}
|
| 174 |
+
{provided.placeholder}
|
| 175 |
+
</>
|
| 176 |
</div>
|
| 177 |
)}
|
| 178 |
</Droppable>
|