Skip to content

Commit

Permalink
fix(auth): prevent login error when watch history item is removed
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed May 23, 2022
1 parent 7b0b0ae commit cfc82ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export const getMediaById = (id: string): Promise<PlaylistItem | undefined> => {
*/
export const getMediaByIds = async (ids: string[]): Promise<PlaylistItem[]> => {
// @todo this should be updated when it will become possible to request multiple media items in a single request
const responses = await Promise.all(ids.map((id) => getMediaById(id)));
const responses = await Promise.allSettled(ids.map((id) => getMediaById(id)));

function notEmpty<Value>(value: Value | null | undefined): value is Value {
return value !== null && value !== undefined;
}

return responses.filter(notEmpty);
return responses.map((result) => (result.status === 'fulfilled' ? result.value : null)).filter(notEmpty);
};
18 changes: 11 additions & 7 deletions src/stores/WatchHistoryController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useAccountStore } from '#src/stores/AccountStore';
import * as persist from '#src/utils/persist';
import { getMediaById } from '#src/services/api.service';
import { getMediaByIds } from '#src/services/api.service';
import { updatePersonalShelves } from '#src/stores/AccountController';
import { useWatchHistoryStore } from '#src/stores/WatchHistoryStore';
import type { PlaylistItem } from '#types/playlist';
Expand All @@ -18,13 +18,17 @@ export const restoreWatchHistory = async () => {
// Store savedItems immediately, so we can show the watch history while we fetch the mediaItems
useWatchHistoryStore.setState({ watchHistory: savedItems });

const watchHistory = await Promise.all(
savedItems.map(async (item) => {
const mediaItem = await getMediaById(item.mediaid);
const watchHistoryItems = await getMediaByIds(savedItems.map((item) => item.mediaid));
const watchHistoryItemsDict = Object.fromEntries(watchHistoryItems.map((item) => [item.mediaid, item]));

if (mediaItem) return createWatchHistoryItem(mediaItem, { progress: item.progress, duration: item.duration });
}),
);
const watchHistory = savedItems.map((item) => {
if (watchHistoryItemsDict[item.mediaid]) {
return createWatchHistoryItem(watchHistoryItemsDict[item.mediaid], {
progress: item.progress,
duration: item.duration,
});
}
});

useWatchHistoryStore.setState({
watchHistory: watchHistory.filter((item): item is WatchHistoryItem => !!item?.mediaid),
Expand Down

0 comments on commit cfc82ec

Please sign in to comment.