2024-05-08 04:01:23 +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, useMemo } from 'react';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { jsonSaveParse } from '@/lib/HelperFunctions.ts';
|
2024-10-05 23:17:20 +02:00
|
|
|
import { requestUpdateSourceMetadata } from '@/modules/metadata/services/MetadataUpdater.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2025-04-21 17:00:13 +02:00
|
|
|
import { ISourceMetadata, SourceIdInfo, SourceMetadataKeys } from '@/modules/source/Source.types.ts';
|
2024-10-05 23:17:20 +02:00
|
|
|
import { convertFromGqlMeta } from '@/modules/metadata/services/MetadataConverter.ts';
|
|
|
|
|
import { getMetadataFrom } from '@/modules/metadata/services/MetadataReader.ts';
|
|
|
|
|
import {
|
|
|
|
|
AllowedMetadataValueTypes,
|
|
|
|
|
AppMetadataKeys,
|
|
|
|
|
GqlMetaHolder,
|
|
|
|
|
Metadata,
|
|
|
|
|
} from '@/modules/metadata/Metadata.types.ts';
|
2024-05-08 04:01:23 +02:00
|
|
|
|
2024-10-26 17:25:04 +02:00
|
|
|
const DEFAULT_SOURCE_METADATA: ISourceMetadata = {
|
|
|
|
|
savedSearches: undefined,
|
2025-05-17 22:17:15 +02:00
|
|
|
isPinned: false,
|
2024-10-26 17:25:04 +02:00
|
|
|
};
|
|
|
|
|
|
2024-05-08 04:01:23 +02:00
|
|
|
const convertAppMetadataToGqlMetadata = (
|
|
|
|
|
metadata: Partial<ISourceMetadata>,
|
|
|
|
|
): Metadata<string, AllowedMetadataValueTypes> => ({
|
|
|
|
|
...metadata,
|
|
|
|
|
savedSearches: metadata.savedSearches ? JSON.stringify(metadata.savedSearches) : undefined,
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-26 17:25:04 +02:00
|
|
|
const convertGqlMetadataToAppMetadata = (
|
2024-05-08 04:01:23 +02:00
|
|
|
metadata: Partial<Metadata<AppMetadataKeys, AllowedMetadataValueTypes>>,
|
|
|
|
|
): ISourceMetadata => ({
|
|
|
|
|
...(metadata as unknown as ISourceMetadata),
|
|
|
|
|
savedSearches: jsonSaveParse<ISourceMetadata['savedSearches']>(metadata.savedSearches as string) ?? undefined,
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-21 17:00:13 +02:00
|
|
|
const getMetadata = (metaHolder: SourceIdInfo & GqlMetaHolder, useEffectFn?: typeof useEffect): ISourceMetadata =>
|
2024-05-08 04:01:23 +02:00
|
|
|
convertGqlMetadataToAppMetadata(
|
2024-10-26 17:25:04 +02:00
|
|
|
getMetadataFrom(
|
|
|
|
|
'source',
|
|
|
|
|
{ ...metaHolder, meta: convertFromGqlMeta(metaHolder.meta) },
|
|
|
|
|
convertAppMetadataToGqlMetadata(DEFAULT_SOURCE_METADATA),
|
2024-10-28 18:03:29 +01:00
|
|
|
undefined,
|
2024-10-26 17:25:04 +02:00
|
|
|
useEffectFn,
|
|
|
|
|
),
|
2024-05-08 04:01:23 +02:00
|
|
|
);
|
|
|
|
|
|
2025-04-21 17:00:13 +02:00
|
|
|
export const getSourceMetadata = (metaHolder: SourceIdInfo & GqlMetaHolder): ISourceMetadata => getMetadata(metaHolder);
|
2024-10-26 17:25:04 +02:00
|
|
|
|
2025-04-21 17:00:13 +02:00
|
|
|
export const useGetSourceMetadata = (metaHolder: SourceIdInfo & GqlMetaHolder): ISourceMetadata => {
|
2024-10-26 17:25:04 +02:00
|
|
|
const metadata = getMetadata(metaHolder, useEffect);
|
|
|
|
|
return useMemo(() => metadata, [metaHolder]);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-08 04:01:23 +02:00
|
|
|
export const updateSourceMetadata = async <
|
|
|
|
|
MetadataKeys extends SourceMetadataKeys = SourceMetadataKeys,
|
|
|
|
|
MetadataKey extends MetadataKeys = MetadataKeys,
|
|
|
|
|
>(
|
2025-04-21 17:00:13 +02:00
|
|
|
source: SourceIdInfo & GqlMetaHolder,
|
2024-05-08 04:01:23 +02:00
|
|
|
metadataKey: MetadataKey,
|
|
|
|
|
value: ISourceMetadata[MetadataKey],
|
|
|
|
|
): Promise<void[]> =>
|
|
|
|
|
requestUpdateSourceMetadata(source, [
|
|
|
|
|
[metadataKey, convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey]],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
export const createUpdateSourceMetadata =
|
|
|
|
|
<Settings extends SourceMetadataKeys>(
|
2025-04-21 17:00:13 +02:00
|
|
|
source: SourceIdInfo & GqlMetaHolder,
|
2024-05-08 04:01:23 +02:00
|
|
|
handleError: (error: any) => void = defaultPromiseErrorHandler('createUpdateSourceMetadata'),
|
|
|
|
|
): ((...args: OmitFirst<Parameters<typeof updateSourceMetadata<Settings>>>) => Promise<void | void[]>) =>
|
|
|
|
|
(metadataKey, value) =>
|
|
|
|
|
updateSourceMetadata(source, metadataKey, value).catch(handleError);
|