Open History to see the last commit
1import type { DesktopUpdateState } from "@t3tools/contracts";
2
3import { getCanRetryAfterDownloadFailure, nextStatusAfterDownloadFailure } from "./updateState";
4
5export function createInitialDesktopUpdateState(currentVersion: string): DesktopUpdateState {
6 return {
7 enabled: false,
8 status: "disabled",
9 currentVersion,
10 availableVersion: null,
11 downloadedVersion: null,
12 downloadPercent: null,
13 checkedAt: null,
14 message: null,
15 errorContext: null,
16 canRetry: false,
17 };
18}
19
20export function reduceDesktopUpdateStateOnCheckStart(
21 state: DesktopUpdateState,
22 checkedAt: string,
23): DesktopUpdateState {
24 return {
25 ...state,
26 status: "checking",
27 checkedAt,
28 message: null,
29 downloadPercent: null,
30 errorContext: null,
31 canRetry: false,
32 };
33}
34
35export function reduceDesktopUpdateStateOnCheckFailure(
36 state: DesktopUpdateState,
37 message: string,
38 checkedAt: string,
39): DesktopUpdateState {
40 return {
41 ...state,
42 status: "error",
43 message,
44 checkedAt,
45 downloadPercent: null,
46 errorContext: "check",
47 canRetry: true,
48 };
49}
50
51export function reduceDesktopUpdateStateOnUpdateAvailable(
52 state: DesktopUpdateState,
53 version: string,
54 checkedAt: string,
55): DesktopUpdateState {
56 return {
57 ...state,
58 status: "available",
59 availableVersion: version,
60 downloadedVersion: null,
61 downloadPercent: null,
62 checkedAt,
63 message: null,
64 errorContext: null,
65 canRetry: false,
66 };
67}
68
69export function reduceDesktopUpdateStateOnNoUpdate(
70 state: DesktopUpdateState,
71 checkedAt: string,
72): DesktopUpdateState {
73 return {
74 ...state,
75 status: "up-to-date",
76 availableVersion: null,
77 downloadedVersion: null,
78 downloadPercent: null,
79 checkedAt,
80 message: null,
81 errorContext: null,
82 canRetry: false,
83 };
84}
85
86export function reduceDesktopUpdateStateOnDownloadStart(
87 state: DesktopUpdateState,
88): DesktopUpdateState {
89 return {
90 ...state,
91 status: "downloading",
92 downloadPercent: 0,
93 message: null,
94 errorContext: null,
95 canRetry: false,
96 };
97}
98
99export function reduceDesktopUpdateStateOnDownloadFailure(
100 state: DesktopUpdateState,
101 message: string,
102): DesktopUpdateState {
103 return {
104 ...state,
105 status: nextStatusAfterDownloadFailure(state),
106 message,
107 downloadPercent: null,
108 errorContext: "download",
109 canRetry: getCanRetryAfterDownloadFailure(state),
110 };
111}
112
113export function reduceDesktopUpdateStateOnDownloadProgress(
114 state: DesktopUpdateState,
115 percent: number,
116): DesktopUpdateState {
117 return {
118 ...state,
119 status: "downloading",
120 downloadPercent: percent,
121 message: null,
122 errorContext: null,
123 canRetry: false,
124 };
125}
126
127export function reduceDesktopUpdateStateOnDownloadComplete(
128 state: DesktopUpdateState,
129 version: string,
130): DesktopUpdateState {
131 return {
132 ...state,
133 status: "downloaded",
134 availableVersion: version,
135 downloadedVersion: version,
136 downloadPercent: 100,
137 message: null,
138 errorContext: null,
139 canRetry: true,
140 };
141}
142
143export function reduceDesktopUpdateStateOnInstallFailure(
144 state: DesktopUpdateState,
145 message: string,
146): DesktopUpdateState {
147 return {
148 ...state,
149 status: "downloaded",
150 message,
151 errorContext: "install",
152 canRetry: true,
153 };
154}
155