2024-10-05 23:17:20 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-10-26 17:25:04 +02:00
|
|
|
import { useEffect } from 'react';
|
2025-01-05 02:15:03 +01:00
|
|
|
import { convertValueFromMetadata } from '@/modules/metadata/services/MetadataConverter.ts';
|
2024-10-05 23:17:20 +02:00
|
|
|
import {
|
|
|
|
|
AllowedMetadataValueTypes,
|
|
|
|
|
AppMetadataKeys,
|
|
|
|
|
Metadata,
|
|
|
|
|
MetadataHolder,
|
2024-10-26 17:25:04 +02:00
|
|
|
MetadataHolderType,
|
2024-10-05 23:17:20 +02:00
|
|
|
} from '@/modules/metadata/Metadata.types.ts';
|
2024-10-26 17:25:04 +02:00
|
|
|
import { MangaIdInfo } from '@/modules/manga/Manga.types.ts';
|
2025-01-05 02:15:03 +01:00
|
|
|
|
2024-10-26 17:25:04 +02:00
|
|
|
import { ChapterIdInfo } from '@/modules/chapter/services/Chapters.ts';
|
|
|
|
|
import { CategoryIdInfo } from '@/modules/category/Category.types.ts';
|
|
|
|
|
import { SourceType } from '@/lib/graphql/generated/graphql.ts';
|
2025-01-05 02:15:03 +01:00
|
|
|
import { doesMetadataKeyExistIn, getMetadataKey } from '@/modules/metadata/Metadata.utils.ts';
|
2025-01-04 21:42:41 +01:00
|
|
|
import { applyMetadataMigrations } from '@/modules/metadata/services/MetadataMigrations.ts';
|
2024-10-05 23:17:20 +02:00
|
|
|
|
2025-01-05 14:54:20 +01:00
|
|
|
const getMetadataValueFrom = <Key extends AppMetadataKeys, Value extends AllowedMetadataValueTypes>(
|
2025-01-04 21:42:41 +01:00
|
|
|
metadata: Metadata | undefined,
|
2024-10-05 23:17:20 +02:00
|
|
|
key: Key,
|
|
|
|
|
defaultValue?: Value,
|
2024-10-28 18:03:29 +01:00
|
|
|
prefixes?: string[],
|
2024-10-05 23:17:20 +02:00
|
|
|
): Value | undefined => {
|
2024-10-14 03:12:51 +02:00
|
|
|
if (
|
|
|
|
|
metadata === undefined ||
|
2024-10-28 18:03:29 +01:00
|
|
|
!doesMetadataKeyExistIn(metadata, key, prefixes) ||
|
|
|
|
|
metadata[getMetadataKey(key, prefixes)] === undefined
|
2024-10-14 03:12:51 +02:00
|
|
|
) {
|
2024-10-05 23:17:20 +02:00
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 18:03:29 +01:00
|
|
|
return convertValueFromMetadata(metadata[getMetadataKey(key, prefixes)]);
|
2024-10-05 23:17:20 +02:00
|
|
|
};
|
|
|
|
|
|
2024-10-26 17:25:04 +02:00
|
|
|
export function getMetadataFrom<METADATA extends Partial<Metadata<AppMetadataKeys, AllowedMetadataValueTypes>>>(
|
|
|
|
|
type: 'global',
|
|
|
|
|
metadataHolder: MetadataHolder,
|
|
|
|
|
metadataWithDefaultValues: METADATA,
|
2024-10-28 18:03:29 +01:00
|
|
|
prefixes?: string[],
|
2024-10-26 17:25:04 +02:00
|
|
|
useEffectFn?: typeof useEffect,
|
|
|
|
|
): METADATA;
|
|
|
|
|
export function getMetadataFrom<METADATA extends Partial<Metadata<AppMetadataKeys, AllowedMetadataValueTypes>>>(
|
|
|
|
|
type: 'manga',
|
|
|
|
|
metadataHolder: MangaIdInfo & MetadataHolder,
|
2024-10-05 23:17:20 +02:00
|
|
|
metadataWithDefaultValues: METADATA,
|
2024-10-28 18:03:29 +01:00
|
|
|
prefixes?: string[],
|
2024-10-26 17:25:04 +02:00
|
|
|
useEffectFn?: typeof useEffect,
|
|
|
|
|
): METADATA;
|
|
|
|
|
export function getMetadataFrom<METADATA extends Partial<Metadata<AppMetadataKeys, AllowedMetadataValueTypes>>>(
|
|
|
|
|
type: 'chapter',
|
|
|
|
|
metadataHolder: ChapterIdInfo & MetadataHolder,
|
|
|
|
|
metadataWithDefaultValues: METADATA,
|
2024-10-28 18:03:29 +01:00
|
|
|
prefixes?: string[],
|
2024-10-26 17:25:04 +02:00
|
|
|
useEffectFn?: typeof useEffect,
|
|
|
|
|
): METADATA;
|
|
|
|
|
export function getMetadataFrom<METADATA extends Partial<Metadata<AppMetadataKeys, AllowedMetadataValueTypes>>>(
|
|
|
|
|
type: 'category',
|
|
|
|
|
metadataHolder: CategoryIdInfo & MetadataHolder,
|
|
|
|
|
metadataWithDefaultValues: METADATA,
|
2024-10-28 18:03:29 +01:00
|
|
|
prefixes?: string[],
|
2024-10-26 17:25:04 +02:00
|
|
|
useEffectFn?: typeof useEffect,
|
|
|
|
|
): METADATA;
|
|
|
|
|
export function getMetadataFrom<METADATA extends Partial<Metadata<AppMetadataKeys, AllowedMetadataValueTypes>>>(
|
|
|
|
|
type: 'source',
|
|
|
|
|
metadataHolder: Pick<SourceType, 'id'> & MetadataHolder,
|
|
|
|
|
metadataWithDefaultValues: METADATA,
|
2024-10-28 18:03:29 +01:00
|
|
|
prefixes?: string[],
|
2024-10-26 17:25:04 +02:00
|
|
|
useEffectFn?: typeof useEffect,
|
|
|
|
|
): METADATA;
|
|
|
|
|
export function getMetadataFrom<METADATA extends Partial<Metadata<AppMetadataKeys, AllowedMetadataValueTypes>>>(
|
|
|
|
|
type: MetadataHolderType,
|
|
|
|
|
metadataHolder:
|
|
|
|
|
| MetadataHolder
|
|
|
|
|
| (MangaIdInfo & MetadataHolder)
|
|
|
|
|
| (ChapterIdInfo & MetadataHolder)
|
|
|
|
|
| (CategoryIdInfo & MetadataHolder)
|
|
|
|
|
| (Pick<SourceType, 'id'> & MetadataHolder),
|
|
|
|
|
metadataWithDefaultValues: METADATA,
|
2024-10-28 18:03:29 +01:00
|
|
|
prefixes?: string[],
|
2024-10-26 17:25:04 +02:00
|
|
|
useEffectFn: typeof useEffect = (fn: () => void) => fn(),
|
|
|
|
|
): METADATA {
|
2025-01-05 02:15:03 +01:00
|
|
|
const migratedMetadata = applyMetadataMigrations(type, metadataHolder, useEffectFn);
|
2024-10-05 23:17:20 +02:00
|
|
|
const appMetadata = {} as METADATA;
|
|
|
|
|
|
|
|
|
|
Object.entries(metadataWithDefaultValues).forEach(([key, defaultValue]) => {
|
|
|
|
|
appMetadata[key as AppMetadataKeys] = getMetadataValueFrom(
|
2025-01-04 21:42:41 +01:00
|
|
|
migratedMetadata,
|
2024-10-05 23:17:20 +02:00
|
|
|
key as AppMetadataKeys,
|
|
|
|
|
defaultValue,
|
2024-10-28 18:03:29 +01:00
|
|
|
prefixes,
|
2024-10-05 23:17:20 +02:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return appMetadata;
|
2024-10-26 17:25:04 +02:00
|
|
|
}
|