Skip to content

Commit

Permalink
refactor: channel videos provider
Browse files Browse the repository at this point in the history
  • Loading branch information
AXeL-dev committed Oct 15, 2022
1 parent b7bd310 commit e321352
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/providers/ChannelVideosProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useRef,
useState,
} from 'react';
import { views } from 'store/reducers/settings';
import { GetChannelVideosResponse } from 'store/services/youtube';
import { Channel, Video, HomeView } from 'types';

Expand All @@ -32,9 +33,7 @@ type ChannelVideosContextType = {
) => Video | undefined;
};

const ALL_VIEWS = Object.values(HomeView);

const INITIAL_COUNT = ALL_VIEWS.reduce(
const initialVideosCount = views.reduce(
(acc, view) => ({
...acc,
[view]: {
Expand All @@ -45,7 +44,7 @@ const INITIAL_COUNT = ALL_VIEWS.reduce(
{},
);

const INITIAL_MAP = ALL_VIEWS.reduce(
const initialChannelsMap = views.reduce(
(acc, view) => ({ ...acc, [view]: new Map() }),
{},
);
Expand All @@ -56,9 +55,9 @@ const ChannelVideosContext = createContext<

export const ChannelVideosProvider: FC = memo(({ children }) => {
const [videosCount, setVideosCount] =
useState<ChannelVideosContextType['videosCount']>(INITIAL_COUNT);
useState<ChannelVideosContextType['videosCount']>(initialVideosCount);
const channelsMap = useRef<{ [key: string]: Map<string, ChannelData> }>(
INITIAL_MAP,
initialChannelsMap,
);

const updateCount = useCallback(
Expand Down
2 changes: 1 addition & 1 deletion src/store/reducers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const defaultSettings = {
},
};

const views = Object.values(HomeView);
export const views = Object.values(HomeView);

interface SettingsState extends Settings {}

Expand Down

0 comments on commit e321352

Please sign in to comment.