From 7e5cd4e1b4fecf95391b7b89e973af8ab7885fb6 Mon Sep 17 00:00:00 2001 From: Marcus Date: Fri, 20 Dec 2024 21:01:04 -0800 Subject: [PATCH] Implement New XKit migration --- src/features/postblock.js | 48 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/features/postblock.js b/src/features/postblock.js index 8021074870..33306184f8 100644 --- a/src/features/postblock.js +++ b/src/features/postblock.js @@ -1,6 +1,6 @@ import { getTimelineItemWrapper, filterPostElements } from '../utils/interface.js'; import { registerMeatballItem, unregisterMeatballItem } from '../utils/meatballs.js'; -import { showModal, hideModal, modalCancelButton } from '../utils/modals.js'; +import { showModal, hideModal, modalCancelButton, modalCompleteButton } from '../utils/modals.js'; import { timelineObject } from '../utils/react_props.js'; import { onNewPosts, pageModifications } from '../utils/mutations.js'; import { dom } from '../utils/dom.js'; @@ -56,12 +56,58 @@ export const onStorageChanged = async function (changes, areaName) { } }; +const migrateBlockedPosts = async ({ detail }) => { + const newBlockedPostRootIDs = JSON.parse(detail); + + if (Array.isArray(newBlockedPostRootIDs)) { + window.dispatchEvent(new CustomEvent('xkit-postblock-migration-success')); + + const toAdd = newBlockedPostRootIDs + .map(id => String(id)) + .filter(id => !blockedPostRootIDs.includes(id)); + + if (toAdd.length) { + await new Promise(resolve => { + showModal({ + title: 'Add blocked posts?', + message: [ + `Would you like to import ${toAdd.length} blocked post id${ + toAdd.length === 1 ? '' : 's' + } from New XKit to XKit Rewritten?` + ], + buttons: [ + modalCancelButton, + dom('button', { class: 'blue' }, { click: resolve }, ['Confirm']) + ] + }); + }); + + blockedPostRootIDs.push(...toAdd); + await browser.storage.local.set({ [storageKey]: blockedPostRootIDs }); + + showModal({ + title: 'Success', + message: `Imported ${toAdd.length > 1 ? `${toAdd.length} blocked posts` : 'a blocked post'}!`, + buttons: [modalCompleteButton] + }); + } else { + showModal({ + title: 'No new blocked posts!', + message: 'Your XKit Rewritten configuration has these posts blocked already.', + buttons: [modalCompleteButton] + }); + } + } +}; + export const main = async function () { ({ [storageKey]: blockedPostRootIDs = [] } = await browser.storage.local.get(storageKey)); registerMeatballItem({ id: meatballButtonId, label: meatballButtonLabel, onclick: onButtonClicked }); onNewPosts.addListener(processPosts); + + window.addEventListener('xkit-postblock-migration', migrateBlockedPosts); }; export const clean = async function () {