forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.tsx
More file actions
185 lines (148 loc) · 5.36 KB
/
options.tsx
File metadata and controls
185 lines (148 loc) · 5.36 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import 'webext-base-css/webext-base.css';
import './options.css';
import {$, $optional} from 'select-dom/strict.js';
import {$$} from 'select-dom';
import fitTextarea from 'fit-textarea';
import {enableTabToIndent} from 'indent-textarea';
import delegate, {type DelegateEvent} from 'delegate-it';
import {isChrome, isFirefox} from 'webext-detect';
import type {SyncedForm} from 'webext-options-sync-per-domain';
import 'webext-bugs/target-blank';
import clearCacheHandler from './helpers/clear-cache-handler.js';
import {brokenFeatures, styleHotfixes} from './helpers/hotfix.js';
import {importedFeatures} from './feature-data.js';
import {perDomainOptions} from './options-storage.js';
import isDevelopmentVersion from './helpers/is-development-version.js';
import {doesBrowserActionOpenOptions} from './helpers/feature-utils.js';
import {state as bisectState} from './helpers/bisect.js';
import initFeatureList, {updateListDom} from './options/feature-list.js';
import initTokenValidation from './options/token-validation.js';
import initToggleAllButtons from './options/toggle-all.js';
const supportsFieldSizing = CSS.supports('field-sizing', 'content');
let syncedForm: SyncedForm | undefined;
const {version} = chrome.runtime.getManifest();
async function findFeatureHandler(this: HTMLButtonElement): Promise<void> {
// TODO: Add support for GHE
const options = await perDomainOptions.getOptionsForOrigin().getAll();
const enabledFeatures = importedFeatures.filter(featureId => options['feature:' + featureId]);
await bisectState.set(enabledFeatures);
this.disabled = true;
setTimeout(() => {
this.disabled = false;
}, 10_000);
$('#find-feature-message').hidden = false;
}
function focusSection({delegateTarget: section}: DelegateEvent<Event, HTMLDetailsElement>): void {
const rect = section.getBoundingClientRect();
if (rect.bottom > window.innerHeight || rect.top < 0) {
section.scrollIntoView({behavior: 'smooth', block: 'nearest'});
}
if (section.open) {
const field = $optional('input, textarea', section);
if (field) {
field.focus({preventScroll: true});
if (!supportsFieldSizing && field instanceof HTMLTextAreaElement) {
// #6404
fitTextarea(field);
}
}
}
}
function updateRateLink(): void {
if (isChrome()) {
return;
}
$('a#rate-link').href = isFirefox() ? 'https://addons.mozilla.org/en-US/firefox/addon/refined-github-' : 'https://apps.apple.com/app/id1519867270?action=write-review';
}
function isEnterprise(): boolean {
return syncedForm!.getSelectedDomain() !== 'default';
}
function getExclusions(): string | void {
if (isEnterprise())
return 'Hotfixes are not applied on GitHub Enterprise.';
if (isDevelopmentVersion()) {
return 'Hotfixes are not applied in the development version';
}
}
async function showStoredCssHotfixes(): Promise<void> {
$('#hotfixes-field').textContent
= getExclusions()
?? await styleHotfixes.getCached(version)
?? 'No CSS found in cache.';
}
async function fetchHotfixes(event: MouseEvent): Promise<void> {
const button = event.currentTarget as HTMLButtonElement;
button.disabled = true;
try {
// Style
$('#hotfixes-field').textContent
= getExclusions()
?? await styleHotfixes.getFresh(version)
?? 'No hotfixes needed for this version! 🎉';
// Broken features
const storage = await brokenFeatures.getFresh();
const field = $('#broken-features-field');
field.hidden = false;
field.textContent = JSON.stringify(storage, undefined, 2);
} finally {
button.disabled = false;
}
}
async function generateDom(): Promise<void> {
// Generate list
await initFeatureList();
// Update list from saved options
syncedForm = await perDomainOptions.syncForm('form');
// Decorate list
updateListDom();
initToggleAllButtons();
// Only now the form is ready, we can show it
$('#js-failed').remove();
// Enable token validation
void initTokenValidation(syncedForm);
// Update rate link if necessary
updateRateLink();
// Hide non-applicable "Button link" section
if (doesBrowserActionOpenOptions) {
$('#action').hidden = true;
}
// Show stored CSS hotfixes
void showStoredCssHotfixes();
$('#version').textContent = version;
}
function addEventListeners(): void {
// Update domain-dependent page content when the domain is changed
syncedForm?.onChange(async domain => {
// Point the link to the right domain
$('a#personal-token-link').host = domain === 'default' ? 'github.com' : domain;
for (const element of $$('storage-usage[item]')) {
element.setAttribute('item', domain === 'default' ? 'options' : 'options:' + domain);
}
updateListDom();
});
// Refresh page when permissions are changed (because the dropdown selector needs to be regenerated)
chrome.permissions.onRemoved.addListener(() => {
location.reload();
});
chrome.permissions.onAdded.addListener(() => {
location.reload();
});
// Improve textareas editing
enableTabToIndent('textarea');
if (!supportsFieldSizing) {
fitTextarea.watch('textarea');
}
// Bring section into view when opened
delegate('details', 'toggle', focusSection, {capture: true});
// Add cache clearer
$('#clear-cache').addEventListener('click', clearCacheHandler);
// Add bisect tool
$('#find-feature').addEventListener('click', findFeatureHandler);
// Handle "Fetch hotfixes" button
$('#fetch-hotfixes').addEventListener('click', fetchHotfixes);
}
async function init(): Promise<void> {
await generateDom();
addEventListeners();
}
void init();