|
| 1 | +import React, { useCallback, useLayoutEffect, useRef, useState } from 'react'; |
| 2 | +import classNames from 'classnames'; |
| 3 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
| 4 | +import { vsDiffAdded, vsDiffRemoved, vsWarning } from '@deephaven/icons'; |
| 5 | +import { |
| 6 | + useDebouncedCallback, |
| 7 | + useResizeObserver, |
| 8 | +} from '@deephaven/react-hooks'; |
| 9 | +import CopyButton from './CopyButton'; |
| 10 | +import Button from './Button'; |
| 11 | +import './ErrorView.scss'; |
| 12 | + |
| 13 | +export type ErrorViewerProps = { |
| 14 | + /** The message to display in the error view */ |
| 15 | + message: string; |
| 16 | + |
| 17 | + /** Set to true if you want the error view to display expanded. Will not show the Show More/Less buttons if true. Defaults to false. */ |
| 18 | + isExpanded?: boolean; |
| 19 | + |
| 20 | + /** The type of error message to display in the header. Defaults to Error. */ |
| 21 | + type?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * Component that displays an error message in a textarea so user can scroll and a copy button. |
| 26 | + */ |
| 27 | +function ErrorView({ |
| 28 | + message, |
| 29 | + isExpanded: isExpandedProp = false, |
| 30 | + type = 'Error', |
| 31 | +}: ErrorViewerProps): JSX.Element { |
| 32 | + const [isExpandable, setIsExpandable] = useState(false); |
| 33 | + const [isExpanded, setIsExpanded] = useState(false); |
| 34 | + const viewRef = useRef<HTMLDivElement>(null); |
| 35 | + const textRef = useRef<HTMLPreElement>(null); |
| 36 | + |
| 37 | + const handleResize = useCallback(() => { |
| 38 | + if (isExpanded || isExpandedProp || textRef.current == null) { |
| 39 | + return; |
| 40 | + } |
| 41 | + const newIsExpandable = |
| 42 | + textRef.current.scrollHeight > textRef.current.clientHeight; |
| 43 | + setIsExpandable(newIsExpandable); |
| 44 | + }, [isExpanded, isExpandedProp]); |
| 45 | + |
| 46 | + const debouncedHandleResize = useDebouncedCallback(handleResize, 100); |
| 47 | + |
| 48 | + useResizeObserver(viewRef.current, debouncedHandleResize); |
| 49 | + |
| 50 | + useLayoutEffect(debouncedHandleResize, [debouncedHandleResize]); |
| 51 | + |
| 52 | + return ( |
| 53 | + <div |
| 54 | + className={classNames('error-view', { |
| 55 | + expanded: isExpanded || isExpandedProp, |
| 56 | + })} |
| 57 | + ref={viewRef} |
| 58 | + > |
| 59 | + <div className="error-view-header"> |
| 60 | + <div className="error-view-header-text"> |
| 61 | + <FontAwesomeIcon icon={vsWarning} /> |
| 62 | + <span>{type}</span> |
| 63 | + </div> |
| 64 | + <div className="error-view-buttons"> |
| 65 | + <CopyButton |
| 66 | + kind="danger" |
| 67 | + className="error-view-copy-button" |
| 68 | + tooltip="Copy exception contents" |
| 69 | + copy={`${type}: ${message}`.trim()} |
| 70 | + /> |
| 71 | + {(isExpandable || isExpanded) && !isExpandedProp && ( |
| 72 | + <Button |
| 73 | + kind="danger" |
| 74 | + className="error-view-expand-button" |
| 75 | + onClick={() => { |
| 76 | + setIsExpanded(!isExpanded); |
| 77 | + }} |
| 78 | + icon={isExpanded ? vsDiffRemoved : vsDiffAdded} |
| 79 | + > |
| 80 | + {isExpanded ? 'Show Less' : 'Show More'} |
| 81 | + </Button> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + <pre className="error-view-text" ref={textRef}> |
| 86 | + {message} |
| 87 | + </pre> |
| 88 | + </div> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +export default ErrorView; |
0 commit comments