Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 5, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import type {Dispatch} from 'react';

export interface GlobalState {
runs: string[];
model: FileList | File[] | null;
}

export const globalState: GlobalState = {
runs: []
runs: [],
model: null
};

export const GlobalStateContext = createContext<GlobalState>(globalState);
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/core/src/hooks/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function useRequest<D = unknown, E = unknown>(
config?: ConfigInterface<D, E, fetcherFn<D>>
): Response<D, E> {
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};
}

Expand Down
24 changes: 16 additions & 8 deletions frontend/packages/core/src/pages/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Search from '~/components/GraphPage/Search';
import Title from '~/components/Title';
import Uploader from '~/components/GraphPage/Uploader';
import styled from 'styled-components';
import useGlobalState from '~/hooks/useGlobalState';
import useRequest from '~/hooks/useRequest';
import {useTranslation} from 'react-i18next';

Expand Down Expand Up @@ -70,23 +71,30 @@ const Loading = styled.div`
const Graph: FunctionComponent = () => {
const {t} = useTranslation(['graph', 'common']);

const {data, loading} = useRequest<BlobResponse>('/graph/graph', blobFetcher);
const [globalState, globalDispatch] = useGlobalState();

const graph = useRef<GraphRef>(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(() => {
if (file.current) {
file.current.value = '';
file.current.click();
}
}, []);
const onChangeFile = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const target = e.target;
if (target && target.files && target.files.length) {
setFiles(target.files);
}
}, []);
const onChangeFile = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
const target = e.target;
if (target && target.files && target.files.length) {
globalDispatch({model: target.files});
setFiles(target.files);
}
},
[globalDispatch]
);

const {data, loading} = useRequest<BlobResponse>(files ? null : '/graph/graph', blobFetcher);

useEffect(() => {
if (data?.data.size) {
setFiles([new File([data.data], data.filename || 'unknwon_model')]);
Expand Down