2022-10-21 19:17:06 -04:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-05-05 19:36:00 -04:00
|
|
|
import { useSettings } from "@api/Settings";
|
2022-11-28 07:58:14 -05:00
|
|
|
import { ErrorCard } from "@components/ErrorCard";
|
|
|
|
import { Flex } from "@components/Flex";
|
|
|
|
import { Link } from "@components/Link";
|
2023-03-01 15:35:08 -05:00
|
|
|
import { Margins } from "@utils/margins";
|
2023-05-05 19:36:00 -04:00
|
|
|
import { classes } from "@utils/misc";
|
2023-04-03 19:16:29 -04:00
|
|
|
import { relaunch } from "@utils/native";
|
2023-05-05 19:36:00 -04:00
|
|
|
import { useAwaiter } from "@utils/react";
|
2023-05-01 20:50:51 -04:00
|
|
|
import { changes, checkForUpdates, getRepo, isNewer, update, updateError, UpdateLogger } from "@utils/updater";
|
2023-03-01 15:35:08 -05:00
|
|
|
import { Alerts, Button, Card, Forms, Parser, React, Switch, Toasts } from "@webpack/common";
|
2022-11-28 07:37:55 -05:00
|
|
|
|
2022-10-23 17:23:52 -04:00
|
|
|
import gitHash from "~git-hash";
|
2022-10-22 12:18:41 -04:00
|
|
|
|
2023-05-11 19:40:43 -04:00
|
|
|
import { SettingsTab, wrapTab } from "./shared";
|
|
|
|
|
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 {
|
2023-10-26 22:06:05 -04:00
|
|
|
var err = `An error occurred while running \`${cmd}\`:\n`;
|
2022-09-30 18:42:50 -04:00
|
|
|
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!",
|
2022-10-01 20:46:41 -04:00
|
|
|
body: (
|
|
|
|
<ErrorCard>
|
2023-10-31 18:56:13 -04:00
|
|
|
{err.split("\n").map(line => <div>{Parser.parse(line)}</div>)}
|
2022-10-01 20:46:41 -04:00
|
|
|
</ErrorCard>
|
|
|
|
)
|
2022-09-30 18:42:50 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
dispatcher(false);
|
|
|
|
}
|
|
|
|
};
|
2022-10-08 14:36:57 -04:00
|
|
|
}
|
2022-09-30 18:42:50 -04:00
|
|
|
|
2022-10-05 11:09:37 -04:00
|
|
|
interface CommonProps {
|
|
|
|
repo: string;
|
|
|
|
repoPending: boolean;
|
|
|
|
}
|
2022-10-01 16:09:20 -04:00
|
|
|
|
2023-01-02 20:30:54 -05:00
|
|
|
function HashLink({ repo, hash, disabled = false }: { repo: string, hash: string, disabled?: boolean; }) {
|
|
|
|
return <Link href={`${repo}/commit/${hash}`} disabled={disabled}>
|
|
|
|
{hash}
|
|
|
|
</Link>;
|
|
|
|
}
|
|
|
|
|
2022-10-05 11:09:37 -04:00
|
|
|
function Changes({ updates, repo, repoPending }: CommonProps & { updates: typeof changes; }) {
|
2022-09-30 18:42:50 -04:00
|
|
|
return (
|
2024-01-05 18:55:14 -05:00
|
|
|
<Card style={{ padding: "0 0.5em" }}>
|
2022-10-05 11:09:37 -04:00
|
|
|
{updates.map(({ hash, author, message }) => (
|
2024-01-05 18:55:14 -05:00
|
|
|
<div style={{
|
|
|
|
marginTop: "0.5em",
|
|
|
|
marginBottom: "0.5em"
|
|
|
|
}}>
|
2023-01-02 20:30:54 -05:00
|
|
|
<code><HashLink {...{ repo, hash }} disabled={repoPending} /></code>
|
2022-10-05 11:09:37 -04:00
|
|
|
<span style={{
|
|
|
|
marginLeft: "0.5em",
|
|
|
|
color: "var(--text-normal)"
|
|
|
|
}}>{message} - {author}</span>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
2022-09-30 18:42:50 -04:00
|
|
|
|
2022-10-05 11:09:37 -04:00
|
|
|
function Updatable(props: CommonProps) {
|
|
|
|
const [updates, setUpdates] = React.useState(changes);
|
|
|
|
const [isChecking, setIsChecking] = React.useState(false);
|
|
|
|
const [isUpdating, setIsUpdating] = React.useState(false);
|
2022-10-01 16:09:20 -04:00
|
|
|
|
2022-10-18 03:57:32 -04:00
|
|
|
const isOutdated = (updates?.length ?? 0) > 0;
|
2022-10-01 16:09:20 -04:00
|
|
|
|
2022-10-05 11:09:37 -04:00
|
|
|
return (
|
|
|
|
<>
|
2022-10-01 20:46:41 -04:00
|
|
|
{!updates && updateError ? (
|
|
|
|
<>
|
|
|
|
<Forms.FormText>Failed to check updates. Check the console for more info</Forms.FormText>
|
|
|
|
<ErrorCard style={{ padding: "1em" }}>
|
|
|
|
<p>{updateError.stderr || updateError.stdout || "An unknown error occurred"}</p>
|
|
|
|
</ErrorCard>
|
|
|
|
</>
|
2022-10-05 11:09:37 -04:00
|
|
|
) : (
|
2023-03-01 15:35:08 -05:00
|
|
|
<Forms.FormText className={Margins.bottom8}>
|
2024-01-05 18:10:16 -05:00
|
|
|
{isOutdated ? (updates.length === 1 ? "There is 1 Update" : `There are ${updates.length} Updates`) : "Up to Date!"}
|
2022-10-05 11:09:37 -04:00
|
|
|
</Forms.FormText>
|
2022-10-01 16:09:20 -04:00
|
|
|
)}
|
2022-09-30 18:42:50 -04:00
|
|
|
|
2022-10-05 11:09:37 -04:00
|
|
|
{isOutdated && <Changes updates={updates} {...props} />}
|
|
|
|
|
2023-03-01 15:35:08 -05:00
|
|
|
<Flex className={classes(Margins.bottom8, Margins.top8)}>
|
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()) {
|
2022-10-01 16:14:57 -04:00
|
|
|
setUpdates([]);
|
2022-09-30 18:42:50 -04:00
|
|
|
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() {
|
2023-04-08 22:04:02 -04:00
|
|
|
relaunch();
|
2022-09-30 18:42:50 -04:00
|
|
|
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:14:57 -04:00
|
|
|
setUpdates([]);
|
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-05 11:09:37 -04:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Newer(props: CommonProps) {
|
|
|
|
return (
|
|
|
|
<>
|
2023-03-01 15:35:08 -05:00
|
|
|
<Forms.FormText className={Margins.bottom8}>
|
2022-10-05 11:09:37 -04:00
|
|
|
Your local copy has more recent commits. Please stash or reset them.
|
|
|
|
</Forms.FormText>
|
|
|
|
<Changes {...props} updates={changes} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-16 11:15:15 -04:00
|
|
|
function Updater() {
|
2023-03-19 05:21:26 -04:00
|
|
|
const settings = useSettings(["notifyAboutUpdates", "autoUpdate", "autoUpdateNotification"]);
|
2023-01-23 19:42:57 -05:00
|
|
|
|
2022-12-02 10:43:37 -05:00
|
|
|
const [repo, err, repoPending] = useAwaiter(getRepo, { fallbackValue: "Loading..." });
|
2022-10-05 11:09:37 -04:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (err)
|
|
|
|
UpdateLogger.error("Failed to retrieve repo", err);
|
|
|
|
}, [err]);
|
|
|
|
|
|
|
|
const commonProps: CommonProps = {
|
|
|
|
repo,
|
|
|
|
repoPending
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-05-11 19:40:43 -04:00
|
|
|
<SettingsTab title="Vencord Updater">
|
2023-01-23 19:42:57 -05:00
|
|
|
<Forms.FormTitle tag="h5">Updater Settings</Forms.FormTitle>
|
|
|
|
<Switch
|
|
|
|
value={settings.notifyAboutUpdates}
|
|
|
|
onChange={(v: boolean) => settings.notifyAboutUpdates = v}
|
2023-03-19 05:21:26 -04:00
|
|
|
note="Shows a notification on startup"
|
2023-01-23 19:42:57 -05:00
|
|
|
disabled={settings.autoUpdate}
|
|
|
|
>
|
|
|
|
Get notified about new updates
|
|
|
|
</Switch>
|
|
|
|
<Switch
|
|
|
|
value={settings.autoUpdate}
|
|
|
|
onChange={(v: boolean) => settings.autoUpdate = v}
|
|
|
|
note="Automatically update Vencord without confirmation prompt"
|
|
|
|
>
|
|
|
|
Automatically update
|
|
|
|
</Switch>
|
2023-03-19 05:21:26 -04:00
|
|
|
<Switch
|
|
|
|
value={settings.autoUpdateNotification}
|
|
|
|
onChange={(v: boolean) => settings.autoUpdateNotification = v}
|
|
|
|
note="Shows a notification when Vencord automatically updates"
|
|
|
|
disabled={!settings.autoUpdate}
|
|
|
|
>
|
|
|
|
Get notified when an automatic update completes
|
|
|
|
</Switch>
|
2023-01-23 19:42:57 -05:00
|
|
|
|
2022-10-05 11:09:37 -04:00
|
|
|
<Forms.FormTitle tag="h5">Repo</Forms.FormTitle>
|
|
|
|
|
2023-04-08 17:28:12 -04:00
|
|
|
<Forms.FormText className="vc-text-selectable">
|
|
|
|
{repoPending
|
|
|
|
? repo
|
|
|
|
: err
|
|
|
|
? "Failed to retrieve - check console"
|
|
|
|
: (
|
|
|
|
<Link href={repo}>
|
|
|
|
{repo.split("/").slice(-2).join("/")}
|
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{" "}(<HashLink hash={gitHash} repo={repo} disabled={repoPending} />)
|
|
|
|
</Forms.FormText>
|
2022-10-05 11:09:37 -04:00
|
|
|
|
2023-03-01 15:35:08 -05:00
|
|
|
<Forms.FormDivider className={Margins.top8 + " " + Margins.bottom8} />
|
2022-10-05 11:09:37 -04:00
|
|
|
|
|
|
|
<Forms.FormTitle tag="h5">Updates</Forms.FormTitle>
|
|
|
|
|
|
|
|
{isNewer ? <Newer {...commonProps} /> : <Updatable {...commonProps} />}
|
2023-05-11 19:40:43 -04:00
|
|
|
</SettingsTab>
|
2022-09-30 18:42:50 -04:00
|
|
|
);
|
2022-10-16 11:15:15 -04:00
|
|
|
}
|
|
|
|
|
2023-08-25 08:15:02 -04:00
|
|
|
export default IS_UPDATER_DISABLED ? null : wrapTab(Updater, "Updater");
|