summaryrefslogtreecommitdiff
path: root/source/features/clean-conversation-sidebar.tsx
blob: 22906be65f17908ce05f9ef35ff486236d370a37 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import './clean-conversation-sidebar.css';
import React from 'dom-chef';
import {$, elementExists} from 'select-dom';
import onetime from 'onetime';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager.js';
import onElementRemoval from '../helpers/on-element-removal.js';
import observe from '../helpers/selector-observer.js';
import {removeTextNodeContaining} from '../helpers/dom-utils.js';
import {isHasSelectorSupported} from '../helpers/select-has.js';

const canEditSidebar = onetime((): boolean => elementExists('.discussion-sidebar-item [data-hotkey="l"]'));

function getNodesAfter(node: Node): Range {
	const range = new Range();
	range.selectNodeContents(node.parentElement!);
	range.setStartAfter(node);
	return range;
}

async function cleanReviewers(): Promise<void> {
	const possibleReviewers = $('[src$="/suggested-reviewers"]');
	if (possibleReviewers) {
		await onElementRemoval(possibleReviewers);
	}

	const content = $('[aria-label="Select reviewers"] > .css-truncate')!;
	if (!content.firstElementChild) {
		removeTextNodeContaining(content, 'No reviews');
	}
}

/**
Smartly removes "No content" or the whole section, depending on `canEditSidebar`.

Expected DOM:

```pug
.discussion-sidebar-item
	form (may be missing)
		details or div.discussion-sidebar-heading
		.css-truncate (may be missing)
			"No issues"
```

@param selector Element that contains `details` or `.discussion-sidebar-heading` or distinctive element inside it
*/
function cleanSection(selector: string): boolean {
	const container = $(`:is(form, .discussion-sidebar-item):has(${selector})`);
	if (!container) {
		return false;
	}

	const identifiers = [
		'.IssueLabel',
		'[aria-label="Select milestones"] .Progress-item',
		'[aria-label="Link issues"] [data-hovercard-type]',
		'[aria-label="Select projects"] .Link--primary',
	];

	const heading = $([
		'details:has(> .discussion-sidebar-heading)', // Can edit sidebar, has a dropdown
		'.discussion-sidebar-heading', // Cannot editor sidebar, has a plain heading
	], container)!;
	if (heading.closest('form, .discussion-sidebar-item')!.querySelector(identifiers)) {
		return false;
	}

	const section = container.closest('.discussion-sidebar-item')!;
	if (canEditSidebar()) {
		getNodesAfter(heading).deleteContents();
		section.classList.add('rgh-clean-sidebar');
	} else {
		section.remove();
	}

	return true;
}

async function cleanSidebar(): Promise<void> {
	$('#partial-discussion-sidebar')!.classList.add('rgh-clean-sidebar');

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

	// Reviewers
	if (pageDetect.isPR()) {
		void cleanReviewers();
	}

	// Labels
	if (!cleanSection('.js-issue-labels') && !canEditSidebar()) {
		// Hide heading in any case except `canEditSidebar`
		$('.discussion-sidebar-item:has(.js-issue-labels) .discussion-sidebar-heading')!
			.remove();
	}

	// Development (linked issues/PRs)
	const developmentHint = $('[aria-label="Link issues"] p');
	if (developmentHint) { // This may not exist if issues are disabled
		removeTextNodeContaining(developmentHint, /No branches or pull requests|Successfully merging/);
	}

	const createBranchLink = $('button[data-action="click:create-issue-branch#openDialog"]');
	if (createBranchLink) {
		createBranchLink.classList.add('Link--muted');
		$('[aria-label="Link issues"] summary')!.append(
			<span style={{fontWeight: 'normal'}}>{createBranchLink}</span>,
		);
	}

	cleanSection('[aria-label="Link issues"]');

	// Projects
	cleanSection('[aria-label="Select projects"]');

	// Milestones
	cleanSection('[aria-label="Select milestones"]');
}

function init(signal: AbortSignal): void {
	observe('#partial-discussion-sidebar', cleanSidebar, {signal});
}

void features.add(import.meta.url, {
	asLongAs: [
		isHasSelectorSupported,
	],
	include: [
		pageDetect.isConversation,
	],
	awaitDomReady: true, // The sidebar is at the end of the page + it needs to be fully loaded
	init,
});

/*

Test URLs:

* open issue: https://github.com/refined-github/sandbox/issues/15
* open issue with linked PR: https://github.com/refined-github/sandbox/issues/3
* closed issue: https://github.com/refined-github/sandbox/issues/56
* draft PR: https://github.com/refined-github/sandbox/pull/7
* merged PR: https://github.com/refined-github/sandbox/pull/58
* open issue with a milestone and assignee: https://github.com/microsoft/TypeScript/issues/18836
* User has triage access
  * issue: https://github.com/download-directory/download-directory.github.io/issues/39
  * PR: https://github.com/download-directory/download-directory.github.io/pull/37

*/