summaryrefslogtreecommitdiff
path: root/source/features/clean-issue-filters.tsx
blob: 1d9e98dfc484187758e3cb7b837ccaa5a1312b7a (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
import select from 'select-dom';
import cache from 'webext-storage-cache';
import features from '../libs/features';
import * as api from '../libs/api';
import {getOwnerAndRepo} from '../libs/utils';

type CacheEntry = {
	repoProjectCount: number;
	orgProjectCount: number;
	milestoneCount: number;
};

async function getCount(): Promise<CacheEntry> {
	const {ownerName, repoName} = getOwnerAndRepo();
	const cacheKey = `clean-issue-filters:${ownerName}/${repoName}`;
	const cachedData = await cache.get<CacheEntry>(cacheKey);
	if (cachedData) {
		return cachedData;
	}

	const result = await api.v4(`
		repository(owner: "${ownerName}", name: "${repoName}") {
			projects { totalCount }
			milestones { totalCount }
		}
		organization(login: "${ownerName}") {
			projects { totalCount }
		}
	`, {
		allowErrors: true
	});

	const cacheEntry = {
		repoProjectCount: result.repository.projects.totalCount,
		orgProjectCount: result.organization ? result.organization.projects.totalCount : 0,
		milestoneCount: result.repository.milestones.totalCount
	};

	cache.set(cacheKey, cacheEntry, 1);
	return cacheEntry;
}

async function init(): Promise<void> {
	const {repoProjectCount, orgProjectCount, milestoneCount} = await getCount();

	// If the repo and organization has no projects, its selector will be empty
	if (repoProjectCount === 0 && orgProjectCount === 0 && select.exists('[data-hotkey="p"')) {
		select('[data-hotkey="p"')!.parentElement!.remove();
	}

	// If the repo has no milestones, its selector will be empty
	if (milestoneCount === 0 && select.exists('[data-hotkey="m"')) {
		select('[data-hotkey="m"')!.parentElement!.remove();
	}
}

features.add({
	id: __featureName__,
	description: 'Hides `Projects` and `Milestones` filters in discussion lists if they are empty.',
	screenshot: 'https://user-images.githubusercontent.com/37769974/59083449-0ef88f80-8915-11e9-8296-68af1ddcf191.png',
	load: features.onAjaxedPages,
	include: [
		features.isRepoDiscussionList
	],
	init
});