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