2022-09-30 18:42:50 -04:00
|
|
|
import gitHash from "git-hash";
|
|
|
|
import { changes, checkForUpdates, getRepo, rebuild, update, UpdateLogger } from "../utils/updater";
|
2022-10-01 16:09:20 -04:00
|
|
|
import { React, Forms, Button, Margins, Alerts, Card, Parser, Toasts } from '../webpack/common';
|
2022-09-30 18:42:50 -04:00
|
|
|
import { Flex } from "./Flex";
|
|
|
|
import { useAwaiter } from '../utils/misc';
|
|
|
|
import { Link } from "./Link";
|
2022-10-01 16:09:20 -04:00
|
|
|
import ErrorBoundary from "./ErrorBoundary";
|
2022-09-30 18:42:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
function withDispatcher(dispatcher: React.Dispatch<React.SetStateAction<boolean>>, action: () => any) {
|
|
|
|
return async () => {
|
|
|
|
dispatcher(true);
|
|
|
|
try {
|
|
|
|
await action();
|
|
|
|
} catch (e: any) {
|
|
|
|
UpdateLogger.error("Failed to update", e);
|
|
|
|
if (!e) {
|
|
|
|
var err = "An unknown error occurred (error is undefined).\nPlease try again.";
|
|
|
|
} else if (e.code && e.cmd) {
|
|
|
|
const { code, path, cmd, stderr } = e;
|
|
|
|
|
|
|
|
if (code === "ENOENT")
|
|
|
|
var err = `Command \`${path}\` not found.\nPlease install it and try again`;
|
|
|
|
else {
|
|
|
|
var err = `An error occured while running \`${cmd}\`:\n`;
|
|
|
|
err += stderr || `Code \`${code}\`. See the console for more info`;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
var err = "An unknown error occurred. See the console for more info.";
|
|
|
|
}
|
|
|
|
Alerts.show({
|
|
|
|
title: "Oops!",
|
|
|
|
body: err.split("\n").map(line => <div>{Parser.parse(line)}</div>)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
dispatcher(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-01 16:09:20 -04:00
|
|
|
export default ErrorBoundary.wrap(function Updater() {
|
2022-09-30 18:42:50 -04:00
|
|
|
const [repo, err, repoPending] = useAwaiter(getRepo, "Loading...");
|
|
|
|
const [isChecking, setIsChecking] = React.useState(false);
|
|
|
|
const [isUpdating, setIsUpdating] = React.useState(false);
|
|
|
|
const [updates, setUpdates] = React.useState(changes);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (err)
|
|
|
|
UpdateLogger.error("Failed to retrieve repo", err);
|
|
|
|
}, [err]);
|
|
|
|
|
2022-10-01 16:09:20 -04:00
|
|
|
const isOutdated = updates.length > 0;
|
|
|
|
|
2022-09-30 18:42:50 -04:00
|
|
|
return (
|
2022-10-01 16:09:20 -04:00
|
|
|
<Forms.FormSection tag="h1" title="Vencord Updater">
|
|
|
|
<Forms.FormTitle tag="h5">Repo</Forms.FormTitle>
|
|
|
|
|
|
|
|
<Forms.FormText>{repoPending ? repo : err ? "Failed to retrieve - check console" : (
|
2022-09-30 18:42:50 -04:00
|
|
|
<Link href={repo}>
|
|
|
|
{repo.split("/").slice(-2).join("/")}
|
|
|
|
</Link>
|
|
|
|
)} ({gitHash})</Forms.FormText>
|
|
|
|
|
2022-10-01 16:09:20 -04:00
|
|
|
<Forms.FormDivider />
|
|
|
|
|
|
|
|
<Forms.FormTitle tag="h5">Updates</Forms.FormTitle>
|
|
|
|
|
2022-09-30 18:42:50 -04:00
|
|
|
<Forms.FormText className={Margins.marginBottom8}>
|
2022-10-01 16:09:20 -04:00
|
|
|
{updates.length ? `There are ${updates.length} Updates` : "Up to Date!"}
|
2022-09-30 18:42:50 -04:00
|
|
|
</Forms.FormText>
|
|
|
|
|
2022-10-01 16:09:20 -04:00
|
|
|
{updates.length > 0 && (
|
|
|
|
<Card style={{ padding: ".5em" }}>
|
|
|
|
{updates.map(({ hash, author, message }) => (
|
|
|
|
<div>
|
|
|
|
<Link href={`${repo}/commit/${hash}`} disabled={repoPending}>
|
|
|
|
<code>{hash}</code>
|
|
|
|
</Link>
|
|
|
|
<span style={{
|
|
|
|
marginLeft: "0.5em",
|
|
|
|
color: "var(--text-normal)"
|
|
|
|
}}>{message} - {author}</span>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</Card>
|
|
|
|
)}
|
2022-09-30 18:42:50 -04:00
|
|
|
|
|
|
|
<Flex className={`${Margins.marginBottom8} ${Margins.marginTop8}`}>
|
2022-10-01 16:09:20 -04:00
|
|
|
{isOutdated && <Button
|
2022-09-30 18:42:50 -04:00
|
|
|
size={Button.Sizes.SMALL}
|
|
|
|
disabled={isUpdating || isChecking}
|
|
|
|
onClick={withDispatcher(setIsUpdating, async () => {
|
|
|
|
if (await update()) {
|
|
|
|
const needFullRestart = await rebuild();
|
|
|
|
await new Promise<void>(r => {
|
|
|
|
Alerts.show({
|
|
|
|
title: "Update Success!",
|
|
|
|
body: "Successfully updated. Restart now to apply the changes?",
|
|
|
|
confirmText: "Restart",
|
|
|
|
cancelText: "Not now!",
|
|
|
|
onConfirm() {
|
|
|
|
if (needFullRestart)
|
|
|
|
window.DiscordNative.app.relaunch();
|
|
|
|
else
|
|
|
|
location.reload();
|
|
|
|
r();
|
|
|
|
},
|
|
|
|
onCancel: r
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
>
|
2022-10-01 16:09:20 -04:00
|
|
|
Update Now
|
|
|
|
</Button>}
|
2022-09-30 18:42:50 -04:00
|
|
|
<Button
|
|
|
|
size={Button.Sizes.SMALL}
|
|
|
|
disabled={isUpdating || isChecking}
|
|
|
|
onClick={withDispatcher(setIsChecking, async () => {
|
2022-10-01 16:09:20 -04:00
|
|
|
const outdated = await checkForUpdates();
|
|
|
|
if (outdated) {
|
2022-09-30 18:42:50 -04:00
|
|
|
setUpdates(changes);
|
|
|
|
} else {
|
2022-10-01 16:09:20 -04:00
|
|
|
Toasts.show({
|
|
|
|
message: "No updates found!",
|
|
|
|
id: Toasts.genId(),
|
|
|
|
type: Toasts.Type.MESSAGE,
|
|
|
|
options: {
|
|
|
|
position: Toasts.Position.BOTTOM
|
|
|
|
}
|
|
|
|
});
|
2022-09-30 18:42:50 -04:00
|
|
|
}
|
|
|
|
})}
|
|
|
|
>
|
2022-10-01 16:09:20 -04:00
|
|
|
Check for Updates
|
2022-09-30 18:42:50 -04:00
|
|
|
</Button>
|
|
|
|
</Flex>
|
2022-10-01 16:09:20 -04:00
|
|
|
</Forms.FormSection>
|
2022-09-30 18:42:50 -04:00
|
|
|
);
|
2022-10-01 16:09:20 -04:00
|
|
|
});
|