Skip to content

Commit

Permalink
feat(settings): add query timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
AXeL-dev committed Jul 23, 2022
1 parent e33073a commit a6eab93
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/store/reducers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const defaultSettings = {
hiddenViews: [],
},
enableNotifications: true,
queryTimeout: 10000,
};

const views = Object.values(HomeView);
Expand Down
15 changes: 8 additions & 7 deletions src/store/services/youtube/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
fetchBaseQuery,
} from '@reduxjs/toolkit/query/react';
import { BaseQueryApi } from '@reduxjs/toolkit/dist/query/baseQueryTypes';
import { RootState } from 'store';
import { FetchError } from 'types';
import store, { RootState } from 'store';
import { FetchError, Settings } from 'types';

const defaultBaseQuery = fetchBaseQuery({
baseUrl: 'https://www.googleapis.com/youtube/v3/',
Expand All @@ -25,15 +25,16 @@ const baseQuery = (
) =>
Promise.race([
defaultBaseQuery(args, api, extraOptions),
new Promise((resolve) =>
setTimeout(
new Promise((resolve) => {
const { queryTimeout = 10000 } = store.getState().settings as Settings;
return setTimeout(
() =>
resolve({
error: { status: 'FETCH_ERROR', error: FetchError.TIMEOUT },
}),
extraOptions.timeout ?? 10000
)
) as ReturnType<typeof defaultBaseQuery>,
extraOptions.timeout ?? queryTimeout
);
}) as ReturnType<typeof defaultBaseQuery>,
]);

export const youtubeApi = createApi({
Expand Down
9 changes: 9 additions & 0 deletions src/types/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Settings {
watchLaterViewFilters: WatchLaterViewFilters;
homeDisplayOptions: HomeDisplayOptions;
enableNotifications: boolean;
queryTimeout: number;
}

export interface HomeDisplayOptions {
Expand Down Expand Up @@ -46,6 +47,14 @@ export enum VideosSeniority {
OneMonth = 31,
}

export enum QueryTimeout {
TenSeconds = 10000,
FifteenSeconds = 15000,
TwentySeconds = 20000,
ThirtySeconds = 30000,
OneMinute = 60000,
}

export enum SettingType {
String,
Secret,
Expand Down
32 changes: 31 additions & 1 deletion src/ui/components/pages/Settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect, useMemo } from 'react';
import { Stack, Divider, Link } from '@mui/material';
import { Layout } from 'ui/components/shared';
import { HomeView, SettingType, VideosSeniority } from 'types';
import { HomeView, QueryTimeout, SettingType, VideosSeniority } from 'types';
import { ControlledField, CustomField } from './Field';
import { useAppDispatch, useAppSelector } from 'store';
import { selectSettings } from 'store/selectors/settings';
Expand Down Expand Up @@ -104,6 +104,36 @@ export function Settings(props: SettingsProps) {
]}
type={SettingType.List}
/>
<ControlledField
label="Queries timeout"
value={settings.queryTimeout}
onChange={(queryTimeout: QueryTimeout) => {
dispatch(setSettings({ queryTimeout }));
}}
options={[
{
label: '10 seconds',
value: QueryTimeout.TenSeconds,
},
{
label: '15 seconds',
value: QueryTimeout.FifteenSeconds,
},
{
label: '20 seconds',
value: QueryTimeout.TwentySeconds,
},
{
label: '30 seconds',
value: QueryTimeout.ThirtySeconds,
},
{
label: '1 minute',
value: QueryTimeout.OneMinute,
},
]}
type={SettingType.List}
/>
<ControlledField
label="Dark mode"
value={settings.darkMode}
Expand Down

0 comments on commit a6eab93

Please sign in to comment.