summaryrefslogtreecommitdiff
path: root/source/features/extend-conversation-status-filters.tsx
blob: 8ccce012dfab829a649dbd820e5835b904b4efd2 (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
import React from 'dom-chef';
import {$, $$} from 'select-dom';
import {CheckIcon} from '@primer/octicons-react';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager.js';
import SearchQuery from '../github-helpers/search-query.js';

function addMergeLink(): void {
	if (!pageDetect.isPRList()) {
		return;
	}

	// The links in `.table-list-header-toggle` are either:
	//   1 Open | 1 Closed
	//   1 Total            // Apparently appears with is:merged/is:unmerged
	for (const lastLink of $$('.table-list-header-toggle.states a:last-child')) {
		const lastLinkQuery = SearchQuery.from(lastLink);

		if (lastLinkQuery.includes('is:merged')) {
			// It's a "Total" link for "is:merged"
			lastLink.lastChild!.textContent = lastLink.lastChild!.textContent.replace('Total', 'Merged');
			continue;
		}

		if (lastLinkQuery.includes('is:unmerged')) {
			// It's a "Total" link for "is:unmerged"
			lastLink.lastChild!.textContent = lastLink.lastChild!.textContent.replace('Total', 'Unmerged');
			continue;
		}

		// In this case, `lastLink` is expected to be a "Closed" link
		const mergeLink = lastLink.cloneNode(true);
		mergeLink.textContent = 'Merged';
		mergeLink.classList.toggle('selected', SearchQuery.from(location).includes('is:merged'));
		mergeLink.href = SearchQuery.from(mergeLink).replace('is:closed', 'is:merged').href;
		lastLink.after(' ', mergeLink);
	}
}

function togglableFilters(): void {
	for (const link of $$('.table-list-header-toggle.states a')) {
		$('.octicon', link)?.remove();
		if (link.classList.contains('selected')) {
			link.prepend(<CheckIcon/>);
			link.href = SearchQuery
				.from(link)
				.remove(
					'is:open',
					'is:closed',
					'is:merged',
					'is:unmerged',
				)
				.href;
		}
	}
}

async function init(): Promise<void | false> {
	await elementReady('.table-list-filters');

	addMergeLink();
	togglableFilters();
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isRepoIssueOrPRList,
	],
	deduplicate: 'has-rgh-inner',
	init,
}, {
	include: [
		pageDetect.isGlobalIssueOrPRList,
	],
	deduplicate: 'has-rgh',
	init,
});