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
|
import React from 'dom-chef';
import select from 'select-dom';
import onetime from 'onetime';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';
import features from '.';
import {observeOneMutation} from '../helpers/simplified-element-observer';
async function getProjectsTab(): Promise<HTMLElement | undefined> {
return elementReady([
'[data-hotkey="g b"]', // In organizations and repos
'[aria-label="User profile"] [href$="?tab=projects"]' // In user profiles
].join());
}
// We can't detect whether the user can create projects on a repo, so this link is potentially a 404
async function addNewProjectLink(): Promise<void | false> {
if (!await getProjectsTab()) {
return false;
}
// URLs patterns:
// https://github.com/orgs/USER/projects/new
// https://github.com/USER/REPO/projects/new
const path = location.pathname.split('/', 3);
const base = path.length > 2 ? path.join('/') : '/orgs' + path.join('/');
select('.Header [href="/new"]')!.parentElement!.append(
<a className="dropdown-item" href={base + '/projects/new'}>
New project
</a>
);
}
export default async function getTabCount(tab: Element): Promise<number> {
const counter = select('.Counter, .num', tab);
if (!counter) {
// GitHub might have already dropped the counter, which means it's 0
return 0;
}
if (!counter.firstChild) {
// It's still loading
await observeOneMutation(tab);
}
return Number(counter.textContent);
}
async function removeProjectsTab(): Promise<void | false> {
const projectsTab = await getProjectsTab();
if (
!projectsTab || // Projects disabled 🎉
projectsTab.matches('.selected') || // User is on Projects tab 👀
await getTabCount(projectsTab) > 0 // There are open projects
) {
return false;
}
projectsTab.remove();
}
void features.add(__filebasename, {
include: [
pageDetect.isRepo,
pageDetect.isUserProfile,
pageDetect.isOrganizationProfile
],
exclude: [
// Repo/Organization owners should see the tab. If they don't need it, they should disable Projects altogether
pageDetect.canUserEditRepo,
pageDetect.canUserEditOrganization
],
awaitDomReady: false,
init: removeProjectsTab
}, {
include: [
pageDetect.isRepo,
pageDetect.isOrganizationProfile
],
awaitDomReady: false,
init: onetime(addNewProjectLink)
});
|