Skip to content

Commit

Permalink
fix bugs from Kristy and improve interaction (#809)
Browse files Browse the repository at this point in the history
* feat: split page runs in global states

* fix: bring loading back when switching to a visited page

* fix: scroll tooltips in scalar & pr-curve to prevent content overflow
  • Loading branch information
PeterPanZH authored Sep 11, 2020
1 parent 507627e commit b7726fd
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 13 deletions.
18 changes: 17 additions & 1 deletion frontend/packages/core/src/components/GlobalState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,24 @@ interface GlobalDispatch {
(state: GlobalStateType, newState: Partial<GlobalStateType>): GlobalStateType;
}

// TODO: use redux
const GlobalState: FunctionComponent = ({children}) => {
const [state, dispatch] = useReducer<GlobalDispatch>((state, newState) => ({...state, ...newState}), globalState);
const [state, dispatch] = useReducer<GlobalDispatch>(
(state, newState) =>
Object.entries(newState).reduce(
(m, [key, value]) => {
if (m.hasOwnProperty(key)) {
m[key] = {...m[key], ...value};
} else {
m[key] = value;
}
return m;
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{...state} as any
),
globalState
);

return (
<GlobalStateContext.Provider value={state}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ const PRCurveChart: FunctionComponent<PRCurveChartProps> = ({cid, runs, tag, run
...chartOptions,
tooltip: {
...chartOptions.tooltip,
formatter
formatter,
hideDelay: 300,
enterable: true
}
}),
[formatter]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ const ScalarChart: FunctionComponent<ScalarChartProps> = ({
...chartOptions,
tooltip: {
...chartOptions.tooltip,
formatter
formatter,
hideDelay: 300,
enterable: true
},
xAxis: {
type: xAxisType,
Expand Down
4 changes: 4 additions & 0 deletions frontend/packages/core/src/components/TooltipTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type {Run} from '~/types';
import styled from 'styled-components';

const Wrapper = styled.div`
max-height: ${rem(160)};
overflow: hidden auto;
overscroll-behavior: auto contain;
table {
border-spacing: none;
text-align: left;
Expand Down
9 changes: 7 additions & 2 deletions frontend/packages/core/src/hooks/useECharts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const useECharts = <T extends HTMLElement, W extends HTMLElement = HTMLDivElemen
const onInit = useRef(options.onInit);
const onDispose = useRef(options.onDispose);

const hideTip = useCallback(() => echartInstance.current?.dispatchAction({type: 'hideTip'}), []);

const createChart = useCallback(() => {
(async () => {
const {default: echarts} = await import('echarts');
Expand All @@ -41,6 +43,8 @@ const useECharts = <T extends HTMLElement, W extends HTMLElement = HTMLDivElemen
}
echartInstance.current = echarts.init((ref.current as unknown) as HTMLDivElement);

ref.current.addEventListener('mouseleave', hideTip);

setTimeout(() => {
if (options.zoom) {
echartInstance.current?.dispatchAction({
Expand All @@ -57,15 +61,16 @@ const useECharts = <T extends HTMLElement, W extends HTMLElement = HTMLDivElemen

setEchart(echartInstance.current);
})();
}, [options.gl, options.zoom]);
}, [options.gl, options.zoom, hideTip]);

const destroyChart = useCallback(() => {
if (echartInstance.current) {
onDispose.current?.(echartInstance.current);
}
echartInstance.current?.dispose();
ref.current?.removeEventListener('mouseleave', hideTip);
setEchart(null);
}, []);
}, [hideTip]);

useEffect(() => {
createChart();
Expand Down
40 changes: 36 additions & 4 deletions frontend/packages/core/src/hooks/useGlobalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,45 @@ import {createContext, useContext} from 'react';
import type {Dispatch} from 'react';

export interface GlobalState {
runs: string[];
model: FileList | File[] | null;
scalar: {
runs: string[];
};
histogram: {
runs: string[];
};
image: {
runs: string[];
};
audio: {
runs: string[];
};
prCurve: {
runs: string[];
};
graph: {
model: FileList | File[] | null;
};
}

export const globalState: GlobalState = {
runs: [],
model: null
scalar: {
runs: []
},
histogram: {
runs: []
},
image: {
runs: []
},
audio: {
runs: []
},
prCurve: {
runs: []
},
graph: {
model: null
}
};

export const GlobalStateContext = createContext<GlobalState>(globalState);
Expand Down
23 changes: 21 additions & 2 deletions frontend/packages/core/src/hooks/useTagFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type {Run, Tag, TagWithSingleRun, TagsData} from '~/types';
import {color, colorAlt} from '~/utils/chart';
import {useCallback, useEffect, useMemo, useReducer} from 'react';

import {cache} from 'swr';
import camelCase from 'lodash/camelCase';
import groupBy from 'lodash/groupBy';
import intersection from 'lodash/intersection';
import intersectionBy from 'lodash/intersectionBy';
Expand Down Expand Up @@ -163,7 +165,16 @@ const useTagFilter = (type: string, running: boolean) => {

const {data, loading, error} = useRunningRequest<TagsData>(`/${type}/tags`, running);

// clear cache in order to fully reload data when switching page
useEffect(() => () => cache.delete(`/${type}/tags`), [type]);

const pageName = useMemo(() => camelCase(type), [type]);

const [globalState, globalDispatch] = useGlobalState();
const storedRuns = useMemo(
() => ((globalState as unknown) as Record<string, {runs: string[]}>)[camelCase(pageName)]?.runs ?? [],
[pageName, globalState]
);

const runs: string[] = useMemo(() => data?.runs ?? [], [data]);
const tags: Tags = useMemo(
Expand All @@ -183,7 +194,7 @@ const useTagFilter = (type: string, running: boolean) => {

const [state, dispatch] = useReducer(reducer, {
initRuns: [],
globalRuns: globalState.runs,
globalRuns: storedRuns,
runs: [],
selectedRuns: [],
initTags: {},
Expand Down Expand Up @@ -211,7 +222,15 @@ const useTagFilter = (type: string, running: boolean) => {
});
}
}, [queryRuns, state.runs]);
useEffect(() => globalDispatch({runs: state.globalRuns}), [state.globalRuns, globalDispatch]);
useEffect(
() =>
globalDispatch({
[pageName]: {
runs: state.globalRuns
}
}),
[pageName, state.globalRuns, globalDispatch]
);

const tagsWithSingleRun = useMemo(
() =>
Expand Down
4 changes: 2 additions & 2 deletions frontend/packages/core/src/pages/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const Graph: FunctionComponent = () => {

const graph = useRef<GraphRef>(null);
const file = useRef<HTMLInputElement>(null);
const [files, setFiles] = useState<FileList | File[] | null>(globalState.model);
const [files, setFiles] = useState<FileList | File[] | null>(globalState.graph.model);
const onClickFile = useCallback(() => {
if (file.current) {
file.current.value = '';
Expand All @@ -86,7 +86,7 @@ const Graph: FunctionComponent = () => {
(e: React.ChangeEvent<HTMLInputElement>) => {
const target = e.target;
if (target && target.files && target.files.length) {
globalDispatch({model: target.files});
globalDispatch({graph: {model: target.files}});
setFiles(target.files);
}
},
Expand Down

0 comments on commit b7726fd

Please sign in to comment.