Skip to content

Commit

Permalink
feat: add onError property (#24)
Browse files Browse the repository at this point in the history
* fix: memoize fixes

* feat: add onError property

* marginy review fixes
  • Loading branch information
korvin89 authored Jul 18, 2022
1 parent 11cfe6c commit 0b51f19
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/components/ChartKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import './ChartKit.scss';

const b = block('chartkit');

export const ChartKit = React.forwardRef<ChartKitRef | undefined, ChartKitProps<ChartkitType>>(
const ChartKitComponent = React.forwardRef<ChartKitRef | undefined, ChartKitProps<ChartkitType>>(
(props, ref) => {
const widgetRef = React.useRef<ChartKitWidgetRef>();
const {id = getRandomCKId(), type, data, onLoad, ...restProps} = props;
const {id = getRandomCKId(), type, data, onLoad, onError, ...restProps} = props;
const lang = settings.get('lang');
const plugins = settings.get('plugins');
const plugin = plugins.find((iteratedPlugin) => iteratedPlugin.type === type);
Expand All @@ -39,7 +39,7 @@ export const ChartKit = React.forwardRef<ChartKitRef | undefined, ChartKitProps<
);

return (
<ErrorBoundary>
<ErrorBoundary onError={onError}>
<React.Suspense fallback={<Loader />}>
<div className={b()}>
<ChartComponent
Expand All @@ -56,3 +56,5 @@ export const ChartKit = React.forwardRef<ChartKitRef | undefined, ChartKitProps<
);
},
);

export const ChartKit = React.memo(ChartKitComponent);
15 changes: 14 additions & 1 deletion src/components/ErrorBoundary/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import React from 'react';
import type {ChartKitOnError} from '../../types';
import {ErrorView} from '../ErrorView/ErrorView';

type Props = {
onError?: ChartKitOnError;
};

type State = {
error?: Error;
};

export class ErrorBoundary extends React.Component<{}, State> {
export class ErrorBoundary extends React.Component<Props, State> {
static getDerivedStateFromError(error: Error) {
return {error};
}
Expand All @@ -14,6 +19,14 @@ export class ErrorBoundary extends React.Component<{}, State> {
error: undefined,
};

componentDidUpdate() {
const {error} = this.state;

if (error) {
this.props.onError?.({error});
}
}

render() {
if (this.state.error) {
return <ErrorView />;
Expand Down
13 changes: 9 additions & 4 deletions src/plugins/yagr/renderer/YagrWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import moment from 'moment';
import debounce from 'lodash/debounce';
import {useThemeValue} from '@yandex-cloud/uikit';
import YagrComponent from 'yagr/dist/react';
import YagrComponent, {YagrChartProps} from 'yagr/dist/react';
import {
YagrConfig,
TooltipRow,
Expand Down Expand Up @@ -233,6 +233,13 @@ const YagrWidget = React.forwardRef<ChartKitWidgetRef | undefined, YagrWidgetPro
.join(', ') || id
: id;

const handleChartLoading: NonNullable<YagrChartProps['onChartLoad']> = React.useCallback(
(chart, {renderTime}) => {
onLoad?.({...data, widget: chart, widgetRendering: renderTime});
},
[onLoad, data],
);

const onWindowResize = React.useCallback(() => {
if (yagrRef.current?.chart) {
const chart = yagrRef.current.chart;
Expand Down Expand Up @@ -268,9 +275,7 @@ const YagrWidget = React.forwardRef<ChartKitWidgetRef | undefined, YagrWidgetPro
ref={yagrRef}
id={id}
config={config}
onChartLoad={(chart, {renderTime}) =>
onLoad && onLoad({...props.data, widget: chart, widgetRendering: renderTime})
}
onChartLoad={handleChartLoading}
debug={{filename: debugFileName}}
/>
);
Expand Down
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ export type ChartKitOnLoadData<T extends ChartkitType> = {
widgetRendering?: number;
};

export type ChartKitOnError = (data: {error: any}) => void;

export type ChartKitProps<T extends ChartkitType> = {
type: T;
data: ChartkitWidget[T]['data'];
id?: string;
onLoad?: (data?: ChartKitOnLoadData<T>) => void;
onError?: ChartKitOnError;
} & {[key in keyof Omit<ChartkitWidget[T], 'data' | 'widget'>]: ChartkitWidget[T][key]};

export type ChartKitPlugin = {
Expand Down

0 comments on commit 0b51f19

Please sign in to comment.