CodeDialog.tsx 2.0 KB

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