summaryrefslogtreecommitdiff
path: root/source/features/one-click-review-submission.tsx
blob: 1952fa80f6fad59014e60e948c58d81bae14e163 (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
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
106
107
108
import React from 'dom-chef';
import select from 'select-dom';
import delegate, {DelegateEvent} from 'delegate-it';
import * as pageDetect from 'github-url-detection';
import {CheckIcon, FileDiffIcon} from '@primer/octicons-react';

import features from '../feature-manager.js';
import looseParseInt from '../helpers/loose-parse-int.js';

function addButtons(radios: HTMLInputElement[]): void {
	const form = radios[0].form!;
	const container = select('.form-actions', form)!;

	// Set the default action for cmd+enter to Comment
	if (radios.length > 1) {
		container.append(
			<input
				type="hidden"
				name="pull_request_review[event]"
				value="comment"
			/>,
		);
	}

	// "Comment" button must be first
	if (radios.length > 1) {
		radios.push(radios.shift()!);
	}

	// Generate the new buttons
	for (const radio of radios) {
		const tooltip = radio.parentElement!.getAttribute('aria-label');

		const classes = ['btn btn-sm'];
		if (radio.value === 'comment') {
			classes.push('btn-primary');
		}

		if (tooltip) {
			classes.push('tooltipped tooltipped-nw tooltipped-no-delay');
		}

		const button = (
			<button
				type="submit"
				name="pull_request_review[event]"
				value={radio.value}
				className={classes.join(' ')}
				aria-label={tooltip!}
				disabled={radio.disabled}
			>
				{radio.nextSibling}
			</button>
		);

		if (!radio.disabled && radio.value === 'approve') {
			button.prepend(<CheckIcon className="color-fg-success"/>);
		} else if (!radio.disabled && radio.value === 'reject') {
			button.prepend(<FileDiffIcon className="color-fg-danger"/>);
		}

		container.append(button);
	}

	// Remove original fields at last to avoid leaving a broken form
	for (const radio of radios) {
		radio.closest('.form-checkbox')!.remove();
	}

	select('[type="submit"]:not([name])', form)!.remove(); // The selector excludes the "Cancel" button
}

function handleSubmission(event: DelegateEvent): void {
	// Delay disabling the fields to let them be submitted first
	setTimeout(() => {
		for (const control of select.all('button, textarea', event.delegateTarget)) {
			control.disabled = true;
		}
	});
}

function init(signal: AbortSignal): false | void {
	// Freeze form to avoid duplicate submissions
	delegate('[action$="/reviews"]', 'submit', handleSubmission, {signal});

	// This will prevent submission when clicking "Comment" and "Request changes" without entering a comment and no other review comments are pending
	delegate('[action$="/reviews"] button', 'click', ({delegateTarget: {value, form}}) => {
		const pendingComments = looseParseInt(select('.js-reviews-toggle .js-pending-review-comment-count'));
		const submissionRequiresComment = pendingComments === 0 && (value === 'reject' || value === 'comment');
		select('#pull_request_review_body', form!)!.toggleAttribute('required', submissionRequiresComment);
	}, {signal});

	// `return false` must always be after delegated events are added
	const radios = select.all('input[type="radio"][name="pull_request_review[event]"]');
	if (radios.length === 0) {
		return false;
	}

	addButtons(radios);
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isPR,
	],
	awaitDomReady: true,
	init,
});