summaryrefslogtreecommitdiff
path: root/source/features/follow-file-renames.tsx
blob: 0657f38614378c643c8059d4f323e1c805655363 (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
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;
}

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 : select.last)('.commit .sha')!;

		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: __featureName__,
	description: 'Enhances files’ commit lists navigation to follow file renames.',
	screenshot: 'https://user-images.githubusercontent.com/1402241/54799957-7306a280-4c9a-11e9-86de-b9764ed93397.png',
	include: [
		features.isCommitList
	],
	load: features.onAjaxedPages,
	init
});