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

Add visual feedback when query is running #1454

Merged
merged 1 commit into from
Dec 15, 2022
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
19 changes: 19 additions & 0 deletions packages/toolpad-app/src/toolpadDataSources/QueryPreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { LinearProgress } from '@mui/material';
import { Box } from '@mui/system';
import * as React from 'react';
import ErrorAlert from '../toolpad/AppEditor/PageEditor/ErrorAlert';

export interface QueryPreviewProps {
children?: React.ReactNode;
error?: unknown;
isLoading?: boolean;
}

export default function QueryPreview({ children, error, isLoading }: QueryPreviewProps) {
return (
<Box sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
{isLoading ? <LinearProgress /> : null}
<Box sx={{ flex: 1, minHeight: 0 }}>{error ? <ErrorAlert error={error} /> : children}</Box>
</Box>
);
}
30 changes: 13 additions & 17 deletions packages/toolpad-app/src/toolpadDataSources/function/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import lazyComponent from '../../utils/lazyComponent';
import ParametersEditor from '../../toolpad/AppEditor/PageEditor/ParametersEditor';
import SplitPane from '../../components/SplitPane';
import { usePrivateQuery } from '../context';
import JsonView from '../../components/JsonView';
import ErrorAlert from '../../toolpad/AppEditor/PageEditor/ErrorAlert';
import { LogEntry } from '../../components/Console';
import MapEntriesEditor from '../../components/MapEntriesEditor';
import { Maybe } from '../../utils/types';
Expand All @@ -46,6 +44,8 @@ import { MOVIES_API_DEMO_URL } from '../demo';
import * as appDom from '../../appDom';
import { clientExec } from './runtime';
import config from '../../config';
import QueryPreview from '../QueryPreview';
import JsonView from '../../components/JsonView';

const EVENT_INTERFACE_IDENTIFIER = 'ToolpadFunctionEvent';

Expand Down Expand Up @@ -163,17 +163,16 @@ function QueryEditor({

const [previewLogs, setPreviewLogs] = React.useState<LogEntry[]>([]);
const [previewHar, setPreviewHar] = React.useState(() => createHarLog());
const { preview, runPreview: handleRunPreview } = useQueryPreview(
fetchPreview,
input.attributes.query.value,
previewParams,
{
onPreview(result) {
setPreviewLogs((existing) => [...existing, ...result.logs]);
setPreviewHar((existing) => mergeHar(createHarLog(), existing, result.har));
},
const {
preview,
runPreview: handleRunPreview,
isLoading: previewIsLoading,
} = useQueryPreview(fetchPreview, input.attributes.query.value, previewParams, {
onPreview(result) {
setPreviewLogs((existing) => [...existing, ...result.logs]);
setPreviewHar((existing) => mergeHar(createHarLog(), existing, result.har));
},
);
});

const { data: secretsKeys = [] } = usePrivateQuery<FunctionPrivateQuery, string[]>({
kind: 'secretsKeys',
Expand Down Expand Up @@ -254,12 +253,9 @@ function QueryEditor({
</SplitPane>

<SplitPane split="horizontal" size="30%" minSize={30} primary="second" allowResize>
{preview?.error ? (
<ErrorAlert error={preview?.error} />
) : (
<QueryPreview isLoading={previewIsLoading} error={preview?.error}>
<JsonView sx={{ height: '100%' }} copyToClipboard src={preview?.data} />
)}

</QueryPreview>
<Devtools
sx={{ width: '100%', height: '100%' }}
log={previewLogs}
Expand Down
24 changes: 13 additions & 11 deletions packages/toolpad-app/src/toolpadDataSources/googleSheets/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
} from './types';
import useDebounced from '../../utils/useDebounced';
import { usePrivateQuery } from '../context';
import ErrorAlert from '../../toolpad/AppEditor/PageEditor/ErrorAlert';
import QueryInputPanel from '../QueryInputPanel';
import SplitPane from '../../components/SplitPane';
import useQueryPreview from '../useQueryPreview';
Expand Down Expand Up @@ -128,11 +127,11 @@ function QueryEditor({
[fetchPrivate],
);

const { preview, runPreview: handleRunPreview } = useQueryPreview(
fetchServerPreview,
input.attributes.query.value,
{},
);
const {
preview,
runPreview: handleRunPreview,
isLoading: isPreviewLoading,
} = useQueryPreview(fetchServerPreview, input.attributes.query.value, {});

const rawRows: any[] = preview?.data || EMPTY_ROWS;
const columns: GridColDef[] = React.useMemo(() => parseColumns(inferColumns(rawRows)), [rawRows]);
Expand Down Expand Up @@ -201,11 +200,14 @@ function QueryEditor({
</Stack>
</QueryInputPanel>

{preview?.error ? (
<ErrorAlert error={preview?.error} />
) : (
<DataGridPro sx={{ border: 'none' }} columns={columns} key={previewGridKey} rows={rows} />
)}
<DataGridPro
sx={{ border: 'none' }}
columns={columns}
key={previewGridKey}
rows={rows}
error={preview?.error}
loading={isPreviewLoading}
/>
</SplitPane>
);
}
Expand Down
24 changes: 13 additions & 11 deletions packages/toolpad-app/src/toolpadDataSources/postgres/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import SyncIcon from '@mui/icons-material/Sync';
import { getObjectKey } from '@mui/toolpad-core/objectKey';
import { BindableAttrEntries, BindableAttrValue } from '@mui/toolpad-core';
import SplitPane from '../../components/SplitPane';
import ErrorAlert from '../../toolpad/AppEditor/PageEditor/ErrorAlert';
import ParametersEditor from '../../toolpad/AppEditor/PageEditor/ParametersEditor';
import { useEvaluateLiveBindingEntries } from '../../toolpad/AppEditor/useEvaluateLiveBinding';
import { ClientDataSource, ConnectionEditorProps, QueryEditorProps } from '../../types';
Expand Down Expand Up @@ -190,11 +189,11 @@ function QueryEditor({
[fetchPrivate],
);

const { preview, runPreview: handleRunPreview } = useQueryPreview(
fetchServerPreview,
input.attributes.query.value,
previewParams,
);
const {
preview,
runPreview: handleRunPreview,
isLoading: previewIsLoading,
} = useQueryPreview(fetchServerPreview, input.attributes.query.value, previewParams);

const rawRows: any[] = preview?.data || EMPTY_ROWS;
const columns: GridColDef[] = React.useMemo(() => parseColumns(inferColumns(rawRows)), [rawRows]);
Expand Down Expand Up @@ -227,11 +226,14 @@ function QueryEditor({
</Box>
</SplitPane>

{preview?.error ? (
<ErrorAlert error={preview?.error} />
) : (
<DataGridPro sx={{ border: 'none' }} columns={columns} key={previewGridKey} rows={rows} />
)}
<DataGridPro
sx={{ border: 'none' }}
columns={columns}
key={previewGridKey}
rows={rows}
error={preview?.error}
loading={previewIsLoading}
/>
</SplitPane>
);
}
Expand Down
29 changes: 13 additions & 16 deletions packages/toolpad-app/src/toolpadDataSources/rest/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import ParametersEditor from '../../toolpad/AppEditor/PageEditor/ParametersEdito
import BodyEditor from './BodyEditor';
import TabPanel from '../../components/TabPanel';
import SplitPane from '../../components/SplitPane';
import ErrorAlert from '../../toolpad/AppEditor/PageEditor/ErrorAlert';
import JsonView from '../../components/JsonView';
import useQueryPreview from '../useQueryPreview';
import TransformInput from '../TranformInput';
Expand All @@ -60,6 +59,7 @@ import config from '../../config';
import QueryInputPanel from '../QueryInputPanel';
import useFetchPrivate from '../useFetchPrivate';
import { clientExec } from './runtime';
import QueryPreview from '../QueryPreview';

const HTTP_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];

Expand Down Expand Up @@ -392,18 +392,17 @@ function QueryEditor({
clientExec(query, params, fetchServerPreview);

const [previewHar, setPreviewHar] = React.useState(() => createHarLog());
const { preview, runPreview: handleRunPreview } = useQueryPreview(
fetchPreview,
input.attributes.query.value,
previewParams,
{
onPreview(result) {
setPreviewHar((existing) =>
result.har ? mergeHar(createHarLog(), existing, result.har) : existing,
);
},
const {
preview,
runPreview: handleRunPreview,
isLoading: previewIsLoading,
} = useQueryPreview(fetchPreview, input.attributes.query.value, previewParams, {
onPreview(result) {
setPreviewHar((existing) =>
result.har ? mergeHar(createHarLog(), existing, result.har) : existing,
);
},
);
});

const handleHarClear = React.useCallback(() => setPreviewHar(createHarLog()), []);

Expand Down Expand Up @@ -546,11 +545,9 @@ function QueryEditor({
allowResize
pane1Style={{ overflow: 'auto' }}
>
{preview?.error ? (
<ErrorAlert error={preview?.error} />
) : (
<QueryPreview isLoading={previewIsLoading} error={preview?.error}>
<ResolvedPreview preview={preview} onShowTransform={() => setActiveTab('transform')} />
)}
</QueryPreview>
<Devtools
sx={{ width: '100%', height: '100%' }}
har={previewHar}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function useQueryPreview<Q, P, R extends ExecFetchResult<any> & P
{ onPreview = () => {} }: UseQueryPreviewOptions<R> = {},
) {
const [preview, setPreview] = React.useState<R | null>(null);
const [isLoading, setIsloading] = React.useState(false);

const cancelRunPreview = React.useRef<(() => void) | null>(null);
const runPreview = React.useCallback(() => {
Expand All @@ -23,6 +24,7 @@ export default function useQueryPreview<Q, P, R extends ExecFetchResult<any> & P
canceled = true;
};

setIsloading(true);
dofetch(query, params)
.then(
(result) => {
Expand All @@ -36,9 +38,10 @@ export default function useQueryPreview<Q, P, R extends ExecFetchResult<any> & P
},
)
.finally(() => {
setIsloading(false);
cancelRunPreview.current = null;
});
}, [dofetch, query, params, onPreview]);

return { preview, runPreview };
return { preview, runPreview, isLoading };
}