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
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
2022-11-02 21:48:22 +01:00
|
|
|
import React, { useContext, useEffect } from 'react';
|
2021-10-30 16:10:34 +03:30
|
|
|
import NavbarContext from 'components/context/NavbarContext';
|
2021-08-06 04:53:53 +04:30
|
|
|
import { useParams } from 'react-router-dom';
|
2022-11-02 21:48:22 +01:00
|
|
|
import client, { useQuery } from 'util/client';
|
2023-02-08 18:19:26 +01:00
|
|
|
import { SwitchPreferenceCompat, CheckBoxPreference } from 'components/sourceConfiguration/TwoStatePreference';
|
2021-10-30 16:10:34 +03:30
|
|
|
import ListPreference from 'components/sourceConfiguration/ListPreference';
|
|
|
|
|
import EditTextPreference from 'components/sourceConfiguration/EditTextPreference';
|
2021-11-29 19:08:02 +03:30
|
|
|
import MultiSelectListPreference from 'components/sourceConfiguration/MultiSelectListPreference';
|
2021-09-09 17:51:22 +04:30
|
|
|
import List from '@mui/material/List';
|
2021-09-13 19:18:21 +04:30
|
|
|
import cloneObject from 'util/cloneObject';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { SourcePreferences } from 'typings';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2021-08-06 04:53:53 +04:30
|
|
|
|
|
|
|
|
function getPrefComponent(type: string) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'CheckBoxPreference':
|
|
|
|
|
return CheckBoxPreference;
|
2021-09-13 19:18:21 +04:30
|
|
|
case 'SwitchPreferenceCompat':
|
|
|
|
|
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:
|
|
|
|
|
return CheckBoxPreference;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function SourceConfigure() {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2021-08-06 04:53:53 +04:30
|
|
|
const { setTitle, setAction } = useContext(NavbarContext);
|
|
|
|
|
|
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);
|
2023-03-28 23:14:03 +02:00
|
|
|
}, [t]);
|
2021-08-06 04:53:53 +04:30
|
|
|
|
|
|
|
|
const { sourceId } = useParams<{ sourceId: string }>();
|
2023-02-06 10:06:33 +01:00
|
|
|
const { data: sourcePreferences = [], mutate } = useQuery<SourcePreferences[]>(
|
|
|
|
|
`/api/v1/source/${sourceId}/preferences`,
|
|
|
|
|
);
|
2021-09-13 19:18:21 +04:30
|
|
|
|
2021-11-29 19:08:02 +03:30
|
|
|
const convertToString = (position: number, value: any): string => {
|
|
|
|
|
switch (sourcePreferences[position].props.defaultValueType) {
|
|
|
|
|
case 'Set<String>':
|
|
|
|
|
return JSON.stringify(value);
|
|
|
|
|
default:
|
|
|
|
|
return value.toString();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const updateValue = (position: number) => (value: any) => {
|
|
|
|
|
client
|
|
|
|
|
.post(
|
|
|
|
|
`/api/v1/source/${sourceId}/preferences`,
|
|
|
|
|
JSON.stringify({ position, value: convertToString(position, value) }),
|
|
|
|
|
)
|
|
|
|
|
.then(() => mutate());
|
|
|
|
|
};
|
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) => {
|
|
|
|
|
const props = cloneObject(it.props);
|
|
|
|
|
props.updateValue = updateValue(index);
|
|
|
|
|
props.key = index;
|
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
|
|
|
|
|
return React.createElement(getPrefComponent(it.type), props);
|
|
|
|
|
})}
|
|
|
|
|
</List>
|
2021-08-06 04:53:53 +04:30
|
|
|
);
|
|
|
|
|
}
|