Feature/support settings per device (#640)
* Optionally prevent deletion and mutation of "MutableListSetting" items * Save server metadata per device This makes it possible to e.g. have different settings on a desktop and a smartphone.
This commit is contained in:
15
src/util/HelperFunctions.ts
Normal file
15
src/util/HelperFunctions.ts
Normal 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 jsonSaveParse = <T = any>(...args: Parameters<typeof JSON.parse>): T | null => {
|
||||
try {
|
||||
return JSON.parse(...args);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
15
src/util/device.ts
Normal file
15
src/util/device.ts
Normal 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;
|
||||
};
|
||||
@@ -20,9 +20,12 @@ import {
|
||||
} from '@/typings';
|
||||
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
||||
import { MetaType } from '@/lib/graphql/generated/graphql.ts';
|
||||
import { DEFAULT_DEVICE, getActiveDevice } from '@/util/device.ts';
|
||||
|
||||
const APP_METADATA_KEY_PREFIX = 'webUI_';
|
||||
|
||||
const GLOBAL_METADATA_KEYS: AppMetadataKeys[] = ['devices', 'activeDevice'];
|
||||
|
||||
/**
|
||||
* Once all changes have been done in the current branch, a new migration for all changes should be
|
||||
* created.
|
||||
@@ -97,7 +100,12 @@ const getAppKeyPrefixForMigration = (migrationId: number): string => {
|
||||
return appKeyPrefix?.appKeyPrefix?.newPrefix ?? APP_METADATA_KEY_PREFIX;
|
||||
};
|
||||
|
||||
const getMetadataKey = (key: string, appPrefix: string = APP_METADATA_KEY_PREFIX) => `${appPrefix}${key}`;
|
||||
const getMetadataKey = (key: string, appPrefix: string = APP_METADATA_KEY_PREFIX) => {
|
||||
const isGlobalMetadataKey = GLOBAL_METADATA_KEYS.includes(key as AppMetadataKeys);
|
||||
const addActiveDevicePrefix = !isGlobalMetadataKey && getActiveDevice() !== DEFAULT_DEVICE;
|
||||
|
||||
return `${appPrefix}${addActiveDevicePrefix ? `${getActiveDevice()}_` : ''}${key}`;
|
||||
};
|
||||
|
||||
const doesMetadataKeyExistIn = (meta: Metadata | undefined, key: string, appPrefix?: string): boolean =>
|
||||
Object.prototype.hasOwnProperty.call(meta ?? {}, getMetadataKey(key, appPrefix));
|
||||
|
||||
@@ -6,9 +6,11 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { Metadata, MetadataServerSettings } from '@/typings';
|
||||
import { AllowedMetadataValueTypes, AppMetadataKeys, Metadata, MetadataServerSettings } from '@/typings';
|
||||
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
||||
import { convertFromGqlMeta, getMetadataFrom } from '@/util/metadata';
|
||||
import { jsonSaveParse } from '@/util/HelperFunctions.ts';
|
||||
import { DEFAULT_DEVICE } from '@/util/device.ts';
|
||||
|
||||
export const getDefaultSettings = (): MetadataServerSettings => ({
|
||||
// downloads
|
||||
@@ -21,13 +23,36 @@ export const getDefaultSettings = (): MetadataServerSettings => ({
|
||||
showAddToLibraryCategorySelectDialog: true,
|
||||
ignoreFilters: false,
|
||||
removeMangaFromCategories: false,
|
||||
|
||||
// client
|
||||
devices: [DEFAULT_DEVICE],
|
||||
activeDevice: DEFAULT_DEVICE,
|
||||
});
|
||||
|
||||
export const convertSettingsToMetadata = (
|
||||
settings: Partial<MetadataServerSettings>,
|
||||
): Metadata<string, AllowedMetadataValueTypes> => ({
|
||||
...settings,
|
||||
devices: JSON.stringify(settings.devices),
|
||||
});
|
||||
|
||||
export const convertMetadataToSettings = (
|
||||
metadata: Partial<Metadata<AppMetadataKeys, AllowedMetadataValueTypes>>,
|
||||
): MetadataServerSettings =>
|
||||
({
|
||||
...getDefaultSettings(),
|
||||
...(metadata as unknown as MetadataServerSettings),
|
||||
devices: jsonSaveParse<string[]>((metadata.devices as string) ?? '') ?? getDefaultSettings().devices,
|
||||
}) satisfies MetadataServerSettings;
|
||||
|
||||
const getMetadataServerSettingsWithDefaultFallback = (
|
||||
meta?: Metadata,
|
||||
defaultSettings: MetadataServerSettings = getDefaultSettings(),
|
||||
applyMetadataMigration: boolean = true,
|
||||
): MetadataServerSettings => getMetadataFrom({ meta }, defaultSettings, applyMetadataMigration);
|
||||
): MetadataServerSettings =>
|
||||
convertMetadataToSettings(
|
||||
getMetadataFrom({ meta }, convertSettingsToMetadata(defaultSettings), applyMetadataMigration),
|
||||
);
|
||||
export const useMetadataServerSettings = (): {
|
||||
metadata?: Metadata;
|
||||
settings: MetadataServerSettings;
|
||||
|
||||
Reference in New Issue
Block a user