2021-08-06 04:53:53 +04:30
|
|
|
/*
|
|
|
|
|
* 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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-08-06 04:53:53 +04:30
|
|
|
|
2023-08-14 21:43:06 +02:00
|
|
|
import { createElement, useContext, useEffect } from 'react';
|
2021-08-06 04:53:53 +04:30
|
|
|
import { useParams } from 'react-router-dom';
|
2021-09-09 17:51:22 +04:30
|
|
|
import List from '@mui/material/List';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
import { cloneObject } from '@/util/cloneObject';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { SwitchPreferenceCompat, CheckBoxPreference } from '@/components/sourceConfiguration/TwoStatePreference';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { ListPreference } from '@/components/sourceConfiguration/ListPreference';
|
|
|
|
|
import { EditTextPreference } from '@/components/sourceConfiguration/EditTextPreference';
|
|
|
|
|
import { MultiSelectListPreference } from '@/components/sourceConfiguration/MultiSelectListPreference';
|
2023-09-25 23:32:26 +02:00
|
|
|
import { PreferenceProps } from '@/typings.ts';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
|
2024-04-27 16:24:55 +02:00
|
|
|
import { LoadingPlaceholder } from '@/components/util/LoadingPlaceholder.tsx';
|
2024-04-29 15:29:33 +02:00
|
|
|
import { EmptyViewAbsoluteCentered } from '@/components/util/EmptyViewAbsoluteCentered.tsx';
|
2024-04-27 22:28:55 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
2024-07-03 23:15:50 +02:00
|
|
|
import { GetCategoriesSettingsQueryVariables, GetSourceSettingsQuery } from '@/lib/graphql/generated/graphql.ts';
|
|
|
|
|
import { GET_SOURCE_SETTINGS } from '@/lib/graphql/queries/SourceQuery.ts';
|
2021-08-06 04:53:53 +04:30
|
|
|
|
|
|
|
|
function getPrefComponent(type: string) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'CheckBoxPreference':
|
|
|
|
|
return CheckBoxPreference;
|
2023-09-25 23:32:26 +02:00
|
|
|
case 'SwitchPreference':
|
2021-09-13 19:18:21 +04:30
|
|
|
return SwitchPreferenceCompat;
|
|
|
|
|
case 'ListPreference':
|
|
|
|
|
return ListPreference;
|
|
|
|
|
case 'EditTextPreference':
|
|
|
|
|
return EditTextPreference;
|
2021-11-29 19:08:02 +03:30
|
|
|
case 'MultiSelectListPreference':
|
|
|
|
|
return MultiSelectListPreference;
|
2021-08-06 04:53:53 +04:30
|
|
|
default:
|
2023-09-25 23:32:26 +02:00
|
|
|
throw new Error(`Unexpected preference type "${type}"`);
|
2021-08-06 04:53:53 +04:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export function SourceConfigure() {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2023-10-28 00:32:02 +02:00
|
|
|
const { setTitle, setAction } = useContext(NavBarContext);
|
2021-08-06 04:53:53 +04:30
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
useEffect(() => {
|
2023-03-23 13:59:32 +01:00
|
|
|
setTitle(t('source.configuration.title'));
|
2023-03-11 18:05:58 +01:00
|
|
|
setAction(null);
|
2024-01-26 20:50:51 +01:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
setTitle('');
|
|
|
|
|
setAction(null);
|
|
|
|
|
};
|
2023-03-28 23:14:03 +02:00
|
|
|
}, [t]);
|
2021-08-06 04:53:53 +04:30
|
|
|
|
|
|
|
|
const { sourceId } = useParams<{ sourceId: string }>();
|
2024-07-03 23:15:50 +02:00
|
|
|
const { data, loading, error, refetch } = requestManager.useGetSource<
|
|
|
|
|
GetSourceSettingsQuery,
|
|
|
|
|
GetCategoriesSettingsQueryVariables
|
|
|
|
|
>(GET_SOURCE_SETTINGS, sourceId, {
|
2024-04-27 22:28:55 +02:00
|
|
|
notifyOnNetworkStatusChange: true,
|
|
|
|
|
});
|
2023-09-25 23:32:26 +02:00
|
|
|
const sourcePreferences = data?.source.preferences ?? [];
|
2021-11-29 19:08:02 +03:30
|
|
|
|
2023-09-25 23:32:26 +02:00
|
|
|
const updateValue =
|
|
|
|
|
(position: number): PreferenceProps['updateValue'] =>
|
|
|
|
|
(type, value) => {
|
|
|
|
|
requestManager.setSourcePreferences(sourceId, { position, [type]: value });
|
|
|
|
|
};
|
2021-08-06 04:53:53 +04:30
|
|
|
|
2024-04-27 16:24:55 +02:00
|
|
|
if (loading) {
|
|
|
|
|
return <LoadingPlaceholder />;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 22:28:55 +02:00
|
|
|
if (error) {
|
|
|
|
|
return (
|
2024-04-29 15:29:33 +02:00
|
|
|
<EmptyViewAbsoluteCentered
|
2024-04-27 22:28:55 +02:00
|
|
|
message={t('global.error.label.failed_to_load_data')}
|
|
|
|
|
messageExtra={error.message}
|
|
|
|
|
retry={() => refetch().catch(defaultPromiseErrorHandler('SourceConfigure::refetch'))}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 04:53:53 +04:30
|
|
|
return (
|
2023-03-11 18:05:58 +01:00
|
|
|
<List sx={{ padding: 0 }}>
|
|
|
|
|
{sourcePreferences.map((it, index) => {
|
2023-09-25 23:32:26 +02:00
|
|
|
const props = cloneObject(it);
|
2021-09-13 19:18:21 +04:30
|
|
|
|
2023-03-11 18:05:58 +01:00
|
|
|
// TypeScript is dumb in detecting extra props
|
|
|
|
|
// @ts-ignore
|
2023-09-25 23:32:26 +02:00
|
|
|
return createElement(getPrefComponent(it.type), {
|
|
|
|
|
...props,
|
|
|
|
|
updateValue: updateValue(index),
|
|
|
|
|
});
|
2023-03-11 18:05:58 +01:00
|
|
|
})}
|
|
|
|
|
</List>
|
2021-08-06 04:53:53 +04:30
|
|
|
);
|
|
|
|
|
}
|