shivam413 commited on
Commit
93051bd
·
verified ·
1 Parent(s): 8a0fc61

Update components/playlist/PlaylistMenu.tsx

Browse files
Files changed (1) hide show
  1. components/playlist/PlaylistMenu.tsx +45 -63
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 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,10 +13,6 @@ import IconChevron from "../icon/IconChevron"
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,70 +105,56 @@ const PlaylistMenu: FC<Props> = ({ socket }) => {
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>
@@ -183,4 +165,4 @@ const PlaylistMenu: FC<Props> = ({ socket }) => {
183
  )
184
  }
185
 
186
- export default PlaylistMenu
 
1
  import { FC, useEffect, useRef, useState } from "react"
2
  import { MediaElement, Playlist, RoomState } from "../../lib/types"
3
+ import { DragDropContext, Droppable } from "@hello-pangea/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
  interface Props {
17
  socket: Socket<ServerToClientEvents, ClientToServerEvents>
18
  }
 
105
  )
106
 
107
  if (
108
+ newPlaylist.currentIndex === result.source.index ||
109
+ newPlaylist.currentIndex === result.destination.index
 
 
 
 
 
110
  ) {
111
+ // keep currentIndex pointing to the same item after reorder
112
+ const movedFrom = result.source.index
113
+ const movedTo = result.destination.index
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 ref={provided.innerRef} {...provided.droppableProps} className={"flex flex-col gap-1"}>
135
+ {playlist.items.map((item, index) => (
136
+ <PlaylistItem
137
+ key={item.src[0].src + "-" + index}
138
+ item={item}
139
+ index={index}
140
+ updateTitle={(title: string) => {
141
+ const newPlaylist: Playlist = JSON.parse(JSON.stringify(playlist))
142
+ newPlaylist.items[index].title = title
143
+ socket.emit("updatePlaylist", newPlaylist)
144
+ }}
145
+ deleteItem={(i: number) => {
146
+ const newPlaylist: Playlist = JSON.parse(JSON.stringify(playlist))
147
+ newPlaylist.items.splice(i, 1)
148
+ if (newPlaylist.currentIndex >= newPlaylist.items.length) {
149
+ newPlaylist.currentIndex = newPlaylist.items.length - 1
150
+ }
151
+ socket.emit("updatePlaylist", newPlaylist)
152
+ }}
153
+ playing={playlist.currentIndex === index}
154
+ play={() => playItemFromPlaylist(socket, index)}
155
+ />
156
+ ))}
157
+ {provided.placeholder}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  </div>
159
  )}
160
  </Droppable>
 
165
  )
166
  }
167
 
168
+ export default PlaylistMenu