Skip to content

Commit 9780fbc

Browse files
committed
feat: pending updates window
1 parent 3b948b8 commit 9780fbc

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

src/hooks/updates.js

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import * as settings from 'src/utils/settings';
77
import each from 'src/utils/each';
88
import wrap from 'src/utils/2.pokemon';
99
import Translation from 'src/structures/constants/translation';
10-
import { buttonCSS } from 'src/utils/1.variables';
10+
import { buttonCSS, scriptVersion } from 'src/utils/1.variables';
1111
import sleep from 'src/utils/sleep';
1212
import createParser from 'src/utils/parser';
13+
import DialogHelper from 'src/utils/DialogHelper';
14+
import { getVersion } from 'src/utils/plugin';
1315

1416
const HOUR = 60 * 60 * 1000;
1517
const DAY = 24 * HOUR;
@@ -28,6 +30,8 @@ export const disabled = settings.register({
2830
category: 'Updates',
2931
});
3032

33+
const silent = settings.register();
34+
3135
const frequency = settings.register({
3236
// TODO: translation
3337
name: 'Update Frequency',
@@ -47,6 +51,7 @@ const frequency = settings.register({
4751
},
4852
});
4953

54+
const dialog = new DialogHelper();
5055
const pendingUpdates = new Map();
5156

5257
export function register({
@@ -152,7 +157,7 @@ async function check(auto = true) {
152157
if (updateFound) {
153158
finish();
154159
// TODO: translation
155-
notify('Updates found.', true);
160+
notify('Updates available.', true);
156161
} else if (!auto && !pendingUpdates.size) {
157162
// TODO: translation
158163
notify('No updates available.');
@@ -182,7 +187,10 @@ function setup() {
182187
}
183188

184189
function open() {
185-
// TODO: window for showing pending updates
190+
dialog.open({
191+
title: 'Pending Updates', // TODO: translation
192+
message: build,
193+
});
186194
}
187195

188196
menu.addButton({
@@ -228,3 +236,62 @@ each(localStorage, (data, key) => wrap(() => {
228236

229237
sessionStorage.removeItem(CHECKING);
230238
eventManager.on('underscript:ready', setup);
239+
240+
function build() {
241+
let addedRefresh = false;
242+
const container = $('<div>');
243+
function refreshButton() {
244+
if (addedRefresh) return;
245+
dialog.prependButton({
246+
label: 'Refresh Page',
247+
cssClass: 'btn-success',
248+
action() {
249+
location.reload();
250+
},
251+
});
252+
addedRefresh = true;
253+
}
254+
function add({
255+
currentVersion,
256+
name,
257+
url,
258+
version,
259+
}) {
260+
const button = $(`<a>`)
261+
.text(`Update to ${version}`) // TODO: translation
262+
.attr({
263+
href: url,
264+
rel: 'noreferrer',
265+
target: 'updateUserScript',
266+
})
267+
.addClass('btn btn-primary')
268+
.on('click', () => {
269+
refreshButton();
270+
button.addClass('btn-success');
271+
});
272+
container.append($('<fieldset>').append(
273+
$('<legend>').text(name),
274+
$('<div>').text(`Current: ${currentVersion}`),
275+
button,
276+
));
277+
}
278+
const underscript = pendingUpdates.get('UnderScript');
279+
if (underscript) {
280+
add({
281+
...underscript,
282+
name: 'UnderScript',
283+
currentVersion: scriptVersion,
284+
});
285+
}
286+
[...pendingUpdates.entries()].forEach(
287+
([name, data]) => {
288+
if (name === 'UnderScript') return;
289+
add({
290+
...data,
291+
name,
292+
currentVersion: getVersion(name),
293+
});
294+
},
295+
);
296+
return container.children();
297+
}

src/utils/DialogHelper.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class DialogHelper {
1717
title,
1818
...options
1919
} = BootstrapDialog.defaultOptions) {
20-
if (this.#instance || !message || !title) return;
20+
if (this.isOpen() || !message || !title) return;
2121
BootstrapDialog.show({
2222
...options,
2323
title,
@@ -53,4 +53,20 @@ export default class DialogHelper {
5353
onOpen(callback) {
5454
this.#events.on('open', callback);
5555
}
56+
57+
appendButton(...buttons) {
58+
const diag = this.#instance;
59+
if (!diag) return;
60+
61+
diag.options.buttons.push(...buttons);
62+
diag.updateButtons();
63+
}
64+
65+
prependButton(...buttons) {
66+
const diag = this.#instance;
67+
if (!diag) return;
68+
69+
diag.options.buttons.unshift(...buttons);
70+
diag.updateButtons();
71+
}
5672
}

src/utils/plugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ export function registerModule(name, mod, ...dependencies) {
6868
export function getPluginNames() {
6969
return [...registry.keys()];
7070
}
71+
72+
export function getVersion(key) {
73+
return registry.get(key)?.version ?? '';
74+
}

0 commit comments

Comments
 (0)