|
1 | | -import { useCallback, useState } from "react"; |
| 1 | +import { useState } from "react"; |
2 | 2 | import { check } from "@tauri-apps/plugin-updater"; |
3 | | -import { ask, message } from "@tauri-apps/plugin-dialog"; |
4 | 3 | import { invoke } from "@tauri-apps/api/core"; // Import Tauri invoke |
| 4 | +import { ToastAction } from "@/components/ui/toast"; |
5 | 5 |
|
6 | 6 | // Custom hook for handling updates |
7 | | -export const useUpdater = () => { |
| 7 | +export const useUpdater = (toast: any) => { |
8 | 8 | const [progress, setProgress] = useState(0); |
9 | 9 | const [showProgressDialog, setShowProgressDialog] = useState(false); |
10 | 10 |
|
11 | | - const checkForUpdates = useCallback(async (onUserClick = false) => { |
12 | | - try { |
13 | | - // Check for updates |
14 | | - const update = await check(); |
15 | | - if (update === null) { |
16 | | - await message("Failed to check for updates.\nPlease try again later.", { |
17 | | - title: "Error", |
18 | | - kind: "error", |
19 | | - okLabel: "OK", |
20 | | - }); |
21 | | - return; |
22 | | - } else if (update?.available) { |
23 | | - const userConfirmed = await ask( |
24 | | - `Update to ${update.version} is available!\n\nRelease notes: ${update.body}`, |
25 | | - { |
26 | | - title: "Update Available", |
27 | | - kind: "info", |
28 | | - okLabel: "Update", |
29 | | - cancelLabel: "Cancel", |
30 | | - }, |
31 | | - ); |
32 | | - if (userConfirmed) { |
33 | | - setShowProgressDialog(true); |
| 11 | + const checkForUpdates = async () => { |
| 12 | + // Check for updates |
| 13 | + const update = await check(); |
| 14 | + if (update === null) { |
| 15 | + toast({ |
| 16 | + title: "Failed to check for updates", |
| 17 | + description: "Please try again later.", |
| 18 | + variant: "destructive", |
| 19 | + }); |
| 20 | + return; |
| 21 | + } else if (update?.available) { |
| 22 | + toast({ |
| 23 | + title: "Update Available", |
| 24 | + description: `Version ${update.version} is available!`, |
| 25 | + action: ( |
| 26 | + <ToastAction |
| 27 | + onClick={async () => { |
| 28 | + try { |
| 29 | + setShowProgressDialog(true); |
34 | 30 |
|
35 | | - let downloaded = 0; |
36 | | - let contentLength = 0; |
| 31 | + let downloaded = 0; |
| 32 | + let contentLength = 0; |
37 | 33 |
|
38 | | - await update.downloadAndInstall((event) => { |
39 | | - switch (event.event) { |
40 | | - case "Started": |
41 | | - contentLength = event.data.contentLength || 0; |
42 | | - break; |
43 | | - case "Progress": |
44 | | - downloaded += event.data.chunkLength; |
45 | | - const percentage = (downloaded / contentLength) * 100; |
46 | | - setProgress(percentage); |
47 | | - break; |
48 | | - case "Finished": |
49 | | - setProgress(100); |
50 | | - break; |
51 | | - } |
52 | | - }); |
| 34 | + await update.downloadAndInstall((event) => { |
| 35 | + switch (event.event) { |
| 36 | + case "Started": |
| 37 | + contentLength = event.data.contentLength || 0; |
| 38 | + break; |
| 39 | + case "Progress": |
| 40 | + downloaded += event.data.chunkLength; |
| 41 | + const percentage = (downloaded / contentLength) * 100; |
| 42 | + setProgress(percentage); |
| 43 | + break; |
| 44 | + case "Finished": |
| 45 | + setProgress(100); |
| 46 | + break; |
| 47 | + } |
| 48 | + }); |
53 | 49 |
|
54 | | - setShowProgressDialog(false); |
55 | | - // After the update is downloaded and installed, gracefully restart the app |
56 | | - await invoke("graceful_restart"); |
57 | | - } |
58 | | - } else if (onUserClick) { |
59 | | - await message("You are on the latest version. Stay awesome!", { |
60 | | - title: "No Update Available", |
61 | | - kind: "info", |
62 | | - okLabel: "OK", |
63 | | - }); |
64 | | - } |
65 | | - } catch (error) { |
66 | | - console.error("Error during update check:", error); |
67 | | - await message( |
68 | | - "An error occurred while checking for updates. Please try again later.", |
69 | | - { |
70 | | - title: "Error", |
71 | | - kind: "error", |
72 | | - okLabel: "OK", |
73 | | - }, |
74 | | - ); |
| 50 | + setShowProgressDialog(false); |
| 51 | + // After the update is downloaded and installed, gracefully restart the app |
| 52 | + await invoke("graceful_restart"); |
| 53 | + } catch (error) { |
| 54 | + console.error("Error during update check:", error); |
| 55 | + toast({ |
| 56 | + title: "An error occurred while checking for updates", |
| 57 | + description: "Please try again later.", |
| 58 | + variant: "destructive", |
| 59 | + }); |
| 60 | + } |
| 61 | + }} |
| 62 | + altText="Update" |
| 63 | + > |
| 64 | + Update |
| 65 | + </ToastAction> |
| 66 | + ), |
| 67 | + }); |
75 | 68 | } |
76 | | - }, []); |
| 69 | + }; |
77 | 70 |
|
78 | 71 | return { checkForUpdates, progress, showProgressDialog }; |
79 | 72 | }; |
0 commit comments