Array of validated, merged manga items.
True if any match results were updated; false otherwise.
export function updateMatchResults(validMergedManga: KenmeiManga[]): boolean {
console.debug("[MangaImport] Attempting to update existing match results");
const matchResultsRaw = localStorage.getItem("match_results");
if (!matchResultsRaw) {
console.debug("[MangaImport] No existing match results found");
return false;
}
const matchResults: MatchResult[] = JSON.parse(matchResultsRaw);
console.debug(
`[MangaImport] Found ${matchResults.length} existing match results to update`,
);
// Create maps for quick lookup by id or title with better null handling
const mangaById = new Map(
validMergedManga
.filter((m) => m.id != null)
.map((m) => [m.id.toString(), m]),
);
const mangaByTitle = new Map(
validMergedManga
.filter((m) => m.title != null)
.map((m) => [m.title.toLowerCase(), m]),
);
let updated = false;
const updatedResults = matchResults.map((result: MatchResult) => {
let newMangaData = null;
// Try to find by ID first
if (
result.kenmeiManga?.id != null &&
mangaById.has(result.kenmeiManga.id.toString())
) {
newMangaData = mangaById.get(result.kenmeiManga.id.toString());
}
// If not found by ID, try by title
else if (
result.kenmeiManga?.title != null &&
mangaByTitle.has(result.kenmeiManga.title.toLowerCase())
) {
newMangaData = mangaByTitle.get(result.kenmeiManga.title.toLowerCase());
}
if (newMangaData) {
updated = true;
return {
...result,
kenmeiManga: { ...result.kenmeiManga, ...newMangaData },
};
}
return result;
});
if (updated) {
const updatedCount = updatedResults.filter(
(_, i) => updatedResults[i] !== matchResults[i],
).length;
console.info(
`[MangaImport] Updated ${updatedCount} existing matches with new import data`,
);
const updatedResultsJson = JSON.stringify(updatedResults);
if (globalThis.electronStore) {
globalThis.electronStore.setItem("match_results", updatedResultsJson);
}
}
return updated;
}
Updates match results with new manga data from import by matching on ID or title.