refactor: move check updates function, use new api

This commit is contained in:
UndefinedPony
2024-12-20 17:26:31 +01:00
parent a5cd8f18cd
commit 196603126b

View File

@@ -20,10 +20,28 @@ export const UpdateServer = () => {
const [isUpdateAvailable, setIsUpdateAvailable] = useState<null | boolean>( const [isUpdateAvailable, setIsUpdateAvailable] = useState<null | boolean>(
null, null,
); );
const { mutateAsync: checkServerUpdates, isLoading } = const { mutateAsync: checkForUpdate, isLoading } =
api.settings.checkServerUpdates.useMutation(); api.settings.checkForUpdate.useMutation();
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const handleCheckUpdates = async () => {
try {
const updateAvailable = await checkForUpdate();
setIsUpdateAvailable(updateAvailable);
if (updateAvailable) {
toast.success("Update is available!");
} else {
toast.info("No updates available");
}
} catch (error) {
console.error("Error checking for updates:", error);
setIsUpdateAvailable(false);
toast.error(
"An error occurred while checking for updates, please try again.",
);
}
};
return ( return (
<Dialog open={isOpen} onOpenChange={setIsOpen}> <Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild> <DialogTrigger asChild>
@@ -76,24 +94,7 @@ export const UpdateServer = () => {
) : ( ) : (
<Button <Button
className="w-full" className="w-full"
onClick={async () => { onClick={handleCheckUpdates}
await checkServerUpdates()
.then(async (updateAvailable) => {
setIsUpdateAvailable(updateAvailable);
toast.info(
updateAvailable
? "Update is available"
: "No updates available",
);
})
.catch((error) => {
console.error("Error checking for updates:", error);
setIsUpdateAvailable(false);
toast.error(
"An error occurred while checking for updates, please try again.",
);
});
}}
isLoading={isLoading} isLoading={isLoading}
> >
{isLoading ? "Checking for updates..." : "Check for updates"} {isLoading ? "Checking for updates..." : "Check for updates"}