2025-04-20 21:40:28 +02: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 { useEffect, useMemo } from 'react';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { DEFAULT_CHAPTER_OPTIONS } from '@/features/chapter/Chapter.constants.ts';
|
|
|
|
|
import { getMetadataFrom } from '@/features/metadata/services/MetadataReader.ts';
|
|
|
|
|
import { MangaIdInfo, MangaMetadata, MangaMetadataKeys } from '@/features/manga/Manga.types.ts';
|
2025-04-20 21:40:28 +02:00
|
|
|
import {
|
|
|
|
|
AllowedMetadataValueTypes,
|
|
|
|
|
GqlMetaHolder,
|
|
|
|
|
Metadata,
|
|
|
|
|
MetadataHolder,
|
2025-08-15 22:02:58 +02:00
|
|
|
} from '@/features/metadata/Metadata.types.ts';
|
|
|
|
|
import { convertFromGqlMeta } from '@/features/metadata/services/MetadataConverter.ts';
|
2026-01-25 20:32:56 +01:00
|
|
|
import { requestMangaMetadataUpdate } from '@/features/metadata/services/MetadataUpdater.ts';
|
2025-04-20 21:40:28 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
|
|
|
|
|
|
|
|
|
const DEFAULT_MANGA_METADATA: MangaMetadata = {
|
|
|
|
|
...DEFAULT_CHAPTER_OPTIONS,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const convertAppMetadataToGqlMetadata = (
|
|
|
|
|
metadata: Partial<MangaMetadata>,
|
|
|
|
|
): Metadata<string, AllowedMetadataValueTypes> => ({
|
|
|
|
|
...metadata,
|
2025-06-09 15:32:04 +02:00
|
|
|
excludedScanlators: JSON.stringify(metadata.excludedScanlators),
|
2025-04-20 21:40:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const getMangaMetadataWithDefaultValueFallback = (
|
|
|
|
|
meta: MangaIdInfo & MetadataHolder,
|
|
|
|
|
defaultMetadata: MangaMetadata = DEFAULT_MANGA_METADATA,
|
|
|
|
|
useEffectFn?: typeof useEffect,
|
2025-06-25 00:38:01 +02:00
|
|
|
): MangaMetadata => getMetadataFrom('manga', meta, defaultMetadata, undefined, useEffectFn);
|
2025-04-20 21:40:28 +02:00
|
|
|
|
|
|
|
|
const getMetadata = (
|
|
|
|
|
metaHolder: MangaIdInfo & GqlMetaHolder,
|
|
|
|
|
defaultMetadata?: MangaMetadata,
|
|
|
|
|
useEffectFn?: typeof useEffect,
|
|
|
|
|
) =>
|
|
|
|
|
getMangaMetadataWithDefaultValueFallback(
|
|
|
|
|
{ ...metaHolder, meta: convertFromGqlMeta(metaHolder.meta) },
|
|
|
|
|
defaultMetadata,
|
|
|
|
|
useEffectFn,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const getMangaMetadata = (
|
|
|
|
|
metaHolder: MangaIdInfo & GqlMetaHolder,
|
|
|
|
|
defaultMetadata?: MangaMetadata,
|
|
|
|
|
): MangaMetadata => getMetadata(metaHolder, defaultMetadata);
|
|
|
|
|
|
|
|
|
|
export const useGetMangaMetadata = (
|
|
|
|
|
metaHolder: MangaIdInfo & GqlMetaHolder,
|
|
|
|
|
defaultMetadata?: MangaMetadata,
|
|
|
|
|
): MangaMetadata => {
|
|
|
|
|
const metadata = getMetadata(metaHolder, defaultMetadata, useEffect);
|
|
|
|
|
return useMemo(() => metadata, [metaHolder, defaultMetadata]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const updateMangaMetadata = async <
|
|
|
|
|
MetadataKeys extends MangaMetadataKeys = MangaMetadataKeys,
|
|
|
|
|
MetadataKey extends MetadataKeys = MetadataKeys,
|
|
|
|
|
>(
|
|
|
|
|
manga: MangaIdInfo & GqlMetaHolder,
|
|
|
|
|
metadataKey: MetadataKey,
|
|
|
|
|
value: MangaMetadata[MetadataKey],
|
2026-01-24 21:04:06 +01:00
|
|
|
): Promise<void> =>
|
2026-01-25 20:32:56 +01:00
|
|
|
requestMangaMetadataUpdate(manga, {
|
|
|
|
|
update: [[metadataKey, convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey]]],
|
|
|
|
|
});
|
2025-04-20 21:40:28 +02:00
|
|
|
|
|
|
|
|
export const createUpdateMangaMetadata =
|
|
|
|
|
<Settings extends MangaMetadataKeys>(
|
|
|
|
|
manga: MangaIdInfo & GqlMetaHolder,
|
|
|
|
|
handleError: (error: any) => void = defaultPromiseErrorHandler('createUpdateMangaMetadata'),
|
2026-01-24 21:04:06 +01:00
|
|
|
): ((...args: OmitFirst<Parameters<typeof updateMangaMetadata<Settings>>>) => Promise<void>) =>
|
2025-04-20 21:40:28 +02:00
|
|
|
(metadataKey, value) =>
|
|
|
|
|
updateMangaMetadata(manga, metadataKey, value).catch(handleError);
|