summaryrefslogtreecommitdiff
path: root/source/features/open-all-notifications.tsx
blob: f242700cdd9d08befe04b80145307900f79dcaef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import './open-all-notifications.css';
import React from 'dom-chef';
import select from 'select-dom';
import delegate from 'delegate-it';
import * as pageDetect from 'github-url-detection';
import {LinkExternalIcon} from '@primer/octicons-react';

import features from '.';

const confirmationRequiredCount = 10;

function getUnreadNotifications(container: ParentNode = document): HTMLElement[] {
	return select.all('.notification-unread', container);
}

function openNotifications(notifications: Element[]): void {
	// Ask for confirmation
	if (
		notifications.length >= confirmationRequiredCount
		&& !confirm(`This will open ${notifications.length} new tabs. Continue?`)
	) {
		return;
	}

	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);
	}

	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) {
			continue;
		}

		select('.js-grouped-notifications-mark-all-read-button', repository)!.before(
			<button type="button" className="btn btn-sm mr-2 tooltipped tooltipped-w rgh-open-notifications-button" aria-label="Open all unread notifications from this repo">
				<LinkExternalIcon width={16}/> Open unread
			</button>,
		);
	}
}

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 d-none ' + className} type="button">
			<LinkExternalIcon className="mr-1"/>{text}
		</button>,
	);
}

function init(): void {
	if (getUnreadNotifications().length > 0) {
		delegate(document, '.rgh-open-notifications-button', 'click', openUnreadNotifications);
		addOpenAllButton('rgh-open-notifications-button', 'Open all unread');
		addOpenReposButton();
	}

	delegate(document, '.rgh-open-selected-button', 'click', openSelectedNotifications);
	addOpenAllButton('rgh-open-selected-button', 'Open all selected');
}

void features.add(__filebasename, {
	include: [
		pageDetect.isNotifications,
	],
	init,
});