-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
212 lines (199 loc) · 6.23 KB
/
index.tsx
File metadata and controls
212 lines (199 loc) · 6.23 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
/*
* SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and Juno contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useState, useRef, useEffect } from "react"
import {
Modal,
ModalFooter,
Button,
Stack,
Textarea,
TextInput,
DateTimePicker,
Message,
} from "@cloudoperators/juno-ui-components"
import { RemediationInput, RemediationTypeValues, SeverityValues } from "../../../../generated/graphql"
type FalsePositiveModalProps = {
open: boolean
onClose: () => void
onConfirm: (input: RemediationInput) => Promise<{ error: string } | void>
vulnerability: string
severity?: string
service: string
image: string
/** User ID from auth (provided by parent under AuthProvider). When set, User ID field is read-only. */
authUserId?: string | null
/** Error message to show when createRemediation fails. */
errorMessage?: string | null
/** Called when submit fails so the parent can set errorMessage. */
onSetError?: (message: string | null) => void
}
const CONFIRM_LABEL = "Mark as False Positive"
const CANCEL_LABEL = "Cancel"
const toSeverityValue = (severity: string): SeverityValues | undefined => {
if (!severity) return undefined
const normalized = severity.charAt(0).toUpperCase() + severity.slice(1).toLowerCase()
const value = normalized as SeverityValues
return Object.values(SeverityValues).includes(value) ? value : undefined
}
export const FalsePositiveModal: React.FC<FalsePositiveModalProps> = ({
open,
onClose,
onConfirm,
vulnerability,
severity,
service,
image,
authUserId = null,
errorMessage,
onSetError,
}) => {
const [description, setDescription] = useState<string>("")
const [manualUserId, setManualUserId] = useState<string>("")
const [expirationDate, setExpirationDate] = useState<Date | null>(null)
const [isSubmitting, setIsSubmitting] = useState(false)
const [descriptionError, setDescriptionError] = useState<string>("")
const [userIdError, setUserIdError] = useState<string>("")
const isMountedRef = useRef(true)
const manualUserIdTrimmed = manualUserId.trim()
const remediatedBy = authUserId ?? (manualUserIdTrimmed || undefined)
const isUserIdValid = !!remediatedBy
useEffect(() => {
return () => {
isMountedRef.current = false
}
}, [])
const descriptionTrimmed = description.trim()
const handleConfirm = async () => {
if (!descriptionTrimmed) {
setDescriptionError("Description is required")
return
}
if (!remediatedBy) {
setUserIdError("User ID is required")
return
}
setDescriptionError("")
setUserIdError("")
setIsSubmitting(true)
try {
const input: RemediationInput = {
type: RemediationTypeValues.FalsePositive,
vulnerability,
service,
image,
description: descriptionTrimmed,
...(remediatedBy && { remediatedBy }),
...(severity && { severity: toSeverityValue(severity) }),
...(expirationDate && { expirationDate: expirationDate.toISOString() }),
}
const result = await onConfirm(input)
if (isMountedRef.current) {
if (result?.error) {
onSetError?.(result.error)
} else {
setDescription("")
setManualUserId("")
setExpirationDate(null)
onClose()
}
}
} catch (error) {
const message = error instanceof Error ? error.message : "Failed to create remediation"
if (isMountedRef.current) {
onSetError?.(message)
}
} finally {
if (isMountedRef.current) {
setIsSubmitting(false)
}
}
}
const handleClose = () => {
setDescription("")
setManualUserId("")
setExpirationDate(null)
setDescriptionError("")
setUserIdError("")
onSetError?.(null)
onClose()
}
const handleDescriptionChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setDescription(e.target.value)
// Clear error when user starts typing
if (descriptionError) {
setDescriptionError("")
}
}
return (
<Modal
title="Mark as False Positive"
open={open}
onCancel={handleClose}
modalFooter={
<ModalFooter>
<Stack direction="horizontal" gap="2" distribution="end" className="w-full">
<Button onClick={handleClose} label={CANCEL_LABEL} disabled={isSubmitting} />
<Button
onClick={handleConfirm}
label={CONFIRM_LABEL}
variant="primary"
disabled={isSubmitting || !descriptionTrimmed || !isUserIdValid}
/>
</Stack>
</ModalFooter>
}
>
<Stack gap="4" direction="vertical">
{errorMessage && <Message text={errorMessage} variant="error" />}
<div>
<strong>Vulnerability:</strong> {vulnerability}
</div>
<div>
<strong>Service:</strong> {service}
</div>
<div>
<strong>Image:</strong> {image}
</div>
<div>
<TextInput
label="User ID"
value={authUserId ?? manualUserId}
onChange={(e) => {
setManualUserId(e.target.value)
if (userIdError) setUserIdError("")
}}
disabled={!!authUserId}
required
invalid={!!userIdError}
errortext={userIdError}
placeholder={authUserId ? undefined : "Enter your user ID"}
helptext={authUserId ? "User ID from current session (read-only)." : "Enter your user ID."}
/>
</div>
<div>
<DateTimePicker
label="Expiration Date"
value={expirationDate ?? undefined}
onChange={(dates) => setExpirationDate(dates?.[0] ?? null)}
minDate="today"
helptext="Optional. When this false positive should no longer be considered valid."
/>
</div>
<div>
<Textarea
label="Description"
placeholder="Add a description explaining why this is a false positive..."
value={description}
onChange={handleDescriptionChange}
rows={14}
required
invalid={!!descriptionError}
errortext={descriptionError || ""}
/>
</div>
</Stack>
</Modal>
)
}