2024-03-06 23:07:48 +01: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 { List, ListItem, ListItemText, MenuItem, Select } from '@mui/material';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import { useContext, useEffect } from 'react';
|
|
|
|
|
import { convertSettingsToMetadata, useMetadataServerSettings } from '@/util/metadataServerSettings.ts';
|
|
|
|
|
import { MetadataServerSettingKeys, MetadataServerSettings } from '@/typings.ts';
|
|
|
|
|
import { convertToGqlMeta, requestUpdateServerMetadata } from '@/util/metadata.ts';
|
|
|
|
|
import { makeToast } from '@/components/util/Toast.tsx';
|
|
|
|
|
import { MutableListSetting } from '@/components/settings/MutableListSetting.tsx';
|
2024-03-07 01:12:35 +01:00
|
|
|
import { NavBarContext, useSetDefaultBackTo } from '@/components/context/NavbarContext.tsx';
|
|
|
|
|
import { ActiveDevice, DEFAULT_DEVICE } from '@/util/device.ts';
|
2024-03-06 23:07:48 +01:00
|
|
|
|
|
|
|
|
export const DeviceSetting = () => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const { setTitle, setAction } = useContext(NavBarContext);
|
|
|
|
|
|
2024-03-07 01:12:35 +01:00
|
|
|
useSetDefaultBackTo('settings');
|
|
|
|
|
|
2024-03-06 23:07:48 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
setTitle(t('settings.device.title.settings'));
|
|
|
|
|
setAction(null);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
setTitle('');
|
|
|
|
|
setAction(null);
|
|
|
|
|
};
|
|
|
|
|
}, [t]);
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
metadata,
|
2024-03-07 01:12:35 +01:00
|
|
|
settings: { devices },
|
2024-03-06 23:07:48 +01:00
|
|
|
} = useMetadataServerSettings();
|
|
|
|
|
|
2024-03-07 01:12:35 +01:00
|
|
|
const { activeDevice, setActiveDevice } = useContext(ActiveDevice);
|
|
|
|
|
|
|
|
|
|
const updateMetadataSetting = <Setting extends MetadataServerSettingKeys>(
|
2024-03-06 23:07:48 +01:00
|
|
|
setting: Setting,
|
|
|
|
|
value: MetadataServerSettings[Setting],
|
|
|
|
|
) => {
|
|
|
|
|
if (!metadata) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const wasActiveDeviceDeleted = setting === 'devices' && !(value as string[]).includes(activeDevice);
|
|
|
|
|
if (wasActiveDeviceDeleted) {
|
2024-03-07 01:12:35 +01:00
|
|
|
setActiveDevice(DEFAULT_DEVICE);
|
2024-03-06 23:07:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestUpdateServerMetadata(convertToGqlMeta(metadata) ?? [], [
|
|
|
|
|
[setting, convertSettingsToMetadata({ [setting]: value })[setting]],
|
|
|
|
|
]).catch(() => makeToast(t('global.error.label.failed_to_save_changes'), 'error'));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<List>
|
|
|
|
|
<MutableListSetting
|
|
|
|
|
settingName={t('settings.device.devices.label.title')}
|
|
|
|
|
description={t('settings.device.devices.label.description')}
|
|
|
|
|
handleChange={(deviceList) => {
|
|
|
|
|
updateMetadataSetting('devices', [...new Set([DEFAULT_DEVICE, ...deviceList])]);
|
|
|
|
|
}}
|
|
|
|
|
valueInfos={devices.map((device) => [device, { mutable: false, deletable: device !== DEFAULT_DEVICE }])}
|
|
|
|
|
addItemButtonTitle={t('global.button.create')}
|
2024-03-06 23:44:05 +01:00
|
|
|
validateItem={(device) => device.length <= 16 && !!device.match(/^[a-zA-Z0-9\-_]+$/g)}
|
|
|
|
|
placeholder={t('settings.device.label.placeholder')}
|
2024-03-06 23:07:48 +01:00
|
|
|
/>
|
|
|
|
|
<ListItem>
|
|
|
|
|
<ListItemText
|
|
|
|
|
primary={t('settings.device.active_device.label.title')}
|
|
|
|
|
secondary={t('settings.device.active_device.label.description')}
|
|
|
|
|
/>
|
|
|
|
|
<Select
|
|
|
|
|
MenuProps={{ PaperProps: { style: { maxHeight: 150 } } }}
|
|
|
|
|
value={activeDevice}
|
2024-03-07 01:12:35 +01:00
|
|
|
onChange={({ target: { value: device } }) => setActiveDevice(device)}
|
2024-03-06 23:07:48 +01:00
|
|
|
>
|
|
|
|
|
{devices.map((device) => (
|
|
|
|
|
<MenuItem key={device} value={device}>
|
|
|
|
|
{device}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
</ListItem>
|
|
|
|
|
</List>
|
|
|
|
|
);
|
|
|
|
|
};
|