diff options
Diffstat (limited to 'source/features/open-all-notifications.tsx')
| -rw-r--r-- | source/features/open-all-notifications.tsx | 56 |
1 files changed, 34 insertions, 22 deletions
diff --git a/source/features/open-all-notifications.tsx b/source/features/open-all-notifications.tsx index 08152307..f242700c 100644 --- a/source/features/open-all-notifications.tsx +++ b/source/features/open-all-notifications.tsx @@ -13,33 +13,46 @@ function getUnreadNotifications(container: ParentNode = document): HTMLElement[] return select.all('.notification-unread', container); } -function openNotifications({delegateTarget}: delegate.Event): void { - const container = delegateTarget.closest('.js-notifications-group') ?? document; - +function openNotifications(notifications: Element[]): void { // Ask for confirmation - const unreadNotifications = getUnreadNotifications(container); if ( - unreadNotifications.length >= confirmationRequiredCount - && !confirm(`This will open ${unreadNotifications.length} new tabs. Continue?`) + notifications.length >= confirmationRequiredCount + && !confirm(`This will open ${notifications.length} new tabs. Continue?`) ) { return; } - void browser.runtime.sendMessage({ - openUrls: unreadNotifications.map(element => element.querySelector('a')!.href), - }); - - // Mark all as read - for (const notification of unreadNotifications) { + const urls: string[] = []; + for (const notification of notifications) { + // Mark all as read notification.classList.replace('notification-unread', 'notification-read'); + urls.push(notification.querySelector('a')!.href); } - // Remove all now-unnecessary buttons + void browser.runtime.sendMessage({openUrls: urls}); +} + +function removeOpenAllButtons(container: ParentNode = document): void { for (const button of select.all('.rgh-open-notifications-button', container)) { button.remove(); } } +function openUnreadNotifications({delegateTarget}: delegate.Event): void { + const container = delegateTarget.closest('.js-notifications-group') ?? document; + openNotifications(getUnreadNotifications(container)); + // Remove all now-unnecessary buttons + removeOpenAllButtons(container); +} + +function openSelectedNotifications(): void { + const selectedNotifications = select.all('.notifications-list-item :checked').map(checkbox => checkbox.closest('.notifications-list-item')!); + openNotifications(selectedNotifications); + if (!select.exists('.notification-unread')) { + removeOpenAllButtons(); + } +} + function addOpenReposButton(): void { for (const repository of select.all('.js-notifications-group')) { if (getUnreadNotifications(repository).length === 0) { @@ -54,28 +67,27 @@ function addOpenReposButton(): void { } } -function addOpenAllButton(): void { +function addOpenAllButton(className: string, text: string): void { // Selector works on: // https://github.com/notifications (Grouped by date) // https://github.com/notifications (Grouped by repo) // https://github.com/notifications?query=reason%3Acomment (which is an unsaved filter) select('.js-check-all-container .js-bulk-action-toasts ~ div .Box-header')!.append( - <button className="btn btn-sm rgh-open-notifications-button" type="button"> - <LinkExternalIcon className="mr-1"/>Open all unread + <button className={'btn btn-sm d-none ' + className} type="button"> + <LinkExternalIcon className="mr-1"/>{text} </button>, ); } -function update(): void { +function init(): void { if (getUnreadNotifications().length > 0) { - addOpenAllButton(); + delegate(document, '.rgh-open-notifications-button', 'click', openUnreadNotifications); + addOpenAllButton('rgh-open-notifications-button', 'Open all unread'); addOpenReposButton(); } -} -function init(): void { - delegate(document, '.rgh-open-notifications-button', 'click', openNotifications); - update(); + delegate(document, '.rgh-open-selected-button', 'click', openSelectedNotifications); + addOpenAllButton('rgh-open-selected-button', 'Open all selected'); } void features.add(__filebasename, { |
