/
dashboard.tsx
350 lines (317 loc) · 11 KB
/
dashboard.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import React, { useState } from 'react'
import {
Grid,
Button,
makeStyles,
GridList,
GridListTile
} from '@material-ui/core'
import AddIcon from '@material-ui/icons/Add'
import {
Playlist as PlaylistObj,
Track as TrackObj
} from '../../types'
import {
allGenresFromPlaylist,
asPlaylistTrack,
touchTrack,
} from '../../helpers/helpers'
import '../../gradientBG.css'
import { RouteComponentProps, withRouter } from 'react-router-dom'
import { DragDropContext } from 'react-beautiful-dnd'
import { v4 as uuid } from 'uuid'
import Auth from '../../auth'
import PlaylistWrapper from '../../components/PlaylistWrapper/'
import Subplaylist from '../../components/Playlists/Subplaylist'
export const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1
},
playlist: {
//Add styling for playlists here
},
gridList: {
flexWrap: 'nowrap',
// Promote the list into his own layer on Chrome. This cost memory but helps keeping high FPS.
transform: 'translateZ(0)'
},
gridListTileRoot: {
paddingLeft: 20,
paddingRight: 20,
padding: 2,
width: '45%'
},
gridListTileTile: {
overflow: 'visible'
}
}))
interface IDashboardProps extends RouteComponentProps {}
const Dashboard: React.FC<IDashboardProps> = () => {
//The width of the grids have to be dynamic, not a fixed width
const classes = useStyles()
const [masterPlaylist, setMasterPlaylist] = useState<PlaylistObj>()
const [genres, setGenres] = useState<string[]>([])
const [usedTracks, setUsedTracks] = useState<TrackObj[]>([])
const filteredLists: { [id: string]: TrackObj[] } = useState({})[0]
function loadPlaylist (playlist: PlaylistObj) {
Promise.all(playlist.tracks.map(t => t.expand())).then(() => {
setMasterPlaylist(playlist)
setPlaylists([])
setGenres(allGenresFromPlaylist(playlist))
})
}
interface PlaylistObjectPP extends PlaylistObj {
sourcePool: TrackObj[]
}
const createPlaylist = (): PlaylistObjectPP => {
return {
id: 'temp:' + uuid(),
name: 'Sub-playlist',
description: '',
image: '',
owner: { id: 'owner', display_name: 'Owner' },
snapshot_id: '',
tracks: [],
uri: '',
public: false,
collaborative: false,
sourcePool: masterPlaylist!.tracks.map(t => asPlaylistTrack(t).clone!()),
expand: async function () {
return this
}
}
}
const [playlists, setPlaylists] = useState<PlaylistObjectPP[]>([])
const addPlaylist = () => {
const playlist = createPlaylist()
console.log('Adding playlist', playlist.id)
setPlaylists([...playlists, playlist])
}
const deletePlaylist = (playlist: PlaylistObj) => {
console.log('Deleting playlist', playlist.id)
delete filteredLists[playlist.id]
setPlaylists(playlists.filter(p => p.id !== playlist.id))
}
// Find playlist object given its ID
const findPlaylist = (id: string) =>
(id === masterPlaylist?.id && masterPlaylist) ||
playlists.find(p => p.id === id)
// Resolves filter index to track source index
const getPlaylistIndexFromFilterIndex = (
playlist: PlaylistObj,
fIDX: number
) => {
let filterList = filteredLists[playlist.id]
if (fIDX === filterList.length) return -1 // Drag to the end
let key = asPlaylistTrack(filterList[fIDX])
let targetTrackUUID = asPlaylistTrack(key).uuid
return playlist.tracks.findIndex(
t => asPlaylistTrack(t).uuid === targetTrackUUID
)
}
const updateTracks = () => {
setUsedTracks(
Array.from(new Set(playlists.map((p: PlaylistObj) => p.tracks).flat()))
)
}
const removeTracksFromPlaylist = (playlist: PlaylistObj, ids: string[]) => {
let targetPlaylist = findPlaylist(playlist.id)
targetPlaylist!.tracks = targetPlaylist!.tracks.filter(
t => !ids.includes(asPlaylistTrack(t).uuid!)
)
setPlaylists([...playlists])
}
// const groupTracks = () => {
// let checkedTracks: PlaylistTrack[] = []
// let sourcePlaylist: PlaylistObj | undefined
// let source_newTracks: TrackObj[]
// let index: number
// checked.forEach(checkedList => {
// sourcePlaylist = findPlaylist(checkedList.id)
// if (!sourcePlaylist) throw new Error('Failed to get playlist')
// source_newTracks = [...sourcePlaylist!.tracks]
// checkedList.tracks.forEach((track, index) => {
// if (track) {
// checkedTracks.push(asPlaylistTrack(track))
// }
// })
// //we have a list of all selected tracks
// let newGroup = createTrackGroup(...checkedTracks)
// //remove tracks
// checkedTracks.forEach(track => {
// index = source_newTracks
// .map(function (e) {
// return e.id
// })
// .indexOf(track.id)
// source_newTracks!.splice(index, 1)
// })
// //add in track group
// source_newTracks.unshift(newGroup)
// sourcePlaylist.tracks = source_newTracks
// setPlaylists([...playlists])
// })
// let allChecked = checked
// allChecked.map(checkedPlaylist => (checkedPlaylist.tracks = []))
// setChecked([...checked])
// }
return (
<div className={`${classes.root} gradientAnim`}>
<Button
variant='contained'
color='primary'
style={{ float: 'right', margin: 5 }}
onClick={() =>
Auth.logout().then(() => {
window.location.href = window.location.origin + '/'
})
}
>
Logout
</Button>
{/* <DeleteTracksButton /> */}
<Grid style={{ width: '100%', margin: 0 }} container spacing={5}>
<DragDropContext
onDragEnd={evt => {
if (!evt.destination) return
let sourcePlaylist = findPlaylist(evt.source.droppableId)
let destPlaylist = findPlaylist(evt.destination.droppableId)
if (!sourcePlaylist || !destPlaylist)
throw new Error('Failed to find playlist')
let sourceIdx = getPlaylistIndexFromFilterIndex(
sourcePlaylist,
evt.source.index
)
let destIdx = getPlaylistIndexFromFilterIndex(
destPlaylist,
evt.destination.index
)
if (sourcePlaylist === masterPlaylist) {
console.log('drag from master')
let trackCopy = asPlaylistTrack(masterPlaylist.tracks[sourceIdx])
.clone!({
isCustom: true,
sourceID: masterPlaylist.id,
sourceName: () => 'Master Playlist'
})
const dest_newTracks = [...destPlaylist.tracks]
dest_newTracks.splice(
destIdx !== -1 ? destIdx : dest_newTracks.length,
0,
trackCopy
)
destPlaylist.tracks = dest_newTracks
setPlaylists([...playlists])
return
}
const source_newTracks = [...sourcePlaylist.tracks]
let removed = source_newTracks.splice(sourceIdx, 1)[0]
if (sourcePlaylist !== destPlaylist) {
// Move between playlists, also updating the destination playlist
const dest_newTracks = [...destPlaylist.tracks]
let removedPP = asPlaylistTrack(removed)
if (removedPP.isCustom) {
if (removedPP.sourceID === destPlaylist.id) {
// Dragged back to the original playlist
removed = touchTrack(removed, {
isCustom: false
})
}
} else {
// Dragged from original to new playlist
removed = touchTrack(removed, {
isCustom: true,
sourceID: sourcePlaylist.id,
sourceName: () => sourcePlaylist?.name
})
// Remove item from source pool
let sourcePlaylistPP = sourcePlaylist as PlaylistObjectPP
let poolIdx = sourcePlaylistPP.sourcePool.findIndex(
t => asPlaylistTrack(t).uuid === removedPP.uuid
)
if (poolIdx > -1) {
sourcePlaylistPP.sourcePool.splice(poolIdx, 1)
}
}
dest_newTracks.splice(
destIdx !== -1 ? destIdx : dest_newTracks.length,
0,
removed
)
destPlaylist.tracks = dest_newTracks
} else {
source_newTracks.splice(destIdx, 0, removed)
}
sourcePlaylist.tracks = source_newTracks
setPlaylists([...playlists])
}}
>
<Grid item xs={4}>
<PlaylistWrapper
usedTracks={usedTracks}
onSelect={p => loadPlaylist(p)}
onFilterUpdate={tracks =>
masterPlaylist && (filteredLists[masterPlaylist.id] = tracks)
}
/>
</Grid>
<Grid item xs={8} style={{ paddingRight: 0, overflowX: 'hidden' }}>
<GridList className={classes.gridList} cols={2}>
{masterPlaylist ? (
<>
{playlists.map(p => (
<GridListTile
key={p.id}
classes={{
root: classes.gridListTileRoot,
tile: classes.gridListTileTile
}}
>
<Subplaylist
genres={genres}
source={p.sourcePool}
playlist={p}
onAction={(action, data) => {
switch (action) {
case 'groupTracks':
break
case 'sortTracks':
// TODO: Sort tracks
break
case 'deleteTracks':
removeTracksFromPlaylist(p, data)
break
case 'deletePlaylist':
deletePlaylist(p)
break
case 'filterUpdate':
filteredLists[p.id] = data
break
case 'trackUpdate':
updateTracks()
break
}
}}
/>
</GridListTile>
))}
<GridListTile style={{ paddingRight: 15 }}>
<Button
variant='contained'
color='primary'
onClick={() => addPlaylist()}
startIcon={<AddIcon />}
>
Add
</Button>
</GridListTile>
</>
) : null}
</GridList>
</Grid>
</DragDropContext>
</Grid>
</div>
)
}
export default withRouter(Dashboard)