Repository navigation
Expand file tree
/
Copy pathpatch-diff-links.tsx
More file actions
105 lines (91 loc) · 2.9 KB
/
Copy pathpatch-diff-links.tsx
File metadata and controls
105 lines (91 loc) · 2.9 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React from 'dom-chef';
import * as pageDetect from 'github-url-detection';
import DiffIcon from 'octicons-plain-react/Diff';
import features from '../feature-manager.js';
import {getCleanPathname} from '../github-helpers/index.js';
import observe from '../helpers/selector-observer.js';
function getPrUrl(extension: 'patch' | 'diff'): string {
const pathname = getCleanPathname();
const [owner, repo, , id] = pathname.split('/', 4);
return `/${owner}/${repo}/pull/${id}.${extension}`;
}
function getCommitUrl(extension: 'patch' | 'diff'): string {
// The replacement avoids a redirection isPRCommit
const pathname = getCleanPathname().replace(/\/pull\/\d+\/commits/, '/commit');
return `/${pathname}.${extension}`;
}
function updateCommitUrl(
{currentTarget: link}: React.FocusEvent<HTMLAnchorElement> | React.MouseEvent<HTMLAnchorElement>,
): void {
link.href = getCommitUrl(link.textContent as 'patch' | 'diff');
}
function createLink(type: 'patch' | 'diff'): JSX.Element {
return (
<a
href={getCommitUrl(type)}
className="sha color-fg-default"
// Update URL because it might be out of date due to SPA navigation
// https://github.com/refined-github/refined-github/issues/8737
onMouseEnter={updateCommitUrl}
onFocus={updateCommitUrl}
>
{type}
</a>
);
}
async function addPatchDiffLinks(commitMeta: HTMLElement): Promise<void> {
commitMeta.classList.remove('no-wrap'); // #5987
commitMeta.prepend(
<span className="sha-block" data-turbo="false">
{createLink('patch')} {createLink('diff')}
{commitMeta.tagName !== 'DIV' && <span className="px-2 tmp-px-2">·</span>}
</span>,
);
}
async function addPrPatchDiffLinks(prHeader: HTMLElement): Promise<void> {
prHeader.append(
<li className="Box-row p-3 tmp-p-3 mt-0 tmp-mt-0 d-flex flex-items-center">
<DiffIcon className="mr-2 tmp-mr-2" />
<div data-turbo="false">
<span className="text-bold">Git URLs: </span>
<a href={getPrUrl('patch')}>patch</a>
{' · '}
<a href={getPrUrl('diff')}>diff</a>
</div>
</li>,
);
}
async function init(signal: AbortSignal): Promise<void> {
observe(
[
'.commit-meta > :is(span, div):last-child', // `isPRCommit` + old `isSingleCommit`
'[class*="commit-header-actions"] + div pre',
],
addPatchDiffLinks,
{signal},
);
observe(
[
'.react-overview-code-button-action-list > ul',
'#local-panel > ul', // TODO [2027-01-01]: Drop after legacy PR files view is removed
],
addPrPatchDiffLinks,
{signal},
);
}
void features.add(import.meta.url, {
include: [
pageDetect.isCommit,
pageDetect.isPR,
],
exclude: [
pageDetect.isPRCommit404,
],
init,
});
/*
Test URLs:
- Commit:https://github.com/refined-github/refined-github/commit/132272786fdc058193e089d8c06f2a158844e101
- PR Commit: https://github.com/refined-github/refined-github/pull/7751/commits/07ddf838c211075701e9a681ab061a158b05ee79
- PR: https://github.com/refined-github/refined-github/pull/7751
*/