summaryrefslogtreecommitdiff
path: root/source/features/hide-low-quality-comments.tsx
blob: e50a83baca11a2bfff9022dc2aaa20494707d2dd (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
import './hide-low-quality-comments.css';
import delay from 'delay';
import React from 'dom-chef';
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';
import delegate, {DelegateEvent} from 'delegate-it';

import features from '../feature-manager.js';
import isLowQualityComment from '../helpers/is-low-quality-comment.js';

export const singleParagraphCommentSelector = '.comment-body > p:only-child';

async function unhide(event: DelegateEvent): Promise<void> {
	for (const comment of select.all('.rgh-hidden-comment')) {
		comment.hidden = false;
	}

	await delay(10); // "Similar comments" aren't expanded without this in Safari #3830

	// Expand all "similar comments" boxes
	for (const similarCommentsExpandButton of select.all('.rgh-hidden-comment > summary')) {
		similarCommentsExpandButton.click();
	}

	select('.rgh-hidden-comment')!.scrollIntoView();
	event.delegateTarget.parentElement!.remove();
}

function hideComment(comment: HTMLElement): void {
	comment.hidden = true;
	comment.classList.add('rgh-hidden-comment');
}

function init(): void | false {
	if (select.exists('.rgh-low-quality-comments-note')) {
		return false;
	}

	for (const similarCommentsBox of select.all('.js-discussion .Details-element:not([data-body-version])')) {
		hideComment(similarCommentsBox);
	}

	const linkedComment = location.hash.startsWith('#issuecomment-') ? select(`${location.hash} ${singleParagraphCommentSelector}`) : undefined;

	for (const commentText of select.all(singleParagraphCommentSelector)) {
		// Exclude explicitely linked comments #5363
		if (commentText === linkedComment) {
			continue;
		}

		if (!isLowQualityComment(commentText.textContent!)) {
			continue;
		}

		// Comments that contain useful images or links shouldn't be removed
		// Images are wrapped in <a> tags on GitHub hence included in the selector
		// TODO: use :has()
		if (select.exists('a', commentText)) {
			continue;
		}

		// Ensure that they're not by VIPs (owner, collaborators, etc)
		// TODO: use :has()
		const comment = commentText.closest('.js-timeline-item')!;
		if (select.exists('.timeline-comment-label', comment)) {
			continue;
		}

		// If the person is having a conversation, then don't hide it
		const author = select('.author', comment)!.getAttribute('href')!;
		// If the first comment left by the author isn't a low quality comment
		// (previously hidden or about to be hidden), then leave this one as well
		const previousComment = select(`.js-timeline-item:not([hidden]) .unminimized-comment .author[href="${author}"]`);
		if (previousComment?.closest('.js-timeline-item') !== comment) {
			continue;
		}

		hideComment(comment);
	}

	const lowQualityCount = select.all('.rgh-hidden-comment').length;
	if (lowQualityCount > 0) {
		select('.discussion-timeline-actions')!.prepend(
			<p className="rgh-low-quality-comments-note">
				{`${lowQualityCount} unhelpful comment${lowQualityCount > 1 ? 's were' : ' was'} automatically hidden. `}
				<button className="btn-link text-emphasized rgh-unhide-low-quality-comments" type="button">Show</button>
			</p>,
		);

		// No need to add the signal here
		delegate('.rgh-unhide-low-quality-comments', 'click', unhide);
	}
}

// This should not be made dynamic via observer, it's not worth updating the lowQuality count for fresh comments
void features.add(import.meta.url, {
	include: [
		pageDetect.isIssue,
	],
	awaitDomReady: true,
	init,
});

/*
## Test URLs
https://github.com/facebook/jest/issues/5311
*/