Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create an ErrorView that can be used to display errors #1965

Merged
merged 14 commits into from
Apr 30, 2024
59 changes: 59 additions & 0 deletions packages/code-studio/src/styleguide/ErrorViews.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable react/jsx-props-no-spreading */
/* eslint no-alert: "off" */
import React, { CSSProperties } from 'react';
import { ErrorView } from '@deephaven/components';
import { sampleSectionIdAndClasses } from './utils';

function ErrorViews(): React.ReactElement {
const columnStyle: CSSProperties = {
maxHeight: 500,
display: 'flex',
flexDirection: 'column',
maxWidth: 400,
};

const shortErrorMessage = 'This is a short error message';
const midErrorMessage = 'Mid length error message\n'.repeat(10);
const longErrorMessage = 'Really long error message\n'.repeat(100);

const midErrorType = 'MidError';
const longErrorType = 'SuperLongErrorMessageType';

return (
<div {...sampleSectionIdAndClasses('error-views')}>
<h2 className="ui-title" title="Display error messages easily">
Error Views
</h2>
<h3>Expandable</h3>
<div className="row" style={{ maxHeight: 500 }}>
<div className="col" style={columnStyle}>
<ErrorView message={shortErrorMessage} />
</div>
<div className="col" style={columnStyle}>
<ErrorView message={midErrorMessage} type={midErrorType} />
</div>
<div className="col" style={columnStyle}>
<ErrorView message={longErrorMessage} type={longErrorType} />
</div>
</div>
<h3>Always expanded</h3>
<div className="row" style={{ maxHeight: 500 }}>
<div className="col" style={columnStyle}>
<ErrorView message={shortErrorMessage} isExpanded />
</div>
<div className="col" style={columnStyle}>
<ErrorView message={midErrorMessage} type={midErrorType} isExpanded />
</div>
<div className="col" style={columnStyle}>
<ErrorView
message={longErrorMessage}
type={longErrorType}
isExpanded
/>
</div>
</div>
</div>
);
}

export default ErrorViews;
2 changes: 2 additions & 0 deletions packages/code-studio/src/styleguide/StyleGuide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { RandomAreaPlotAnimation } from './RandomAreaPlotAnimation';
import SpectrumComparison from './SpectrumComparison';
import Pickers from './Pickers';
import ListViews from './ListViews';
import ErrorViews from './ErrorViews';

const stickyProps = {
position: 'sticky',
Expand Down Expand Up @@ -133,6 +134,7 @@ function StyleGuide(): React.ReactElement {

<SampleMenuCategory data-menu-category="Spectrum Comparison" />
<SpectrumComparison />
<ErrorViews />
</div>
</div>
);
Expand Down
83 changes: 83 additions & 0 deletions packages/components/src/ErrorView.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
@import '../scss/custom.scss';

.error-view {
position: relative;
color: $danger;
border-radius: $border-radius;
background-color: negative-opacity($exception-transparency);
display: flex;
flex-direction: column;
flex-grow: 0;
font-family: $font-family-monospace;
transition: all $transition ease-in-out;
max-height: 150px;

&.expanded {
max-height: 100%;
}

.error-view-header {
display: flex;
flex-direction: row;
justify-content: space-between;
text-wrap: nowrap;
width: 100%;

.error-view-header-text {
display: flex;
flex-direction: row;
align-items: center;
padding-left: $spacer;
padding-right: $spacer;
font-weight: bold;
flex-shrink: 1;
overflow: hidden;
white-space: nowrap;

span {
flex-shrink: 1;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
padding-left: $spacer-1;
}
}

.error-view-buttons {
display: flex;
flex-direction: row;
gap: 1px;
overflow: hidden;
border-radius: 0 $border-radius 0 0;
flex-shrink: 0;

.btn-danger {
border-radius: 0;
color: var(--dh-color-contrast-dark);
opacity: 0.8;
padding: $spacer-1;
&:active {
color: var(--dh-color-contrast-dark);
}
}

.error-view-copy-button {
min-width: 3rem;
}
}
}

.error-view-text {
width: 100%;
padding: $spacer;
margin-bottom: 0;
color: $danger;
background-color: transparent;
border: 0;
resize: none;
outline: none;
white-space: pre;
flex-grow: 1;
overflow: auto;
}
}
92 changes: 92 additions & 0 deletions packages/components/src/ErrorView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React, { useCallback, useLayoutEffect, useRef, useState } from 'react';
import classNames from 'classnames';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { vsDiffAdded, vsDiffRemoved, vsWarning } from '@deephaven/icons';
import {
useDebouncedCallback,
useResizeObserver,
} from '@deephaven/react-hooks';
import CopyButton from './CopyButton';
import Button from './Button';
import './ErrorView.scss';

export type ErrorViewerProps = {
/** The message to display in the error view */
message: string;

/** 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. */
isExpanded?: boolean;

/** The type of error message to display in the header. Defaults to Error. */
type?: string;
};

/**
* Component that displays an error message in a textarea so user can scroll and a copy button.
*/
function ErrorView({
message,
isExpanded: isExpandedProp = false,
type = 'Error',
}: ErrorViewerProps): JSX.Element {
const [isExpandable, setIsExpandable] = useState(false);
const [isExpanded, setIsExpanded] = useState(false);
const viewRef = useRef<HTMLDivElement>(null);
const textRef = useRef<HTMLPreElement>(null);

const handleResize = useCallback(() => {
if (isExpanded || isExpandedProp || textRef.current == null) {
return;
}
const newIsExpandable =
textRef.current.scrollHeight > textRef.current.clientHeight;
setIsExpandable(newIsExpandable);
}, [isExpanded, isExpandedProp]);

const debouncedHandleResize = useDebouncedCallback(handleResize, 100);

useResizeObserver(viewRef.current, debouncedHandleResize);

useLayoutEffect(debouncedHandleResize, [debouncedHandleResize]);

return (
<div
className={classNames('error-view', {
expanded: isExpanded || isExpandedProp,
})}
ref={viewRef}
>
<div className="error-view-header">
<div className="error-view-header-text">
<FontAwesomeIcon icon={vsWarning} />
<span>{type}</span>
</div>
<div className="error-view-buttons">
<CopyButton
kind="danger"
className="error-view-copy-button"
tooltip="Copy exception contents"
copy={`${type}: ${message}`.trim()}
/>
{(isExpandable || isExpanded) && !isExpandedProp && (
<Button
kind="danger"
className="error-view-expand-button"
onClick={() => {
setIsExpanded(!isExpanded);
}}
icon={isExpanded ? vsDiffRemoved : vsDiffAdded}
>
{isExpanded ? 'Show Less' : 'Show More'}
</Button>
)}
</div>
</div>
<pre className="error-view-text" ref={textRef}>
{message}
</pre>
</div>
);
}

export default ErrorView;
1 change: 1 addition & 0 deletions packages/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from './DraggableItemList';
export { default as DragUtils } from './DragUtils';
export { default as EditableItemList } from './EditableItemList';
export * from './ErrorBoundary';
export { default as ErrorView } from './ErrorView';
export { default as HierarchicalCheckboxMenu } from './HierarchicalCheckboxMenu';
export * from './HierarchicalCheckboxMenu';
export * from './ItemList';
Expand Down
1 change: 1 addition & 0 deletions tests/styleguide.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'sample-section-spectrum-forms',
'sample-section-spectrum-overlays',
'sample-section-spectrum-well',
'sample-section-error-views',
];
const buttonSectionIds: string[] = [
'sample-section-buttons-regular',
Expand Down Expand Up @@ -85,7 +86,7 @@

const sampleSection = page.locator(`#${id}`);

await expect(sampleSection).toHaveScreenshot(

Check failure on line 89 in tests/styleguide.spec.ts

View workflow job for this annotation

GitHub Actions / merge-reports

[webkit] › styleguide.spec.ts:83:7 › UI regression test - Styleguide section - sample-section-chart-colors

1) [webkit] › styleguide.spec.ts:83:7 › UI regression test - Styleguide section - sample-section-chart-colors Error: Screenshot comparison failed: Timeout 45000ms exceeded. Call log: - expect.toHaveScreenshot(chart-colors.png) with timeout 45000ms - verifying given screenshot expectation - waiting for locator('#sample-section-chart-colors') - locator resolved to <div class="sample-section" id="sample-section-chart-…>…</div> - taking element screenshot - disabled all CSS animations - waiting for element to be visible and stable - Timeout 45000ms exceeded. Expected: /home/runner/work/web-client-ui/web-client-ui/tests/styleguide.spec.ts-snapshots/chart-colors-webkit-linux.png 87 | const sampleSection = page.locator(`#${id}`); 88 | > 89 | await expect(sampleSection).toHaveScreenshot( | ^ 90 | `${id.replace(/^sample-section-/, '')}.png`, 91 | { timeout: 45000 } 92 | ); at /home/runner/work/web-client-ui/web-client-ui/tests/styleguide.spec.ts:89:33

Check failure on line 89 in tests/styleguide.spec.ts

View workflow job for this annotation

GitHub Actions / merge-reports

[webkit] › styleguide.spec.ts:83:7 › UI regression test - Styleguide section - sample-section-editor-colors

2) [webkit] › styleguide.spec.ts:83:7 › UI regression test - Styleguide section - sample-section-editor-colors Error: Screenshot comparison failed: Timeout 45000ms exceeded. Call log: - expect.toHaveScreenshot(editor-colors.png) with timeout 45000ms - verifying given screenshot expectation - waiting for locator('#sample-section-editor-colors') - locator resolved to <div class="sample-section" id="sample-section-editor…>…</div> - Timeout 45000ms exceeded. Expected: /home/runner/work/web-client-ui/web-client-ui/tests/styleguide.spec.ts-snapshots/editor-colors-webkit-linux.png 87 | const sampleSection = page.locator(`#${id}`); 88 | > 89 | await expect(sampleSection).toHaveScreenshot( | ^ 90 | `${id.replace(/^sample-section-/, '')}.png`, 91 | { timeout: 45000 } 92 | ); at /home/runner/work/web-client-ui/web-client-ui/tests/styleguide.spec.ts:89:33
`${id.replace(/^sample-section-/, '')}.png`,
{ timeout: 45000 }
);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading