Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(website): error viewer in playground (#5061)
* chore(website): add POC for error viewer in playground * chore(website): correct colors * chore(website): correct issue with optional fixers * chore(website): expose fixers to errors-viewer * chore(website): add simple locking system for fixers * chore(website): change label apply to fix * fix: correct small issue with compilerOptions not updating in eslint * fix: apply changes requested in code review * fix: apply changes from code review * fix: add useMemo to cache model * Update packages/website/src/components/ErrorsViewer.tsx Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com> Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>
- Loading branch information
1 parent
3cd54b7
commit eaa5d7b
Showing
11 changed files
with
238 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.list { | ||
font-family: var(--ifm-font-family-monospace); | ||
background: transparent; | ||
border: none; | ||
padding-left: 1.5rem; | ||
padding-right: 1.5rem; | ||
font-size: 13px; | ||
line-height: 18px; | ||
letter-spacing: 0; | ||
font-feature-settings: 'liga' 0, 'calt' 0; | ||
box-sizing: border-box; | ||
white-space: break-spaces; | ||
margin: 0; | ||
} | ||
|
||
.fixer { | ||
margin: 0.5rem 1.5rem; | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import type Monaco from 'monaco-editor'; | ||
import type { ErrorItem } from './types'; | ||
|
||
import styles from './ErrorsViewer.module.css'; | ||
|
||
export interface ErrorsViewerProps { | ||
readonly value?: ErrorItem[]; | ||
} | ||
|
||
export interface ErrorBlockProps { | ||
readonly item: ErrorItem; | ||
readonly setIsLocked: (value: boolean) => void; | ||
readonly isLocked: boolean; | ||
} | ||
|
||
function severityClass(severity: Monaco.MarkerSeverity): string { | ||
switch (severity) { | ||
case 8: | ||
return 'danger'; | ||
case 4: | ||
return 'caution'; | ||
case 2: | ||
return 'note'; | ||
} | ||
return 'info'; | ||
} | ||
|
||
function groupErrorItems(items: ErrorItem[]): [string, ErrorItem[]][] { | ||
return Object.entries( | ||
items.reduce<Record<string, ErrorItem[]>>((acc, obj) => { | ||
if (!acc[obj.group]) { | ||
acc[obj.group] = []; | ||
} | ||
acc[obj.group].push(obj); | ||
return acc; | ||
}, {}), | ||
).sort(([a], [b]) => a.localeCompare(b)); | ||
} | ||
|
||
function ErrorBlock({ | ||
item, | ||
setIsLocked, | ||
isLocked, | ||
}: ErrorBlockProps): JSX.Element { | ||
return ( | ||
<div className={`admonition alert alert--${severityClass(item.severity)}`}> | ||
<div className="admonition-content"> | ||
<div className="row row--no-gutters"> | ||
<div className="col col--12"> | ||
{item.message} {item.location} | ||
</div> | ||
{item.hasFixers && ( | ||
<div className="col col--12"> | ||
{item.fixers.map((fixer, index) => ( | ||
<div key={index} className={styles.fixer}> | ||
<span>> {fixer.message}</span> | ||
<button | ||
className="button button--primary button--sm" | ||
disabled={isLocked} | ||
onClick={(): void => { | ||
fixer.fix(); | ||
setIsLocked(true); | ||
}} | ||
> | ||
fix | ||
</button> | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default function ErrorsViewer({ | ||
value, | ||
}: ErrorsViewerProps): JSX.Element { | ||
const model = useMemo( | ||
() => (value ? groupErrorItems(value) : undefined), | ||
[value], | ||
); | ||
|
||
const [isLocked, setIsLocked] = useState(false); | ||
|
||
useEffect(() => { | ||
setIsLocked(false); | ||
}, [value]); | ||
|
||
return ( | ||
<div className={styles.list}> | ||
{model?.map(([group, data]) => { | ||
return ( | ||
<div className="margin-top--sm" key={group}> | ||
<h4>{group}</h4> | ||
{data.map((item, index) => ( | ||
<ErrorBlock | ||
isLocked={isLocked} | ||
setIsLocked={setIsLocked} | ||
item={item} | ||
key={index} | ||
/> | ||
))} | ||
</div> | ||
); | ||
})} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.