/
follow-file-renames.tsx
71 lines (60 loc) · 2.26 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
import React from 'dom-chef';
import select from 'select-dom';
import features from '../libs/features';
import * as api from '../libs/api';
import {getCleanPathname} from '../libs/utils';
interface File {
previous_filename: string; // eslint-disable-line @typescript-eslint/camelcase
filename: string;
status: string;
}
// TODO: Ensure that pages with a single commit aren't fetched twice (api.ts' cache should do it automatically)
async function findRename(
user: string,
repo: string,
lastCommitOnPage: string
): Promise<File[]> {
// API v4 doesn't support it: https://github.community/t5/GitHub-API-Development-and/What-is-the-corresponding-object-in-GraphQL-API-v4-for-patch/m-p/14502?collapse_discussion=true&filter=location&location=board:api&q=files%20changed%20commit&search_type=thread
const {files} = await api.v3(`repos/${user}/${repo}/commits/${lastCommitOnPage}`);
return files as Promise<File[]>;
}
async function init(): Promise<false | void> {
const disabledPagination = select.all('.paginate-container [disabled], .paginate-container .disabled');
if (disabledPagination.length === 0) {
return false;
}
const [user, repo,, ref, ...path] = getCleanPathname().split('/');
const currentFilename = path.join('/');
disabledPagination.forEach(async button => {
const isNewer = button.textContent === 'Newer';
const fromKey = isNewer ? 'previous_filename' : 'filename';
const toKey = isNewer ? 'filename' : 'previous_filename';
const sha = isNewer ? select('.commit .sha') : select.all('.commit .sha').pop();
const files = await findRename(user, repo, sha!.textContent!.trim());
for (const file of files) {
if (file[fromKey] === currentFilename) {
if (file.status === 'renamed') {
const url = `/${user}/${repo}/commits/${ref}/${file[toKey]}`;
button.replaceWith(
<a
href={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;
}
}
});
}
features.add({
id: 'follow-file-renames',
description: 'Enable Newer/Older buttons in commit lists to follow file renames',
include: [
features.isCommitList
],
load: features.onAjaxedPages,
init
});