-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathModal.tsx
More file actions
168 lines (149 loc) · 4.69 KB
/
Modal.tsx
File metadata and controls
168 lines (149 loc) · 4.69 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
import { Modal, Title, Content } from '@node-core/ui-components/Common/Modal';
import classNames from 'classnames';
import { useTranslations } from 'next-intl';
import type { FC } from 'react';
import VulnerabilityChip from '#site/components/EOL/VulnerabilityChips/Chip';
import LinkWithArrow from '#site/components/LinkWithArrow';
import type { ModalProps } from '#site/providers/modalProvider';
import type { NodeRelease } from '#site/types';
import type { Vulnerability } from '#site/types/vulnerabilities';
import { SEVERITY_ORDER } from './VulnerabilityChips';
type EOLModalData = {
release: NodeRelease;
vulnerabilities: Array<Vulnerability>;
};
type KnownVulnerability = Vulnerability & {
severity: (typeof SEVERITY_ORDER)[number];
};
const VulnerabilitiesTable: FC<{
vulnerabilities: Array<Vulnerability>;
maxWidth?: string;
}> = ({ vulnerabilities, maxWidth = 'max-w-2xs' }) => {
const t = useTranslations();
return (
<table className="w-full">
<thead>
<tr>
<th>{t('components.eolModal.table.cves')}</th>
<th>{t('components.eolModal.table.severity')}</th>
<th>{t('components.eolModal.table.overview')}</th>
<th>{t('components.eolModal.table.details')}</th>
</tr>
</thead>
<tbody>
{vulnerabilities.map((vuln, i) => (
<tr key={i}>
<td>
{vuln.cve.length
? vuln.cve.map(cveId => (
<div key={cveId}>
<LinkWithArrow
href={`https://www.cve.org/CVERecord?id=${cveId}`}
target="_blank"
rel="noopener noreferrer"
>
{cveId}
</LinkWithArrow>
</div>
))
: '-'}
</td>
<td>
<VulnerabilityChip severity={vuln.severity} />
</td>
<td className={classNames(maxWidth, 'truncate')}>
{vuln.description || vuln.overview || '-'}
</td>
<td>
{vuln.ref ? (
<LinkWithArrow
href={vuln.ref}
target="_blank"
rel="noopener noreferrer"
>
{t('components.eolModal.blogLinkText')}
</LinkWithArrow>
) : (
'—'
)}
</td>
</tr>
))}
</tbody>
</table>
);
};
const UnknownSeveritySection: FC<{
vulnerabilities: Array<Vulnerability>;
hasKnownVulns: boolean;
}> = ({ vulnerabilities, hasKnownVulns }) => {
const t = useTranslations();
if (!vulnerabilities.length) {
return null;
}
return (
<details open={!hasKnownVulns}>
<summary className="cursor-pointer font-semibold">
{t('components.eolModal.showUnknownSeverities')} (
{vulnerabilities.length})
</summary>
<div className="mt-4">
<VulnerabilitiesTable
vulnerabilities={vulnerabilities}
maxWidth={'max-w-3xs'}
/>
</div>
</details>
);
};
const EOLModal: FC<ModalProps> = ({ open, closeModal, data }) => {
const { release, vulnerabilities } = data as EOLModalData;
const t = useTranslations();
const modalHeading = t(
release.codename
? 'components.eolModal.title'
: 'components.eolModal.titleWithoutCodename',
{
version: release.major,
codename: release.codename ?? '',
}
);
const [knownVulns, unknownVulns] = vulnerabilities.reduce(
(acc, vuln) => {
acc[vuln.severity === 'unknown' ? 1 : 0].push(vuln as KnownVulnerability);
return acc;
},
[[], []] as [Array<KnownVulnerability>, Array<Vulnerability>]
);
knownVulns.sort(
(a, b) =>
SEVERITY_ORDER.indexOf(a.severity) - SEVERITY_ORDER.indexOf(b.severity)
);
const hasKnownVulns = knownVulns.length > 0;
const hasAnyVulns = hasKnownVulns || unknownVulns.length > 0;
return (
<Modal open={open} onOpenChange={closeModal}>
<Title>{modalHeading}</Title>
<Content>
{vulnerabilities.length > 0 && (
<p className="m-1">
{t('components.eolModal.vulnerabilitiesMessage', {
count: vulnerabilities.length,
})}
</p>
)}
{hasKnownVulns && <VulnerabilitiesTable vulnerabilities={knownVulns} />}
<UnknownSeveritySection
vulnerabilities={unknownVulns}
hasKnownVulns={hasKnownVulns}
/>
{!hasAnyVulns && (
<p className="m-1">
{t('components.eolModal.noVulnerabilitiesMessage')}
</p>
)}
</Content>
</Modal>
);
};
export default EOLModal;