summaryrefslogtreecommitdiff
path: root/source/features/preview-hidden-comments.tsx
blob: 939ded74cc32df9ddde3a56468f635ef27827287 (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
import './preview-hidden-comments.css';
import React from 'dom-chef';
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '.';
import {upperCaseFirst} from '../github-helpers';

function init(): void {
	// We target `.comment-body` directly because hidden review comments are only loaded when first expanded, except when opening a link
	// pointing to another review comment in the same thread (e.g. https://github.com/refined-github/refined-github/pull/4520#discussion_r659341139) #4915
	for (const comment of select.all('.minimized-comment:not(.d-none) > details:not(.rgh-preview-hidden-comments) .comment-body:not(.js-preview-body)')) {
		const details = comment.closest('details')!;
		details.classList.add('rgh-preview-hidden-comments');

		const commentText = comment.textContent!.trim();
		if (commentText.length === 0) {
			continue;
		}

		const header = select([
			'.timeline-comment-header-text', // Issue and commit comments
			'summary h3', // Review Comments
		], details)!;

		const reason = /off-topic|hidden/.exec(header.textContent!)?.[0];
		if (!reason) {
			continue;
		}

		header.classList.add('css-truncate', 'css-truncate-overflow', 'mr-2');
		header.append(
			<span className="Details-content--open">{select(':scope > .d-inline-block', header) ?? header.firstChild}</span>,
			<span className="Details-content--closed">
				<span className="Label mr-2">{upperCaseFirst(reason)}</span>{commentText.slice(0, 100)}
			</span>,
		);
	}
}

void features.add(import.meta.url, {
	include: [
		pageDetect.hasComments,
	],
	deduplicate: 'has-rgh-inner',
	init,
});