summaryrefslogtreecommitdiff
path: root/source/features/clean-sidebar.tsx
blob: 23be8186f8f7cc99596dc056f9ce86e12097ff79 (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
import './clean-sidebar.css';
import React from 'dom-chef';
import select from 'select-dom';
import features from '../libs/features';
import onUpdatableContentUpdate from '../libs/on-updatable-content-update';
import {isPR} from '../libs/page-detect';

let canEditSidebar = false;

// Selector points to element containing list of elements or "No labels" text
function cleanSection(selector: string): boolean {
	const list = select(selector)!;
	if (list.children.length === 0) {
		const section = list.closest('.discussion-sidebar-item')!;
		if (canEditSidebar) {
			list.remove();
			section.classList.add('rgh-clean-sidebar');
		} else {
			section.remove();
		}

		return true;
	}

	return false;
}

function clean(): void {
	if (select.exists('.rgh-clean-sidebar')) {
		return;
	}

	select('#partial-discussion-sidebar')!.classList.add('rgh-clean-sidebar');

	// Assignees
	const assignees = select('.js-issue-assignees')!;
	if (assignees.children.length === 0) {
		assignees.closest('.discussion-sidebar-item')!.remove();
	} else {
		const assignYourself = select('.js-issue-assign-self');
		if (assignYourself) {
			(assignYourself.previousSibling as ChildNode).remove(); // Drop "No one — "
			select('[aria-label="Select assignees"] summary')!.append(
				<span style={{fontWeight: 'normal'}}>{assignYourself}</span>
			);
			assignees.closest('.discussion-sidebar-item')!.classList.add('rgh-clean-sidebar');
		}
	}

	// Reviewers
	if (isPR()) {
		cleanSection('[aria-label="Select reviewers"] > .css-truncate');
	}

	// Labels
	if (!cleanSection('.js-issue-labels') && !canEditSidebar) {
		select('.sidebar-labels div.discussion-sidebar-heading')!.remove();
	}

	// Projects
	cleanSection('.sidebar-projects');

	// Milestones
	const milestones = select('.sidebar-milestone')!;
	const milestonesInfo = milestones.lastChild!.lastChild!;
	if (milestonesInfo.textContent!.trim() === 'No milestone') {
		if (canEditSidebar) {
			milestonesInfo.remove();
			milestones.classList.add('rgh-clean-sidebar');
		} else {
			milestones.remove();
		}
	}
}

function init(): void {
	canEditSidebar = select.exists('.sidebar-labels .octicon-gear');
	clean();
	onUpdatableContentUpdate(select('#partial-discussion-sidebar')!, clean);
}

features.add({
	id: __featureName__,
	description: 'Hides empty sections (or just their "empty" label) in the discussion sidebar.',
	screenshot: 'https://user-images.githubusercontent.com/1402241/57199809-20691780-6fb6-11e9-9672-1ad3f9e1b827.png',
	include: [
		features.isIssue,
		features.isPRConversation
	],
	load: features.onAjaxedPages,
	init
});