diff --git a/src/state/watched/migrations/v4.ts b/src/state/watched/migrations/v4.ts new file mode 100644 index 00000000..70e5c3af --- /dev/null +++ b/src/state/watched/migrations/v4.ts @@ -0,0 +1,78 @@ +import { MWMediaType } from "@/backend/metadata/types/mw"; +import { ProgressMediaItem } from "@/stores/progress"; + +import { WatchedStoreData } from "../types"; + +export function migrateV4Videos(old: WatchedStoreData) { + // Convert items + const newItems: Record = {}; + + for (const oldItem of old.items) { + if (oldItem.item.meta.type === MWMediaType.SERIES) { + // Upsert + if (!newItems[oldItem.item.meta.id]) { + newItems[oldItem.item.meta.id] = { + type: "show", + episodes: {}, + seasons: {}, + title: oldItem.item.meta.title, + updatedAt: oldItem.watchedAt, + poster: oldItem.item.meta.poster, + year: Number(oldItem.item.meta.year), + }; + } + + // Add episodes + if ( + oldItem.item.series && + !newItems[oldItem.item.meta.id].episodes[oldItem.item.series.episodeId] + ) { + // Find episode ID (barely ever works) + const episodeTitle = oldItem.item.meta.seasonData.episodes.find( + (ep) => ep.number === oldItem.item.series?.episode + )?.title; + + // Add season to season data + newItems[oldItem.item.meta.id].seasons[oldItem.item.series.seasonId] = { + id: oldItem.item.series.seasonId, + number: oldItem.item.series.season, + title: + oldItem.item.meta.seasons.find( + (s) => s.number === oldItem.item.series?.season + )?.title || "Unknown season", + }; + + // Populate episode data + newItems[oldItem.item.meta.id].episodes[oldItem.item.series.episodeId] = + { + title: episodeTitle || "Unknown", + id: oldItem.item.series.episodeId, + number: oldItem.item.series.episode, + seasonId: oldItem.item.series.seasonId, + updatedAt: oldItem.watchedAt, + progress: { + duration: (100 / oldItem.percentage) * oldItem.progress, + watched: oldItem.progress, + }, + }; + } + } else { + newItems[oldItem.item.meta.id] = { + type: "movie", + episodes: {}, + seasons: {}, + title: oldItem.item.meta.title, + updatedAt: oldItem.watchedAt, + year: Number(oldItem.item.meta.year), + poster: oldItem.item.meta.poster, + progress: { + duration: (100 / oldItem.percentage) * oldItem.progress, + watched: oldItem.progress, + }, + }; + } + } + + console.log(newItems); + return newItems; +} diff --git a/src/state/watched/store.ts b/src/state/watched/store.ts index 53dc6ef7..75d05b93 100644 --- a/src/state/watched/store.ts +++ b/src/state/watched/store.ts @@ -1,9 +1,9 @@ -import { MWMediaType } from "@/backend/metadata/types/mw"; -import { ProgressMediaItem, useProgressStore } from "@/stores/progress"; +import { useProgressStore } from "@/stores/progress"; import { createVersionedStore } from "@/utils/storage"; import { OldData, migrateV2Videos } from "./migrations/v2"; import { migrateV3Videos } from "./migrations/v3"; +import { migrateV4Videos } from "./migrations/v4"; import { WatchedStoreData } from "./types"; export const VideoProgressStore = createVersionedStore() @@ -31,84 +31,7 @@ export const VideoProgressStore = createVersionedStore() .addVersion({ version: 3, migrate(old: WatchedStoreData): WatchedStoreData { - console.log(old); - - // Convert items - const newItems: Record = {}; - - for (const oldItem of old.items) { - if (oldItem.item.meta.type === MWMediaType.SERIES) { - // Upsert - if (!newItems[oldItem.item.meta.id]) { - newItems[oldItem.item.meta.id] = { - type: "show", - episodes: {}, - seasons: {}, - title: oldItem.item.meta.title, - updatedAt: oldItem.watchedAt, - poster: oldItem.item.meta.poster, - year: Number(oldItem.item.meta.year), - }; - } - - // Add episodes - if ( - oldItem.item.series && - !newItems[oldItem.item.meta.id].episodes[ - oldItem.item.series.episodeId - ] - ) { - // Find episode ID (barely ever works) - const episodeTitle = oldItem.item.meta.seasonData.episodes.find( - (ep) => ep.number === oldItem.item.series?.episode - )?.title; - - // Add season to season data - newItems[oldItem.item.meta.id].seasons[ - oldItem.item.series.seasonId - ] = { - id: oldItem.item.series.seasonId, - number: oldItem.item.series.season, - title: - oldItem.item.meta.seasons.find( - (s) => s.number === oldItem.item.series?.season - )?.title || "Unknown season", - }; - - // Populate episode data - newItems[oldItem.item.meta.id].episodes[ - oldItem.item.series.episodeId - ] = { - title: episodeTitle || "Unknown", - id: oldItem.item.series.episodeId, - number: oldItem.item.series.episode, - seasonId: oldItem.item.series.seasonId, - updatedAt: oldItem.watchedAt, - progress: { - duration: (100 / oldItem.percentage) * oldItem.progress, - watched: oldItem.progress, - }, - }; - } - } else { - newItems[oldItem.item.meta.id] = { - type: "movie", - episodes: {}, - seasons: {}, - title: oldItem.item.meta.title, - updatedAt: oldItem.watchedAt, - year: Number(oldItem.item.meta.year), - poster: oldItem.item.meta.poster, - progress: { - duration: (100 / oldItem.percentage) * oldItem.progress, - watched: oldItem.progress, - }, - }; - } - } - - console.log(newItems); - useProgressStore.getState().replaceItems(newItems); + useProgressStore.getState().replaceItems(migrateV4Videos(old)); return { items: [],