diff --git a/src/views/other/v2Migration.tsx b/src/views/other/v2Migration.tsx index 85f3685c..0f1e0424 100644 --- a/src/views/other/v2Migration.tsx +++ b/src/views/other/v2Migration.tsx @@ -11,6 +11,52 @@ function fromBinary(str: string): Uint8Array { return result; } +export function importV2Data({ data, time }: { data: any; time: Date }) { + const savedTime = localStorage.getItem("mw-migration-date"); + if (savedTime) { + if (new Date(savedTime) >= time) { + // has already migrated this or something newer, skip + return false; + } + } + + // restore migration data + if (data.bookmarks) + localStorage.setItem("mw-bookmarks", JSON.stringify(data.bookmarks)); + if (data.videoProgress) + localStorage.setItem("video-progress", JSON.stringify(data.videoProgress)); + + localStorage.setItem("mw-migration-date", time.toISOString()); + + return true; +} + +export function EmbedMigration() { + let hasReceivedMigrationData = false; + + const onMessage = (e: any) => { + const data = e.data; + if (data && data.isMigrationData && !hasReceivedMigrationData) { + hasReceivedMigrationData = true; + const didImport = importV2Data({ + data: data.data, + time: data.date, + }); + if (didImport) window.location.reload(); + } + }; + + useEffect(() => { + window.addEventListener("message", onMessage); + + return () => { + window.removeEventListener("message", onMessage); + }; + }); + + return