lbry-desktop/ui/redux/reducers/collections.js
Rafael Saes 83dbe8ec7c
Playlists v2: Refactors, touch ups + Queue Mode (#1604)
* Playlists v2

* Style pass

* Change playlist items arrange icon

* Playlist card body open by default

* Refactor collectionEdit components

* Paginate & Refactor bid field

* Collection page changes

* Add Thumbnail optional

* Replace extra info for description on collection page

* Playlist card right below video on medium screen

* Allow editing private collections

* Add edit option to menus

* Allow deleting a public playlist but keeping a private version

* Add queue to Save menu, remove edit option from Builtin pages, show queue on playlists page

* Fix scroll to recent persisting on medium screen

* Fix adding to queue from menu

* Fixes for delete

* PublishList: delay mounting Items tab to prevent lock-up (#1783)

For a large list, the playlist publish form is unusable (super-slow typing) due to the entire list being mounted despite the tab is not active.
The full solution is still to paginate it, but for now, don't mount the tab until it is selected. Add a spinner to indicate something is loading. It's not prefect, but it's throwaway code anyway. At least we can fill in the fields properly now.

* Batch-resolve private collections (#1782)

* makeSelectClaimForClaimId --> selectClaimForClaimId

Move away from the problematic `makeSelect*`, especially in large loops.

* Batch-resolve private collections
1758

This alleviates the lock-up that is caused by large number of invidual resolves. There will still be some minor stutter due to the large DOM that React needs to handle -- that is logged in 1758 and will be handled separately.

At least the stutter is short (1-2s) and the app is still usable.
Private list items are being resolve individually, super slow if the list is large (>100). Published lists doesn't have this issue.
doFetchItemsInCollections contains most of the useful logic, but it isn't called for private/built-in lists because it's not an actual claim.
Tweaked doFetchItemsInCollections to handle private (UUID-based) collections.

* Use persisted state for floating player playlist card body
- I find it annoying being open everytime

* Fix removing edits from published playlist

* Fix scroll on mobile

* Allow going editing items from toast

* Fix ClaimShareButton

* Prevent edit/publish of builtin

* Fix async inside forEach

* Fix sync on queue edit

* Fix autoplayCountdown replay

* Fix deleting an item scrolling the playlist

* CreatedAt fixes

* Remove repost for now

* Anon publish fixes

* Fix mature case on floating

Co-authored-by: infinite-persistence <64950861+infinite-persistence@users.noreply.github.com>
2022-07-13 10:59:59 -03:00

260 lines
8.4 KiB
JavaScript

// @flow
import { handleActions } from 'util/redux-utils';
import { getCurrentTimeInSec } from 'util/time';
import * as ACTIONS from 'constants/action_types';
import * as COLS from 'constants/collections';
const defaultState: CollectionState = {
builtin: {
watchlater: {
items: [],
id: COLS.WATCH_LATER_ID,
name: COLS.WATCH_LATER_NAME,
createdAt: undefined,
updatedAt: getCurrentTimeInSec(),
type: COLS.COL_TYPE_PLAYLIST,
},
favorites: {
items: [],
id: COLS.FAVORITES_ID,
name: COLS.FAVORITES_NAME,
createdAt: undefined,
updatedAt: getCurrentTimeInSec(),
type: COLS.COL_TYPE_PLAYLIST,
},
},
resolved: {},
unpublished: {}, // sync
lastUsedCollection: undefined,
edited: {},
pending: {},
saved: [],
isResolvingCollectionById: {},
error: null,
queue: {
items: [],
id: COLS.QUEUE_ID,
name: COLS.QUEUE_NAME,
updatedAt: getCurrentTimeInSec(),
type: COLS.COL_TYPE_PLAYLIST,
},
};
const collectionsReducer = handleActions(
{
[ACTIONS.COLLECTION_NEW]: (state, action) => {
const { entry: params } = action.data; // { id:, items: Array<string>}
const currentTime = getCurrentTimeInSec();
// entry
const newListTemplate: Collection = {
id: params.id,
name: params.name,
items: [],
createdAt: currentTime,
updatedAt: currentTime,
type: params.type,
};
const newList = Object.assign({}, newListTemplate, { ...params });
const { unpublished: lists } = state;
const newLists = Object.assign({}, lists, { [params.id]: newList });
return {
...state,
unpublished: newLists,
lastUsedCollection: params.id,
};
},
[ACTIONS.COLLECTION_DELETE]: (state, action) => {
const { edited: editList, unpublished: unpublishedList, pending: pendingList, lastUsedCollection } = state;
const { id, collectionKey } = action.data;
const newEditList = Object.assign({}, editList);
const newPendingList = Object.assign({}, pendingList);
const newUnpublishedList = Object.assign({}, unpublishedList);
const collectionsForKey = state[collectionKey];
const collectionForId = collectionsForKey && collectionsForKey[id];
const isDeletingLastUsedCollection = lastUsedCollection === id;
if (collectionForId) {
const newList = Object.assign({}, state[collectionKey]);
delete newList[id];
return {
...state,
[collectionKey]: newList,
lastUsedCollection: isDeletingLastUsedCollection ? undefined : lastUsedCollection,
};
} else if (collectionKey === 'all') {
delete newEditList[id];
delete newUnpublishedList[id];
delete newPendingList[id];
} else {
if (newEditList[id]) {
delete newEditList[id];
} else if (newUnpublishedList[id]) {
delete newUnpublishedList[id];
} else if (newPendingList[id]) {
delete newPendingList[id];
}
}
return {
...state,
edited: newEditList,
unpublished: newUnpublishedList,
pending: newPendingList,
lastUsedCollection: isDeletingLastUsedCollection ? undefined : lastUsedCollection,
};
},
[ACTIONS.COLLECTION_PENDING]: (state, action) => {
const { localId, claimId } = action.data;
const { resolved: resolvedList, edited: editList, unpublished: unpublishedList, pending: pendingList } = state;
const newEditList = Object.assign({}, editList);
const newResolvedList = Object.assign({}, resolvedList);
const newUnpublishedList = Object.assign({}, unpublishedList);
const newPendingList = Object.assign({}, pendingList);
if (localId) {
// new publish
newPendingList[claimId] = Object.assign({}, newUnpublishedList[localId] || {});
delete newUnpublishedList[localId];
} else {
// edit update
newPendingList[claimId] = Object.assign({}, newEditList[claimId] || newResolvedList[claimId]);
delete newEditList[claimId];
}
return {
...state,
edited: newEditList,
unpublished: newUnpublishedList,
pending: newPendingList,
lastUsedCollection: claimId,
};
},
[ACTIONS.QUEUE_EDIT]: (state, action) => {
const { collectionKey, collection } = action.data;
const { [collectionKey]: currentQueue } = state;
return { ...state, queue: { ...currentQueue, ...collection, updatedAt: getCurrentTimeInSec() } };
},
[ACTIONS.COLLECTION_EDIT]: (state, action) => {
const { collectionKey, collection, collectionId } = action.data;
const id = collection?.id || collectionId;
const { [collectionKey]: lists } = state;
const currentCollectionState = lists[id];
const newCollection = Object.assign({}, currentCollectionState);
if (collection) {
Object.assign(newCollection, collection);
if (collectionKey === COLS.COL_KEY_EDITED) delete newCollection.editsCleared;
} else if (collectionKey === COLS.COL_KEY_EDITED) {
newCollection.editsCleared = true;
}
newCollection.updatedAt = getCurrentTimeInSec();
return {
...state,
[collectionKey]: { ...lists, [id]: newCollection },
lastUsedCollection: id,
};
},
[ACTIONS.COLLECTION_ERROR]: (state, action) => {
return Object.assign({}, state, {
error: action.data.message,
});
},
[ACTIONS.COLLECTION_ITEMS_RESOLVE_STARTED]: (state, action) => {
const { ids } = action.data;
const { isResolvingCollectionById } = state;
const newResolving = Object.assign({}, isResolvingCollectionById);
ids.forEach((id) => {
newResolving[id] = true;
});
return Object.assign({}, state, {
...state,
error: '',
isResolvingCollectionById: newResolving,
});
},
[ACTIONS.USER_STATE_POPULATE]: (state, action) => {
const { builtinCollections, savedCollections, unpublishedCollections, editedCollections } = action.data;
return {
...state,
edited: editedCollections || state.edited,
unpublished: unpublishedCollections || state.unpublished,
builtin: builtinCollections || state.builtin,
saved: savedCollections || state.saved,
};
},
[ACTIONS.COLLECTION_ITEMS_RESOLVE_COMPLETED]: (state, action) => {
const { resolvedPrivateCollectionIds, resolvedCollections, failedCollectionIds } = action.data;
const { pending, edited, isResolvingCollectionById, resolved } = state;
const newPending = Object.assign({}, pending);
const newEdited = Object.assign({}, edited);
const newResolved = Object.assign({}, resolved, resolvedCollections);
const resolvedIds = Object.keys(resolvedCollections);
const newResolving = Object.assign({}, isResolvingCollectionById);
if (resolvedCollections && Object.keys(resolvedCollections).length) {
resolvedIds.forEach((resolvedId) => {
if (newEdited[resolvedId]) {
if (newEdited[resolvedId]['updatedAt'] < resolvedCollections[resolvedId]['updatedAt']) {
delete newEdited[resolvedId];
}
}
delete newResolving[resolvedId];
if (newPending[resolvedId]) {
delete newPending[resolvedId];
}
});
}
if (failedCollectionIds && Object.keys(failedCollectionIds).length) {
failedCollectionIds.forEach((failedId) => {
delete newResolving[failedId];
});
}
if (resolvedPrivateCollectionIds && resolvedPrivateCollectionIds.length > 0) {
resolvedPrivateCollectionIds.forEach((id) => {
delete newResolving[id];
});
}
return Object.assign({}, state, {
...state,
pending: newPending,
resolved: newResolved,
edited: newEdited,
isResolvingCollectionById: newResolving,
});
},
[ACTIONS.COLLECTION_ITEMS_RESOLVE_FAILED]: (state, action) => {
const { ids } = action.data;
const { isResolvingCollectionById } = state;
const newResolving = Object.assign({}, isResolvingCollectionById);
ids.forEach((id) => {
delete newResolving[id];
});
return Object.assign({}, state, {
...state,
isResolvingCollectionById: newResolving,
error: action.data.message,
});
},
},
defaultState
);
export { collectionsReducer };