Skip to content

Commit

Permalink
Feature/modify global update settings (#432)
Browse files Browse the repository at this point in the history
* Move setting components in "settings" folder

* Add setting to change global update automation
  • Loading branch information
schroda authored Oct 29, 2023
1 parent 4c6d507 commit 678df88
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import List from '@mui/material/List';
import ListSubheader from '@mui/material/ListSubheader';
import { useTranslation } from 'react-i18next';
import { GlobalUpdateSettingsCategories } from '@/components/globalUpdate/GlobalUpdateSettingsCategories.tsx';
import { GlobalUpdateSettingsEntries } from '@/components/globalUpdate/GlobalUpdateSettingsEntries.tsx';
import { GlobalUpdateSettingsCategories } from '@/components/settings/globalUpdate/GlobalUpdateSettingsCategories.tsx';
import { GlobalUpdateSettingsEntries } from '@/components/settings/globalUpdate/GlobalUpdateSettingsEntries.tsx';
import { GlobalUpdateSettingsInterval } from '@/components/settings/globalUpdate/GlobalUpdateSettingsInterval.tsx';

export const GlobalUpdateSettings = () => {
const { t } = useTranslation();
Expand All @@ -23,6 +24,7 @@ export const GlobalUpdateSettings = () => {
</ListSubheader>
}
>
<GlobalUpdateSettingsInterval />
<GlobalUpdateSettingsEntries />
<GlobalUpdateSettingsCategories />
</List>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { makeToast } from '@/components/util/Toast.tsx';
import { IncludeInUpdate } from '@/lib/graphql/generated/graphql.ts';
import { TCategory } from '@/typings.ts';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { CheckboxContainer } from '@/components/globalUpdate/CheckboxContainer.ts';
import { CheckboxContainer } from '@/components/settings/globalUpdate/CheckboxContainer.ts';

const booleanToIncludeInStatus = (status: boolean | null | undefined): IncludeInUpdate => {
switch (status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import { useEffect, useState } from 'react';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemText from '@mui/material/ListItemText';
import { Button, Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material';
import { GetServerSettingsQuery } from '@/lib/graphql/generated/graphql.ts';
import { TranslationKey } from '@/typings.ts';
import { ServerSettings, TranslationKey } from '@/typings.ts';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { makeToast } from '@/components/util/Toast.tsx';
import { CheckboxContainer } from '@/components/globalUpdate/CheckboxContainer';
import { CheckboxInput } from '@/components/atoms/CheckboxInput';
import { CheckboxContainer } from '@/components/settings/globalUpdate/CheckboxContainer.ts';
import { CheckboxInput } from '@/components/atoms/CheckboxInput.tsx';

type GlobalUpdateSkipEntriesSettings = Pick<
GetServerSettingsQuery['settings'],
ServerSettings,
'excludeUnreadChapters' | 'excludeNotStarted' | 'excludeCompleted'
>;

Expand Down Expand Up @@ -61,9 +60,7 @@ const getSkipMangasText = (settings: GlobalUpdateSkipEntriesSettings | undefined
return skipSettings.join(', ');
};

const extractSkipEntriesSettings = (
serverSettings: GetServerSettingsQuery['settings'],
): GlobalUpdateSkipEntriesSettings => ({
const extractSkipEntriesSettings = (serverSettings: ServerSettings): GlobalUpdateSkipEntriesSettings => ({
excludeCompleted: serverSettings.excludeCompleted,
excludeNotStarted: serverSettings.excludeNotStarted,
excludeUnreadChapters: serverSettings.excludeUnreadChapters,
Expand Down
120 changes: 120 additions & 0 deletions src/components/settings/globalUpdate/GlobalUpdateSettingsInterval.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { useTranslation } from 'react-i18next';
import { InputAdornment, List, ListItem, ListItemText, Switch } from '@mui/material';
import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction';
import ListItemButton from '@mui/material/ListItemButton';
import { useEffect, useState } from 'react';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import DialogActions from '@mui/material/DialogActions';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import TextField from '@mui/material/TextField';
import { requestManager } from '@/lib/requests/RequestManager.ts';

const DEFAULT_INTERVAL_HOURS = 12;
const MIN_INTERVAL_HOURS = 6;

export const GlobalUpdateSettingsInterval = () => {
const { t } = useTranslation();

const { data } = requestManager.useGetServerSettings();
const autoUpdateIntervalHours = data?.settings.globalUpdateInterval ?? 0;
const doAutoUpdates = !!autoUpdateIntervalHours;
const [mutateSettings] = requestManager.useUpdateServerSettings();

const [isDialogOpen, setIsDialogOpen] = useState(false);
const [dialogUpdateIntervalHours, setDialogUpdateIntervalHours] = useState(autoUpdateIntervalHours);

const closeDialog = () => {
setIsDialogOpen(false);
};

const updateSetting = (globalUpdateInterval: number) => {
closeDialog();

const didIntervalChange = autoUpdateIntervalHours !== globalUpdateInterval;
if (!didIntervalChange) {
return;
}

mutateSettings({ variables: { input: { settings: { globalUpdateInterval } } } });
};

const setDoAutoUpdates = (enable: boolean) => {
const globalUpdateInterval = enable ? DEFAULT_INTERVAL_HOURS : 0;
updateSetting(globalUpdateInterval);
};

useEffect(() => {
setDialogUpdateIntervalHours(autoUpdateIntervalHours);
}, [autoUpdateIntervalHours]);

return (
<List>
<ListItem>
<ListItemText primary={t('library.settings.global_update.auto_update.label.title')} />
<ListItemSecondaryAction>
<Switch edge="end" checked={doAutoUpdates} onChange={(e) => setDoAutoUpdates(e.target.checked)} />
</ListItemSecondaryAction>
</ListItem>
{doAutoUpdates ? (
<>
<ListItemButton onClick={() => setIsDialogOpen(true)}>
<ListItemText
primary={t('library.settings.global_update.auto_update.interval.label.title')}
secondary={t('library.settings.global_update.auto_update.interval.label.value', {
hours: autoUpdateIntervalHours,
})}
secondaryTypographyProps={{ style: { display: 'flex', flexDirection: 'column' } }}
/>
</ListItemButton>

<Dialog open={isDialogOpen} onClose={closeDialog}>
<DialogContent>
<DialogTitle sx={{ paddingLeft: 0 }}>
{t('library.settings.global_update.auto_update.interval.label.title')}
</DialogTitle>
<TextField
sx={{
width: '100%',
margin: 'auto',
}}
InputProps={{
inputProps: { min: MIN_INTERVAL_HOURS },
startAdornment: (
<InputAdornment position="start">{t('global.time.hour_short')}</InputAdornment>
),
}}
autoFocus
value={dialogUpdateIntervalHours}
type="number"
onChange={(e) => setDialogUpdateIntervalHours(Number(e.target.value))}
/>
</DialogContent>
<DialogActions>
<Button onClick={closeDialog} color="primary">
{t('global.button.cancel')}
</Button>
<Button
onClick={() => {
updateSetting(dialogUpdateIntervalHours);
}}
color="primary"
>
{t('global.button.ok')}
</Button>
</DialogActions>
</Dialog>
</>
) : null}
</List>
);
};
2 changes: 1 addition & 1 deletion src/i18n/i18next.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import 'i18next';
import resources from '@/i18n/translations';
import { resources } from '@/i18n/translations';

declare module 'i18next' {
interface CustomTypeOptions {
Expand Down
14 changes: 14 additions & 0 deletions src/i18n/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@
"start": "Start",
"submit": "Submit"
},
"time": {
"hour_short": "h"
},
"date": {
"label": {
"today": "Today",
Expand Down Expand Up @@ -321,6 +324,17 @@
"unread_chapters": "With unread chapter(s)"
}
},
"auto_update": {
"interval": {
"label": {
"value": "{{hours}}$t(global.time.hour_short)",
"title": "Automatic update interval"
}
},
"label": {
"title": "Automatic updates"
}
},
"title": "Global update"
},
"title": "Library Settings"
Expand Down
2 changes: 1 addition & 1 deletion src/screens/settings/LibrarySettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { useTranslation } from 'react-i18next';
import { useContext, useEffect } from 'react';
import { NavBarContext, useSetDefaultBackTo } from '@/components/context/NavbarContext.tsx';
import { GlobalUpdateSettings } from '@/components/globalUpdate/GlobalUpdateSettings.tsx';
import { GlobalUpdateSettings } from '@/components/settings/globalUpdate/GlobalUpdateSettings.tsx';
import { SearchSettings } from '@/screens/settings/SearchSettings.tsx';

export function LibrarySettings() {
Expand Down
3 changes: 3 additions & 0 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
GetChapterQuery,
GetExtensionQuery,
GetMangaQuery,
GetServerSettingsQuery,
GetSourceQuery,
MetaType,
SourcePreferenceChangeInput,
Expand Down Expand Up @@ -376,3 +377,5 @@ export type BackupValidationResult = {
missingTrackers: string[];
mangasMissingSources: string[];
};

export type ServerSettings = GetServerSettingsQuery['settings'];

0 comments on commit 678df88

Please sign in to comment.