summaryrefslogtreecommitdiff
path: root/source/features/unwrap-useless-dropdowns.tsx
blob: 28a517c77be596b852125243e398373f7377a880 (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
import select from 'select-dom';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';

import features from '.';

// Replace dropdown while keeping its sizing/positioning classes
function replaceDropdownInPlace(dropdown: Element, form: Element): void {
	dropdown.replaceWith(form);
	form.classList.add(...dropdown.classList);
	form.classList.remove('dropdown', 'details-reset', 'details-overlay');
}

async function unwrapNotifications(): Promise<void | false> {
	await elementReady('.js-check-all-container > :nth-child(2)'); // Wait for filters to be ready
	const forms = select.all('[action="/notifications/beta/update_view_preference"]');
	if (forms.length === 0) {
		return false;
	}

	if (forms.length > 2) {
		throw new Error('GitHub added new view types. This feature is obsolete.');
	}

	const dropdown = forms[0].closest('details')!;
	const currentView = select('summary i', dropdown)!.nextSibling!.textContent!.trim();
	const desiredForm = currentView === 'Date' ? forms[0] : forms[1];

	// Replace dropdown
	replaceDropdownInPlace(dropdown, desiredForm);

	// Fix button’s style
	const button = select('[type="submit"]', desiredForm)!;
	button.className = 'btn';
	button.textContent = `Group by ${button.textContent!.toLowerCase()}`;
}

async function unwrapActionRun(): Promise<void | false> {
	const desiredForm = await elementReady('.js-check-suite-rerequest-form');
	if (!desiredForm) {
		return false;
	}

	const availableOptions = desiredForm
		.closest('.dropdown-menu')!
		.querySelectorAll('li > *'); // GitHub left an empty `li` in there 😒
	if (availableOptions.length > 1) {
		throw new Error('GitHub added items to the dropdown. This feature is obsolete.');
	}

	// Fix button’s style
	const button = select('button', desiredForm)!;
	button.className = 'btn btn-sm';
	button.prepend(select('.octicon-sync')!);

	// Replace dropdown
	const dropdown = desiredForm.closest('details')!;
	replaceDropdownInPlace(dropdown, desiredForm);
}

void features.add(__filebasename, {
	include: [
		pageDetect.isNotifications
	],
	awaitDomReady: false,
	init: unwrapNotifications
}, {
	include: [
		pageDetect.isActionRun
	],
	awaitDomReady: false,
	init: unwrapActionRun
});