import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/contracts"; export type DesktopUpdateButtonAction = "download" | "install" | "none"; export function resolveDesktopUpdateButtonAction( state: DesktopUpdateState, ): DesktopUpdateButtonAction { if (state.status === "available") { return "download"; } if (state.status === "downloaded") { return "install"; } if (state.status === "error") { if (state.errorContext === "install" && state.downloadedVersion) { return "install"; } if (state.errorContext === "download" && state.availableVersion) { return "download"; } } return "none"; } export function shouldShowDesktopUpdateButton(state: DesktopUpdateState | null): boolean { if (!state || !state.enabled) { return false; } if (state.status === "downloading") { return true; } return resolveDesktopUpdateButtonAction(state) !== "none"; } export function isDesktopUpdateButtonDisabled(state: DesktopUpdateState | null): boolean { return state?.status === "downloading"; } export function getDesktopUpdateButtonTooltip(state: DesktopUpdateState): string { if (state.status === "available") { return `Update ${state.availableVersion ?? "available"} ready to download`; } if (state.status === "downloading") { const progress = typeof state.downloadPercent === "number" ? ` (${Math.floor(state.downloadPercent)}%)` : ""; return `Downloading update${progress}`; } if (state.status === "downloaded") { return `Update ${state.downloadedVersion ?? state.availableVersion ?? "ready"} downloaded. Click to restart and install.`; } if (state.status === "error") { if (state.errorContext === "download" && state.availableVersion) { return `Download failed for ${state.availableVersion}. Click to retry.`; } if (state.errorContext === "install" && state.downloadedVersion) { return `Install failed for ${state.downloadedVersion}. Click to retry.`; } return state.message ?? "Update failed"; } return "Update available"; } export function getDesktopUpdateActionError(result: DesktopUpdateActionResult): string | null { if (!result.accepted || result.completed) return null; if (typeof result.state.message !== "string") return null; const message = result.state.message.trim(); return message.length > 0 ? message : null; } export function shouldToastDesktopUpdateActionResult(result: DesktopUpdateActionResult): boolean { return result.accepted && !result.completed; } export function shouldHighlightDesktopUpdateError(state: DesktopUpdateState | null): boolean { if (!state || state.status !== "error") return false; return state.errorContext === "download" || state.errorContext === "install"; }