From 792c0345999b97210bb75459610ad1c866d6d5b4 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 21 Jun 2026 15:36:12 +0200 Subject: [PATCH] Extract migration entry search state update into function --- src/features/migration/Migration.types.ts | 7 ++ src/features/migration/MigrationManager.ts | 116 +++++++++++---------- 2 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/features/migration/Migration.types.ts b/src/features/migration/Migration.types.ts index ddcbd377..c5ebd94e 100644 --- a/src/features/migration/Migration.types.ts +++ b/src/features/migration/Migration.types.ts @@ -7,9 +7,11 @@ */ import type { + ChapterListFieldsFragment, GetMangaToMigrateQuery, GetMangaToMigrateToFetchMutation, GetMigratableSourcesQuery, + MangaMigrationFieldsFragment, } from '@/lib/graphql/generated/graphql.ts'; import type { SourceDisplayNameInfo, @@ -176,3 +178,8 @@ export type MigrationChapter = ChapterIdInfo & export type MigrateAction = { copy: () => Promise[]; cleanup: () => Promise[] }; export type MigrateActionCreator = () => MigrateAction; + +export type EntrySourceSearchResult = { + manga: MangaMigrationFieldsFragment; + chapters: ChapterListFieldsFragment[] | null; +}; diff --git a/src/features/migration/MigrationManager.ts b/src/features/migration/MigrationManager.ts index 5576cd24..34763fc1 100644 --- a/src/features/migration/MigrationManager.ts +++ b/src/features/migration/MigrationManager.ts @@ -12,6 +12,7 @@ import { create } from 'zustand'; import { immer } from 'zustand/middleware/immer'; import { devtools, persist } from 'zustand/middleware'; import { + type EntrySourceSearchResult, type MigratableEntry, type MigrateOptions, type MigrationBulkSearchSettings, @@ -33,7 +34,6 @@ import { import { requestManager } from '@/lib/requests/RequestManager.ts'; import { GET_MIGRATION_SOURCE_MANGAS_FETCH } from '@/lib/graphql/source/SourceMutation.ts'; import type { - ChapterListFieldsFragment, GetMigrationSourceMangasFetchMutation, GetMigrationSourceMangasFetchMutationVariables, GetServerSettingsQuery, @@ -796,7 +796,7 @@ export class MigrationManager { sourceId: SourceIdInfo['id'], signal: AbortSignal, { selectHighestChapterNumberSource, performAdvancedSearch }: MigrationBulkSearchSettings, - ): Promise<{ manga: MangaMigrationFieldsFragment; chapters: ChapterListFieldsFragment[] | null }[]> { + ): Promise { if (signal.aborted) { throw new Error(signal.reason); } @@ -891,57 +891,17 @@ export class MigrationManager { return updatedMatches; } - private static async searchForMangaInSource( - sourceId: SourceIdInfo['id'], + private static updateEntrySearchState( mangaId: MangaIdInfo['id'], - mangaTitle: string, - signal: AbortSignal, - searchController: AbortController, - options: MigrationBulkSearchSettings, - ): Promise { - const { + sourceId: SourceIdInfo['id'], + foundMatches: EntrySourceSearchResult[], + { selectHighestChapterNumberSource, ignoreOutdatedMatches, requireAdditionalChapters, ignoreWithMissingChapters, - } = options; - - signal.throwIfAborted(); - - if (!selectHighestChapterNumberSource && MigrationManager.hasHigherSourcePriorityMatch(mangaId, sourceId)) { - return; - } - - const foundMatches = await (async () => { - try { - return await MigrationManager.findMatchesForMangaInSource( - mangaId, - mangaTitle, - sourceId, - signal, - options, - ); - } catch (e) { - MigrationManager.updateState((draft) => { - const draftEntry = draft.entries[mangaId]; - - draftEntry.destSourceIdToSearchState[sourceId] = false; - }); - - throw e; - } - })(); - - if (!foundMatches.length) { - MigrationManager.updateState((draft) => { - const draftEntry = draft.entries[mangaId]; - - draftEntry.destSourceIdToSearchState[sourceId] = false; - }); - - return; - } - + }: MigrationBulkSearchSettings, + ): void { MigrationManager.updateState((draft) => { const draftEntry = draft.entries[mangaId]; const draftMatchEntry = draftEntry.selectedMatchMangaId @@ -1009,16 +969,62 @@ export class MigrationManager { draftEntry.selectedMatchMangaId = bestMatch.id; draftEntry.selectedMatchSourceId = sourceId; } - - if ( - !selectHighestChapterNumberSource && - !MigrationManager.isHigherPrioritySourceUnsettled(mangaId, sourceId) - ) { - searchController.abort(`Found best match in source "${sourceId}"`); - } }); } + private static async searchForMangaInSource( + sourceId: SourceIdInfo['id'], + mangaId: MangaIdInfo['id'], + mangaTitle: string, + signal: AbortSignal, + searchController: AbortController, + options: MigrationBulkSearchSettings, + ): Promise { + const { selectHighestChapterNumberSource } = options; + + signal.throwIfAborted(); + + if (!selectHighestChapterNumberSource && MigrationManager.hasHigherSourcePriorityMatch(mangaId, sourceId)) { + return; + } + + const foundMatches = await (async () => { + try { + return await MigrationManager.findMatchesForMangaInSource( + mangaId, + mangaTitle, + sourceId, + signal, + options, + ); + } catch (e) { + MigrationManager.updateState((draft) => { + const draftEntry = draft.entries[mangaId]; + + draftEntry.destSourceIdToSearchState[sourceId] = false; + }); + + throw e; + } + })(); + + if (!foundMatches.length) { + MigrationManager.updateState((draft) => { + const draftEntry = draft.entries[mangaId]; + + draftEntry.destSourceIdToSearchState[sourceId] = false; + }); + + return; + } + + MigrationManager.updateEntrySearchState(mangaId, sourceId, foundMatches, options); + + if (!selectHighestChapterNumberSource && !MigrationManager.isHigherPrioritySourceUnsettled(mangaId, sourceId)) { + searchController.abort(`Found best match in source "${sourceId}"`); + } + } + private static async searchForManga( mangaId: MangaIdInfo['id'], mangaTitle: string,