Files
suwayomi-material-you-webui/src/components/library/LibraryOptionsPanel.tsx
T

148 lines
7.0 KiB
TypeScript
Raw Normal View History

2022-11-24 09:41:03 +01:00
/*
* 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/.
*/
2022-11-24 09:41:03 +01:00
import { FormLabel, RadioGroup } from '@mui/material';
import React from 'react';
2023-03-23 13:59:32 +01:00
import { useTranslation } from 'react-i18next';
2023-06-05 01:42:39 +02:00
import { LibraryOptions, LibrarySortMode, TranslationKey } from '@/typings';
2023-10-28 00:32:02 +02:00
import { CheckboxInput } from '@/components/atoms/CheckboxInput';
import { RadioInput } from '@/components/atoms/RadioInput';
import { SortRadioInput } from '@/components/atoms/SortRadioInput';
import { ThreeStateCheckboxInput } from '@/components/atoms/ThreeStateCheckboxInput';
2023-06-05 01:42:39 +02:00
import { GridLayout, useLibraryOptionsContext } from '@/components/context/LibraryOptionsContext';
2023-10-28 00:32:02 +02:00
import { OptionsTabs } from '@/components/molecules/OptionsTabs';
2022-11-24 09:41:03 +01:00
2023-03-23 13:59:32 +01:00
const TITLES: { [key in 'filter' | 'sort' | 'display']: TranslationKey } = {
filter: 'global.label.filter',
sort: 'global.label.sort',
display: 'global.label.display',
2022-11-24 09:41:03 +01:00
};
2023-03-23 13:59:32 +01:00
const SORT_OPTIONS: [LibrarySortMode, TranslationKey][] = [
['sortToRead', 'library.option.sort.label.by_unread_chapters'],
['sortAlph', 'library.option.sort.label.alphabetically'],
['sortDateAdded', 'library.option.sort.label.by_date_added'],
['sortLastRead', 'library.option.sort.label.by_last_read'],
2024-01-11 03:56:04 +01:00
['sortLatestFetchedChapter', 'library.option.sort.label.by_latest_fetched_chapter'],
['sortLatestUploadedChapter', 'library.option.sort.label.by_latest_uploaded_chapter'],
2022-11-24 09:41:03 +01:00
];
interface IProps {
2023-02-06 10:06:33 +01:00
open: boolean;
onClose: () => void;
2022-11-24 09:41:03 +01:00
}
2023-10-28 00:32:02 +02:00
export const LibraryOptionsPanel: React.FC<IProps> = ({ open, onClose }) => {
2023-03-23 13:59:32 +01:00
const { t } = useTranslation();
2022-11-24 09:41:03 +01:00
const { options, setOptions } = useLibraryOptionsContext();
2023-02-08 18:19:26 +01:00
const handleFilterChange = <T extends keyof LibraryOptions>(key: T, value: LibraryOptions[T]) => {
2022-11-24 09:41:03 +01:00
setOptions((v) => ({ ...v, [key]: value }));
};
return (
<OptionsTabs<'filter' | 'sort' | 'display'>
open={open}
onClose={onClose}
tabs={['filter', 'sort', 'display']}
2023-07-04 21:53:25 +02:00
tabTitle={(key) => t(TITLES[key])}
2022-11-24 09:41:03 +01:00
tabContent={(key) => {
if (key === 'filter') {
return (
<>
2023-02-06 10:06:33 +01:00
<ThreeStateCheckboxInput
2023-03-23 13:59:32 +01:00
label={t('global.filter.label.unread')}
2023-02-06 10:06:33 +01:00
checked={options.unread}
onChange={(c) => handleFilterChange('unread', c)}
/>
<ThreeStateCheckboxInput
2023-03-23 13:59:32 +01:00
label={t('global.filter.label.downloaded')}
2023-02-06 10:06:33 +01:00
checked={options.downloaded}
onChange={(c) => handleFilterChange('downloaded', c)}
/>
2022-11-24 09:41:03 +01:00
</>
);
}
if (key === 'sort') {
return SORT_OPTIONS.map(([mode, label]) => (
<SortRadioInput
key={mode}
2023-07-04 21:53:25 +02:00
label={t(label)}
2022-11-24 09:41:03 +01:00
checked={options.sorts === mode}
sortDescending={options.sortDesc}
2023-02-06 10:06:33 +01:00
onClick={() =>
mode !== options.sorts
? handleFilterChange('sorts', mode)
: handleFilterChange('sortDesc', !options.sortDesc)
}
2022-11-24 09:41:03 +01:00
/>
));
}
if (key === 'display') {
2023-12-19 15:17:00 +01:00
const { gridLayout, showContinueReadingButton, showDownloadBadge, showUnreadBadge, showTabSize } =
options;
2022-11-24 09:41:03 +01:00
return (
<>
2023-03-23 13:59:32 +01:00
<FormLabel>{t('global.grid_layout.title')}</FormLabel>
2022-11-24 09:41:03 +01:00
<RadioGroup
2023-02-08 18:19:26 +01:00
onChange={(e) => handleFilterChange('gridLayout', Number(e.target.value))}
2022-11-24 09:41:03 +01:00
value={gridLayout}
>
2023-02-06 10:06:33 +01:00
<RadioInput
2023-03-23 13:59:32 +01:00
label={t('global.grid_layout.label.compact_grid')}
2023-02-06 10:06:33 +01:00
value={GridLayout.Compact}
2023-02-08 18:19:26 +01:00
checked={gridLayout == null || gridLayout === GridLayout.Compact}
2023-02-06 10:06:33 +01:00
/>
<RadioInput
2023-03-23 13:59:32 +01:00
label={t('global.grid_layout.label.comfortable_grid')}
2023-02-06 10:06:33 +01:00
value={GridLayout.Comfortable}
checked={gridLayout === GridLayout.Comfortable}
/>
<RadioInput
2023-03-23 13:59:32 +01:00
label={t('global.grid_layout.label.list')}
2023-02-06 10:06:33 +01:00
value={GridLayout.List}
checked={gridLayout === GridLayout.List}
/>
2022-11-24 09:41:03 +01:00
</RadioGroup>
2023-03-23 13:59:32 +01:00
<FormLabel sx={{ mt: 2 }}>{t('library.option.display.badge.title')}</FormLabel>
2022-11-24 09:41:03 +01:00
<CheckboxInput
2023-03-23 13:59:32 +01:00
label={t('library.option.display.badge.label.unread_badges')}
2023-12-19 15:17:00 +01:00
checked={showUnreadBadge}
2023-02-08 18:19:26 +01:00
onChange={() => handleFilterChange('showUnreadBadge', !showUnreadBadge)}
2022-11-24 09:41:03 +01:00
/>
<CheckboxInput
2023-03-23 13:59:32 +01:00
label={t('library.option.display.badge.label.download_badges')}
2023-12-19 15:17:00 +01:00
checked={showDownloadBadge}
2023-02-08 18:19:26 +01:00
onChange={() => handleFilterChange('showDownloadBadge', !showDownloadBadge)}
2022-11-24 09:41:03 +01:00
/>
<FormLabel sx={{ mt: 2 }}>{t('library.option.display.tab.title')}</FormLabel>
<CheckboxInput
label={t('library.option.display.tab.label.show_number_of_items')}
checked={showTabSize}
onChange={() => handleFilterChange('showTabSize', !showTabSize)}
/>
2023-12-19 15:17:00 +01:00
<FormLabel sx={{ mt: 2 }}>{t('global.label.other')}</FormLabel>
<CheckboxInput
label={t('library.option.display.other.label.show_continue_reading_button')}
checked={showContinueReadingButton}
onChange={() =>
handleFilterChange('showContinueReadingButton', !showContinueReadingButton)
}
/>
2022-11-24 09:41:03 +01:00
</>
);
}
return null;
}}
/>
);
};