|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +import { useParams } from 'react-router-dom' |
| 15 | +import { useEffect, useState } from 'react' |
| 16 | +import { Alert, Box, CircularProgress, Grid2 as Grid } from '@mui/material' |
| 17 | +import { Texts } from '../constant.ts' |
| 18 | +import { queryStatusApi, QueryStatusInfo } from '../api/webapp/api.ts' |
| 19 | +import { ApiResponse } from '../api/base.ts' |
| 20 | +import { CodeBlock } from './CodeBlock.tsx' |
| 21 | + |
| 22 | +export const QueryJson = () => { |
| 23 | + const { queryId } = useParams() |
| 24 | + |
| 25 | + const [queryJson, setQueryJson] = useState<string | null>(null) |
| 26 | + const [loading, setLoading] = useState<boolean>(true) |
| 27 | + const [error, setError] = useState<string | null>(null) |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (queryId) { |
| 31 | + getQueryJson() |
| 32 | + } |
| 33 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 34 | + }, [queryId]) |
| 35 | + |
| 36 | + const getQueryJson = () => { |
| 37 | + if (queryId) { |
| 38 | + queryStatusApi(queryId).then((apiResponse: ApiResponse<QueryStatusInfo>) => { |
| 39 | + setLoading(false) |
| 40 | + if (apiResponse.status === 200 && apiResponse.data) { |
| 41 | + setQueryJson(JSON.stringify(apiResponse.data, null, 2)) |
| 42 | + setError(null) |
| 43 | + } else { |
| 44 | + setError(`${Texts.Error.Communication} ${apiResponse.status}: ${apiResponse.message}`) |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + <> |
| 52 | + {loading && <CircularProgress />} |
| 53 | + {error && <Alert severity="error">{Texts.Error.QueryNotFound}</Alert>} |
| 54 | + |
| 55 | + {!loading && !error && queryJson && ( |
| 56 | + <Grid container spacing={0}> |
| 57 | + <Grid size={{ xs: 12 }}> |
| 58 | + <Box sx={{ pt: 2 }}> |
| 59 | + <CodeBlock language="json" code={queryJson} /> |
| 60 | + </Box> |
| 61 | + </Grid> |
| 62 | + </Grid> |
| 63 | + )} |
| 64 | + </> |
| 65 | + ) |
| 66 | +} |
0 commit comments