未验证 提交 381d8b80 编写于 作者: P Peter Pan 提交者: GitHub

feat: restore selected model when navigating back to graph page (#789)

上级 767c6d30
...@@ -4,10 +4,12 @@ import type {Dispatch} from 'react'; ...@@ -4,10 +4,12 @@ import type {Dispatch} from 'react';
export interface GlobalState { export interface GlobalState {
runs: string[]; runs: string[];
model: FileList | File[] | null;
} }
export const globalState: GlobalState = { export const globalState: GlobalState = {
runs: [] runs: [],
model: null
}; };
export const GlobalStateContext = createContext<GlobalState>(globalState); export const GlobalStateContext = createContext<GlobalState>(globalState);
......
...@@ -21,7 +21,7 @@ function useRequest<D = unknown, E = unknown>( ...@@ -21,7 +21,7 @@ function useRequest<D = unknown, E = unknown>(
config?: ConfigInterface<D, E, fetcherFn<D>> config?: ConfigInterface<D, E, fetcherFn<D>>
): Response<D, E> { ): Response<D, E> {
const {data, error, ...other} = useSWR<D, E>(key, fetcher, config); const {data, error, ...other} = useSWR<D, E>(key, fetcher, config);
const loading = useMemo(() => !data && !error, [data, error]); const loading = useMemo(() => !!key && !data && !error, [key, data, error]);
return {data, error, loading, ...other}; return {data, error, loading, ...other};
} }
......
...@@ -20,6 +20,7 @@ import Search from '~/components/GraphPage/Search'; ...@@ -20,6 +20,7 @@ import Search from '~/components/GraphPage/Search';
import Title from '~/components/Title'; import Title from '~/components/Title';
import Uploader from '~/components/GraphPage/Uploader'; import Uploader from '~/components/GraphPage/Uploader';
import styled from 'styled-components'; import styled from 'styled-components';
import useGlobalState from '~/hooks/useGlobalState';
import useRequest from '~/hooks/useRequest'; import useRequest from '~/hooks/useRequest';
import {useTranslation} from 'react-i18next'; import {useTranslation} from 'react-i18next';
...@@ -70,23 +71,30 @@ const Loading = styled.div` ...@@ -70,23 +71,30 @@ const Loading = styled.div`
const Graph: FunctionComponent = () => { const Graph: FunctionComponent = () => {
const {t} = useTranslation(['graph', 'common']); const {t} = useTranslation(['graph', 'common']);
const {data, loading} = useRequest<BlobResponse>('/graph/graph', blobFetcher); const [globalState, globalDispatch] = useGlobalState();
const graph = useRef<GraphRef>(null); const graph = useRef<GraphRef>(null);
const file = useRef<HTMLInputElement>(null); const file = useRef<HTMLInputElement>(null);
const [files, setFiles] = useState<FileList | File[] | null>(null); const [files, setFiles] = useState<FileList | File[] | null>(globalState.model);
const onClickFile = useCallback(() => { const onClickFile = useCallback(() => {
if (file.current) { if (file.current) {
file.current.value = ''; file.current.value = '';
file.current.click(); file.current.click();
} }
}, []); }, []);
const onChangeFile = useCallback((e: React.ChangeEvent<HTMLInputElement>) => { const onChangeFile = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
const target = e.target; const target = e.target;
if (target && target.files && target.files.length) { if (target && target.files && target.files.length) {
globalDispatch({model: target.files});
setFiles(target.files); setFiles(target.files);
} }
}, []); },
[globalDispatch]
);
const {data, loading} = useRequest<BlobResponse>(files ? null : '/graph/graph', blobFetcher);
useEffect(() => { useEffect(() => {
if (data?.data.size) { if (data?.data.size) {
setFiles([new File([data.data], data.filename || 'unknwon_model')]); setFiles([new File([data.data], data.filename || 'unknwon_model')]);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册