forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loads the widget and displays it if there is a widget plugin that matches - Displays an error if no plugin is found that can display the type of widget, or a widget with that name is not found - Fixes deephaven#1629
- Loading branch information
Showing
15 changed files
with
173 additions
and
79 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,39 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import ErrorBoundary, { ErrorBoundaryProps } from './ErrorBoundary'; | ||
|
||
function ThrowComponent(): JSX.Element { | ||
throw new Error('Test error'); | ||
} | ||
|
||
function makeWrapper({ | ||
children = 'Hello World', | ||
className, | ||
onError = jest.fn(), | ||
fallback, | ||
}: Partial<ErrorBoundaryProps> = {}) { | ||
return render( | ||
<ErrorBoundary className={className} fallback={fallback} onError={onError}> | ||
{children} | ||
</ErrorBoundary> | ||
); | ||
} | ||
|
||
it('should render the children if there is no error', () => { | ||
const onError = jest.fn(); | ||
const { getByText } = makeWrapper({ onError }); | ||
expect(getByText('Hello World')).toBeInTheDocument(); | ||
expect(onError).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should render the fallback if there is an error', () => { | ||
const onError = jest.fn(); | ||
const error = new Error('Test error'); | ||
const { getByText } = makeWrapper({ | ||
children: <ThrowComponent />, | ||
fallback: <div>Fallback</div>, | ||
onError, | ||
}); | ||
expect(getByText('Fallback')).toBeInTheDocument(); | ||
expect(onError).toHaveBeenCalledWith(error, expect.anything()); | ||
}); |
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,70 @@ | ||
import Log from '@deephaven/log'; | ||
import React, { Component, ReactNode } from 'react'; | ||
import LoadingOverlay from './LoadingOverlay'; | ||
|
||
const log = Log.module('ErrorBoundary'); | ||
|
||
export interface ErrorBoundaryProps { | ||
/** Children to catch errors from */ | ||
children: ReactNode; | ||
|
||
/** Classname to wrap the error message with */ | ||
className?: string; | ||
|
||
/** Callback for when an error occurs */ | ||
onError?: (error: Error, errorInfo: React.ErrorInfo) => void; | ||
|
||
/** Custom fallback element */ | ||
fallback?: ReactNode; | ||
} | ||
|
||
export interface ErrorBoundaryState { | ||
error?: Error; | ||
} | ||
|
||
/** | ||
* Error boundary for catching render errors in React. Displays an error message if an error is caught by default, or you can specify a fallback component to render. | ||
* https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary | ||
*/ | ||
export class ErrorBoundary extends Component< | ||
ErrorBoundaryProps, | ||
ErrorBoundaryState | ||
> { | ||
static getDerivedStateFromError(error: Error): ErrorBoundaryState { | ||
return { error }; | ||
} | ||
|
||
constructor(props: ErrorBoundaryProps) { | ||
super(props); | ||
this.state = { error: undefined }; | ||
} | ||
|
||
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void { | ||
const { onError } = this.props; | ||
log.error('Error caught by ErrorBoundary', error, errorInfo); | ||
onError?.(error, errorInfo); | ||
} | ||
|
||
render(): ReactNode { | ||
const { children, className, fallback } = this.props; | ||
const { error } = this.state; | ||
if (error != null) { | ||
if (fallback != null) { | ||
return fallback; | ||
} | ||
|
||
return ( | ||
<div className={className}> | ||
<LoadingOverlay | ||
errorMessage={`${error}`} | ||
isLoading={false} | ||
isLoaded={false} | ||
/> | ||
</div> | ||
); | ||
} | ||
return children; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { useMemo } from 'react'; | ||
import usePlugins from './usePlugins'; | ||
import { isWidgetPlugin } from './PluginTypes'; | ||
|
||
export type WidgetViewProps = { | ||
/** Fetch function to return the widget */ | ||
fetch: () => Promise<unknown>; | ||
|
||
/** Type of the widget */ | ||
type: string; | ||
}; | ||
|
||
export function WidgetView({ fetch, type }: WidgetViewProps): JSX.Element { | ||
const plugins = usePlugins(); | ||
const plugin = useMemo( | ||
() => | ||
[...plugins.values()] | ||
.filter(isWidgetPlugin) | ||
.find(p => [p.supportedTypes].flat().includes(type)), | ||
[plugins, type] | ||
); | ||
|
||
if (plugin != null) { | ||
const Component = plugin.component; | ||
return <Component fetch={fetch} />; | ||
} | ||
|
||
throw new Error(`Unknown widget type '${type}'`); | ||
} | ||
|
||
export default WidgetView; |
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