Open History to see the last commit
1import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/contracts";
2
3export type DesktopUpdateButtonAction = "download" | "install" | "none";
4
5export function resolveDesktopUpdateButtonAction(
6 state: DesktopUpdateState,
7): DesktopUpdateButtonAction {
8 if (state.status === "available") {
9 return "download";
10 }
11 if (state.status === "downloaded") {
12 return "install";
13 }
14 if (state.status === "error") {
15 if (state.errorContext === "install" && state.downloadedVersion) {
16 return "install";
17 }
18 if (state.errorContext === "download" && state.availableVersion) {
19 return "download";
20 }
21 }
22 return "none";
23}
24
25export function shouldShowDesktopUpdateButton(state: DesktopUpdateState | null): boolean {
26 if (!state || !state.enabled) {
27 return false;
28 }
29 if (state.status === "downloading") {
30 return true;
31 }
32 return resolveDesktopUpdateButtonAction(state) !== "none";
33}
34
35export function isDesktopUpdateButtonDisabled(state: DesktopUpdateState | null): boolean {
36 return state?.status === "downloading";
37}
38
39export function getDesktopUpdateButtonTooltip(state: DesktopUpdateState): string {
40 if (state.status === "available") {
41 return `Update ${state.availableVersion ?? "available"} ready to download`;
42 }
43 if (state.status === "downloading") {
44 const progress =
45 typeof state.downloadPercent === "number" ? ` (${Math.floor(state.downloadPercent)}%)` : "";
46 return `Downloading update${progress}`;
47 }
48 if (state.status === "downloaded") {
49 return `Update ${state.downloadedVersion ?? state.availableVersion ?? "ready"} downloaded. Click to restart and install.`;
50 }
51 if (state.status === "error") {
52 if (state.errorContext === "download" && state.availableVersion) {
53 return `Download failed for ${state.availableVersion}. Click to retry.`;
54 }
55 if (state.errorContext === "install" && state.downloadedVersion) {
56 return `Install failed for ${state.downloadedVersion}. Click to retry.`;
57 }
58 return state.message ?? "Update failed";
59 }
60 return "Update available";
61}
62
63export function getDesktopUpdateActionError(result: DesktopUpdateActionResult): string | null {
64 if (!result.accepted || result.completed) return null;
65 if (typeof result.state.message !== "string") return null;
66 const message = result.state.message.trim();
67 return message.length > 0 ? message : null;
68}
69
70export function shouldToastDesktopUpdateActionResult(result: DesktopUpdateActionResult): boolean {
71 return result.accepted && !result.completed;
72}
73
74export function shouldHighlightDesktopUpdateError(state: DesktopUpdateState | null): boolean {
75 if (!state || state.status !== "error") return false;
76 return state.errorContext === "download" || state.errorContext === "install";
77}