summaryrefslogtreecommitdiff
path: root/source/features/linkify-branch-references.tsx
blob: 855c46bad1507ee60e3e1ef644808d2a35e68564 (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
import React from 'dom-chef';
import select from 'select-dom';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager.js';
import GitHubURL from '../github-helpers/github-url.js';
import {buildRepoURL} from '../github-helpers/index.js';

async function init(): Promise<void | false> {
	const element = await elementReady(pageDetect.isQuickPR() ? '.branch-name' : '.commit-form .branch-name');
	if (!element) {
		return false;
	}

	const branchUrl = buildRepoURL('tree', element.textContent!);
	element.replaceWith(
		<span className="commit-ref">
			<a className="no-underline" href={branchUrl} data-turbo-frame="repo-content-turbo-frame">
				{element.textContent}
			</a>
		</span>,
	);
}

const hovercardObserver = new MutationObserver(([mutation]) => {
	const hovercard = (mutation.target as HTMLElement).querySelector('[data-hydro-view*="pull-request-hovercard-hover"] ~ .d-flex.mt-2');
	if (!hovercard) {
		return;
	}

	const {href} = hovercard.querySelector('a.Link--primary')!;

	for (const reference of hovercard.querySelectorAll('.commit-ref')) {
		const url = new GitHubURL(href).assign({
			route: 'tree',
			branch: reference.title,
		});

		const user = reference.querySelector('.user');
		if (user) {
			url.user = user.textContent!;
		}

		reference.replaceChildren(
			<a className="no-underline" href={url.href} data-turbo-frame="repo-content-turbo-frame">
				{[...reference.childNodes]}
			</a>,
		);
	}
});

function hovercardInit(): void | Deinit {
	const hovercardContainer = select('.js-hovercard-content > .Popover-message');
	if (hovercardContainer) {
		hovercardObserver.observe(hovercardContainer, {childList: true});
		return hovercardObserver;
	}
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isQuickPR,
		pageDetect.isEditingFile,
		pageDetect.isDeletingFile,
	],
	deduplicate: 'has-rgh',
	init,
}, {
	deduplicate: 'has-rgh',
	awaitDomReady: true, // TODO: Use new observer
	init: hovercardInit,
});