Skip to content

Commit

Permalink
fix(home): update total videos count after hiding a channel
Browse files Browse the repository at this point in the history
  • Loading branch information
AXeL-dev committed Nov 7, 2022
1 parent f2589f5 commit cad2c85
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/providers/ChannelVideosProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { debounce } from 'helpers/utils';
import { useDidMountEffect } from 'hooks';
import {
createContext,
FC,
Expand All @@ -10,7 +11,9 @@ import {
useRef,
useState,
} from 'react';
import { useAppSelector } from 'store';
import { views } from 'store/reducers/settings';
import { selectHiddenChannels } from 'store/selectors/channels';
import { GetChannelVideosResponse } from 'store/services/youtube';
import { Channel, Video, HomeView } from 'types';

Expand Down Expand Up @@ -61,6 +64,9 @@ export const ChannelVideosProvider: FC = memo(({ children }) => {
const channelsMap = useRef<{ [key: string]: Map<string, ChannelData> }>(
initialChannelsMap,
);
const hiddenChannels = useAppSelector(selectHiddenChannels).map(
({ id }) => id,
);

const updateCount = useCallback(
debounce((view: HomeView, count: ChannelVideosCount) => {
Expand All @@ -72,18 +78,31 @@ export const ChannelVideosProvider: FC = memo(({ children }) => {
[],
);

const getCount = (view: HomeView) => {
const channelsData = Array.from(channelsMap.current[view].values());
const count = channelsData
.filter(({ channel }) => !hiddenChannels.includes(channel.id))
.reduce(
(acc, data) => ({
current: acc.current + (data.items?.length || 0),
total: acc.total + (data.total || 0),
}),
{ current: 0, total: 0 },
);
return count;
};

useDidMountEffect(() => {
// update videos count for the all view (since it is the only view where we hide channels)
const count = getCount(HomeView.All);
updateCount(HomeView.All, count);
}, [hiddenChannels]);

const setChannelData = (view: HomeView, data: ChannelData) => {
// save channel data per view
channelsMap.current[view].set(data.channel.id, data);
// update videos count per view
const channelsData = Array.from(channelsMap.current[view].values());
const count = channelsData.reduce(
(acc, data) => ({
current: acc.current + (data.items?.length || 0),
total: acc.total + (data.total || 0),
}),
{ current: 0, total: 0 },
);
const count = getCount(view);
updateCount(view, count);
};

Expand Down

0 comments on commit cad2c85

Please sign in to comment.