/* * 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 from '@mui/material/List'; import ListItem from '@mui/material/ListItem'; import ListItemText from '@mui/material/ListItemText'; import MenuItem from '@mui/material/MenuItem'; import { useTranslation } from 'react-i18next'; import { useContext, useLayoutEffect } from 'react'; import { updateMetadataServerSettings, useMetadataServerSettings, } from '@/modules/settings/services/ServerSettingsMetadata.ts'; import { makeToast } from '@/modules/core/utils/Toast.ts'; import { MutableListSetting } from '@/modules/core/components/settings/MutableListSetting.tsx'; import { NavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx'; import { DeviceContext } from '@/modules/device/contexts/DeviceContext.tsx'; import { Select } from '@/modules/core/components/inputs/Select.tsx'; import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx'; import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder/EmptyViewAbsoluteCentered.tsx'; import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts'; import { DEFAULT_DEVICE } from '@/modules/device/services/Device.ts'; import { MetadataServerSettingKeys, MetadataServerSettings } from '@/modules/settings/Settings.types.ts'; import { getErrorMessage } from '@/lib/HelperFunctions.ts'; export const DeviceSetting = () => { const { t } = useTranslation(); const { setTitle, setAction } = useContext(NavBarContext); useLayoutEffect(() => { setTitle(t('settings.device.title.settings')); setAction(null); return () => { setTitle(''); setAction(null); }; }, [t]); const { metadata, settings: { devices }, loading, request: { error, refetch }, } = useMetadataServerSettings(); const { activeDevice, setActiveDevice } = useContext(DeviceContext); const updateMetadataSetting = ( setting: Setting, value: MetadataServerSettings[Setting], ) => { if (!metadata) { return; } const wasActiveDeviceDeleted = setting === 'devices' && !(value as string[]).includes(activeDevice); if (wasActiveDeviceDeleted) { setActiveDevice(DEFAULT_DEVICE); } updateMetadataServerSettings(setting, value).catch((e) => makeToast(t('global.error.label.failed_to_save_changes'), 'error', getErrorMessage(e)), ); }; if (loading) { return ; } if (error) { return ( refetch().catch(defaultPromiseErrorHandler('DeviceSetting::refetch'))} /> ); } return ( { updateMetadataSetting('devices', [ ...new Set( [DEFAULT_DEVICE, ...deviceList].filter((device) => device !== t('global.label.default')), ), ]); }} valueInfos={devices.map((device) => [ device === DEFAULT_DEVICE ? t('global.label.default') : device, { mutable: false, deletable: device !== DEFAULT_DEVICE }, ])} addItemButtonTitle={t('global.button.create')} validateItem={(device) => device.length <= 16 && !!device.match(/^[a-zA-Z0-9\-_]+$/g)} placeholder={t('settings.device.label.placeholder')} /> ); };