summaryrefslogtreecommitdiff
path: root/source/features/new-repo-disable-projects-and-wikis.tsx
blob: 5ee566628fc74e5eedc4a3d05dbfab402da677c1 (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
import React from 'dom-chef';
import select from 'select-dom';
import onetime from 'onetime';
import delegate from 'delegate-it';
import domLoaded from 'dom-loaded';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager.js';
import api from '../github-helpers/api.js';
import selectHas from '../helpers/select-has.js';
import attachElement from '../helpers/attach-element.js';

const documentation = 'https://github.com/refined-github/refined-github/wiki/Extended-feature-descriptions#new-repo-disable-projects-and-wikis';

async function disableWikiAndProjects(): Promise<void> {
	delete sessionStorage.rghNewRepo;

	await api.v3('', {
		method: 'PATCH',
		body: {
			has_projects: false,
			has_wiki: false,
		},
	});
	await domLoaded;
	select('[data-menu-item$="wiki-tab"]')?.remove();
	select('[data-menu-item$="projects-tab"]')?.remove();
	selectHas('li:has([data-content="Wiki"]')?.remove();
	selectHas('li:has([data-content="Projects"])')?.remove();
}

function setStorage(): void {
	if (select('input#rgh-disable-project')!.checked) {
		sessionStorage.rghNewRepo = true;
	}
}

async function init(signal: AbortSignal): Promise<void> {
	await api.expectToken();

	const anchor = select.last([
		'.js-repo-init-setting-container', // IsNewRepo
		'.form-checkbox', // IsNewRepoTemplate
	]);
	attachElement(anchor, {
		after: () => (
			<div className="flash flash-warn py-0">
				<div className="form-checkbox checked">
					<label>
						<input
							checked
							type="checkbox"
							id="rgh-disable-project"
						/> Disable Projects and Wikis
					</label>
					<span className="note mb-2">
						After creating the repository disable the projects and wiki. <a href={documentation} target="_blank" rel="noreferrer">Suggestion by Refined GitHub.</a>
					</span>
				</div>
			</div>
		),
	});

	delegate('#new_repository, #new_new_repository', 'submit', setStorage, {signal});
}

void features.add(import.meta.url, {
	include: [
		pageDetect.isNewRepo,
		pageDetect.isNewRepoTemplate,
	],
	awaitDomReady: true,
	init,
}, {
	include: [
		() => Boolean(sessionStorage.rghNewRepo),
	],
	init: onetime(disableWikiAndProjects),
});