/
ErrorsViewer.tsx
143 lines (132 loc) · 3.4 KB
/
ErrorsViewer.tsx
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
import React, { useEffect, useState } from 'react';
import type Monaco from 'monaco-editor';
import clsx from 'clsx';
import type { ErrorItem, ErrorGroup } from './types';
import IconExternalLink from '@theme/IconExternalLink';
import styles from './ErrorsViewer.module.css';
export interface ErrorsViewerProps {
readonly value?: ErrorGroup[] | Error;
}
export interface ErrorBlockProps {
readonly item: ErrorItem;
readonly setIsLocked: (value: boolean) => void;
readonly isLocked: boolean;
}
export interface FixButtonProps {
readonly fix: () => void;
readonly setIsLocked: (value: boolean) => void;
readonly disabled: boolean;
}
function severityClass(severity: Monaco.MarkerSeverity): string {
switch (severity) {
case 8:
return 'danger';
case 4:
return 'warning';
case 2:
return 'note';
}
return 'info';
}
function FixButton(props: FixButtonProps): JSX.Element {
return (
<button
className="button button--primary button--sm"
disabled={props.disabled}
onClick={(): void => {
props.fix();
props.setIsLocked(true);
}}
>
fix
</button>
);
}
function ErrorBlock({
item,
setIsLocked,
isLocked,
}: ErrorBlockProps): JSX.Element {
return (
<div className={`admonition alert alert--${severityClass(item.severity)}`}>
<div className="admonition-content">
<div className={clsx(!!item.fixer && styles.fixerContainer)}>
<div>
{item.message} {item.location}
</div>
{item.fixer && (
<FixButton
disabled={isLocked}
fix={item.fixer.fix}
setIsLocked={setIsLocked}
/>
)}
</div>
{item.suggestions.length > 0 && (
<div>
{item.suggestions.map((fixer, index) => (
<div
key={index}
className={clsx(styles.fixerContainer, styles.fixer)}
>
<span>> {fixer.message}</span>
<FixButton
disabled={isLocked}
fix={fixer.fix}
setIsLocked={setIsLocked}
/>
</div>
))}
</div>
)}
</div>
</div>
);
}
export default function ErrorsViewer({
value,
}: ErrorsViewerProps): JSX.Element {
const [isLocked, setIsLocked] = useState(false);
useEffect(() => {
setIsLocked(false);
}, [value]);
if (value && !Array.isArray(value)) {
return (
<div className={styles.list}>
<div className="margin-top--sm">
<h4>ESLint internal error</h4>
{value?.stack}
</div>
</div>
);
}
return (
<div className={styles.list}>
{value?.map(({ group, uri, items }) => {
return (
<div className="margin-top--sm" key={group}>
<h4>
{group}
{uri && (
<>
{' - '}
<a href={uri} target="_blank">
docs <IconExternalLink width={13.5} height={13.5} />
</a>
</>
)}
</h4>
{items.map((item, index) => (
<ErrorBlock
isLocked={isLocked}
setIsLocked={setIsLocked}
item={item}
key={index}
/>
))}
</div>
);
})}
</div>
);
}