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
187import { ReactNode, useState } from "react";
import styles from "./AuditHeader.module.scss";
import { Link, useNavigate } from "react-router-dom";
import { StyledButton } from "./StyledButton";
import { FaPen, FaTrash, FaClipboard } from "react-icons/fa";
import { GrPowerCycle } from "react-icons/gr";
import { createLog } from "#src/utils/createLog.ts";
import { useGlobalStore } from "../utils";
import * as API from "aws-amplify/api";
import { QueryClient, useQueryClient } from "@tanstack/react-query";
import { SkeletonAuditHeader } from "./Skeleton";
interface AuditHeaderProps extends React.PropsWithChildren {
isShared: boolean;
queryClient: QueryClient;
audit: any;
auditId: string | undefined;
scans?: any[];
}
export const AuditHeader = ({
isShared,
queryClient,
audit,
auditId,
scans,
}: AuditHeaderProps) => {
const navigate = useNavigate();
const { setAnnounceMessage } = useGlobalStore();
const [isScanning, setIsScanning] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [isRenaming, setIsRenaming] = useState(false);
const copyCurrentLocationToClipboard = async () => {
try {
await navigator.clipboard.writeText(
window.location.origin +
location.pathname.replace("/audits/", "/shared/")
);
console.log(
`URL ${window.location.origin + location.pathname} copied to clipboard!`
);
setAnnounceMessage(
`URL ${window.location.origin + location.pathname} copied to clipboard!`, "success"
);
} catch (err) {
console.error("Failed to copy URLs: ", err);
}
};
const deleteAudit = async () => {
if (confirm(`Are you sure you want to delete this audit?`)) {
setIsDeleting(true);
try {
const response = await (
await API.post({
apiName: "auth",
path: "/deleteAudit",
options: { body: { id: auditId! } },
}).response
).body.json();
//console.log(response);
await queryClient.refetchQueries({ queryKey: ["audits"] });
// aria & logging
setAnnounceMessage(`Deleted audit ${audit.name}.`, "success");
await createLog(`Deleted audit ${audit.name}.`, auditId);
navigate("/audits");
} finally {
setIsDeleting(false);
}
return;
}
};
const rescanAudit = async () => {
if (confirm(`Are you sure you want to re-scan this audit?`)) {
setIsScanning(true);
try {
const response = await (
await API.post({
apiName: "auth",
path: "/rescanAudit",
options: { body: { id: auditId! } },
}).response
).body.json();
//console.log(response);
await queryClient.refetchQueries({ queryKey: ["audits"] });
// aria & logging
setAnnounceMessage(`Scanning audit ${audit.name}...`);
} finally {
setIsScanning(false);
}
return;
}
};
// Check if there's an active scan (not complete or failed)
const hasActiveScan = scans && scans.length > 0 &&
scans[scans.length - 1].status !== "complete" &&
scans[scans.length - 1].status !== "failed";
const renameAudit = async () => {
const newName = prompt(
`What would you like to rename this audit to?`,
audit?.name
);
if (newName) {
setIsRenaming(true);
try {
const response = await (
await API.post({
apiName: "auth",
path: "/updateAudit",
options: { body: { id: auditId!, name: newName } },
}).response
).body.json();
//console.log(response);
await queryClient.refetchQueries({ queryKey: ["audit", auditId] });
// aria & logging
setAnnounceMessage(`Audit ${audit.name} renamed to ${newName}`, "success");
} finally {
setIsRenaming(false);
}
return;
}
};
// Show skeleton while audit is loading
if (!audit) {
return <SkeletonAuditHeader />;
}
return (
<div className={styles.AuditHeader}>
<div className={styles["inner"]}>
<div className={styles["header-l"]}>
<h1 className="initial-focus-element">
<span className={styles["audit-name-label"]}>Audit</span> {audit?.name}
</h1>
{!isShared && (
<div className={styles["buttons-l"]}>
<StyledButton
onClick={renameAudit}
label="Rename Audit"
icon={<FaPen />}
showLabel={false}
loading={isRenaming}
disabled={isRenaming || isDeleting}
/>
<StyledButton
onClick={deleteAudit}
label="Delete Audit"
icon={<FaTrash />}
showLabel={false}
loading={isDeleting}
disabled={isRenaming || isDeleting}
/>
</div>
)}
</div>
<div className={styles["buttons-r"]}>
{!isShared && (
<StyledButton
onClick={rescanAudit}
label={hasActiveScan ? "Scanning..." : "Scan Now"}
icon={<GrPowerCycle />}
variant="dark"
loading={isScanning || hasActiveScan}
loadingText="Scanning..."
disabled={hasActiveScan}
className="audit-main-button"
//title={hasActiveScan ? "A scan is already in progress" : undefined}
/>
)}
<StyledButton
onClick={copyCurrentLocationToClipboard}
label="Share"
className="audit-main-button"
icon={<FaClipboard />}
/>
</div>
</div>
</div>
);
};