BetterDiscordApp-rauenzi/renderer/src/ui/updater.jsx

114 lines
4.8 KiB
React
Raw Normal View History

2023-05-20 00:37:21 +02:00
import Config from "@data/config";
2023-05-19 23:14:55 +02:00
import React from "@modules/react";
import Strings from "@modules/strings";
import Events from "@modules/emitter";
2023-05-20 00:37:21 +02:00
2022-10-03 10:40:18 +02:00
import Drawer from "./settings/drawer";
import SettingItem from "./settings/components/item";
import SettingsTitle from "./settings/title";
import Toasts from "./toasts";
2023-05-20 00:37:21 +02:00
import Checkmark from "@ui/icons/check";
2022-10-03 10:40:18 +02:00
2023-03-09 02:09:10 +01:00
const {useState, useCallback, useEffect} = React;
2023-03-20 03:23:11 +01:00
function CoreUpdaterPanel({hasUpdate, remoteVersion, update}) {
2023-03-06 08:50:48 +01:00
return <Drawer name="BetterDiscord" collapsible={true}>
2023-03-20 03:23:11 +01:00
<SettingItem name={`Core v${Config.version}`} note={hasUpdate ? Strings.Updater.versionAvailable.format({version: remoteVersion}) : Strings.Updater.noUpdatesAvailable} inline={true} id={"core-updater"}>
{!hasUpdate && <div className="bd-filled-checkmark"><Checkmark /></div>}
{hasUpdate && <button className="bd-button" onClick={update}>{Strings.Updater.updateButton}</button>}
2023-03-06 08:50:48 +01:00
</SettingItem>
</Drawer>;
2022-10-03 10:40:18 +02:00
}
2023-03-20 03:23:11 +01:00
function NoUpdates({type}) {
2023-03-06 08:50:48 +01:00
return <div className="bd-empty-updates">
<Checkmark size="48px" />
2023-03-20 03:23:11 +01:00
{Strings.Updater.upToDateBlankslate.format({type: type})}
2023-03-06 08:50:48 +01:00
</div>;
2022-10-03 10:40:18 +02:00
}
2023-03-20 03:23:11 +01:00
function AddonUpdaterPanel({pending, type, updater, update, updateAll}) {
const filenames = pending;
return <Drawer name={Strings.Panels[type]} collapsible={true} button={filenames.length ? {title: Strings.Updater.updateAll, onClick: () => updateAll(type)} : null}>
{!filenames.length && <NoUpdates type={type} />}
2023-03-06 08:50:48 +01:00
{filenames.map(f => {
2023-03-20 03:23:11 +01:00
const info = updater.cache[f];
const addon = updater.manager.addonList.find(a => a.filename === f);
2023-03-06 08:50:48 +01:00
return <SettingItem name={`${addon.name} v${addon.version}`} note={Strings.Updater.versionAvailable.format({version: info.version})} inline={true} id={addon.name}>
2023-03-20 03:23:11 +01:00
<button className="bd-button" onClick={() => update(type, f)}>{Strings.Updater.updateButton}</button>
2023-03-06 08:50:48 +01:00
</SettingItem>;
})}
</Drawer>;
2022-10-03 10:40:18 +02:00
}
2023-03-20 03:23:11 +01:00
export default function UpdaterPanel({coreUpdater, pluginUpdater, themeUpdater}) {
const [hasCoreUpdate, setCoreUpdate] = useState(coreUpdater.hasUpdate);
const [updates, setUpdates] = useState({plugins: pluginUpdater.pending.slice(0), themes: themeUpdater.pending.slice(0)});
2022-10-03 10:40:18 +02:00
2023-03-09 02:09:10 +01:00
const checkAddons = useCallback(async (type) => {
2023-03-20 03:23:11 +01:00
const updater = type === "plugins" ? pluginUpdater : themeUpdater;
2023-03-09 02:09:10 +01:00
await updater.checkAll(false);
setUpdates({...updates, [type]: updater.pending.slice(0)});
2023-03-20 03:23:11 +01:00
}, [updates, pluginUpdater, themeUpdater]);
2023-03-09 02:09:10 +01:00
const update = useCallback(() => {
checkAddons("plugins");
checkAddons("themes");
2023-03-20 03:23:11 +01:00
}, [checkAddons]);
2023-03-09 02:09:10 +01:00
useEffect(() => {
Events.on(`plugin-loaded`, update);
Events.on(`plugin-unloaded`, update);
Events.on(`theme-loaded`, update);
Events.on(`theme-unloaded`, update);
return () => {
Events.off(`plugin-loaded`, update);
Events.off(`plugin-unloaded`, update);
Events.off(`theme-loaded`, update);
Events.off(`theme-unloaded`, update);
2022-10-03 10:40:18 +02:00
};
2023-03-20 03:23:11 +01:00
}, [update]);
2022-10-03 10:40:18 +02:00
2023-03-09 02:09:10 +01:00
const checkCoreUpdate = useCallback(async () => {
2023-03-20 03:23:11 +01:00
await coreUpdater.checkForUpdate(false);
setCoreUpdate(coreUpdater.hasUpdate);
}, [coreUpdater]);
2022-10-03 10:40:18 +02:00
2023-03-09 02:09:10 +01:00
const checkForUpdates = useCallback(async () => {
Toasts.info(Strings.Updater.checking);
2023-03-09 02:09:10 +01:00
await checkCoreUpdate();
await checkAddons("plugins");
await checkAddons("themes");
Toasts.info(Strings.Updater.finishedChecking);
2023-03-20 03:23:11 +01:00
}, [checkAddons, checkCoreUpdate]);
2022-10-03 10:40:18 +02:00
2023-03-09 02:09:10 +01:00
const updateCore = useCallback(async () => {
2023-03-20 03:23:11 +01:00
await coreUpdater.update();
2023-03-09 02:09:10 +01:00
setCoreUpdate(false);
2023-03-20 03:23:11 +01:00
}, [coreUpdater]);
2022-10-03 10:40:18 +02:00
2023-03-09 02:09:10 +01:00
const updateAddon = useCallback(async (type, filename) => {
2023-03-20 03:23:11 +01:00
const updater = type === "plugins" ? pluginUpdater : themeUpdater;
2022-10-03 10:40:18 +02:00
await updater.updateAddon(filename);
2023-03-09 02:09:10 +01:00
setUpdates(prev => {
2022-10-03 10:40:18 +02:00
prev[type].splice(prev[type].indexOf(filename), 1);
return prev;
});
2023-03-20 03:23:11 +01:00
}, [pluginUpdater, themeUpdater]);
2022-10-03 10:40:18 +02:00
2023-03-09 02:09:10 +01:00
const updateAllAddons = useCallback(async (type) => {
const toUpdate = updates[type].slice(0);
2022-10-03 10:40:18 +02:00
for (const filename of toUpdate) {
2023-03-09 02:09:10 +01:00
await updateAddon(type, filename);
2022-10-03 10:40:18 +02:00
}
2023-03-20 03:23:11 +01:00
}, [updateAddon, updates]);
2023-03-09 02:09:10 +01:00
return [
<SettingsTitle text={Strings.Panels.updates} button={{title: Strings.Updater.checkForUpdates, onClick: checkForUpdates}} />,
2023-03-20 03:23:11 +01:00
<CoreUpdaterPanel remoteVersion={coreUpdater.remoteVersion} hasUpdate={hasCoreUpdate} update={updateCore} />,
<AddonUpdaterPanel type="plugins" pending={updates.plugins} update={updateAddon} updateAll={updateAllAddons} updater={pluginUpdater} />,
<AddonUpdaterPanel type="themes" pending={updates.themes} update={updateAddon} updateAll={updateAllAddons} updater={themeUpdater} />,
2023-03-09 02:09:10 +01:00
];
2022-10-03 10:40:18 +02:00
}