Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostBlock: Implement New XKit migration #1671

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/features/postblock.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 () {
Expand Down
Loading