CodeDialog.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { FC, useContext, useState } from 'react';
  2. import { Theme, SelectChangeEvent } from '@mui/material';
  3. import { useTranslation } from 'react-i18next';
  4. import { rootContext } from '@/context';
  5. import DialogTemplate from '@/components/customDialog/DialogTemplate';
  6. import CodeBlock from '@/components/code/CodeBlock';
  7. import CustomSelector from '@/components/customSelector/CustomSelector';
  8. import { makeStyles } from '@mui/styles';
  9. const useStyles = makeStyles((theme: Theme) => ({
  10. code: {
  11. backgroundColor: theme.palette.background.default,
  12. width: 800,
  13. height: '40vh',
  14. overflow: 'auto',
  15. },
  16. }));
  17. type CodeDialogProps = {
  18. data: { [key: string]: string };
  19. };
  20. const CodeDialog: FC<CodeDialogProps> = props => {
  21. // props
  22. const langauges = Object.keys(props.data);
  23. // build options
  24. const options = langauges.map(lang => ({
  25. label: lang,
  26. value: lang,
  27. }));
  28. // styles
  29. const classes = useStyles();
  30. // states
  31. const [langauge, setLanguage] = useState(langauges[0]);
  32. const code = props.data[langauge];
  33. // context
  34. const { handleCloseDialog } = useContext(rootContext);
  35. // translations
  36. const { t: btnTrans } = useTranslation('btn');
  37. const disabled = false;
  38. return (
  39. <DialogTemplate
  40. title={'Code View(beta)'}
  41. handleClose={handleCloseDialog}
  42. children={
  43. <>
  44. <CustomSelector
  45. label="Language"
  46. value={langauge}
  47. onChange={(event: SelectChangeEvent<unknown>) => {
  48. setLanguage(event.target.value as string);
  49. }}
  50. options={options}
  51. variant="filled"
  52. />
  53. <CodeBlock
  54. code={code}
  55. language={langauge === 'node.js' ? 'javascript' : langauge}
  56. wrapperClass={classes.code}
  57. />
  58. </>
  59. }
  60. confirmLabel={btnTrans('Ok')}
  61. handleConfirm={() => {
  62. handleCloseDialog();
  63. }}
  64. showCancel={false}
  65. confirmDisabled={disabled}
  66. />
  67. );
  68. };
  69. export default CodeDialog;