-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathuseFlashJobStatus.ts
More file actions
151 lines (142 loc) · 3.48 KB
/
Copy pathuseFlashJobStatus.ts
File metadata and controls
151 lines (142 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { useQuery } from "@apollo/client";
import gql from "gql";
import { exception } from "react-ga";
import { useEffect } from "react";
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default (jobId?: string) => {
const { data, subscribeToMore, error, loading } = useQuery(
gql(`
query FlashJobStatus($jobId: ID!) {
flashJobStatus(jobId: $jobId) {
id
cancelled
meta {
firmware {
target
version
}
deviceId
}
stages {
connect {
...FlashJobStageData
}
build {
...FlashJobStageData
}
download {
...FlashJobStageData
}
erase {
...FlashJobStageData
}
flash {
...FlashJobStageData
}
}
}
}
fragment FlashJobStageData on FlashStage {
started
completed
progress
error
status {
jobStatus
startedAt
}
}
`),
{
variables: {
jobId: jobId ?? "",
},
skip: !jobId,
fetchPolicy: "cache-and-network",
}
);
useEffect(() => {
if (jobId) {
const unsub = subscribeToMore({
document: gql(`
subscription FlashJobUpdates($jobId: ID!) {
flashJobStatusUpdates(jobId: $jobId) {
id
cancelled
stages {
connect {
...FlashJobStageData
}
build {
...FlashJobStageData
}
download {
...FlashJobStageData
}
erase {
...FlashJobStageData
}
flash {
...FlashJobStageData
}
}
}
}
fragment FlashJobStageData on FlashStage {
started
completed
progress
error
status {
jobStatus
startedAt
}
}
`),
variables: {
jobId,
},
onError: (subscriptionError) => {
exception({
description: `Could not subscribe to flash job: ${subscriptionError.message}`,
fatal: true,
});
},
updateQuery: (existing, { subscriptionData }) => ({
flashJobStatus: {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
...existing.flashJobStatus!,
...subscriptionData.data.flashJobStatusUpdates,
},
}),
});
return unsub;
}
return undefined;
}, [jobId, subscribeToMore]);
const jobCancelled = !!data?.flashJobStatus?.cancelled;
const jobExists = !!data?.flashJobStatus;
const jobCompleted = !!data?.flashJobStatus?.stages.flash.completed;
const jobError = (
Object.values(data?.flashJobStatus?.stages ?? {}).find(
(stage) => stage && typeof stage !== "string" && stage.error
) as { error: string } | undefined
)?.error;
useEffect(() => {
if (jobError) {
exception({
description: `Error during flash: ${jobError}`,
fatal: true,
});
}
}, [jobError]);
return {
data,
error,
loading,
jobCancelled,
jobExists,
jobCompleted,
jobError,
};
};