summaryrefslogtreecommitdiff
path: root/source/features/split-issue-pr-search-results.tsx
blob: 9300d11985dd70b4a22dd4be6f7c72a699b2e935 (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
import './split-issue-pr-search-results.css';
import React from 'dom-chef';
import select from 'select-dom';
import features from '../libs/features';

function cleanLinks(): void {
	for (const link of select.all<HTMLAnchorElement>('.menu-item')) {
		const searchParams = new URLSearchParams(link.search);
		searchParams.set('q', cleanSearchQuery(searchParams.get('q')!));
		link.search = String(searchParams);
	}
}

function getSearchQuery(): string {
	return new URLSearchParams(location.search).get('q')!;
}

function cleanSearchQuery(query: string): string {
	return query
		.replace(/\bis:(pr|issue)\b/gi, '')
		.replace(/\s{2,}/g, ' ').trim();
}

type GitHubDiscussionType = 'pr' | 'issue';

function createUrl(type: GitHubDiscussionType, pathname = location.pathname): string {
	const url = new URL(pathname, location.origin);
	url.searchParams.set('q', cleanSearchQuery(getSearchQuery()) + ` is:${type}`);
	url.searchParams.set('type', 'Issues');
	return String(url);
}

function init(): void {
	cleanLinks();

	const issueLink = select<HTMLAnchorElement>('nav.menu a[href*="&type=Issues"]')!;
	issueLink.textContent = 'Issues'; // Drops any possible counter
	issueLink.href = createUrl('issue');
	issueLink.append(
		<include-fragment src={createUrl('issue', location.pathname + '/count')} />
	);

	const prLink = issueLink.cloneNode(true) as HTMLAnchorElement;
	prLink.textContent = 'Pull requests';
	prLink.href = createUrl('pr');
	prLink.append(
		<include-fragment src={createUrl('pr', location.pathname + '/count')} />
	);

	issueLink.after(prLink);

	const title = select('.codesearch-results h3')!.firstChild!;
	if (/\bis:pr\b/.test(getSearchQuery())) {
		// Update UI in PR searches
		issueLink.classList.remove('selected');
		title.textContent = title.textContent!.replace('issue', 'pull request');
	} else if (/\btype=Issues\b/.test(location.search) && !/\bis:issue\b/.test(getSearchQuery())) {
		// Update UI in combined searches (where there's no `is:<type>` query)
		title.textContent = title.textContent!
			.replace(/issue\b/, 'issue or pull request')
			.replace('issues', 'issues and pull requests');

		// `.selected` overrides `:hover`, so we need to reapply `:hover`'s style
		prLink.classList.add('rgh-split-issue-pr-combined');
		issueLink.classList.add('rgh-split-issue-pr-combined');
	} else {
		prLink.classList.remove('selected');
	}
}

features.add({
	id: __featureName__,
	description: 'Separates issues from PRs in the global search.',
	screenshot: 'https://user-images.githubusercontent.com/1402241/52181103-35a09f80-2829-11e9-9c6f-57f2e08fc5b2.png',
	include: [
		features.isRepoSearch,
		features.isGlobalSearchResults
	],
	load: features.onAjaxedPages,
	init
});