Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: add feedback for askviz (#234)

islxyqwe6f7dae0474ed
diff --git a/packages/graphic-walker/src/App.tsx b/packages/graphic-walker/src/App.tsx
index ee3c8759..4a6d4a1a 100644
--- a/packages/graphic-walker/src/App.tsx
+++ b/packages/graphic-walker/src/App.tsx
@@ -131,7 +131,8 @@ export const VizApp = observer(function VizApp(props: BaseVizProps) {
<div style={{ marginTop: '0em', borderTop: 'none' }} className="m-4 p-4 border border-gray-200 dark:border-gray-700">
{enhanceAPI?.features?.askviz && (
<AskViz
- api={typeof enhanceAPI.features.askviz === 'string' ? enhanceAPI.features.askviz : ''}
+ api={typeof enhanceAPI.features.askviz === 'boolean' ? '' : enhanceAPI.features.askviz}
+ feedbackApi={typeof enhanceAPI.features.feedbackAskviz === 'boolean' ? '' : enhanceAPI.features.feedbackAskviz}
headers={enhanceAPI?.header}
/>
)}
diff --git a/packages/graphic-walker/src/components/askViz/index.tsx b/packages/graphic-walker/src/components/askViz/index.tsx
index 40bb7442..e64b24aa 100644
--- a/packages/graphic-walker/src/components/askViz/index.tsx
+++ b/packages/graphic-walker/src/components/askViz/index.tsx
@@ -1,12 +1,12 @@
import { observer } from 'mobx-react-lite';
import React, { useCallback, useState } from 'react';
import { useVizStore } from '../../store';
-import { PaperAirplaneIcon } from '@heroicons/react/24/outline';
+import { HandThumbDownIcon, HandThumbUpIcon, PaperAirplaneIcon } from '@heroicons/react/24/outline';
import Spinner from '../spinner';
-import { IViewField } from '../../interfaces';
+import { IAskVizFeedback, IChartForExport, IViewField, IVisSpecForExport } from '../../interfaces';
import { useTranslation } from 'react-i18next';
-
-type VEGALite = any;
+import { useReporter } from '../../utils/reportError';
+import { parseErrorMessage } from '../../utils';
const api = import.meta.env.DEV ? 'http://localhost:2023/api/vis/text2gw' : 'https://enhanceai.kanaries.net/api/vis/text2gw';
@@ -30,7 +30,7 @@ async function vizQuery(api: string, metas: IViewField[], query: string, headers
});
const result: {
success: boolean;
- data: VEGALite;
+ data: IVisSpecForExport | IChartForExport;
message?: string;
} = await res.json();
if (result.success) {
@@ -40,30 +40,73 @@ async function vizQuery(api: string, metas: IViewField[], query: string, headers
}
}
-const AskViz: React.FC<{ api?: string; headers?: Record<string, string> }> = (props) => {
+async function reportVizQuery(api: string | ((data: IAskVizFeedback) => void), data: IAskVizFeedback, headers: Record<string, string>) {
+ if (typeof api === 'function') {
+ return api(data);
+ }
+ const res = await fetch(api, {
+ headers: {
+ 'Content-Type': 'application/json',
+ ...headers,
+ },
+ credentials: 'include',
+ method: 'POST',
+ body: JSON.stringify(data),
+ });
+ const result = await res.json();
+ if (result.success) {
+ return;
+ } else {
+ throw new Error(result.message);
+ }
+}
+
+const AskViz: React.FC<{
+ api?: string | ((metas: IViewField[], query: string) => PromiseLike<IVisSpecForExport | IChartForExport> | IVisSpecForExport | IChartForExport);
+ feedbackApi?: string | ((data: IAskVizFeedback) => void);
+ headers?: Record<string, string>;
+}> = (props) => {
+ const { feedbackApi } = props;
+
const [query, setQuery] = useState<string>('');
const [loading, setLoading] = useState<boolean>(false);
const vizStore = useVizStore();
- const {t} = useTranslation();
+ const { t } = useTranslation();
+ const [askVizFeedback, setAskVizFeedback] = useState<'vote' | 'report' | 'none'>('none');
const allFields = vizStore.allFields;
+ const { reportError } = useReporter();
+
+ const [lastData, setLastData] = useState<{ question: string; data: string } | null>(null);
+
const startQuery = useCallback(() => {
setLoading(true);
- vizQuery(props.api ?? api, allFields, query, props.headers ?? {})
+ const request =
+ typeof props.api === 'function' ? Promise.resolve(props.api(allFields, query)) : vizQuery(props.api || api, allFields, query, props.headers ?? {});
+ request
.then((data) => {
- vizStore.appendFromOld(data);
+ vizStore.appendFromCode(data);
+ vizStore.setAskvizFeedback(true);
+ setLastData({ question: query, data: JSON.stringify(data) });
+ setAskVizFeedback('vote');
+ })
+ .catch((err) => {
+ reportError(parseErrorMessage(err), 502);
})
.finally(() => {
setLoading(false);
});
}, [props.api, props.headers, allFields, query, vizStore]);
+
+ const showFeedback = feedbackApi && lastData && vizStore.showAskvizFeedbackIndex === vizStore.visIndex;
+
return (
- <div className="right-0 flex">
+ <div className="right-0 flex relative">
<input
type="text"
className="rounded-l-md px-4 block w-full border-0 py-1.5 text-gray-900 dark:text-gray-50 shadow-sm ring-1 ring-inset ring-gray-300 dark:ring-gray-600 placeholder:text-gray-400 focus:ring-1 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6 dark:bg-gray-900"
- placeholder={t("main.tabpanel.askviz.placeholder")}
+ placeholder={t('main.tabpanel.askviz.placeholder')}
value={query}
onChange={(e) => setQuery(e.target.value)}
onKeyDown={(e) => {
@@ -83,6 +126,57 @@ const AskViz: React.FC<{ api?: string; headers?: Record<string, string> }> = (pr
{!loading && <PaperAirplaneIcon className="w-4 ml-1" />}
{loading && <Spinner />}
</button>
+ {showFeedback && askVizFeedback === 'vote' && (
+ <div className="absolute z-10 top-full right-0 flex-col space-y-2 w-56 mt-1 p-4 border border-gray-200 dark:border-gray-700 rounded bg-white dark:bg-zinc-900 dark:text-white">
+ <div>{t('App.feedback.vote')}</div>
+ <div className="flex space-x-2">
+ <button
+ type="button"
+ className="inline-flex space-x-1 items-center rounded border border-transparent bg-indigo-600 px-2.5 py-1.5 text-xs font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
+ onClick={() => {
+ reportVizQuery(feedbackApi, { action: 'voteup', question: lastData.question, spec: lastData.data }, props.headers ?? {});
+ setAskVizFeedback('none');
+ }}
+ >
+ <HandThumbUpIcon className="w-4 h-4" />
+ {t('App.feedback.voteup')}
+ </button>
+ <button
+ type="button"
+ className="inline-flex items-center rounded border border-gray-300 bg-white dark:bg-zinc-900 px-2.5 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-200 shadow-sm hover:bg-gray-50 dark:hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 disabled:opacity-50"
+ onClick={() => {
+ reportVizQuery(feedbackApi, { action: 'votedown', question: lastData.question, spec: lastData.data }, props.headers ?? {});
+ setAskVizFeedback('report');
+ }}
+ >
+ <HandThumbDownIcon className="w-4 h-4" />
+ {t('App.feedback.votedown')}
+ </button>
+ </div>
+ </div>
+ )}
+ {showFeedback && askVizFeedback === 'report' && (
+ <div className="absolute z-10 top-full right-0 flex-col space-y-2 w-56 mt-1 p-4 border border-gray-200 dark:border-gray-700 rounded bg-white dark:bg-zinc-900 dark:text-white">
+ <div>{t('App.feedback.report')}</div>
+ <div>
+ <button
+ type="button"
+ className="inline-flex items-center rounded border border-gray-300 bg-white dark:bg-zinc-900 px-2.5 py-1.5 text-xs font-medium text-gray-700 dark:text-gray-200 shadow-sm hover:bg-gray-50 dark:hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 disabled:opacity-50"
+ disabled={!vizStore.canUndo}
+ onClick={() => {
+ reportVizQuery(
+ feedbackApi,
+ { action: 'report', question: lastData.question, spec: JSON.stringify(vizStore.exportCode()[vizStore.visIndex]) },
+ props.headers ?? {}
+ );
+ setAskVizFeedback('none');
+ }}
+ >
+ {t('App.feedback.report_button')}
+ </button>
+ </div>
+ </div>
+ )}
</div>
);
};
diff --git a/packages/graphic-walker/src/components/errorpanel/index.tsx b/packages/graphic-walker/src/components/errorpanel/index.tsx
index 5a00e1d0..35995d6b 100644
--- a/packages/graphic-walker/src/components/errorpanel/index.tsx
+++ b/packages/graphic-walker/src/components/errorpanel/index.tsx
@@ -66,6 +66,26 @@ export default observer(function ErrorPanel() {
<p className="font-normal my-2">There is some error with Computation service. Here is the Error message:</p>
<p className="font-normal my-2">{vizStore.lastErrorMessage}</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={closeModal}
+ />
+ </div>
+ </fieldset>
+ </div>
+ </Modal>
+ );
+ case 502:
+ 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">There is some error with Askviz service. Here is the Error message:</p>
+ <p className="font-normal my-2">{vizStore.lastErrorMessage}</p>
+
<fieldset className="mt-2 gap-1 flex flex-col justify-center items-end w-full">
<div className="mt-2">
<DefaultButton
diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.ts
index 49ed3966..5b01399d 100644
--- a/packages/graphic-walker/src/interfaces.ts
+++ b/packages/graphic-walker/src/interfaces.ts
@@ -796,7 +796,11 @@ export interface IVizProps {
enhanceAPI?: {
header?: Record<string, string>;
features?: {
- askviz?: string | boolean;
+ askviz?:
+ | string
+ | boolean
+ | ((metas: IViewField[], query: string) => PromiseLike<IVisSpecForExport | IChartForExport> | IVisSpecForExport | IChartForExport);
+ feedbackAskviz?: string | boolean | ((data: IAskVizFeedback) => void);
};
};
geoList?: IGeoDataItem[];
@@ -851,3 +855,9 @@ export interface ITableSpecProps {
export type IVizAppProps = IAppI18nProps & IVizProps & IThemeProps & IErrorHandlerProps & IVizStoreProps & IComputationProps & ISpecProps;
export type ITableProps = IAppI18nProps & IThemeProps & IErrorHandlerProps & IVizStoreProps & IComputationProps & ITableSpecProps;
+
+export interface IAskVizFeedback {
+ action: 'voteup' | 'votedown' | 'report';
+ question: string;
+ spec: string;
+}
diff --git a/packages/graphic-walker/src/locales/en-US.json b/packages/graphic-walker/src/locales/en-US.json
index 438ec732..a0a31110 100644
--- a/packages/graphic-walker/src/locales/en-US.json
+++ b/packages/graphic-walker/src/locales/en-US.json
@@ -131,6 +131,13 @@
"segments": {
"vis": "Visualization",
"data": "Data"
+ },
+ "feedback": {
+ "vote": "Is the result of Askviz good?",
+ "voteup": "Yes",
+ "votedown": "No",
+ "report": "Please help us improving Askviz with correct Chart",
+ "report_button": "Report correct chart"
}
},
"DataSource": {
diff --git a/packages/graphic-walker/src/locales/ja-JP.json b/packages/graphic-walker/src/locales/ja-JP.json
index b749f111..21eba0c4 100644
--- a/packages/graphic-walker/src/locales/ja-JP.json
+++ b/packages/graphic-walker/src/locales/ja-JP.json
@@ -125,6 +125,13 @@
"segments": {
"vis": "可視化",
"data": "データ"
+ },
+ "feedback": {
+ "vote": "Askvizの結果はどうですか?",
+ "voteup": "いいね",
+ "votedown": "ダメかも",
+ "report": "Askvizを改善することに手伝って、正しいチャットを教えてもよろしいですか?",
+ "report_button": "フィードバック"
}
},
"DataSource": {
diff --git a/packages/graphic-walker/src/locales/zh-CN.json b/packages/graphic-walker/src/locales/zh-CN.json
index 3744347f..2fbea0f2 100644
--- a/packages/graphic-walker/src/locales/zh-CN.json
+++ b/packages/graphic-walker/src/locales/zh-CN.json
@@ -131,6 +131,13 @@
"segments": {
"vis": "可视化",
"data": "数据"
+ },
+ "feedback": {
+ "vote": "对Askviz的结果满意吗?",
+ "voteup": "满意",
+ "votedown": "不满意",
+ "report": "为了改进AskViz,请将正确的图表反馈给我们",
+ "report_button": "反馈"
}
},
"DataSource": {
diff --git a/packages/graphic-walker/src/store/dataStore.ts b/packages/graphic-walker/src/store/dataStore.ts
index 5a5b8511..fab6e77c 100644
--- a/packages/graphic-walker/src/store/dataStore.ts
+++ b/packages/graphic-walker/src/store/dataStore.ts
@@ -121,7 +121,7 @@ export class DataStore {
data.specList.forEach((x) => {
const key = encodeMeta(x.encodings.dimensions.concat(x.encodings.measures).filter((x) => !x.computed));
const store = visDict[key] || visDict[defaultId];
- store.appendFromOld(x);
+ store.appendFromCode(x);
});
}
this.metaDict = metaDict;
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index 4e125aac..a1f13055 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -94,7 +94,8 @@ export class VizSpecStore {
createField: ICreateField | undefined = undefined;
localGeoJSON: FeatureCollection | undefined = undefined;
showErrorResolutionPanel: number = 0;
- lastErrorMessage: string = "";
+ lastErrorMessage: string = '';
+ showAskvizFeedbackIndex: number | undefined = 0;
private onMetaChange?: (fid: string, diffMeta: Partial<IMutField>) => void;
@@ -350,7 +351,7 @@ export class VizSpecStore {
destinationKey,
destinationIndex,
uniqueId(),
- limit,
+ limit
);
}
}
@@ -463,13 +464,17 @@ export class VizSpecStore {
this.visIndex = 0;
}
- appendFromOld(data: IVisSpecForExport) {
- const newChart = fromSnapshot(convertChart(visSpecDecoder(forwardVisualConfigs(data))));
+ appendFromCode(data: IVisSpecForExport | IChartForExport) {
+ const newChart = 'layout' in data ? importNow(data) : fromSnapshot(convertChart(visSpecDecoder(forwardVisualConfigs(data))));
this.visList.push(newChart);
this.createdVis += 1;
this.visIndex = this.visList.length - 1;
}
+ setAskvizFeedback(show: boolean) {
+ this.showAskvizFeedbackIndex = show ? this.visIndex : undefined;
+ }
+
replaceNow(chart: IChart) {
this.visList[this.visIndex] = fromSnapshot(chart);
}
@@ -592,7 +597,7 @@ export class VizSpecStore {
this.selectedMarkObject = newMarkObj;
}
- updateShowErrorResolutionPanel(errCode: number, msg = "") {
+ updateShowErrorResolutionPanel(errCode: number, msg = '') {
this.showErrorResolutionPanel = errCode;
this.lastErrorMessage = msg;
}
diff --git a/packages/graphic-walker/src/utils/reportError.ts b/packages/graphic-walker/src/utils/reportError.ts
index 95364fa8..de82bce7 100644
--- a/packages/graphic-walker/src/utils/reportError.ts
+++ b/packages/graphic-walker/src/utils/reportError.ts
@@ -3,6 +3,7 @@ import React from 'react';
export const Errors = {
canvasExceedSize: 500,
computationError: 501,
+ askVizError: 502
} as const;
export const errorContext = React.createContext({