@@ -5,15 +5,13 @@ import { relaunch } from "@tauri-apps/plugin-process"
55
66export type UpdateStatus =
77 | { status : "idle" }
8- | { status : "available" ; version : string }
98 | { status : "downloading" ; progress : number } // 0-100, or -1 if indeterminate
109 | { status : "installing" }
1110 | { status : "ready" }
1211 | { status : "error" ; message : string }
1312
1413interface UseAppUpdateReturn {
1514 updateStatus : UpdateStatus
16- triggerDownload : ( ) => void
1715 triggerInstall : ( ) => void
1816}
1917
@@ -34,70 +32,62 @@ export function useAppUpdate(): UseAppUpdateReturn {
3432 let cancelled = false
3533 mountedRef . current = true
3634
37- const checkForUpdate = async ( ) => {
35+ const checkAndDownload = async ( ) => {
3836 if ( ! isTauri ( ) ) return
3937 try {
4038 const update = await check ( )
4139 if ( cancelled ) return
4240 if ( update ) {
4341 updateRef . current = update
44- setStatus ( { status : "available" , version : update . version } )
42+ // Immediately start downloading (no "available" state)
43+ inFlightRef . current . downloading = true
44+ setStatus ( { status : "downloading" , progress : - 1 } )
45+
46+ let totalBytes : number | null = null
47+ let downloadedBytes = 0
48+
49+ try {
50+ await update . download ( ( event ) => {
51+ if ( ! mountedRef . current ) return
52+ if ( event . event === "Started" ) {
53+ totalBytes = event . data . contentLength ?? null
54+ downloadedBytes = 0
55+ setStatus ( {
56+ status : "downloading" ,
57+ progress : totalBytes ? 0 : - 1 ,
58+ } )
59+ } else if ( event . event === "Progress" ) {
60+ downloadedBytes += event . data . chunkLength
61+ if ( totalBytes && totalBytes > 0 ) {
62+ const pct = Math . min ( 100 , Math . round ( ( downloadedBytes / totalBytes ) * 100 ) )
63+ setStatus ( { status : "downloading" , progress : pct } )
64+ }
65+ } else if ( event . event === "Finished" ) {
66+ setStatus ( { status : "ready" } )
67+ }
68+ } )
69+ setStatus ( { status : "ready" } )
70+ } catch ( err ) {
71+ console . error ( "Update download failed:" , err )
72+ setStatus ( { status : "error" , message : "Download failed" } )
73+ } finally {
74+ inFlightRef . current . downloading = false
75+ }
4576 }
4677 } catch ( err ) {
4778 if ( cancelled ) return
4879 console . error ( "Update check failed:" , err )
4980 }
5081 }
5182
52- void checkForUpdate ( )
83+ void checkAndDownload ( )
5384
5485 return ( ) => {
5586 cancelled = true
5687 mountedRef . current = false
5788 }
5889 } , [ setStatus ] )
5990
60- const triggerDownload = useCallback ( async ( ) => {
61- const update = updateRef . current
62- if ( ! update ) return
63- if ( statusRef . current . status !== "available" ) return
64- if ( inFlightRef . current . downloading || inFlightRef . current . installing ) return
65-
66- inFlightRef . current . downloading = true
67- setStatus ( { status : "downloading" , progress : - 1 } )
68-
69- let totalBytes : number | null = null
70- let downloadedBytes = 0
71-
72- try {
73- await update . download ( ( event ) => {
74- if ( ! mountedRef . current ) return
75- if ( event . event === "Started" ) {
76- totalBytes = event . data . contentLength ?? null
77- downloadedBytes = 0
78- setStatus ( {
79- status : "downloading" ,
80- progress : totalBytes ? 0 : - 1 ,
81- } )
82- } else if ( event . event === "Progress" ) {
83- downloadedBytes += event . data . chunkLength
84- if ( totalBytes && totalBytes > 0 ) {
85- const pct = Math . min ( 100 , Math . round ( ( downloadedBytes / totalBytes ) * 100 ) )
86- setStatus ( { status : "downloading" , progress : pct } )
87- }
88- } else if ( event . event === "Finished" ) {
89- setStatus ( { status : "ready" } )
90- }
91- } )
92- setStatus ( { status : "ready" } )
93- } catch ( err ) {
94- console . error ( "Update download failed:" , err )
95- setStatus ( { status : "error" , message : "Download failed" } )
96- } finally {
97- inFlightRef . current . downloading = false
98- }
99- } , [ setStatus ] )
100-
10191 const triggerInstall = useCallback ( async ( ) => {
10292 const update = updateRef . current
10393 if ( ! update ) return
@@ -118,5 +108,5 @@ export function useAppUpdate(): UseAppUpdateReturn {
118108 }
119109 } , [ setStatus ] )
120110
121- return { updateStatus, triggerDownload , triggerInstall }
111+ return { updateStatus, triggerInstall }
122112}
0 commit comments