Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: add error panel

islxyqwe4c3415889899
diff --git a/packages/graphic-walker/package.json b/packages/graphic-walker/package.json
index 91fd9da0..fe0284e0 100644
--- a/packages/graphic-walker/package.json
+++ b/packages/graphic-walker/package.json
@@ -40,6 +40,7 @@
"@kanaries/web-data-loader": "^0.1.7",
"@tailwindcss/forms": "^0.5.4",
"autoprefixer": "^10.3.5",
+ "canvas-size": "^1.2.6",
"d3-format": "^3.1.0",
"d3-scale": "^4.0.2",
"d3-time-format": "^4.1.0",
@@ -53,6 +54,7 @@
"postcss": "^8.3.7",
"postinstall-postinstall": "^2.1.0",
"re-resizable": "^6.9.8",
+ "react-error-boundary": "^4.0.11",
"react-i18next": "^11.18.6",
"react-leaflet": "^4.2.1",
"react-shadow": "^20.0.0",
diff --git a/packages/graphic-walker/src/App.tsx b/packages/graphic-walker/src/App.tsx
index f647034f..3b1840cf 100644
--- a/packages/graphic-walker/src/App.tsx
+++ b/packages/graphic-walker/src/App.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useRef, useMemo, useState } from 'react';
+import React, { useEffect, useRef, useMemo, useState, useCallback } from 'react';
import { observer } from 'mobx-react-lite';
import { useTranslation } from 'react-i18next';
import { IGeographicData, IComputationFunction, IDarkMode, IMutField, IRow, ISegmentKey, IThemeKey, Specification } from './interfaces';
@@ -30,6 +30,9 @@ import AskViz from './components/askViz';
import { getComputation } from './computation/clientComputation';
import LogPanel from './fields/datasetFields/logPanel';
import BinPanel from './fields/datasetFields/binPanel';
+import { ErrorContext } from './utils/reportError';
+import { ErrorBoundary } from "react-error-boundary";
+import Errorpanel from './components/errorpanel';
export interface IGWProps {
dataSource?: IRow[];
@@ -63,6 +66,7 @@ export interface IGWProps {
}
};
computationTimeout?: number;
+ onError?: (err: Error) => void;
}
const App = observer<IGWProps>(function App(props) {
@@ -168,7 +172,18 @@ const App = observer<IGWProps>(function App(props) {
const rendererRef = useRef<IReactVegaHandler>(null);
+ const reportError = useCallback((msg: string, code?: number) => {
+ const err = new Error(`Error${code ? `(${code})`: ''}: ${msg}`);
+ console.error(err);
+ props.onError?.(err);
+ if (code) {
+ commonStore.updateShowErrorResolutionPanel(code);
+ }
+ }, [props.onError]);
+
return (
+ <ErrorContext value={{reportError}}>
+ <ErrorBoundary fallback={<div>Something went wrong</div>} onError={props.onError} >
<div
className={`${
darkMode === 'dark' ? 'dark' : ''
@@ -195,6 +210,7 @@ const App = observer<IGWProps>(function App(props) {
<CodeExport />
<ExplainData themeKey={themeKey} dark={darkMode}/>
<VisualConfig />
+ <Errorpanel />
<LogPanel />
<BinPanel/>
{commonStore.showGeoJSONConfigPanel && <GeoConfigPanel />}
@@ -254,6 +270,8 @@ const App = observer<IGWProps>(function App(props) {
)}
</div>
</div>
+ </ErrorBoundary>
+ </ErrorContext>
);
});
diff --git a/packages/graphic-walker/src/components/errorpanel/index.tsx b/packages/graphic-walker/src/components/errorpanel/index.tsx
new file mode 100644
index 00000000..829274e9
--- /dev/null
+++ b/packages/graphic-walker/src/components/errorpanel/index.tsx
@@ -0,0 +1,62 @@
+import { observer } from 'mobx-react-lite';
+import { useGlobalStore } from '../../store';
+import React from 'react';
+import Modal from '../smallModal';
+import { useTranslation } from 'react-i18next';
+import DefaultButton from '../button/default';
+
+export default observer(function ErrorPanel() {
+ const { commonStore, vizStore } = useGlobalStore();
+ const { t } = useTranslation();
+ switch (commonStore.showErrorResolutionPanel) {
+ case 0:
+ return null;
+ case 500:
+ return (
+ <Modal show={true}>
+ <div className="flex flex-col justify-center items-start">
+ <h2 className="font-medium text-xl my-2">Oops!</h2>
+ <p className="font-normal my-2">The chart is too large to render. You can try options above:</p>
+ <p className="font-normal my-2">
+ 1. Set the chart to a fixed size.
+ <a
+ className="mx-2 cursor-pointer font-semibold text-indigo-600 hover:text-indigo-500"
+ onClick={() => {
+ vizStore.setChartLayout({ mode: 'fixed', width: 800, height: 600 });
+ commonStore.updateShowErrorResolutionPanel(0);
+ }}
+ >
+ Set Now
+ </a>
+ </p>
+ <p className="font-normal my-2">
+ 2. Change to SVG renderer.
+ <a
+ className="mx-2 cursor-pointer font-semibold text-indigo-600 hover:text-indigo-500"
+ onClick={() => {
+ vizStore.setVisualConfig('useSvg', true);
+ commonStore.updateShowErrorResolutionPanel(0);
+ }}
+ >
+ Set Now
+ </a>
+ </p>
+ <p className="font-normal my-2">3. Close this modal and edit the chart to reduce chart size.</p>
+ <fieldset className="mt-2 gap-1 flex flex-col justify-center items-end w-full">
+ <div className="mt-2">
+ <DefaultButton
+ text={`Close`}
+ className="mr-2 px-2"
+ onClick={() => {
+ commonStore.updateShowErrorResolutionPanel(0);
+ return;
+ }}
+ />
+ </div>
+ </fieldset>
+ </div>
+ </Modal>
+ );
+ }
+ return null;
+});
diff --git a/packages/graphic-walker/src/store/commonStore.ts b/packages/graphic-walker/src/store/commonStore.ts
index 39f3f5d4..bd21fef2 100644
--- a/packages/graphic-walker/src/store/commonStore.ts
+++ b/packages/graphic-walker/src/store/commonStore.ts
@@ -24,6 +24,7 @@ export class CommonStore {
public segmentKey: ISegmentKey = ISegmentKey.vis;
public selectedMarkObject: Record<string, string | number | undefined> = {};
public tableCollapsedHeaderMap: Map<string, INestNode["path"]> = new Map();
+ public showErrorResolutionPanel: number = 0;
constructor () {
this.datasets = [];
@@ -240,6 +241,10 @@ export class CommonStore {
this.selectedMarkObject = newMarkObj;
}
+ public updateShowErrorResolutionPanel(errCode: number) {
+ this.showErrorResolutionPanel = errCode;
+ }
+
public destroy () {
this.dataSources = [];
this.datasets = [];
diff --git a/packages/graphic-walker/src/utils/reportError.ts b/packages/graphic-walker/src/utils/reportError.ts
new file mode 100644
index 00000000..76f0c659
--- /dev/null
+++ b/packages/graphic-walker/src/utils/reportError.ts
@@ -0,0 +1,13 @@
+import React from 'react';
+
+export const Errors = {
+ canvasExceedSize: 500,
+} as const;
+
+export const errorContext = React.createContext({
+ reportError: (() => {}) as (message: string, code: (typeof Errors)[keyof typeof Errors]) => void,
+});
+
+export const useReporter = () => React.useContext(errorContext);
+
+export const ErrorContext = errorContext.Provider;
\ No newline at end of file
diff --git a/packages/graphic-walker/src/vis/react-vega.tsx b/packages/graphic-walker/src/vis/react-vega.tsx
index 7d4a0ded..1a5ca197 100644
--- a/packages/graphic-walker/src/vis/react-vega.tsx
+++ b/packages/graphic-walker/src/vis/react-vega.tsx
@@ -10,6 +10,8 @@ import { IViewField, IRow, IStackMode, VegaGlobalConfig, IVegaChartRef } from '.
import { getVegaTimeFormatRules } from './temporalFormat';
import { getSingleView } from './spec/view';
import { NULL_FIELD } from './spec/field';
+import canvasSize from 'canvas-size';
+import { Errors, useReporter } from '../utils/reportError';
const CanvaContainer = styled.div<{rowSize: number; colSize: number;}>`
display: grid;
@@ -145,6 +147,8 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVe
const colRepeatFields = useMemo(() => colMeas.length === 0 ? colDims.slice(-1) : colMeas, [rowDims, rowMeas]);//colMeas.slice(0, -1);
const allFieldIds = useMemo(() => [...rows, ...columns, color, opacity, size].filter(f => Boolean(f)).map(f => (f as IViewField).fid), [rows, columns, color, opacity, size]);
+ const { reportError: reportGWError } = useReporter();
+
const [crossFilterTriggerIdx, setCrossFilterTriggerIdx] = useState(-1);
useEffect(() => {
@@ -242,6 +246,14 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVe
const task = embed(viewPlaceholders[0].current, spec, { renderer: useSvg ? 'svg' : 'canvas', mode: 'vega-lite', actions: showActions, timeFormatLocale: getVegaTimeFormatRules(locale), config: vegaConfig }).then(res => {
const container = res.view.container();
const canvas = container?.querySelector('canvas') ?? null;
+ const success = useSvg || (canvas && canvasSize.test({ width: canvas.width, height: canvas.height }));
+ if (!success) {
+ if (canvas) {
+ reportGWError("canvas exceed max size", Errors.canvasExceedSize);
+ } else {
+ reportGWError("canvas not found", Errors.canvasExceedSize);
+ }
+ }
vegaRefs.current = [{
w: container?.clientWidth ?? res.view.width(),
h: container?.clientHeight ?? res.view.height(),
@@ -321,6 +333,14 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVe
const task = embed(node, ans, { renderer: useSvg ? 'svg' : 'canvas', mode: 'vega-lite', actions: showActions, timeFormatLocale: getVegaTimeFormatRules(locale), config: vegaConfig }).then(res => {
const container = res.view.container();
const canvas = container?.querySelector('canvas') ?? null;
+ const success = useSvg || (canvas && canvasSize.test({ width: canvas.width, height: canvas.height }));
+ if (!success) {
+ if (canvas) {
+ reportGWError("canvas exceed max size", Errors.canvasExceedSize);
+ } else {
+ reportGWError("canvas not found", Errors.canvasExceedSize);
+ }
+ }
vegaRefs.current[id] = {
w: container?.clientWidth ?? res.view.width(),
h: container?.clientHeight ?? res.view.height(),
diff --git a/yarn.lock b/yarn.lock
index a3a914e4..eacca83e 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -215,6 +215,13 @@
dependencies:
regenerator-runtime "^0.13.11"
+"@babel/runtime@^7.12.5":
+ version "7.22.15"
+ resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.15.tgz#38f46494ccf6cf020bd4eed7124b425e83e523b8"
+ integrity sha512-T0O+aa+4w0u06iNmapipJXMV4HoUir03hpx3/YqXXhu9xim3w+dVphjFWl1OH8NbZHw5Lbm9k45drDkgq2VNNA==
+ dependencies:
+ regenerator-runtime "^0.14.0"
+
"@babel/template@^7.20.7":
version "7.20.7"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.20.7.tgz#a15090c2839a83b02aa996c0b4994005841fd5a8"
@@ -985,6 +992,11 @@ caniuse-lite@^1.0.30001426, caniuse-lite@^1.0.30001449:
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001458.tgz#871e35866b4654a7d25eccca86864f411825540c"
integrity sha512-lQ1VlUUq5q9ro9X+5gOEyH7i3vm+AYVT1WDCVB69XOZ17KZRhnZ9J0Sqz7wTHQaLBJccNCHq8/Ww5LlOIZbB0w==
+canvas-size@^1.2.6:
+ version "1.2.6"
+ resolved "https://registry.yarnpkg.com/canvas-size/-/canvas-size-1.2.6.tgz#1eaa6b56167cf2a70fa4021680829d2073b45706"
+ integrity sha512-x2iVHOrZ5x9V0Hwx6kBz+Yxf/VCAII+jrD6WLjJbytJLozHq/oDJjEva432Os0eHxWMFR0vYlLJwTr6QxyxQqw==
+
chalk@^2.0.0:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
@@ -2027,6 +2039,13 @@ react-dom@^17.x:
object-assign "^4.1.1"
scheduler "^0.20.2"
+react-error-boundary@^4.0.11:
+ version "4.0.11"
+ resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-4.0.11.tgz#36bf44de7746714725a814630282fee83a7c9a1c"
+ integrity sha512-U13ul67aP5DOSPNSCWQ/eO0AQEYzEFkVljULQIjMV0KlffTAhxuDoBKdO0pb/JZ8mDhMKFZ9NZi0BmLGUiNphw==
+ dependencies:
+ "@babel/runtime" "^7.12.5"
+
react-i18next@^11.18.6:
version "11.18.6"
resolved "https://registry.yarnpkg.com/react-i18next/-/react-i18next-11.18.6.tgz#e159c2960c718c1314f1e8fcaa282d1c8b167887"
@@ -2136,6 +2155,11 @@ regenerator-runtime@^0.13.11:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==
+regenerator-runtime@^0.14.0:
+ version "0.14.0"
+ resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.0.tgz#5e19d68eb12d486f797e15a3c6a918f7cec5eb45"
+ integrity sha512-srw17NI0TUWHuGa5CFGGmhfNIeja30WMBfbslPNhf6JrqQlLN5gcrvig1oqPxiVaXb0oW0XRKtH6Nngs5lKCIA==
+
require-directory@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"