summaryrefslogtreecommitdiff
path: root/source/features/jump-to-conversation-close-event.tsx
blob: 609cd649c1483ae579ccb7af07eb24af907facdd (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
import React from 'dom-chef';
import {css} from 'code-tag';
import {lastElement} from 'select-dom';
import * as pageDetect from 'github-url-detection';

import {wrap} from '../helpers/dom-utils.js';
import features from '../feature-manager.js';
import observe from '../helpers/selector-observer.js';

export const closedOrMergedMarkerSelector = css`
	#partial-discussion-header :is(
		[title^="Status: Closed"],
		[title^="Status: Merged"]
	)
`;

export function getLastCloseEvent(): HTMLElement | undefined {
	return lastElement(`
		.TimelineItem-badge :is(
			.octicon-issue-closed,
			.octicon-git-merge,
			.octicon-git-pull-request-closed,
			.octicon-skip
		)
	`)!.closest('.TimelineItem') ?? undefined;
}

function addToConversation(discussionHeader: HTMLElement): void {
	// Avoid native `title` by disabling pointer events, we have our own `aria-label`. We can't drop the `title` attribute because some features depend on it.
	discussionHeader.style.pointerEvents = 'none';

	wrap(discussionHeader,
		<a
			aria-label="Scroll to most recent close event"
			className="tooltipped tooltipped-s"
			href={'#' + getLastCloseEvent()!.id}
		/>,
	);
}

function init(signal: AbortSignal): void {
	observe(
		closedOrMergedMarkerSelector,
		addToConversation,
		{signal},
	);
}

void features.add(import.meta.url, {
	asLongAs: [
		pageDetect.isConversation,
	],
	include: [
		pageDetect.isClosedIssue,
		pageDetect.isClosedPR,
	],
	awaitDomReady: true, // We're specifically looking for the last event
	init,
});

/*
## Test URLs
Closed Issue: https://github.com/refined-github/sandbox/issues/2
Closed Issue (Not Planned): https://github.com/refined-github/sandbox/issues/24
Merged PR: https://github.com/refined-github/sandbox/pull/23
Closed PR: https://github.com/refined-github/sandbox/pull/22
*/