Move device files into new folder

This commit is contained in:
schroda
2024-10-05 22:27:19 +02:00
parent c0eabf8865
commit 3244a6e3df
9 changed files with 52 additions and 27 deletions

View File

@@ -22,7 +22,7 @@ import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx';
import { ThemeMode, ThemeModeContext } from '@/modules/theme/contexts/ThemeModeContext.tsx';
import { NavBarContextProvider } from '@/modules/navigation-bar/contexts/NavBarContextProvider.tsx';
import { LibraryOptionsContextProvider } from '@/modules/library/contexts/LibraryOptionsProvider.tsx';
import { ActiveDevice, DEFAULT_DEVICE, setActiveDevice } from '@/util/device.ts';
import { ActiveDeviceContextProvider } from '@/modules/device/contexts/DeviceContext.tsx';
import { MediaQuery } from '@/lib/ui/MediaQuery.tsx';
import { AppThemes, getTheme } from '@/modules/theme/services/AppThemes.ts';
import { useMetadataServerSettings } from '@/lib/metadata/metadataServerSettings.ts';
@@ -66,7 +66,6 @@ export const AppContext: React.FC<Props> = ({ children }) => {
const [appTheme, setAppTheme] = useLocalStorage<AppThemes>('appTheme', 'default');
const [themeMode, setThemeMode] = useLocalStorage<ThemeMode>('themeMode', ThemeMode.SYSTEM);
const [pureBlackMode, setPureBlackMode] = useLocalStorage<boolean>('pureBlackMode', false);
const [activeDevice, setActiveDeviceContext] = useLocalStorage('activeDevice', DEFAULT_DEVICE);
const darkThemeContext = useMemo(
() => ({
@@ -80,18 +79,11 @@ export const AppContext: React.FC<Props> = ({ children }) => {
[themeMode, pureBlackMode, appTheme],
);
const activeDeviceContext = useMemo(
() => ({ activeDevice, setActiveDevice: setActiveDeviceContext }),
[activeDevice],
);
const theme = useMemo(
() => createAndSetTheme(themeMode, getTheme(appTheme, customThemes), pureBlackMode, currentDirection),
[themeMode, currentDirection, systemThemeMode, pureBlackMode, appTheme, customThemes],
);
setActiveDevice(activeDevice);
return (
<Router>
<StyledEngineProvider injectFirst>
@@ -101,9 +93,9 @@ export const AppContext: React.FC<Props> = ({ children }) => {
<QueryParamProvider adapter={ReactRouter6Adapter}>
<LibraryOptionsContextProvider>
<NavBarContextProvider>
<ActiveDevice.Provider value={activeDeviceContext}>
<ActiveDeviceContextProvider>
<SnackbarProvider>{children}</SnackbarProvider>
</ActiveDevice.Provider>
</ActiveDeviceContextProvider>
</NavBarContextProvider>
</LibraryOptionsContextProvider>
</QueryParamProvider>

View File

@@ -0,0 +1,11 @@
/*
* 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/.
*/
export type MetadataClientSettings = {
devices: string[];
};

View File

@@ -0,0 +1,31 @@
/*
* 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 React, { ReactNode, useMemo } from 'react';
import { DEFAULT_DEVICE, setActiveDevice } from '@/modules/device/services/Device.ts';
import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx';
export const DeviceContext = React.createContext<{
activeDevice: string;
setActiveDevice: (device: string) => void;
}>({
activeDevice: DEFAULT_DEVICE,
setActiveDevice: () => {},
});
export const ActiveDeviceContextProvider = ({ children }: { children?: ReactNode }) => {
const [activeDevice, setActiveDeviceContext] = useLocalStorage('activeDevice', DEFAULT_DEVICE);
const activeDeviceContext = useMemo(
() => ({ activeDevice, setActiveDevice: setActiveDeviceContext }),
[activeDevice],
);
setActiveDevice(activeDevice);
return <DeviceContext.Provider value={activeDeviceContext}>{children}</DeviceContext.Provider>;
};

View File

@@ -0,0 +1,110 @@
/*
* 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 '@/lib/metadata/metadataServerSettings.ts';
import { MetadataServerSettingKeys, MetadataServerSettings } from '@/typings.ts';
import { makeToast } from '@/lib/ui/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';
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 extends MetadataServerSettingKeys>(
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(() =>
makeToast(t('global.error.label.failed_to_save_changes'), 'error'),
);
};
if (loading) {
return <LoadingPlaceholder />;
}
if (error) {
return (
<EmptyViewAbsoluteCentered
message={t('global.error.label.failed_to_load_data')}
messageExtra={error.message}
retry={() => refetch().catch(defaultPromiseErrorHandler('DeviceSetting::refetch'))}
/>
);
}
return (
<List sx={{ pt: 0 }}>
<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')}
validateItem={(device) => device.length <= 16 && !!device.match(/^[a-zA-Z0-9\-_]+$/g)}
placeholder={t('settings.device.label.placeholder')}
/>
<ListItem>
<ListItemText
primary={t('settings.device.active_device.label.title')}
secondary={t('settings.device.active_device.label.description')}
/>
<Select value={activeDevice} onChange={({ target: { value: device } }) => setActiveDevice(device)}>
{devices.map((device) => (
<MenuItem key={device} value={device}>
{device}
</MenuItem>
))}
</Select>
</ListItem>
</List>
);
};

View File

@@ -0,0 +1,15 @@
/*
* 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/.
*/
export const DEFAULT_DEVICE = 'default';
let activeDevice = DEFAULT_DEVICE;
export const getActiveDevice = (): string => activeDevice;
export const setActiveDevice = (device: string) => {
activeDevice = device;
};