-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
follow-file-renames.tsx
72 lines (61 loc) · 1.98 KB
/
follow-file-renames.tsx
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
import React from 'dom-chef';
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';
import features from '.';
import * as api from '../github-helpers/api';
import GitHubURL from '../github-helpers/github-url';
interface File {
previous_filename: string;
filename: string;
status: string;
}
async function findRename(lastCommitOnPage: string): Promise<File[]> {
// API v4 doesn't support it: https://github.community/t/what-is-the-corresponding-object-in-graphql-api-v4-for-patch-which-is-available-in-rest-api-v3/13590
const {files} = await api.v3(`commits/${lastCommitOnPage}`);
return files;
}
async function linkify(button: HTMLButtonElement, url: GitHubURL): Promise<void | false> {
const isNewer = button.textContent === 'Newer';
const fromKey = isNewer ? 'previous_filename' : 'filename';
const toKey = isNewer ? 'filename' : 'previous_filename';
const sha = (isNewer ? select : select.last)('[aria-label="Copy the full SHA"]')!;
const files = await findRename(sha.getAttribute('value')!);
for (const file of files) {
if (file[fromKey] === url.filePath) {
if (file.status === 'renamed') {
url.assign({
route: 'commits',
filePath: file[toKey]
});
button.replaceWith(
<a
href={String(url)}
aria-label={`Renamed ${isNewer ? 'to' : 'from'} ${file[toKey]}`}
className="btn btn-outline BtnGroup-item tooltipped tooltipped-n tooltipped-no-delay"
>
{button.textContent}
</a>
);
}
return;
}
}
}
function init(): void | false {
const disabledPagination = select.all('.paginate-container button[disabled]');
const url = new GitHubURL(location.href);
// Clear the search from the url, so it does not get passed to the rename link
url.search = '';
if (disabledPagination.length === 0 || !url.filePath) {
return false;
}
for (const button of disabledPagination) {
void linkify(button, url);
}
}
void features.add(__filebasename, {
include: [
pageDetect.isRepoCommitList
],
init
});