From afa89c02a0052387d19da3a01264b1532bcebec4 Mon Sep 17 00:00:00 2001 From: Jip Fr Date: Mon, 20 Feb 2023 17:35:09 +0100 Subject: [PATCH] Add iframe logic --- src/views/other/v2Migration.tsx | 68 +- src/views/search/HomeView.tsx | 3 + yarn.lock | 3970 +++++++++++++++---------------- 3 files changed, 2038 insertions(+), 2003 deletions(-) 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