summaryrefslogtreecommitdiff
path: root/source/features/clean-conversation-headers.tsx
blob: 7476a1ca7b7bb49d7995a55292f1b806a1e4e7c6 (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
import './clean-conversation-headers.css';
import React from 'dom-chef';
import {$} from 'select-dom';
import elementReady from 'element-ready';
import {ArrowLeftIcon} from '@primer/octicons-react';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager.js';
import getDefaultBranch from '../github-helpers/get-default-branch.js';
import observe from '../helpers/selector-observer.js';

async function cleanIssueHeader(byline: HTMLElement): Promise<void> {
	byline.classList.add('rgh-clean-conversation-headers', 'rgh-clean-conversation-headers-hide-author');

	// Shows on issues: octocat opened this issue on 1 Jan · [1 comments]
	// Removes on issues: octocat opened this issue on 1 Jan [·] 1 comments
	const commentCount = $('relative-time', byline)!.nextSibling!;
	commentCount.replaceWith(<span>{commentCount.textContent.replace('·', '')}</span>);
}

async function cleanPrHeader(byline: HTMLElement): Promise<void> {
	byline.classList.add('rgh-clean-conversation-headers');

	// Extra author name is only shown on `isPRConversation`
	// Hide if it's the same as the opener (always) or merger
	const shouldHideAuthor = pageDetect.isPRConversation() && $('.author', byline)!.textContent === (await elementReady('.TimelineItem .author'))!.textContent;
	if (shouldHideAuthor) {
		byline.classList.add('rgh-clean-conversation-headers-hide-author');
	}

	const base = $('.commit-ref', byline)!;
	const baseBranchDropdown = $('.commit-ref-dropdown', byline);

	// Shows on PRs: main [←] feature
	const arrowIcon = <ArrowLeftIcon className="v-align-middle mx-1"/>;
	if (baseBranchDropdown) {
		baseBranchDropdown.after(<span>{arrowIcon}</span>); // #5598
	} else {
		base.nextElementSibling!.replaceChildren(arrowIcon);
	}

	const baseBranch = base.title.split(':')[1];
	const wasDefaultBranch = pageDetect.isClosedPR() && baseBranch === 'master';
	const isDefaultBranch = baseBranch === await getDefaultBranch();
	if (!isDefaultBranch && !wasDefaultBranch) {
		base.classList.add('rgh-clean-conversation-headers-non-default-branch');
	}
}

async function init(signal: AbortSignal): Promise<void> {
	const cleanConversationHeader = pageDetect.isIssue() ? cleanIssueHeader : cleanPrHeader;
	observe('.gh-header-meta .flex-auto', cleanConversationHeader, {signal});
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isIssue,
		pageDetect.isPR,
	],
	init,
});

/* Test URLs

- Open PR (default branch): https://github.com/refined-github/sandbox/pull/4
- Open PR (non-default branch): https://github.com/Kenshin/simpread/pull/698

- Merged PR (same author): https://github.com/sindresorhus/refined-github/pull/3402
- Merged PR (different author): https://github.com/parcel-bundler/parcel/pull/78
- Merged PR (different author + first published tag): https://github.com/sindresorhus/refined-github/pull/3227

- Closed PR: https://github.com/sindresorhus/refined-github/pull/4141

*/