Skip to content

Commit

Permalink
fix(EasyArchive): no toast notice when archiving/deleting (#1483)
Browse files Browse the repository at this point in the history
* fix(EasyArchive): no toast notice when archiving/deleting
  • Loading branch information
WaitSpringQW authored Aug 16, 2024
1 parent fbf1370 commit 6b0d63e
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 46 deletions.
120 changes: 95 additions & 25 deletions dist/EasyArchive/EasyArchive.js

Large diffs are not rendered by default.

85 changes: 72 additions & 13 deletions src/EasyArchive/modules/addLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ const addLinks = async ({
indexNo,
anchor,
archiveTo,
toast,
}: {
indexNo: string;
anchor: string;
archiveTo: string;
toast: ToastifyInstance;
}) => {
return onClickWrap(getMessage('Archive'), 'archive', (event) => {
event.preventDefault();
Expand All @@ -91,23 +93,53 @@ const addLinks = async ({
}

replaceChild(parentElement, spanWrap(getMessage('Archiving')));
messageChannel.postMessage(
getMessage('Archiving') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo)
const message1 =
getMessage('Archiving') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo);
messageChannel.postMessage(message1);

toast ||= {
hideToast: () => {},
};
toast.hideToast();
toast = toastify(
{
text: message1,
close: true,
duration: -1,
},
'info'
);

void archiveSection({index: indexNo, anchor, archiveTo}).then(() => {
replaceChild(parentElement, spanWrap(getMessage('Archived')));
messageChannel.postMessage(
getMessage('Archived') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo)
const message2 =
getMessage('Archived') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo);
messageChannel.postMessage(message2);
toast.hideToast();
toast = toastify(
{
text: message2,
close: true,
duration: -1,
},
'info'
);
messageChannel.close();
refreshChannel.postMessage('Refresh');
refresh();
refresh({toastifyInstance: toast});
});
});
};

const removeSectionLink = ({indexNo, anchor}: {indexNo: string; anchor: string}) => {
const removeSectionLink = ({
indexNo,
anchor,
toast,
}: {
indexNo: string;
anchor: string;
toast: ToastifyInstance;
}) => {
return onClickWrap(getMessage('Delete'), 'delete', (event) => {
event.preventDefault();
const parentElement = (event.target as HTMLElement)?.parentElement;
Expand All @@ -116,31 +148,58 @@ const addLinks = async ({
}

replaceChild(parentElement, spanWrap(getMessage('Deleting')));
messageChannel.postMessage(
getMessage('Deleting') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo)
const message1 =
getMessage('Deleting') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo);
messageChannel.postMessage(message1);

toast ||= {
hideToast: () => {},
};
toast.hideToast();
toast = toastify(
{
text: message1,
close: true,
duration: -1,
},
'info'
);

void removeSection({index: indexNo, anchor}).then(() => {
replaceChild(parentElement, spanWrap(getMessage('Deleted')));
messageChannel.postMessage(
getMessage('Deleted') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo)
const message2 =
getMessage('Deleted') + getMessage(':') + getMessage('Section $1').replace('$1', indexNo);
messageChannel.postMessage(message2);
toast.hideToast();
toast = toastify(
{
text: message2,
close: true,
duration: -1,
},
'info'
);
messageChannel.close();
refreshChannel.postMessage('Refresh');
refresh();
refresh({toastifyInstance: toast});
});
});
};

if (secArc === '1') {
const archiveLink = archiveSectionLink({indexNo: index, anchor: id, archiveTo: arcLoc});
const archiveLink = archiveSectionLink({
indexNo: index,
anchor: id,
archiveTo: arcLoc,
toast: toastifyInstance,
});
sectionIdSpan.append(archiveLink);
}
if (secArc === '1' && secDel === '1') {
sectionIdSpan.append(pipeElement());
}
if (secDel === '1') {
const removeLink = removeSectionLink({indexNo: index, anchor: id});
const removeLink = removeSectionLink({indexNo: index, anchor: id, toast: toastifyInstance});
sectionIdSpan.append(removeLink);
}
sectionIdSpans[sectionIdSpans.length] = sectionIdSpan;
Expand Down
4 changes: 2 additions & 2 deletions src/EasyArchive/modules/archiveSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {removeSection} from './removeSection';

const archiveSection = async ({index, anchor, archiveTo}: {index: string; anchor: string; archiveTo: string}) => {
const {wgPageName} = mw.config.get();
const ifSectionExist = await checkIfSectionExist(index, anchor);
const ifSectionExist = await checkIfSectionExist({index, anchor});

if (ifSectionExist !== true) {
return;
}

const content = await getSectionContent(wgPageName, index);
const content = await getSectionContent({title: wgPageName, section: index});

if (content === null) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/EasyArchive/modules/removeSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {getSectionContent} from './util/getSection';

const removeSection = async ({index, anchor, summary}: {index: string; anchor: string; summary?: string}) => {
const {wgPageName} = mw.config.get();
const ifSectionExist = await checkIfSectionExist(index, anchor);
const ifSectionExist = await checkIfSectionExist({index, anchor});

if (ifSectionExist !== true) {
return;
}

const content = await getSectionContent(wgPageName, index);
const content = await getSectionContent({title: wgPageName, section: index});

if (content === null) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/EasyArchive/modules/util/checkIfExist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const checkIfPageExist = async (archiveTo: string) => {
return true;
};

const checkIfSectionExist = async (index: string, anchor: string) => {
const checkIfSectionExist = async ({index, anchor}: {index: string; anchor: string}) => {
const {wgPageName} = mw.config.get();
const sections = await getSections(wgPageName);

Expand Down
2 changes: 1 addition & 1 deletion src/EasyArchive/modules/util/getSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const getSections = async (title: string) => {
return sectionsToArchive;
};

const getSectionContent = async (title: string, section: string): Promise<string | null> => {
const getSectionContent = async ({title, section}: {title: string; section: string}): Promise<string | null> => {
const params: ApiQueryRevisionsParams = {
action: 'query',
prop: ['revisions'],
Expand Down
11 changes: 9 additions & 2 deletions src/EasyArchive/modules/util/refreshPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import {toastify} from 'ext.gadget.Toastify';

const {wgScript} = mw.config.get();

const refresh = (targetPage?: string): void => {
toastify(
const refresh = ({
targetPage,
toastifyInstance,
}: {targetPage?: string; toastifyInstance?: ToastifyInstance} = {}): void => {
toastifyInstance ||= {
hideToast: () => {},
};
toastifyInstance.hideToast();
toastifyInstance = toastify(
{
text: getMessage('Refreshing'),
duration: -1,
Expand Down

0 comments on commit 6b0d63e

Please sign in to comment.