Several Fixes
- Fix error banner style - Cleanup updater code - Make notice api show in crash screens Co-authored-by: Zack Rauen <rauenzi@outlook.com>
This commit is contained in:
parent
18bb2886cd
commit
91e4cd5eb1
|
@ -1,26 +1,22 @@
|
||||||
import {Config} from "data";
|
|
||||||
import fileSystem from "fs";
|
|
||||||
import path from "path";
|
|
||||||
import request from "request";
|
import request from "request";
|
||||||
|
import fileSystem from "fs";
|
||||||
|
import {Config} from "data";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
import PluginManager from "./pluginmanager";
|
import PluginManager from "./pluginmanager";
|
||||||
import ThemeManager from "./thememanager";
|
import ThemeManager from "./thememanager";
|
||||||
|
|
||||||
import Toasts from "../ui/toasts";
|
import Toasts from "../ui/toasts";
|
||||||
import Notices from "../ui/notices";
|
import Notices from "../ui/notices";
|
||||||
import Logger from "../../../common/logger";
|
import Logger from "common/logger";
|
||||||
|
|
||||||
|
|
||||||
const base = "https://api.betterdiscord.app/v2/store/";
|
const base = "https://api.betterdiscord.app/v2/store/";
|
||||||
const route = r => `${base}${r}`;
|
const route = r => `${base}${r}`;
|
||||||
const redirect = addonId => `https://betterdiscord.app/gh-redirect?id=${addonId}`;
|
const redirect = addonId => `https://betterdiscord.app/gh-redirect?id=${addonId}`;
|
||||||
|
|
||||||
// const raceTimeout = (delay, reason) => new Promise((_, rej) => setTimeout(() => rej(reason), delay));
|
|
||||||
// const timeout = (promise, delay, reason) => Promise.race([promise, raceTimeout(delay, reason)]);
|
|
||||||
|
|
||||||
const getJSON = url => {
|
const getJSON = url => {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
request(url, (error, response, body) => {
|
request(url, (error, _, body) => {
|
||||||
if (error) return resolve([]);
|
if (error) return resolve([]);
|
||||||
resolve(JSON.parse(body));
|
resolve(JSON.parse(body));
|
||||||
});
|
});
|
||||||
|
@ -40,19 +36,15 @@ export default class AddonUpdater {
|
||||||
this.shown = false;
|
this.shown = false;
|
||||||
this.pending = [];
|
this.pending = [];
|
||||||
|
|
||||||
Logger.info("AddonUpdater", "Before get api");
|
|
||||||
const pluginData = await getJSON(route("plugins"));
|
const pluginData = await getJSON(route("plugins"));
|
||||||
const themeData = await getJSON(route("themes"));
|
const themeData = await getJSON(route("themes"));
|
||||||
Logger.info("AddonUpdater", "After get api");
|
|
||||||
|
|
||||||
this.temp = {pluginData, themeData};
|
|
||||||
|
|
||||||
pluginData.reduce(reducer, this.cache);
|
pluginData.reduce(reducer, this.cache);
|
||||||
themeData.reduce(reducer, this.cache);
|
themeData.reduce(reducer, this.cache);
|
||||||
|
|
||||||
Logger.info("AddonUpdater", "going to check lists");
|
|
||||||
for (const addon of PluginManager.addonList) this.checkForUpdate(addon.filename, addon.version);
|
for (const addon of PluginManager.addonList) this.checkForUpdate(addon.filename, addon.version);
|
||||||
for (const addon of ThemeManager.addonList) this.checkForUpdate(addon.filename, addon.version);
|
for (const addon of ThemeManager.addonList) this.checkForUpdate(addon.filename, addon.version);
|
||||||
|
|
||||||
this.showUpdateNotice();
|
this.showUpdateNotice();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +53,6 @@ export default class AddonUpdater {
|
||||||
}
|
}
|
||||||
|
|
||||||
static async checkForUpdate(filename, currentVersion) {
|
static async checkForUpdate(filename, currentVersion) {
|
||||||
Logger.info("AddonUpdater", "checkForUpdate", filename, currentVersion);
|
|
||||||
const info = this.cache[path.basename(filename)];
|
const info = this.cache[path.basename(filename)];
|
||||||
if (!info) return;
|
if (!info) return;
|
||||||
const hasUpdate = info.version > currentVersion;
|
const hasUpdate = info.version > currentVersion;
|
||||||
|
@ -71,22 +62,20 @@ export default class AddonUpdater {
|
||||||
|
|
||||||
static async updatePlugin(filename) {
|
static async updatePlugin(filename) {
|
||||||
const info = this.cache[filename];
|
const info = this.cache[filename];
|
||||||
request(redirect(info.id), (err, response) => {
|
request(redirect(info.id), (error, _, body) => {
|
||||||
if (err) return;
|
if (error) {
|
||||||
if (!response.headers.location) return; // expected redirect
|
Logger.stacktrace("AddonUpdater", `Failed to download body for ${info.id}:`, error);
|
||||||
request(response.headers.location, (error, _, body) => {
|
return;
|
||||||
if (error) return;
|
}
|
||||||
const file = path.join(path.resolve(Config.dataPath, info.type + "s"), filename);
|
|
||||||
fileSystem.writeFile(file, body.toString(), () => {
|
|
||||||
Toasts.success(`${info.name} has been updated to version ${info.version}!`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
const file = path.join(path.resolve(Config.dataPath, info.type + "s"), filename);
|
||||||
|
fileSystem.writeFile(file, body.toString(), () => {
|
||||||
|
Toasts.success(`${info.name} has been updated to version ${info.version}!`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static showUpdateNotice() {
|
static showUpdateNotice() {
|
||||||
Logger.info("AddonUpdater", "showUpdateNotice", this.shown, this.pending);
|
|
||||||
if (this.shown || !this.pending.length) return;
|
if (this.shown || !this.pending.length) return;
|
||||||
this.shown = true;
|
this.shown = true;
|
||||||
const close = Notices.info(`BetterDiscord has found updates for ${this.pending.length} of your plugins and themes!`, {
|
const close = Notices.info(`BetterDiscord has found updates for ${this.pending.length} of your plugins and themes!`, {
|
||||||
|
@ -105,5 +94,3 @@ export default class AddonUpdater {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.updater = AddonUpdater;
|
|
|
@ -155,7 +155,7 @@ BdApi.showToast = function(content, options = {}) {
|
||||||
* @param {object} options Options for the notice.
|
* @param {object} options Options for the notice.
|
||||||
* @param {string} [options.type="info" | "error" | "warning" | "success"] Type for the notice. Will affect the color.
|
* @param {string} [options.type="info" | "error" | "warning" | "success"] Type for the notice. Will affect the color.
|
||||||
* @param {Array<{label: string, onClick: function}>} [options.buttons] Buttons that should be added next to the notice text.
|
* @param {Array<{label: string, onClick: function}>} [options.buttons] Buttons that should be added next to the notice text.
|
||||||
* @param {number} [options.timeout=10000] Timeout until the notice is closed. Won't fire if it's set to 0;
|
* @param {number} [options.timeout=0] Timeout until the notice is closed. Won't fire if it's set to 0;
|
||||||
* @returns {function} A callback for closing the notice. Passing `true` as first parameter closes immediately without transitioning out.
|
* @returns {function} A callback for closing the notice. Passing `true` as first parameter closes immediately without transitioning out.
|
||||||
*/
|
*/
|
||||||
BdApi.showNotice = function (content, options = {}) {
|
BdApi.showNotice = function (content, options = {}) {
|
||||||
|
@ -681,4 +681,4 @@ Object.freeze(BdApi.Patcher);
|
||||||
Object.freeze(BdApi.Webpack);
|
Object.freeze(BdApi.Webpack);
|
||||||
Object.freeze(BdApi.Webpack.Filters);
|
Object.freeze(BdApi.Webpack.Filters);
|
||||||
|
|
||||||
export default BdApi;
|
export default BdApi;
|
||||||
|
|
|
@ -112,9 +112,10 @@
|
||||||
|
|
||||||
.bd-description-wrap .banner {
|
.bd-description-wrap .banner {
|
||||||
padding: 5px;
|
padding: 5px;
|
||||||
border-left: 5px solid gray;
|
border: 2px solid gray;
|
||||||
background: #26191E;
|
background: #26191E;
|
||||||
color: #C13A3A;
|
color: #ffffff;
|
||||||
|
font-weight: 700px;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
@ -122,19 +123,18 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.banner.banner-danger {
|
.banner.banner-danger {
|
||||||
border-left-color: #C13A3A;
|
border-color: #F04747;
|
||||||
background: #26191E;
|
background: #473C41;
|
||||||
color: #C13A3A;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.banner .bd-icon {
|
.banner .bd-icon {
|
||||||
fill: red;
|
fill: #ffffff;
|
||||||
margin-right: 5px;
|
margin-right: 5px;
|
||||||
height: 16px !important;
|
height: 16px !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.banner-danger .bd-icon {
|
.banner-danger .bd-icon {
|
||||||
fill: red;
|
fill: #F04747;
|
||||||
}
|
}
|
||||||
|
|
||||||
.bd-addon-list .bd-description {
|
.bd-addon-list .bd-description {
|
||||||
|
@ -315,4 +315,4 @@
|
||||||
|
|
||||||
.bd-addon-list .bd-footer .bd-links .bd-addon-button {
|
.bd-addon-list .bd-footer .bd-links .bd-addon-button {
|
||||||
height: 24px;
|
height: 24px;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
import {WebpackModules} from "modules";
|
import {Utilities, WebpackModules} from "modules";
|
||||||
|
|
||||||
export default class Notices {
|
export default class Notices {
|
||||||
static get baseClass() {return this.__baseClass || (this.__baseClass = WebpackModules.getByProps("container", "base")?.base);}
|
static get baseClass() {return this.__baseClass ??= WebpackModules.getByProps("container", "base")?.base;}
|
||||||
|
static get errorPageClass() {return this.__errorPageClass ??= WebpackModules.getByProps("errorPage")?.errorPage;}
|
||||||
|
|
||||||
/** Shorthand for `type = "info"` for {@link module:Notices.show} */
|
/** Shorthand for `type = "info"` for {@link module:Notices.show} */
|
||||||
static info(content, options = {}) {return this.show(content, Object.assign({}, options, {type: "info"}));}
|
static info(content, options = {}) {return this.show(content, Object.assign({}, options, {type: "info"}));}
|
||||||
|
@ -32,7 +33,7 @@ export default class Notices {
|
||||||
* @param {object} options Options for the notice.
|
* @param {object} options Options for the notice.
|
||||||
* @param {string} [options.type="info" | "error" | "warning" | "success"] Type for the notice. Will affect the color.
|
* @param {string} [options.type="info" | "error" | "warning" | "success"] Type for the notice. Will affect the color.
|
||||||
* @param {Array<{label: string, onClick: (immediately?: boolean = false) => void}>} [options.buttons] Buttons that should be added next to the notice text.
|
* @param {Array<{label: string, onClick: (immediately?: boolean = false) => void}>} [options.buttons] Buttons that should be added next to the notice text.
|
||||||
* @param {number} [options.timeout=10000] Timeout until the toast is closed. Won't fire if it's set to 0;
|
* @param {number} [options.timeout=0] Timeout until the toast is closed. Won't fire if it's set to 0;
|
||||||
* @returns {(immediately?: boolean = false) => void}
|
* @returns {(immediately?: boolean = false) => void}
|
||||||
*/
|
*/
|
||||||
static show(content, options = {}) {
|
static show(content, options = {}) {
|
||||||
|
@ -83,6 +84,14 @@ export default class Notices {
|
||||||
});
|
});
|
||||||
container.prepend(noticeContainer);
|
container.prepend(noticeContainer);
|
||||||
|
|
||||||
|
Utilities.onRemoved(container, async () => {
|
||||||
|
if (!this.errorPageClass) return;
|
||||||
|
|
||||||
|
const element = await new Promise(res => Utilities.onAdded(`.${this.errorPageClass}`, res));
|
||||||
|
|
||||||
|
element.prepend(noticeContainer);
|
||||||
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue