-
Notifications
You must be signed in to change notification settings - Fork 15
/
playlists.js
469 lines (434 loc) · 13.8 KB
/
playlists.js
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import omit from 'just-omit';
import escapeStringRegExp from 'escape-string-regexp';
import indexBy from 'index-by';
import mapValues from 'just-map-values';
import {
INIT_STATE,
LOAD_ALL_PLAYLISTS_COMPLETE,
LOAD_PLAYLIST_START,
LOAD_PLAYLIST_COMPLETE,
PLAYLIST_CYCLED,
SELECT_PLAYLIST,
ACTIVATE_PLAYLIST_START,
ACTIVATE_PLAYLIST_COMPLETE,
CREATE_PLAYLIST_START,
CREATE_PLAYLIST_COMPLETE,
RENAME_PLAYLIST_START,
RENAME_PLAYLIST_COMPLETE,
DELETE_PLAYLIST_START,
DELETE_PLAYLIST_COMPLETE,
ADD_MEDIA_START,
ADD_MEDIA_COMPLETE,
REMOVE_MEDIA_START,
REMOVE_MEDIA_COMPLETE,
MOVE_MEDIA_START,
MOVE_MEDIA_COMPLETE,
UPDATE_MEDIA_START,
UPDATE_MEDIA_COMPLETE,
FILTER_PLAYLIST_ITEMS,
FILTER_PLAYLIST_ITEMS_COMPLETE,
DO_FAVORITE_COMPLETE,
SEARCH_START,
SEARCH_DELETE,
} from '../constants/ActionTypes';
const initialState = {
playlists: {},
playlistItems: {},
activePlaylistID: null,
selectedPlaylistID: null,
currentFilter: {},
};
function deselectAll(playlists) {
return mapValues(playlists, (playlist) => (
playlist.selected
? { ...playlist, selected: false }
: playlist
));
}
function processInsert(list, insert, position) {
let insertIdx = 0;
if (position.at === 'end') {
insertIdx = list.length;
} else if (position.at === 'start') {
insertIdx = 0;
} else if (position.after != null && position.after !== -1) {
insertIdx = list.findIndex((media) => media !== null && media._id === position.after) + 1;
}
return [
...list.slice(0, insertIdx),
...insert,
...list.slice(insertIdx),
];
}
// Moves a list of media items to a given position in the playlist.
function processMove(list, movedMedia, location) {
// Take all moved media items out of the playlist…
const wasMoved = indexBy(movedMedia, '_id');
const newPlaylist = list.filter((media) => media === null || !wasMoved[media._id]);
// …and add them back in at the correct place.
return processInsert(newPlaylist, movedMedia, location);
}
function updatePlaylist(state, playlistID, modify) {
const playlist = state.playlists[playlistID];
if (playlist) {
return {
...state,
playlists: {
...state.playlists,
[playlistID]: modify(playlist),
},
};
}
return state;
}
// Applies a function to the media list belonging to `playlistID` if it is found
// locally, i.e. in either the active or the selected playlist.
function updatePlaylistItems(state, playlistID, modify) {
const playlist = state.playlists[playlistID];
const media = state.playlistItems[playlistID];
if (playlist) {
let nextFilter = state.currentFilter;
if (state.selectedPlaylistID === playlistID && nextFilter) {
nextFilter = {
...nextFilter,
items: modify(nextFilter.items || [], playlist),
};
}
return {
...state,
playlistItems: {
...state.playlistItems,
[playlistID]: modify(media || [], playlist),
},
currentFilter: nextFilter,
};
}
return state;
}
function updatePlaylistAndItems(state, playlistID, modifyPlaylist, modifyItems) {
const newState = updatePlaylist(state, playlistID, modifyPlaylist);
return updatePlaylistItems(newState, playlistID, modifyItems);
}
function setPlaylistLoading(state, id, loading = true) {
return updatePlaylist(state, id, (playlist) => ({
...playlist,
loading,
}));
}
function fill(array, value) {
for (let i = 0, l = array.length; i < l; i += 1) {
array[i] = value; // eslint-disable-line no-param-reassign
}
return array;
}
function mergePlaylistPage(size, oldMedia, newMedia, { page, pageSize }) {
const media = fill(Array(size), null);
oldMedia.forEach((item, i) => {
media[i] = item;
});
newMedia.forEach((item, i) => {
media[(page * pageSize) + i] = item;
});
return media;
}
function filterCachedPlaylistItems(state, playlistID, filter) {
const rx = new RegExp(escapeStringRegExp(filter), 'i');
const playlist = state.playlistItems[playlistID];
if (playlist) {
return playlist.filter((item) => item && (
rx.test(item.artist) || rx.test(item.title)
));
}
return [];
}
export default function reduce(state = initialState, action = {}) {
const {
type, payload, meta, error,
} = action;
switch (type) {
case INIT_STATE:
// Probably not signed in.
if (!payload.playlists) return state;
return {
...state,
playlists: indexBy(payload.playlists.map((playlist) => ({
...playlist,
active: playlist._id === payload.activePlaylist,
selected: playlist._id === payload.activePlaylist,
})), '_id'),
// Preload the first item in the active playlist so it can be shown in
// the footer bar immediately. Else it would flash "This playlist is empty"
// for a moment.
playlistItems: payload.activePlaylist && payload.firstActivePlaylistItem ? {
...state.playlistItems,
[payload.activePlaylist]: [payload.firstActivePlaylistItem],
} : state.playlistItems,
activePlaylistID: payload.activePlaylist,
selectedPlaylistID: payload.activePlaylist,
};
case LOAD_ALL_PLAYLISTS_COMPLETE:
return {
...state,
playlists: indexBy(payload.playlists, '_id'),
};
case ACTIVATE_PLAYLIST_START:
// TODO use a different property here so we can show a loading icon on
// the "Active" button only, instead of on top of the entire playlist
return setPlaylistLoading(state, payload.playlistID);
case ACTIVATE_PLAYLIST_COMPLETE:
if (error) {
return setPlaylistLoading(state, meta.playlistID, false);
}
return {
...state,
// set `active` property on all playlists
playlists: mapValues(state.playlists, (playlist) => ({
...playlist,
loading: playlist._id === payload.playlistID ? false : playlist.loading,
active: playlist._id === payload.playlistID,
})),
activePlaylistID: payload.playlistID,
};
case SELECT_PLAYLIST: {
const { currentFilter } = state;
const shouldClearFilter = currentFilter && currentFilter.playlistID !== payload.playlistID;
return {
...state,
currentFilter: shouldClearFilter ? {} : currentFilter,
// set `selected` property on playlists
playlists: mapValues(state.playlists, (playlist) => ({
...playlist,
selected: playlist._id === payload.playlistID,
})),
selectedPlaylistID: payload.playlistID,
};
}
case SEARCH_START:
// We deselect playlists when doing a search, so the UI can switch to the
// search results view instead.
return {
...state,
playlists: deselectAll(state.playlists),
selectedPlaylistID: null,
};
case SEARCH_DELETE:
// Select the active playlist when search results are closed while they
// were focused.
if (state.selectedPlaylistID) return state;
return {
...state,
playlists: mapValues(state.playlists, (playlist) => ({
...playlist,
selected: playlist.active,
})),
selectedPlaylistID: state.activePlaylistID,
};
case LOAD_PLAYLIST_START: {
if (meta.sneaky || meta.page !== 0 || state.playlistItems[payload.playlistID]) {
return state;
}
// Reserve space in the playlistItems array.
return updatePlaylistItems(
state,
payload.playlistID,
(items, playlist) => fill(Array(playlist.size), null)
.map((item, i) => items[i] || item),
);
}
case LOAD_PLAYLIST_COMPLETE:
if (error) {
return setPlaylistLoading(state, meta.playlistID, false);
}
return updatePlaylistAndItems(
state,
payload.playlistID,
(playlist) => ({ ...playlist, loading: false }),
(items) => mergePlaylistPage(meta.size, items, payload.media, meta),
);
case FILTER_PLAYLIST_ITEMS:
// Only the selected playlist can be filtered.
if (payload.playlistID !== state.selectedPlaylistID) {
return state;
}
if (!payload.filter) {
return {
...state,
currentFilter: null,
};
}
return {
...state,
currentFilter: {
playlistID: payload.playlistID,
filter: payload.filter,
items: filterCachedPlaylistItems(state, payload.playlistID, payload.filter),
},
};
case FILTER_PLAYLIST_ITEMS_COMPLETE: {
// Only the selected playlist can be filtered.
if (payload.playlistID !== state.selectedPlaylistID) {
return state;
}
const { currentFilter } = state;
const items = mergePlaylistPage(meta.size, currentFilter.items, payload.media, meta);
return {
...state,
currentFilter: { ...currentFilter, items },
};
}
case PLAYLIST_CYCLED:
return updatePlaylistItems(state, payload.playlistID, (items, playlist) => {
const newItems = items.slice(1);
newItems[playlist.size - 1] = items[0]; // eslint-disable-line prefer-destructuring
return newItems;
});
// here be dragons
// TODO find a simpler way to store this stuff, that doesn't involve keeping
// millions of properties (six properties to be precise) in sync
// Playlists that are being created have a temporary ID that is used until the
// real ID comes back from the server.
case CREATE_PLAYLIST_START: {
const newPlaylist = {
_id: meta.tempId,
name: payload.name,
description: payload.description,
shared: payload.shared,
selected: true,
creating: true,
};
return {
...state,
playlists: Object.assign(
deselectAll(state.playlists),
{ [meta.tempId]: newPlaylist },
),
selectedPlaylistID: meta.tempId,
};
}
case CREATE_PLAYLIST_COMPLETE:
if (error) {
return {
...state,
playlists: omit(state.playlists, `${meta.tempId}`),
};
}
return {
...state,
playlists: Object.assign(
deselectAll(omit(state.playlists, `${meta.tempId}`)),
{
[payload.playlist._id]: {
...payload.playlist,
selected: true,
},
},
),
selectedPlaylistID: payload.playlist._id,
};
case RENAME_PLAYLIST_START:
return setPlaylistLoading(state, payload.playlistID);
case RENAME_PLAYLIST_COMPLETE: {
if (error) {
return setPlaylistLoading(state, meta.playlistID, false);
}
const renamedPlaylist = state.playlists[payload.playlistID];
if (renamedPlaylist) {
return updatePlaylist(state, payload.playlistID, (playlist) => ({
...playlist,
name: payload.name,
loading: false,
}));
}
return state;
}
case DELETE_PLAYLIST_START:
return setPlaylistLoading(state, payload.playlistID);
case DELETE_PLAYLIST_COMPLETE:
if (error) {
return setPlaylistLoading(state, meta.playlistID, false);
}
return {
...state,
// When deleting the selected playlist, select the active playlist instead.
selectedPlaylistID: state.selectedPlaylistID === payload.playlistID
? state.activePlaylistID
: state.selectedPlaylistID,
playlists: omit(state.playlists, payload.playlistID),
};
case ADD_MEDIA_START:
return setPlaylistLoading(state, payload.playlistID);
case ADD_MEDIA_COMPLETE:
if (error) {
return setPlaylistLoading(state, meta.playlistID, false);
}
return updatePlaylistAndItems(
state,
payload.playlistID,
(playlist) => ({
...playlist,
loading: false,
size: payload.newSize,
}),
(items) => processInsert(items, payload.appendedMedia, { after: payload.afterID }),
);
case DO_FAVORITE_COMPLETE:
return updatePlaylistAndItems(
state,
payload.playlistID,
(playlist) => ({
...playlist,
size: payload.newSize,
}),
(items) => processInsert(items, payload.added, { at: 'end' }),
);
case UPDATE_MEDIA_START:
return updatePlaylistItems(state, payload.playlistID, (items) => (
items.map((media) => (
media && media._id === payload.mediaID
? { ...media, loading: true }
: media
))
));
case UPDATE_MEDIA_COMPLETE:
return updatePlaylistItems(state, payload.playlistID, (items) => (
items.map((media) => (
media && media._id === payload.mediaID
? { ...media, ...payload.media, loading: false }
: media
))
));
case MOVE_MEDIA_START: {
const isMovingMedia = indexBy(payload.medias, '_id');
return updatePlaylistItems(state, payload.playlistID, (items) => (
items.map((media) => media && ({
...media,
loading: isMovingMedia[media._id] || media.loading,
}))
));
}
case MOVE_MEDIA_COMPLETE:
return updatePlaylistItems(state, payload.playlistID, (items) => (
processMove(items, payload.medias, payload.location)
));
case REMOVE_MEDIA_START: {
const isRemovingMedia = indexBy(payload.medias, '_id');
return updatePlaylistItems(state, payload.playlistID, (items) => (
items.map((media) => media && ({
...media,
loading: isRemovingMedia[media._id] || media.loading,
}))
));
}
case REMOVE_MEDIA_COMPLETE: {
const isRemovedMedia = indexBy(payload.removedMedia, '_id');
return updatePlaylistAndItems(
state,
payload.playlistID,
(playlist) => ({ ...playlist, size: payload.newSize }),
(items) => items.filter((media) => media === null || !isRemovedMedia[media._id]),
);
}
default:
return state;
}
}