summaryrefslogtreecommitdiff
path: root/source/features/preview-hidden-comments.tsx
blob: ebc61f31c795be49052262091041b53a82ad81bd (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
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')) {
		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;
		}

		// Hidden review comments that have been preloaded have their header text wrapped in an extra <div>
		const headerTextWrapper = header.tagName === 'H3' ? select(':scope > .d-inline-block:last-child', header) : undefined;
		header.append(
			<span className="Details-content--open">{headerTextWrapper ?? header.firstChild}</span>,
			<span className="Details-content--closed">{`${upperCaseFirst(reason)}  ${commentText.slice(0, 100)}`}</span>,
		);
	}
}

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