/* * 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 { FormLabel, RadioGroup } from '@mui/material'; import React from 'react'; import { useTranslation } from 'react-i18next'; import { LibraryOptions, LibrarySortMode, TranslationKey } from '@/typings'; import { CheckboxInput } from '@/components/atoms/CheckboxInput'; import { RadioInput } from '@/components/atoms/RadioInput'; import { SortRadioInput } from '@/components/atoms/SortRadioInput'; import { ThreeStateCheckboxInput } from '@/components/atoms/ThreeStateCheckboxInput'; import { GridLayout, useLibraryOptionsContext } from '@/components/context/LibraryOptionsContext'; import { OptionsTabs } from '@/components/molecules/OptionsTabs'; import { requestManager } from '@/lib/requests/RequestManager.ts'; import { Trackers } from '@/lib/data/Trackers.ts'; const TITLES: { [key in 'filter' | 'sort' | 'display']: TranslationKey } = { filter: 'global.label.filter', sort: 'global.label.sort', display: 'global.label.display', }; 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'], ['sortLatestFetchedChapter', 'library.option.sort.label.by_latest_fetched_chapter'], ['sortLatestUploadedChapter', 'library.option.sort.label.by_latest_uploaded_chapter'], ]; interface IProps { open: boolean; onClose: () => void; } export const LibraryOptionsPanel: React.FC = ({ open, onClose }) => { const { t } = useTranslation(); const { options, setOptions } = useLibraryOptionsContext(); const trackerList = requestManager.useGetTrackerList(); const loggedInTrackers = Trackers.getLoggedIn(trackerList.data?.trackers.nodes ?? []); const handleFilterChange = (key: T, value: LibraryOptions[T]) => { setOptions((v) => ({ ...v, [key]: value })); }; return ( open={open} onClose={onClose} tabs={['filter', 'sort', 'display']} tabTitle={(key) => t(TITLES[key])} tabContent={(key) => { if (key === 'filter') { return ( <> handleFilterChange('unread', c)} /> handleFilterChange('downloaded', c)} /> {t('global.filter.label.tracked')} {loggedInTrackers.map((tracker) => ( handleFilterChange('tracker', { ...options.tracker, [tracker.id]: checked }) } /> ))} ); } if (key === 'sort') { return SORT_OPTIONS.map(([mode, label]) => ( mode !== options.sorts ? handleFilterChange('sorts', mode) : handleFilterChange('sortDesc', !options.sortDesc) } /> )); } if (key === 'display') { const { gridLayout, showContinueReadingButton, showDownloadBadge, showUnreadBadge, showTabSize } = options; return ( <> {t('global.grid_layout.title')} handleFilterChange('gridLayout', Number(e.target.value))} value={gridLayout} > {t('library.option.display.badge.title')} handleFilterChange('showUnreadBadge', !showUnreadBadge)} /> handleFilterChange('showDownloadBadge', !showDownloadBadge)} /> {t('library.option.display.tab.title')} handleFilterChange('showTabSize', !showTabSize)} /> {t('global.label.other')} handleFilterChange('showContinueReadingButton', !showContinueReadingButton) } /> ); } return null; }} /> ); };