2022-03-04 01:25:10 +00: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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import FilterListIcon from '@mui/icons-material/FilterList';
|
2024-04-14 15:50:12 +02:00
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
|
|
|
|
import Box from '@mui/material/Box';
|
2023-08-14 21:43:06 +02:00
|
|
|
import { useState } from 'react';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-05-08 04:01:23 +02:00
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import SaveIcon from '@mui/icons-material/Save';
|
|
|
|
|
import Chip from '@mui/material/Chip';
|
|
|
|
|
import DeleteIcon from '@mui/icons-material/Delete';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
|
|
|
|
import PopupState, { bindDialog, bindTrigger } from 'material-ui-popup-state';
|
|
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
|
import TextField from '@mui/material/TextField';
|
2025-02-01 14:24:38 +01:00
|
|
|
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
|
2025-05-03 12:14:05 +02:00
|
|
|
import { OptionsPanel } from '@/modules/core/components/modals/OptionsPanel.tsx';
|
2024-10-05 21:22:12 +02:00
|
|
|
import { CheckBoxFilter } from '@/modules/source/components/filters/CheckBoxFilter.tsx';
|
|
|
|
|
import { HeaderFilter } from '@/modules/source/components/filters/HeaderFilter.tsx';
|
|
|
|
|
import { SelectFilter } from '@/modules/source/components/filters/SelectFilter.tsx';
|
|
|
|
|
import { SortFilter } from '@/modules/source/components/filters/SortFilter.tsx';
|
|
|
|
|
import { TextFilter } from '@/modules/source/components/filters/TextFilter.tsx';
|
|
|
|
|
import { TriStateFilter } from '@/modules/source/components/filters/TriStateFilter.tsx';
|
2022-03-04 01:25:10 +00:00
|
|
|
// this can only cycle once, so should be fine
|
|
|
|
|
// eslint-disable-next-line import/no-cycle
|
2024-10-05 21:22:12 +02:00
|
|
|
import { GroupFilter } from '@/modules/source/components/filters/GroupFilter.tsx';
|
|
|
|
|
import { SeparatorFilter } from '@/modules/source/components/filters/SeparatorFilter.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { StyledFab } from '@/modules/core/components/buttons/StyledFab.tsx';
|
2024-10-05 23:22:42 +02:00
|
|
|
import { awaitConfirmation } from '@/modules/core/utils/AwaitableDialog.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-10-05 21:22:12 +02:00
|
|
|
import { ISourceMetadata, SourceFilters } from '@/modules/source/Source.types.ts';
|
2022-03-04 01:25:10 +00:00
|
|
|
|
|
|
|
|
interface IFilters {
|
2023-09-24 21:32:23 +02:00
|
|
|
sourceFilter: SourceFilters[];
|
2023-02-06 10:06:33 +01:00
|
|
|
updateFilterValue: Function;
|
|
|
|
|
group: number | undefined;
|
|
|
|
|
update: any;
|
2022-03-04 01:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IFilters1 {
|
2024-05-08 04:01:23 +02:00
|
|
|
savedSearches: ISourceMetadata['savedSearches'];
|
|
|
|
|
selectSavedSearch: (savedSearch: string) => void;
|
|
|
|
|
updateSavedSearches: (savedSearch: string, updateType: 'create' | 'delete') => void;
|
2023-09-24 21:32:23 +02:00
|
|
|
sourceFilter: SourceFilters[];
|
2023-02-06 10:06:33 +01:00
|
|
|
updateFilterValue: Function;
|
|
|
|
|
resetFilterValue: Function;
|
|
|
|
|
setTriggerUpdate: Function;
|
|
|
|
|
update: any;
|
2022-03-04 01:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
export function Options({ sourceFilter, group, updateFilterValue, update }: IFilters) {
|
2022-03-04 01:25:10 +00:00
|
|
|
return (
|
2022-11-24 09:41:03 +01:00
|
|
|
<Stack key={`filters ${group}`}>
|
2023-09-24 21:32:23 +02:00
|
|
|
{sourceFilter.map((e, index) => {
|
2023-02-06 10:06:33 +01:00
|
|
|
let checkif = update.find(
|
|
|
|
|
(el: { group: number | undefined; position: number }) =>
|
|
|
|
|
el.group === group && el.position === index,
|
|
|
|
|
);
|
2022-03-04 21:48:33 +00:00
|
|
|
checkif = checkif ? checkif.state : checkif;
|
2022-03-04 01:25:10 +00:00
|
|
|
switch (e.type) {
|
2023-09-24 21:32:23 +02:00
|
|
|
case 'CheckBoxFilter':
|
2022-03-04 01:25:10 +00:00
|
|
|
return (
|
|
|
|
|
<CheckBoxFilter
|
2023-09-24 21:32:23 +02:00
|
|
|
key={`filters ${e.name}`}
|
|
|
|
|
name={e.name}
|
|
|
|
|
state={checkif ?? e.CheckBoxFilterDefault}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2023-09-24 21:32:23 +02:00
|
|
|
case 'GroupFilter':
|
2022-03-04 01:25:10 +00:00
|
|
|
return (
|
|
|
|
|
<GroupFilter
|
2023-09-24 21:32:23 +02:00
|
|
|
key={`filters ${e.name}`}
|
|
|
|
|
name={e.name}
|
|
|
|
|
state={e.filters}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2023-09-24 21:32:23 +02:00
|
|
|
case 'HeaderFilter':
|
|
|
|
|
return <HeaderFilter key={`filters ${e.name}`} name={e.name} />;
|
|
|
|
|
case 'SelectFilter':
|
2022-03-04 01:25:10 +00:00
|
|
|
return (
|
|
|
|
|
<SelectFilter
|
2023-09-24 21:32:23 +02:00
|
|
|
key={`filters ${e.name}`}
|
|
|
|
|
name={e.name}
|
|
|
|
|
values={e.values}
|
|
|
|
|
state={checkif != null ? parseInt(checkif, 10) : e.SelectFilterDefault}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2023-09-24 21:32:23 +02:00
|
|
|
case 'SeparatorFilter':
|
2023-10-28 00:32:02 +02:00
|
|
|
return <SeparatorFilter key={`filters ${e.name}`} name={e.name} />;
|
2023-09-24 21:32:23 +02:00
|
|
|
case 'SortFilter':
|
2022-03-04 01:25:10 +00:00
|
|
|
return (
|
|
|
|
|
<SortFilter
|
2023-09-24 21:32:23 +02:00
|
|
|
key={`filters ${e.name}`}
|
|
|
|
|
name={e.name}
|
|
|
|
|
values={e.values}
|
|
|
|
|
state={
|
|
|
|
|
checkif ?? {
|
|
|
|
|
ascending: e.SortFilterDefault?.ascending,
|
|
|
|
|
index: e.SortFilterDefault?.index,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2023-09-24 21:32:23 +02:00
|
|
|
case 'TextFilter':
|
2022-03-04 01:25:10 +00:00
|
|
|
return (
|
|
|
|
|
<TextFilter
|
2023-09-24 21:32:23 +02:00
|
|
|
key={`filters ${e.name}`}
|
|
|
|
|
name={e.name}
|
|
|
|
|
state={checkif ?? e.TextFilterDefault}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2023-09-24 21:32:23 +02:00
|
|
|
case 'TriStateFilter':
|
2022-03-04 01:25:10 +00:00
|
|
|
return (
|
|
|
|
|
<TriStateFilter
|
2023-09-24 21:32:23 +02:00
|
|
|
key={`filters ${e.name}`}
|
|
|
|
|
name={e.name}
|
|
|
|
|
state={checkif != null ? checkif : e.TriStateFilterDefault}
|
2022-03-04 01:25:10 +00:00
|
|
|
position={index}
|
|
|
|
|
group={group}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
2022-03-04 21:48:33 +00:00
|
|
|
update={update}
|
2022-03-04 01:25:10 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
default:
|
2023-09-24 21:32:23 +02:00
|
|
|
throw new Error(`Unknown source filter "${e}"`);
|
2022-03-04 01:25:10 +00:00
|
|
|
}
|
|
|
|
|
})}
|
2022-11-24 09:41:03 +01:00
|
|
|
</Stack>
|
2022-03-04 01:25:10 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export function SourceOptions({
|
2024-05-08 04:01:23 +02:00
|
|
|
savedSearches = {},
|
|
|
|
|
selectSavedSearch,
|
|
|
|
|
updateSavedSearches,
|
2022-03-04 01:25:10 +00:00
|
|
|
sourceFilter,
|
|
|
|
|
updateFilterValue,
|
|
|
|
|
resetFilterValue,
|
|
|
|
|
setTriggerUpdate,
|
2022-03-04 21:48:33 +00:00
|
|
|
update,
|
2022-03-04 01:25:10 +00:00
|
|
|
}: IFilters1) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2023-08-14 21:43:06 +02:00
|
|
|
const [FilterOptions, setFilterOptions] = useState(false);
|
2024-05-08 04:01:23 +02:00
|
|
|
const [newSavedSearch, setNewSavedSearch] = useState('');
|
|
|
|
|
|
|
|
|
|
const savedSearchNames = Object.keys(savedSearches);
|
|
|
|
|
const savedSearchesExist = !!savedSearchNames.length;
|
2022-03-04 01:25:10 +00:00
|
|
|
|
|
|
|
|
function handleReset() {
|
|
|
|
|
resetFilterValue(0);
|
|
|
|
|
setFilterOptions(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleSubmit() {
|
|
|
|
|
setTriggerUpdate(0);
|
|
|
|
|
setFilterOptions(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2023-05-15 18:23:26 +02:00
|
|
|
<StyledFab onClick={() => setFilterOptions(!FilterOptions)} variant="extended" color="primary">
|
2022-03-04 01:25:10 +00:00
|
|
|
<FilterListIcon />
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('global.button.filter')}
|
2023-05-15 18:23:26 +02:00
|
|
|
</StyledFab>
|
2022-03-04 01:25:10 +00:00
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
<OptionsPanel open={FilterOptions} onClose={() => setFilterOptions(false)}>
|
2024-05-08 04:01:23 +02:00
|
|
|
<Box sx={{ p: 2, pb: savedSearchesExist ? undefined : 0 }}>
|
|
|
|
|
<Box sx={{ display: 'flex', pb: 1 }}>
|
|
|
|
|
<Button onClick={handleReset}>{t('global.button.reset')}</Button>
|
|
|
|
|
<PopupState variant="dialog" popupId="source-browse-save-search">
|
|
|
|
|
{(popupState) => (
|
|
|
|
|
<>
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={t('source.filter.save_search.label.save')}>
|
2024-05-08 04:01:23 +02:00
|
|
|
<IconButton sx={{ marginLeft: 'auto' }} {...bindTrigger(popupState)}>
|
|
|
|
|
<SaveIcon />
|
|
|
|
|
</IconButton>
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2024-05-08 04:01:23 +02:00
|
|
|
<Dialog {...bindDialog(popupState)} maxWidth="xs" fullWidth>
|
|
|
|
|
<DialogTitle>{t('source.filter.save_search.dialog.label.title')}</DialogTitle>
|
|
|
|
|
<DialogContent>
|
|
|
|
|
<TextField
|
|
|
|
|
sx={{ width: '100%' }}
|
|
|
|
|
value={newSavedSearch}
|
|
|
|
|
onChange={(e) => setNewSavedSearch(e.target.value as string)}
|
2024-09-03 17:15:47 +02:00
|
|
|
slotProps={{
|
|
|
|
|
htmlInput: { maxLength: 50 },
|
|
|
|
|
}}
|
2024-05-08 04:01:23 +02:00
|
|
|
/>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setNewSavedSearch('');
|
|
|
|
|
popupState.close();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('global.button.cancel')}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
updateSavedSearches(newSavedSearch, 'create');
|
|
|
|
|
setNewSavedSearch('');
|
|
|
|
|
popupState.close();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('global.button.ok')}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</PopupState>
|
|
|
|
|
|
|
|
|
|
<Button variant="contained" onClick={handleSubmit}>
|
|
|
|
|
{t('global.button.submit')}
|
|
|
|
|
</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
{savedSearchesExist && (
|
|
|
|
|
<>
|
|
|
|
|
<Typography sx={{ pb: 1 }}>Saved searches</Typography>
|
|
|
|
|
<Stack sx={{ flexDirection: 'row' }}>
|
|
|
|
|
{savedSearchNames.map((savedSearch) => (
|
|
|
|
|
<Chip
|
|
|
|
|
label={savedSearch}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setFilterOptions(false);
|
|
|
|
|
selectSavedSearch(savedSearch);
|
|
|
|
|
}}
|
|
|
|
|
onDelete={() => {
|
|
|
|
|
awaitConfirmation({
|
|
|
|
|
title: t('global.label.are_you_sure'),
|
|
|
|
|
message: t('source.filter.save_search.dialog.label.delete', {
|
|
|
|
|
name: savedSearch,
|
|
|
|
|
}),
|
|
|
|
|
actions: {
|
|
|
|
|
confirm: { title: t('global.button.delete') },
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.then(() => updateSavedSearches(savedSearch, 'delete'))
|
|
|
|
|
.catch(defaultPromiseErrorHandler('SourceOptions::deleteSavedSearch'));
|
|
|
|
|
}}
|
|
|
|
|
deleteIcon={
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={t('source.filter.save_search.label.delete')}>
|
2024-05-08 04:01:23 +02:00
|
|
|
<DeleteIcon />
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2024-05-08 04:01:23 +02:00
|
|
|
}
|
|
|
|
|
variant="outlined"
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Stack>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2022-03-04 01:25:10 +00:00
|
|
|
</Box>
|
2022-11-24 09:41:03 +01:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
pb: 2,
|
|
|
|
|
mx: 2,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Options
|
|
|
|
|
sourceFilter={sourceFilter}
|
|
|
|
|
updateFilterValue={updateFilterValue}
|
|
|
|
|
group={undefined}
|
|
|
|
|
update={update}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</OptionsPanel>
|
2022-03-04 01:25:10 +00:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|