2023-11-19 21:17:35 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-03-06 23:07:48 +01:00
|
|
|
import { AllowedMetadataValueTypes, AppMetadataKeys, Metadata, MetadataServerSettings } from '@/typings';
|
2023-11-19 21:17:35 +01:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
import { convertFromGqlMeta, getMetadataFrom } from '@/util/metadata';
|
2024-03-06 23:07:48 +01:00
|
|
|
import { jsonSaveParse } from '@/util/HelperFunctions.ts';
|
|
|
|
|
import { DEFAULT_DEVICE } from '@/util/device.ts';
|
2023-11-19 21:17:35 +01:00
|
|
|
|
|
|
|
|
export const getDefaultSettings = (): MetadataServerSettings => ({
|
2023-12-27 18:08:16 +01:00
|
|
|
// downloads
|
2023-11-19 21:17:35 +01:00
|
|
|
deleteChaptersManuallyMarkedRead: false,
|
2023-12-05 22:20:13 +01:00
|
|
|
deleteChaptersWhileReading: 0,
|
2023-11-19 21:17:35 +01:00
|
|
|
deleteChaptersWithBookmark: false,
|
2024-02-16 19:16:18 +01:00
|
|
|
downloadAheadLimit: 0,
|
2023-12-27 18:08:16 +01:00
|
|
|
|
|
|
|
|
// library
|
|
|
|
|
showAddToLibraryCategorySelectDialog: true,
|
|
|
|
|
ignoreFilters: false,
|
2024-03-02 15:28:58 +01:00
|
|
|
removeMangaFromCategories: false,
|
2024-03-06 23:07:48 +01:00
|
|
|
|
|
|
|
|
// client
|
|
|
|
|
devices: [DEFAULT_DEVICE],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const convertSettingsToMetadata = (
|
|
|
|
|
settings: Partial<MetadataServerSettings>,
|
|
|
|
|
): Metadata<string, AllowedMetadataValueTypes> => ({
|
|
|
|
|
...settings,
|
|
|
|
|
devices: JSON.stringify(settings.devices),
|
2023-11-19 21:17:35 +01:00
|
|
|
});
|
|
|
|
|
|
2024-03-06 23:07:48 +01:00
|
|
|
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;
|
|
|
|
|
|
2023-11-19 21:17:35 +01:00
|
|
|
const getMetadataServerSettingsWithDefaultFallback = (
|
|
|
|
|
meta?: Metadata,
|
|
|
|
|
defaultSettings: MetadataServerSettings = getDefaultSettings(),
|
|
|
|
|
applyMetadataMigration: boolean = true,
|
2024-03-06 23:07:48 +01:00
|
|
|
): MetadataServerSettings =>
|
|
|
|
|
convertMetadataToSettings(
|
|
|
|
|
getMetadataFrom({ meta }, convertSettingsToMetadata(defaultSettings), applyMetadataMigration),
|
|
|
|
|
);
|
2023-11-19 21:17:35 +01:00
|
|
|
export const useMetadataServerSettings = (): {
|
|
|
|
|
metadata?: Metadata;
|
|
|
|
|
settings: MetadataServerSettings;
|
|
|
|
|
loading: boolean;
|
|
|
|
|
} => {
|
|
|
|
|
const { data, loading } = requestManager.useGetGlobalMeta();
|
|
|
|
|
const metadata = convertFromGqlMeta(data?.metas.nodes);
|
|
|
|
|
const settings = getMetadataServerSettingsWithDefaultFallback(metadata);
|
|
|
|
|
|
|
|
|
|
return { metadata, settings, loading };
|
|
|
|
|
};
|
2023-12-25 21:37:45 +01:00
|
|
|
|
|
|
|
|
export const getMetadataServerSettings = async (): Promise<MetadataServerSettings> => {
|
|
|
|
|
const { data, error } = await requestManager.getGlobalMeta().response;
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const metadata = convertFromGqlMeta(data?.metas.nodes);
|
|
|
|
|
return getMetadataServerSettingsWithDefaultFallback(metadata);
|
|
|
|
|
};
|