From 2c6cbdf9c84329a06b62461c615f97ef935e5459 Mon Sep 17 00:00:00 2001
From: darkred <rick3162@gmail.com>
Date: Tue, 25 May 2021 09:54:41 +0300
Subject: Extend `separate-draft-pr-button` to `one-click-pr-or-gist` (#4382)

---
 source/features/one-click-pr-or-gist.tsx | 52 ++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 source/features/one-click-pr-or-gist.tsx

(limited to 'source/features/one-click-pr-or-gist.tsx')

diff --git a/source/features/one-click-pr-or-gist.tsx b/source/features/one-click-pr-or-gist.tsx
new file mode 100644
index 00000000..806cd53d
--- /dev/null
+++ b/source/features/one-click-pr-or-gist.tsx
@@ -0,0 +1,52 @@
+import './one-click-pr-or-gist.css';
+import React from 'dom-chef';
+import select from 'select-dom';
+import * as pageDetect from 'github-url-detection';
+
+import features from '.';
+
+function init(): void | false {
+	const draftPROption = select('.new-pr-form [name="draft"], #new_gist [name="gist[public]"]');
+	if (!draftPROption) {
+		// 1. Free accounts can't open Draft PRs in private repos, so this element is missing
+		// 2. PRs can't be created from some comparison pages: Either base is a tag, not a branch; or there already exists a PR.
+		return false;
+	}
+
+	const initialGroupedButtons = draftPROption.closest('.BtnGroup')!;
+
+	for (const dropdownItem of select.all('.select-menu-item', initialGroupedButtons)) {
+		let title = select('.select-menu-item-heading', dropdownItem)!.textContent!.trim();
+		const description = select('.description', dropdownItem)!.textContent!.trim();
+		const radioButton = select('input[type=radio]', dropdownItem)!;
+		const classList = ['btn', 'ml-2', 'tooltipped', 'tooltipped-s'];
+
+		if (/\bdraft\b/i.test(title)) {
+			title = 'Create draft PR';
+		} else {
+			classList.push('btn-primary');
+		}
+
+		initialGroupedButtons.after(
+			<button
+				className={classList.join(' ')}
+				aria-label={description}
+				type="submit"
+				name={radioButton.name}
+				value={radioButton.value}
+			>
+				{title}
+			</button>
+		);
+	}
+
+	initialGroupedButtons.remove();
+}
+
+void features.add(__filebasename, {
+	include: [
+		pageDetect.isCompare,
+		pageDetect.isGist
+	],
+	init
+});
-- 
cgit v1.2.3

