import type { DesktopUpdateState } from "@t3tools/contracts"; import { getCanRetryAfterDownloadFailure, nextStatusAfterDownloadFailure } from "./updateState"; export function createInitialDesktopUpdateState(currentVersion: string): DesktopUpdateState { return { enabled: false, status: "disabled", currentVersion, availableVersion: null, downloadedVersion: null, downloadPercent: null, checkedAt: null, message: null, errorContext: null, canRetry: false, }; } export function reduceDesktopUpdateStateOnCheckStart( state: DesktopUpdateState, checkedAt: string, ): DesktopUpdateState { return { ...state, status: "checking", checkedAt, message: null, downloadPercent: null, errorContext: null, canRetry: false, }; } export function reduceDesktopUpdateStateOnCheckFailure( state: DesktopUpdateState, message: string, checkedAt: string, ): DesktopUpdateState { return { ...state, status: "error", message, checkedAt, downloadPercent: null, errorContext: "check", canRetry: true, }; } export function reduceDesktopUpdateStateOnUpdateAvailable( state: DesktopUpdateState, version: string, checkedAt: string, ): DesktopUpdateState { return { ...state, status: "available", availableVersion: version, downloadedVersion: null, downloadPercent: null, checkedAt, message: null, errorContext: null, canRetry: false, }; } export function reduceDesktopUpdateStateOnNoUpdate( state: DesktopUpdateState, checkedAt: string, ): DesktopUpdateState { return { ...state, status: "up-to-date", availableVersion: null, downloadedVersion: null, downloadPercent: null, checkedAt, message: null, errorContext: null, canRetry: false, }; } export function reduceDesktopUpdateStateOnDownloadStart( state: DesktopUpdateState, ): DesktopUpdateState { return { ...state, status: "downloading", downloadPercent: 0, message: null, errorContext: null, canRetry: false, }; } export function reduceDesktopUpdateStateOnDownloadFailure( state: DesktopUpdateState, message: string, ): DesktopUpdateState { return { ...state, status: nextStatusAfterDownloadFailure(state), message, downloadPercent: null, errorContext: "download", canRetry: getCanRetryAfterDownloadFailure(state), }; } export function reduceDesktopUpdateStateOnDownloadProgress( state: DesktopUpdateState, percent: number, ): DesktopUpdateState { return { ...state, status: "downloading", downloadPercent: percent, message: null, errorContext: null, canRetry: false, }; } export function reduceDesktopUpdateStateOnDownloadComplete( state: DesktopUpdateState, version: string, ): DesktopUpdateState { return { ...state, status: "downloaded", availableVersion: version, downloadedVersion: version, downloadPercent: 100, message: null, errorContext: null, canRetry: true, }; } export function reduceDesktopUpdateStateOnInstallFailure( state: DesktopUpdateState, message: string, ): DesktopUpdateState { return { ...state, status: "downloaded", message, errorContext: "install", canRetry: true, }; }