mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-03 06:47:03 +00:00
* fix(web): make long file errors expandable * fix(web): count graphemes in expandable errors * fix(web): reset expanded errors when messages change * fix(web): reset expansion when error text changes * fix(web): avoid duplicate file error alerts
55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
import { useState } from 'react'
|
|
|
|
const DEFAULT_MAX_LENGTH = 160
|
|
|
|
type ExpandableErrorMessageProps = {
|
|
message: string
|
|
expandLabel: string
|
|
collapseLabel: string
|
|
className?: string
|
|
maxLength?: number
|
|
}
|
|
|
|
function splitGraphemes(value: string) {
|
|
if (typeof Intl.Segmenter === 'function') {
|
|
return Array.from(new Intl.Segmenter(undefined, { granularity: 'grapheme' }).segment(value), ({ segment }) => segment)
|
|
}
|
|
|
|
return Array.from(value)
|
|
}
|
|
|
|
export function ExpandableErrorMessage(props: ExpandableErrorMessageProps) {
|
|
return <ExpandableErrorMessageContent key={props.message} {...props} />
|
|
}
|
|
|
|
function ExpandableErrorMessageContent(props: ExpandableErrorMessageProps) {
|
|
const [expanded, setExpanded] = useState(false)
|
|
const maxLength = props.maxLength ?? DEFAULT_MAX_LENGTH
|
|
const singleLine = props.message.replace(/\s+/g, ' ').trim()
|
|
const graphemes = splitGraphemes(singleLine)
|
|
const truncated = graphemes.length > maxLength
|
|
const preview = truncated
|
|
? `${graphemes.slice(0, maxLength).join('').trimEnd()}…`
|
|
: props.message
|
|
|
|
return (
|
|
<div role="alert" className={props.className}>
|
|
{truncated ? (
|
|
<button
|
|
type="button"
|
|
data-hapi-expandable-error-toggle="true"
|
|
aria-expanded={expanded}
|
|
aria-label={`${expanded ? props.collapseLabel : props.expandLabel}: ${expanded ? props.message : preview}`}
|
|
title={expanded ? props.collapseLabel : props.expandLabel}
|
|
onClick={() => setExpanded((current) => !current)}
|
|
className={`block w-full min-w-0 cursor-pointer text-left hover:text-[var(--app-fg)] ${expanded ? 'whitespace-pre-wrap break-words' : 'truncate'}`}
|
|
>
|
|
{expanded ? props.message : preview}
|
|
</button>
|
|
) : (
|
|
props.message
|
|
)}
|
|
</div>
|
|
)
|
|
}
|