Skip to content

Commit

Permalink
refactor: outdated videos removal
Browse files Browse the repository at this point in the history
  • Loading branch information
AXeL-dev committed Aug 22, 2022
1 parent 671c0ab commit f5af7e2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
22 changes: 4 additions & 18 deletions src/store/reducers/videos.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { elapsedDays } from 'helpers/utils';
import { isWebExtension } from 'helpers/webext';
import { VideoCache, Video, VideoFlags, VideoFlag } from 'types';
import { config as channelCheckerConfig } from 'ui/components/webext/Background/ChannelChecker';

type AddVideoPayload = Video | Omit<VideoCache, 'flags'>;
type RemoveVideoPayload = Video | Pick<VideoCache, 'id'>;
Expand Down Expand Up @@ -104,10 +101,6 @@ export const videosSlice = createSlice({
video,
flags: { toWatchLater: false },
});
if (!isWebExtension) {
// no need to for the webextension, since it will be done by the background page on each launch
videosSlice.caseReducers.removeOutdatedVideos(state);
}
},
clearWatchLaterList: (
state,
Expand Down Expand Up @@ -163,7 +156,10 @@ export const videosSlice = createSlice({
},
saveVideos: (
state,
action: PayloadAction<{ videos: Video[]; flags: VideoFlags }>,
action: PayloadAction<{
videos: Video[];
flags: VideoFlags;
}>,
) => {
const { videos, flags } = action.payload;
for (const video of videos) {
Expand All @@ -174,15 +170,6 @@ export const videosSlice = createSlice({
});
}
},
removeOutdatedVideos: (state) => {
state.list = state.list.filter(
({ flags, publishedAt }) =>
flags.viewed ||
flags.toWatchLater ||
((flags.notified || flags.recent) &&
elapsedDays(publishedAt) <= channelCheckerConfig.videosSeniority),
);
},
},
});

Expand All @@ -197,7 +184,6 @@ export const {
unarchiveVideo,
archiveVideosByFlag,
saveVideos,
removeOutdatedVideos,
} = videosSlice.actions;

export default videosSlice.reducer;
23 changes: 22 additions & 1 deletion src/store/utils/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { setChannels } from '../reducers/channels';
import { setVideos } from '../reducers/videos';
import { setApp } from '../reducers/app';
import { dispatch, storageKey } from './persist';
import { elapsedDays } from 'helpers/utils';
import { config as channelCheckerConfig } from 'ui/components/webext/Background/ChannelChecker';
import { VideoCache, Settings } from 'types';
import { log } from 'helpers/logger';

export const preloadState = async () => {
const state = await storage.get(storageKey);
Expand All @@ -18,8 +22,25 @@ export const preloadState = async () => {
dispatch(setChannels(channels));
}
if (videos) {
dispatch(setVideos(videos));
dispatch(
setVideos({
list: removeOutdatedVideos(videos.list, settings),
}),
);
}
}
dispatch(setApp({ loaded: true }), shouldPersist);
};

const removeOutdatedVideos = (videos: VideoCache[], settings: Settings) => {
log('Removing outdated videos.');
return videos.filter(
({ flags, publishedAt }) =>
flags.viewed ||
flags.toWatchLater ||
(flags.recent
? elapsedDays(publishedAt) <= settings.recentVideosSeniority
: flags.notified &&
elapsedDays(publishedAt) <= channelCheckerConfig.videosSeniority),
);
};
13 changes: 1 addition & 12 deletions src/ui/components/webext/Background/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'helpers/webext';
import { useAppSelector } from 'store';
import { selectNotificationEnabledChannels } from 'store/selectors/channels';
import { saveVideos, removeOutdatedVideos } from 'store/reducers/videos';
import { saveVideos } from 'store/reducers/videos';
import { log } from 'helpers/logger';
import { selectSettings } from 'store/selectors/settings';
import { Video } from 'types';
Expand Down Expand Up @@ -43,17 +43,6 @@ export function Background(props: BackgroundProps) {
}
}, [settings.enableNotifications]);

useEffect(() => {
if (!isWebExtension) {
return;
}
if (app.loaded) {
log('Removing outdated videos.');
dispatch(removeOutdatedVideos(), true);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [app.loaded]);

const updateBadge = async (count: number) => {
const badgeText: string = await getBadgeText();
if (badgeText.length) {
Expand Down

0 comments on commit f5af7e2

Please sign in to comment.