Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: reduce freq of setVisualConfigs #138

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/graphic-walker/src/components/limitSetting.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useDebounceValueBind } from '../hooks';

export default function LimitSetting(props: { value: number; setValue: (v: number) => void }) {
const { t } = useTranslation('translation', { keyPrefix: 'main.tabpanel.settings' });
const [innerValue, setInnerValue] = useDebounceValueBind(props.value, v => props.setValue(v));

return (
<div className=" mt-2">
<input
className="w-full h-2 bg-blue-100 appearance-none"
type="range"
name="limit"
value={props.value > 0 ? props.value : 0}
value={innerValue > 0 ? innerValue : 0}
min="1"
max="50"
disabled={props.value < 0}
disabled={innerValue < 0}
step="1"
onChange={(e) => {
const v = parseInt(e.target.value);
if (!isNaN(v)) {
props.setValue(v);
setInnerValue(v);
}
}}
/>
<output className="text-sm ml-1" htmlFor="height">
<input
type="checkbox"
className="h-4 w-4 mr-1 rounded border-gray-300 text-indigo-600 focus:ring-indigo-600"
checked={props.value > 0}
checked={innerValue > 0}
onChange={(e) => {
props.setValue(e.target.checked ? 30 : -1);
setInnerValue(e.target.checked ? 30 : -1);
}}
></input>
{`${t('limit')}${props.value > 0 ? `: ${props.value}` : ''}`}
{`${t('limit')}${innerValue > 0 ? `: ${innerValue}` : ''}`}
</output>
</div>
);
Expand Down
16 changes: 9 additions & 7 deletions packages/graphic-walker/src/components/sizeSetting.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ArrowsPointingOutIcon, XMarkIcon } from "@heroicons/react/24/outline";
import React, { useState, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useDebounceValueBind } from "../hooks";

interface SizeSettingProps {
onWidthChange: (val: number) => void;
Expand All @@ -12,7 +13,8 @@ interface SizeSettingProps {
export const ResizeDialog: React.FC<SizeSettingProps> = (props) => {
const { onWidthChange, onHeightChange, width, height, children } = props;
const { t } = useTranslation("translation", { keyPrefix: "main.tabpanel.settings.size_setting" });

const [innerWidth, setInnerWidth] = useDebounceValueBind(width, onWidthChange);
const [innerHeight, setInnerHeight] = useDebounceValueBind(height, onHeightChange);
return (
<div className="text-zinc-400">
{children}
Expand All @@ -22,16 +24,16 @@ export const ResizeDialog: React.FC<SizeSettingProps> = (props) => {
style={{ cursor: "ew-resize" }}
type="range"
name="width"
value={Math.sqrt(width / 1000)}
value={Math.sqrt(innerWidth / 1000)}
min="0"
max="1"
step="0.01"
onChange={(e) => {
onWidthChange(Math.round(Number(e.target.value) ** 2 * 1000));
setInnerWidth(Math.round(Number(e.target.value) ** 2 * 1000));
}}
/>
<output className="text-sm ml-1" htmlFor="width">
{`${t("width")}: ${width}`}
{`${t("width")}: ${innerWidth}`}
</output>
</div>
<div className=" mt-2">
Expand All @@ -40,16 +42,16 @@ export const ResizeDialog: React.FC<SizeSettingProps> = (props) => {
style={{ cursor: "ew-resize" }}
type="range"
name="height"
value={Math.sqrt(height / 1000)}
value={Math.sqrt(innerHeight / 1000)}
min="0"
max="1"
step="0.01"
onChange={(e) => {
onHeightChange(Math.round(Number(e.target.value) ** 2 * 1000));
setInnerHeight(Math.round(Number(e.target.value) ** 2 * 1000));
}}
/>
<output className="text-sm ml-1" htmlFor="height">
{`${t("height")}: ${height}`}
{`${t("height")}: ${innerHeight}`}
</output>
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions packages/graphic-walker/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ export function useDebounceValue<T>(value: T, timeout = 200): T {
}, [value]);
return innerValue;
}

export function useDebounceValueBind<T>(value: T, setter: (v: T) => void, timeout = 200): [T, (v: T) => void] {
const [innerValue, setInnerValue] = useState(value);
const valueToSet = useDebounceValue(innerValue, timeout);
useEffect(() => setInnerValue(value), [value])
useEffect(() => setter(valueToSet), [valueToSet]);
return [innerValue, setInnerValue];
}
48 changes: 1 addition & 47 deletions packages/graphic-walker/src/renderer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import type { DeepReadonly, IFilterField, IRow, IViewField, IDataQueryWorkflowSt
import { useGlobalStore } from '../store';
import { useAppRootContext } from '../components/appRoot';
import { toWorkflow } from '../utils/workflow';
import { dataQueryClient } from '../computation/clientComputation';
import { dataQueryServer } from '../computation/serverComputation';
import { useDebounceValue } from '../hooks';

export const useComputationFunc = (): IComputationFunction => {
const { vizStore } = useGlobalStore();
Expand Down Expand Up @@ -40,14 +38,12 @@ export const useRenderer = (props: UseRendererProps): UseRendererResult => {
filters,
defaultAggregated,
sort,
limit: storeLimit,
limit,
computationFunction,
} = props;
const [computing, setComputing] = useState(false);
const taskIdRef = useRef(0);

const limit = useDebounceValue(storeLimit);

const workflow = useMemo(() => {
return toWorkflow(
filters,
Expand All @@ -65,48 +61,6 @@ export const useRenderer = (props: UseRendererProps): UseRendererResult => {

const appRef = useAppRootContext();

// useEffect(() => {
// if (computationMode !== 'client') {
// return;
// }
// if (!data) {
// console.warn('useRenderer error: prop `data` is required for "client" mode, but none is found.');
// return;
// }
// if (!allFields) {
// console.warn('useRenderer error: prop `fields` is required for "client" mode, but none is found.');
// return;
// }
// const taskId = ++taskIdRef.current;
// appRef.current?.updateRenderStatus('computing');
// setComputing(true);
// dataQueryClient(data, allFields, workflow).then(data => {
// if (taskId !== taskIdRef.current) {
// return;
// }
// appRef.current?.updateRenderStatus('rendering');
// unstable_batchedUpdates(() => {
// setComputing(false);
// setViewData(data);
// setParsedWorkflow(workflow);
// });
// }).catch((err) => {
// if (taskId !== taskIdRef.current) {
// return;
// }
// appRef.current?.updateRenderStatus('error');
// console.error(err);
// unstable_batchedUpdates(() => {
// setComputing(false);
// setViewData([]);
// setParsedWorkflow([]);
// });
// });
// return () => {
// taskIdRef.current++;
// };
// }, [computationMode, data, allFields, workflow]);

useEffect(() => {
const taskId = ++taskIdRef.current;
appRef.current?.updateRenderStatus('computing');
Expand Down
2 changes: 1 addition & 1 deletion packages/graphic-walker/src/store/visualSpecStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IReactionDisposer, makeAutoObservable, observable, reaction, toJS } from 'mobx';
import { IReactionDisposer, makeAutoObservable, observable, computed, reaction, toJS } from 'mobx';
import produce from 'immer';
import {
DataSet,
Expand Down
1 change: 0 additions & 1 deletion packages/graphic-walker/src/visualSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import throttle from '../utils/throttle';
import KanariesLogo from '../assets/kanaries.png';
import { ImageWithFallback } from '../components/timeoutImg';
import LimitSetting from '../components/limitSetting';
import { runInAction } from 'mobx';

const Invisible = styled.div`
clip: rect(1px, 1px, 1px, 1px);
Expand Down