|
| 1 | +import type { NextPage } from 'next'; |
| 2 | +import { RedocStandalone } from 'redoc'; |
| 3 | +import * as spec from 'openapi_specs'; |
| 4 | +import { |
| 5 | + AppBar, |
| 6 | + Button, |
| 7 | + CircularProgress, |
| 8 | + Fade, |
| 9 | + LinearProgress, |
| 10 | + MenuItem, |
| 11 | + Toolbar, |
| 12 | + Typography, |
| 13 | +} from '@mui/material'; |
| 14 | +import { useRouter } from 'next/router'; |
| 15 | +import { useEffect, useMemo, useState } from 'react'; |
| 16 | + |
| 17 | +const Home: NextPage = () => { |
| 18 | + const router = useRouter(); |
| 19 | + const [isLoading, setIsLoading] = useState(false); |
| 20 | + useEffect(() => { |
| 21 | + setIsLoading(true); |
| 22 | + }, [router.query.doc]); |
| 23 | + const selectedDoc = useMemo(() => { |
| 24 | + if (!Boolean(router.query.doc)) { |
| 25 | + return spec.health_service_schema; |
| 26 | + } |
| 27 | + const foundValue = Object.entries(spec).find( |
| 28 | + ([key, value], index) => key === router.query.doc, |
| 29 | + ); |
| 30 | + |
| 31 | + if (!foundValue) { |
| 32 | + return undefined; |
| 33 | + } |
| 34 | + |
| 35 | + return foundValue[1]; |
| 36 | + }, [router.query.doc]); |
| 37 | + |
| 38 | + return ( |
| 39 | + <div> |
| 40 | + <AppBar color="primary"> |
| 41 | + <Toolbar> |
| 42 | + <Typography variant={'h5'}>Docs</Typography> |
| 43 | + {Object.keys(spec).map((k) => ( |
| 44 | + <MenuItem |
| 45 | + key={k} |
| 46 | + selected={true} |
| 47 | + onClick={() => router.push(`?doc=${k}`)} |
| 48 | + style={{ color: k === router.query.doc ? 'purple' : 'black' }} |
| 49 | + > |
| 50 | + {k} |
| 51 | + </MenuItem> |
| 52 | + ))} |
| 53 | + </Toolbar> |
| 54 | + </AppBar> |
| 55 | + <div style={{ marginTop: 70 }}> |
| 56 | + <Fade in={isLoading} mountOnEnter unmountOnExit> |
| 57 | + <LinearProgress color={'secondary'} /> |
| 58 | + </Fade> |
| 59 | + {selectedDoc && ( |
| 60 | + <RedocStandalone |
| 61 | + spec={selectedDoc} |
| 62 | + onLoaded={() => { |
| 63 | + setIsLoading(false); |
| 64 | + }} |
| 65 | + options={{ |
| 66 | + theme: { |
| 67 | + sidebar: { width: '0px' }, |
| 68 | + rightPanel: { width: '40%' }, |
| 69 | + }, |
| 70 | + scrollYOffset: 0, |
| 71 | + hideLoading: true, |
| 72 | + nativeScrollbars: false, |
| 73 | + }} |
| 74 | + /> |
| 75 | + )} |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + ); |
| 79 | +}; |
| 80 | + |
| 81 | +export default Home; |
0 commit comments