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

221 lines
11 KiB
TypeScript
Raw Normal View History

/*
* 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/.
*/
2026-03-11 00:11:29 +01:00
import type { MessageDescriptor } from '@lingui/core';
import FormLabel from '@mui/material/FormLabel';
import RadioGroup from '@mui/material/RadioGroup';
import { useLingui } from '@lingui/react/macro';
import { msg } from '@lingui/core/macro';
import { CheckboxInput } from '@/base/components/inputs/CheckboxInput.tsx';
import { RadioInput } from '@/base/components/inputs/RadioInput.tsx';
import { SortRadioInput } from '@/base/components/inputs/SortRadioInput.tsx';
import { ThreeStateCheckboxInput } from '@/base/components/inputs/ThreeStateCheckboxInput.tsx';
import { OptionsTabs } from '@/base/components/modals/OptionsTabs.tsx';
import { requestManager } from '@/lib/requests/RequestManager.ts';
2025-08-15 22:02:58 +02:00
import { Trackers } from '@/features/tracker/services/Trackers.ts';
2026-03-11 00:11:29 +01:00
import type { GetTrackersSettingsQuery } from '@/lib/graphql/generated/graphql.ts';
2026-05-15 20:40:31 +02:00
import { MangaStatus } from '@/lib/graphql/generated/graphql-base.types.ts';
2025-12-22 14:00:44 +01:00
import { GET_TRACKERS_SETTINGS } from '@/lib/graphql/tracker/TrackerQuery.ts';
2026-03-20 03:29:45 +01:00
import { STABLE_EMPTY_ARRAY } from '@/base/Base.constants.ts';
2025-08-15 22:02:58 +02:00
import { createUpdateCategoryMetadata, useGetCategoryMetadata } from '@/features/category/services/CategoryMetadata.ts';
import { makeToast } from '@/base/utils/Toast.ts';
2024-09-17 03:40:55 +02:00
import {
createUpdateMetadataServerSettings,
updateMetadataServerSettings,
2024-09-17 03:40:55 +02:00
useMetadataServerSettings,
2025-08-15 22:02:58 +02:00
} from '@/features/settings/services/ServerSettingsMetadata.ts';
2026-03-11 00:11:29 +01:00
import type { LibrarySortMode } from '@/features/library/Library.types.ts';
import type { CategoryMetadataInfo } from '@/features/category/Category.types.ts';
2025-08-15 22:02:58 +02:00
import { MANGA_STATUS_TO_TRANSLATION } from '@/features/manga/Manga.constants.ts';
import { GridLayout } from '@/base/Base.types';
2024-12-20 17:24:40 +01:00
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
const TITLES: { [key in 'filter' | 'sort' | 'display']: MessageDescriptor } = {
filter: msg`Filter`,
sort: msg`Sort`,
display: msg`Display`,
};
const SORT_OPTIONS: [LibrarySortMode, MessageDescriptor][] = [
['unreadChapters', msg`Unread chapters`],
['totalChapters', msg`Total chapters`],
['alphabetically', msg`A-Z`],
['dateAdded', msg`Recently added`],
['lastRead', msg`Recently read`],
['latestFetchedChapter', msg`Latest fetched chapter`],
['latestUploadedChapter', msg`Latest uploaded chapter`],
];
2024-09-17 03:40:55 +02:00
export const LibraryOptionsPanel = ({
category,
open,
onClose,
}: {
category: CategoryMetadataInfo;
open: boolean;
onClose: () => void;
2024-09-17 03:40:55 +02:00
}) => {
const { t } = useLingui();
const trackerList = requestManager.useGetTrackerList<GetTrackersSettingsQuery>(GET_TRACKERS_SETTINGS);
2026-03-20 03:29:45 +01:00
const loggedInTrackers = Trackers.getLoggedIn(trackerList.data?.trackers.nodes ?? STABLE_EMPTY_ARRAY);
2024-03-28 20:00:46 +01:00
const categoryLibraryOptions = useGetCategoryMetadata(category);
2024-12-20 17:24:40 +01:00
const updateCategoryLibraryOptions = createUpdateCategoryMetadata(category, (e) =>
makeToast(t`Failed to save changes`, 'error', getErrorMessage(e)),
2024-09-17 03:40:55 +02:00
);
const {
settings: { showTabSize, showContinueReadingButton, showDownloadBadge, showUnreadBadge, gridLayout },
2024-09-17 03:40:55 +02:00
} = useMetadataServerSettings();
const setSettingValue = createUpdateMetadataServerSettings((e) =>
makeToast(t`Could not save the default search settings to the server`, 'error', getErrorMessage(e)),
2024-09-17 03:40:55 +02:00
);
return (
<OptionsTabs<'filter' | 'sort' | 'display'>
open={open}
onClose={onClose}
tabs={['filter', 'sort', 'display']}
tabTitle={(key) => t(TITLES[key])}
tabContent={(key) => {
if (key === 'filter') {
return (
<>
<ThreeStateCheckboxInput
label={t`Unread`}
2024-09-17 03:40:55 +02:00
checked={categoryLibraryOptions.hasUnreadChapters}
onChange={(c) => updateCategoryLibraryOptions('hasUnreadChapters', c)}
/>
2024-12-27 03:32:30 +01:00
<ThreeStateCheckboxInput
label={t`Started`}
2024-12-27 03:32:30 +01:00
checked={categoryLibraryOptions.hasReadChapters}
onChange={(c) => updateCategoryLibraryOptions('hasReadChapters', c)}
/>
<ThreeStateCheckboxInput
label={t`Downloaded`}
2024-09-17 03:40:55 +02:00
checked={categoryLibraryOptions.hasDownloadedChapters}
onChange={(c) => updateCategoryLibraryOptions('hasDownloadedChapters', c)}
/>
<ThreeStateCheckboxInput
label={t`Bookmarked`}
2024-09-17 03:40:55 +02:00
checked={categoryLibraryOptions.hasBookmarkedChapters}
onChange={(c) => updateCategoryLibraryOptions('hasBookmarkedChapters', c)}
/>
<ThreeStateCheckboxInput
label={t`Duplicate chapters`}
2024-09-17 03:40:55 +02:00
checked={categoryLibraryOptions.hasDuplicateChapters}
onChange={(c) => updateCategoryLibraryOptions('hasDuplicateChapters', c)}
/>
<FormLabel sx={{ mt: 2 }}>{t`Status`}</FormLabel>
2024-09-15 00:48:32 +02:00
{Object.values(MangaStatus).map((status) => (
<ThreeStateCheckboxInput
2024-09-17 03:40:55 +02:00
key={status}
2025-05-05 01:46:11 +02:00
label={t(MANGA_STATUS_TO_TRANSLATION[status])}
2024-09-17 03:40:55 +02:00
checked={categoryLibraryOptions.hasStatus[status]}
2024-09-15 00:48:32 +02:00
onChange={(checked) =>
2024-09-17 03:40:55 +02:00
updateCategoryLibraryOptions('hasStatus', {
...categoryLibraryOptions.hasStatus,
2024-09-15 00:48:32 +02:00
[status]: checked,
})
}
/>
))}
<FormLabel sx={{ mt: 2 }}>{t`Tracked`}</FormLabel>
2024-03-28 20:00:46 +01:00
{loggedInTrackers.map((tracker) => (
<ThreeStateCheckboxInput
key={tracker.id}
label={tracker.name}
2024-09-17 03:40:55 +02:00
checked={categoryLibraryOptions.hasTrackerBinding[tracker.id]}
2024-03-28 20:00:46 +01:00
onChange={(checked) =>
2024-09-17 03:40:55 +02:00
updateCategoryLibraryOptions('hasTrackerBinding', {
...categoryLibraryOptions.hasTrackerBinding,
2024-09-16 11:35:55 +02:00
[tracker.id]: checked,
})
2024-03-28 20:00:46 +01:00
}
/>
))}
</>
);
}
if (key === 'sort') {
return SORT_OPTIONS.map(([mode, label]) => (
<SortRadioInput
key={mode}
label={t(label)}
2024-09-17 03:40:55 +02:00
checked={categoryLibraryOptions.sortBy === mode}
sortDescending={categoryLibraryOptions.sortDesc}
onClick={() =>
2024-09-17 03:40:55 +02:00
mode !== categoryLibraryOptions.sortBy
? updateCategoryLibraryOptions('sortBy', mode)
: updateCategoryLibraryOptions('sortDesc', !categoryLibraryOptions.sortDesc)
}
/>
));
}
if (key === 'display') {
return (
<>
<FormLabel>{t`Display mode`}</FormLabel>
<RadioGroup
onChange={(e) => updateMetadataServerSettings('gridLayout', Number(e.target.value))}
value={gridLayout}
>
<RadioInput
label={t`Compact grid`}
value={GridLayout.Compact}
checked={gridLayout == null || gridLayout === GridLayout.Compact}
/>
<RadioInput
label={t`Comfortable grid`}
value={GridLayout.Comfortable}
checked={gridLayout === GridLayout.Comfortable}
/>
<RadioInput
label={t`List`}
value={GridLayout.List}
checked={gridLayout === GridLayout.List}
/>
</RadioGroup>
<FormLabel sx={{ mt: 2 }}>{t`Badges`}</FormLabel>
<CheckboxInput
label={t`Unread badges`}
checked={showUnreadBadge}
onChange={() => updateMetadataServerSettings('showUnreadBadge', !showUnreadBadge)}
/>
<CheckboxInput
label={t`Download badges`}
checked={showDownloadBadge}
onChange={() => updateMetadataServerSettings('showDownloadBadge', !showDownloadBadge)}
/>
<FormLabel sx={{ mt: 2 }}>{t`Tabs`}</FormLabel>
<CheckboxInput
label={t`Show number of items`}
checked={showTabSize}
2024-09-17 03:40:55 +02:00
onChange={() => setSettingValue('showTabSize', !showTabSize)}
/>
<FormLabel sx={{ mt: 2 }}>{t`Other`}</FormLabel>
<CheckboxInput
label={t`Show continue reading button`}
checked={showContinueReadingButton}
onChange={() =>
updateMetadataServerSettings(
2024-09-17 03:40:55 +02:00
'showContinueReadingButton',
!showContinueReadingButton,
)
}
/>
</>
);
}
return null;
}}
/>
);
};