-
Notifications
You must be signed in to change notification settings - Fork 931
Expand file tree
/
Copy pathSoftwareScriptDetailsModal.tsx
More file actions
342 lines (307 loc) · 8.65 KB
/
Copy pathSoftwareScriptDetailsModal.tsx
File metadata and controls
342 lines (307 loc) · 8.65 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/** This component is intentionally separate from SoftwareInstallDetailsModal
* because it handles script-only package installs (e.g. sh_packages, ps1_packages, or py_packages)
*
* Key differences from SoftwareInstallDetailsModal:
* - Uses Script/Run/Rerun language in UI instead of Install/Retry.
* - Omits current versions section (no InventoryVersions display).
* - Omits post-install script output.
*
* Keeping these components and its tests separate improves maintainability and clarity
*/
import React, { useState } from "react";
import { useQuery } from "react-query";
import { formatDistanceToNow } from "date-fns";
import { AxiosError } from "axios";
import { DEFAULT_USE_QUERY_OPTIONS } from "utilities/constants";
import {
IHostSoftware,
ISoftwareScriptResult,
ISoftwareInstallResults,
} from "interfaces/software";
import softwareAPI from "services/entities/software";
import deviceUserAPI from "services/entities/device_user";
import Modal from "components/Modal";
import ModalFooter from "components/ModalFooter";
import Button from "components/buttons/Button";
import IconStatusMessage from "components/IconStatusMessage";
import Textarea from "components/Textarea";
import DataError from "components/DataError/DataError";
import DeviceUserError from "components/DeviceUserError";
import Spinner from "components/Spinner/Spinner";
import RevealButton from "components/buttons/RevealButton";
import CustomLink from "components/CustomLink";
import {
SCRIPT_DETAILS_STATUS_ICONS,
getScriptDetailsStatusPredicate,
} from "../constants";
const baseClass = "software-script-details-modal";
export type IPackageInstallDetails = {
host_display_name?: string;
install_uuid?: string; // not actually optional
};
export const renderContactOption = (url?: string) => (
<>
{" "}
or{" "}
{url ? (
<CustomLink url={url} text="contact your IT admin" newTab />
) : (
"contact your IT admin"
)}
</>
);
interface IInstallStatusMessage {
installResult: ISoftwareScriptResult;
isMyDevicePage: boolean;
contactUrl?: string;
}
export const StatusMessage = ({
installResult,
isMyDevicePage,
contactUrl,
}: IInstallStatusMessage) => {
const {
host_display_name,
software_package,
software_title,
status,
updated_at,
created_at,
} = installResult;
const formattedHost = host_display_name ? (
<b>{host_display_name}</b>
) : (
"the host"
);
const displayTimeStamp = ["failed_install", "installed"].includes(
status || ""
)
? ` (${formatDistanceToNow(new Date(updated_at || created_at), {
includeSeconds: true,
addSuffix: true,
})})`
: "";
const renderStatusCopy = () => {
const prefix = (
<>
Fleet {getScriptDetailsStatusPredicate(status)} <b>{software_title}</b>
</>
);
const middle = isMyDevicePage ? (
<>
{" "}
{displayTimeStamp}
{status === "failed_install" && (
<>. You can rerun{renderContactOption(contactUrl)}</>
)}
</>
) : (
<>
{" "}
({software_package}) on {formattedHost}
{status === "pending_install"
? " when it comes online"
: displayTimeStamp}
</>
);
return (
<span>
{prefix}
{middle}
{"."}
</span>
);
};
return (
<IconStatusMessage
className={`${baseClass}__status-message`}
iconName={
SCRIPT_DETAILS_STATUS_ICONS[status || "pending_install"] ??
"pending-outline"
}
message={renderStatusCopy()}
/>
);
};
interface IModalButtonsProps {
deviceAuthToken?: string;
installResultStatus?: string;
hostSoftwareId?: number;
onRerun?: (id: number, isScriptPackage: boolean) => void;
onCancel: () => void;
}
export const ModalButtons = ({
deviceAuthToken,
installResultStatus,
hostSoftwareId,
onRerun,
onCancel,
}: IModalButtonsProps) => {
if (!!deviceAuthToken && installResultStatus === "failed_install") {
const onClickRerun = () => {
// on My Device Page, where this is relevant, both will be defined
if (onRerun && hostSoftwareId) {
onRerun(hostSoftwareId, true); // isScriptPackage defined for copy changes
}
onCancel();
};
return (
<ModalFooter
primaryButtons={
<>
<Button variant="inverse" onClick={onCancel}>
Cancel
</Button>
<Button type="submit" onClick={onClickRerun}>
Rerun
</Button>
</>
}
/>
);
}
return (
<ModalFooter primaryButtons={<Button onClick={onCancel}>Close</Button>} />
);
};
interface ISoftwareInstallDetailsProps {
details: IPackageInstallDetails;
hostSoftware?: IHostSoftware; // for software name when not Fleet installed (not present on activity feeds)
deviceAuthToken?: string; // My Device Page only
onCancel: () => void;
onRerun?: (id: number, isScriptPackage?: boolean) => void; // My Device Page only
contactUrl?: string; // My Device Page only
}
export const SoftwareScriptDetailsModal = ({
details: detailsFromProps,
onCancel,
hostSoftware,
deviceAuthToken,
onRerun,
contactUrl,
}: ISoftwareInstallDetailsProps) => {
// will always be present
const installUUID = detailsFromProps.install_uuid ?? "";
const [showInstallDetails, setShowInstallDetails] = useState(false);
const toggleInstallDetails = () => {
setShowInstallDetails((prev) => !prev);
};
const { data: swInstallResult, isLoading, isError, error } = useQuery<
ISoftwareInstallResults,
AxiosError,
ISoftwareScriptResult
>(
["softwareInstallResults", installUUID],
() => {
return deviceAuthToken
? deviceUserAPI.getSoftwareInstallResult(deviceAuthToken, installUUID)
: softwareAPI.getSoftwareInstallResult(installUUID);
},
{
...DEFAULT_USE_QUERY_OPTIONS,
staleTime: 3000,
select: (data) => data.results as ISoftwareScriptResult,
}
);
const renderScriptDetailsSection = () => {
// Only show details button if there's details to display
const showDetailsButton =
swInstallResult?.status !== "pending_install" && swInstallResult?.output;
return (
<>
{showDetailsButton && (
<RevealButton
isShowing={showInstallDetails}
showText="Details"
hideText="Details"
caretPosition="after"
onClick={toggleInstallDetails}
/>
)}
{showInstallDetails && swInstallResult?.output && (
<Textarea label="Script output:" variant="code">
{swInstallResult.output}
</Textarea>
)}
</>
);
};
const hostDisplayname =
swInstallResult?.host_display_name || detailsFromProps.host_display_name;
const installResultWithHostDisplayName = swInstallResult
? {
...swInstallResult,
host_display_name: hostDisplayname,
}
: undefined;
const renderContent = () => {
if (isLoading) {
return <Spinner />;
}
if (isError) {
if (error?.status === 404) {
return deviceAuthToken ? (
<DeviceUserError />
) : (
<DataError
description="Couldn't get script details"
excludeIssueLink
/>
);
}
if (error?.status === 401) {
return deviceAuthToken ? (
<DeviceUserError />
) : (
<DataError description="Close this modal and try again." />
);
}
}
if (!installResultWithHostDisplayName) {
return deviceAuthToken ? (
<DeviceUserError />
) : (
<DataError description="No data returned." />
);
}
if (
!["installed", "pending_install", "failed_install"].includes(
installResultWithHostDisplayName.status
)
) {
return (
<DataError
description={`Unexpected software install status ${installResultWithHostDisplayName.status}`}
/>
);
}
return (
<div className={`${baseClass}__modal-content`}>
<StatusMessage
installResult={installResultWithHostDisplayName}
isMyDevicePage={!!deviceAuthToken}
contactUrl={contactUrl}
/>
{renderScriptDetailsSection()}
</div>
);
};
return (
<Modal
title="Script details"
onExit={onCancel}
onEnter={onCancel}
className={baseClass}
>
{renderContent()}
<ModalButtons
deviceAuthToken={deviceAuthToken}
installResultStatus={swInstallResult?.status}
hostSoftwareId={hostSoftware?.id}
onRerun={onRerun}
onCancel={onCancel}
/>
</Modal>
);
};
export default SoftwareScriptDetailsModal;