Skip to content

Commit

Permalink
fix(webext): ensure to add missing channels on context menu actions
Browse files Browse the repository at this point in the history
  • Loading branch information
AXeL-dev committed Jul 9, 2022
1 parent 2d065ab commit b9e32e3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
39 changes: 38 additions & 1 deletion src/store/services/youtube/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ type FindChannelByNameResponse = {
total: number;
};

type FindChannelByIdArgs = {
id: string;
maxResults?: number;
};

type FindChannelByIdResponse = {
items: Channel[];
total: number;
};

type GetChannelActivitiesArgs = {
channel: Channel;
publishedAfter?: string;
Expand Down Expand Up @@ -69,6 +79,28 @@ const queries = {
total: response.pageInfo.totalResults,
}),
},
// Channel find by id query
findChannelById: {
query: ({ id, maxResults = 10 }: FindChannelByIdArgs) => ({
url: 'channels',
params: {
part: 'snippet,id',
fields: 'pageInfo,items(snippet,id)',
maxResults,
id,
},
}),
transformResponse: (response: Response): FindChannelByIdResponse => ({
items: response.items.map((item) => ({
title: item.snippet.title,
url: `https://www.youtube.com/channel/${item.snippet.channelId}/videos`,
description: item.snippet.description,
thumbnail: item.snippet.thumbnails.medium.url,
id: item.id,
})),
total: response.pageInfo.totalResults,
}),
},
// Channel activities query
getChannelActivities: {
query: ({
Expand Down Expand Up @@ -128,12 +160,16 @@ const queries = {
},
};

const extendedApi = youtubeApi.injectEndpoints({
export const extendedApi = youtubeApi.injectEndpoints({
endpoints: (builder) => ({
findChannelByName: builder.query<
FindChannelByNameResponse,
FindChannelByNameArgs
>(queries.findChannelByName),
findChannelById: builder.query<
FindChannelByIdResponse,
FindChannelByIdArgs
>(queries.findChannelById),
getChannelActivities: builder.query<
GetChannelActivitiesResponse,
GetChannelActivitiesArgs
Expand Down Expand Up @@ -263,6 +299,7 @@ const extendedApi = youtubeApi.injectEndpoints({

export const {
useFindChannelByNameQuery,
useFindChannelByIdQuery,
useGetChannelActivitiesQuery,
useGetVideosByIdQuery,
useGetChannelVideosQuery,
Expand Down
31 changes: 31 additions & 0 deletions src/store/thunks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createAsyncThunk } from '@reduxjs/toolkit';
import { RootState } from 'store';
import { Channel } from 'types';
import { addChannel } from './reducers/channels';
import { extendedApi } from './services/youtube';

export const fetchChannelById = createAsyncThunk<
Channel | undefined,
{ id: string },
{ state: RootState }
>('channels/fetchChannelById', async ({ id }, { getState, dispatch }) => {
const { channels } = getState();
// check for existing channel
const found = channels.list.find((channel: Channel) => channel.id === id);
if (found) {
return found;
}

// fetch channel by id
const result = dispatch(
extendedApi.endpoints.findChannelById.initiate({ id })
);
result.unsubscribe();
const response = await result;
const channel = response.data?.items[0];
if (channel) {
// save to channels list
dispatch(addChannel(channel));
}
return channel;
});
8 changes: 6 additions & 2 deletions src/store/utils/persist.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import store, { RootState } from 'store';
import storage from 'helpers/storage';
import { AnyAction } from '@reduxjs/toolkit';
import { log } from 'helpers/logger';

export const storageKey = 'APP_YOUTUBE_VIEWER';

let canPersistState = true;
let prevSerializedState = '';

export const dispatch = (action: AnyAction, persist: boolean = false) => {
type DispatchParams = Parameters<typeof store.dispatch>;

export const dispatch = (
action: DispatchParams[0],
persist: boolean = false
) => {
canPersistState = persist;
store.dispatch(action);
};
Expand Down
5 changes: 5 additions & 0 deletions src/ui/components/webext/Background/ContextMenus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
selectWatchLaterVideos,
} from 'store/selectors/videos';
import { getVideoId } from 'helpers/utils';
import { fetchChannelById } from 'store/thunks';

declare var browser: any;

Expand Down Expand Up @@ -60,6 +61,8 @@ export default function ContextMenus(props: ContextMenusProps) {
}),
true
);
// ensure to add channel too (if it does not exist)
dispatch(fetchChannelById({ id: channelId }), true);
browser.contextMenus.update(menuItemId, { enabled: false });
});
break;
Expand All @@ -74,6 +77,8 @@ export default function ContextMenus(props: ContextMenusProps) {
}),
true
);
// ensure to add channel too (if it does not exist)
dispatch(fetchChannelById({ id: channelId }), true);
} else {
dispatch(removeViewedVideo({ id }), true);
}
Expand Down

0 comments on commit b9e32e3

Please sign in to comment.