Skip to content

Commit 55e30f4

Browse files
committed
refactor: streamline update handling by removing download trigger
- Removed the triggerDownload function and its associated logic from the useAppUpdate hook. - Updated the App and PanelFooter components to reflect the changes, simplifying the update process. - The update now directly transitions to the downloading state without an intermediate available state.
1 parent aef1def commit 55e30f4

File tree

3 files changed

+44
-70
lines changed

3 files changed

+44
-70
lines changed

src/App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function App() {
5555
const [maxPanelHeightPx, setMaxPanelHeightPx] = useState<number | null>(null)
5656
const maxPanelHeightPxRef = useRef<number | null>(null)
5757

58-
const { updateStatus, triggerDownload, triggerInstall } = useAppUpdate()
58+
const { updateStatus, triggerInstall } = useAppUpdate()
5959

6060
// Tick state to force re-evaluation of cooldown status
6161
const [cooldownTick, setCooldownTick] = useState(0)
@@ -565,7 +565,6 @@ function App() {
565565
onRefresh={handleRefresh}
566566
refreshDisabled={!canRefreshAll}
567567
updateStatus={updateStatus}
568-
onUpdateDownload={triggerDownload}
569568
onUpdateInstall={triggerInstall}
570569
/>
571570
</div>

src/components/panel-footer.tsx

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,36 @@ interface PanelFooterProps {
77
onRefresh: () => void;
88
refreshDisabled?: boolean;
99
updateStatus: UpdateStatus;
10-
onUpdateDownload: () => void;
1110
onUpdateInstall: () => void;
1211
}
1312

1413
function VersionDisplay({
1514
version,
1615
updateStatus,
17-
onUpdateDownload,
1816
onUpdateInstall,
1917
}: {
2018
version: string;
2119
updateStatus: UpdateStatus;
22-
onUpdateDownload: () => void;
2320
onUpdateInstall: () => void;
2421
}) {
2522
switch (updateStatus.status) {
26-
case "available":
27-
return (
28-
<button
29-
type="button"
30-
onClick={onUpdateDownload}
31-
className="text-xs text-primary hover:underline underline-offset-4 bg-transparent border-none p-0 cursor-pointer"
32-
>
33-
v{updateStatus.version} available
34-
</button>
35-
);
3623
case "downloading":
3724
return (
3825
<span className="text-xs text-muted-foreground">
3926
{updateStatus.progress >= 0
40-
? `Downloading... ${updateStatus.progress}%`
41-
: "Downloading..."}
27+
? `Downloading update ${updateStatus.progress}%`
28+
: "Downloading update..."}
4229
</span>
4330
);
4431
case "ready":
4532
return (
46-
<button
47-
type="button"
33+
<Button
34+
variant="destructive"
35+
size="xs"
4836
onClick={onUpdateInstall}
49-
className="text-xs text-primary hover:underline underline-offset-4 bg-transparent border-none p-0 cursor-pointer"
5037
>
5138
Restart to update
52-
</button>
39+
</Button>
5340
);
5441
case "installing":
5542
return (
@@ -75,15 +62,13 @@ export function PanelFooter({
7562
onRefresh,
7663
refreshDisabled,
7764
updateStatus,
78-
onUpdateDownload,
7965
onUpdateInstall,
8066
}: PanelFooterProps) {
8167
return (
8268
<div className="flex justify-between items-center pt-1.5 border-t">
8369
<VersionDisplay
8470
version={version}
8571
updateStatus={updateStatus}
86-
onUpdateDownload={onUpdateDownload}
8772
onUpdateInstall={onUpdateInstall}
8873
/>
8974
{refreshDisabled ? (

src/hooks/use-app-update.ts

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import { relaunch } from "@tauri-apps/plugin-process"
55

66
export 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

1413
interface 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

Comments
 (0)