Status.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { FC } from 'react';
  2. import { Theme, Typography, CircularProgress } from '@mui/material';
  3. import { useTranslation } from 'react-i18next';
  4. import type { InsertStatusProps } from './Types';
  5. import successPath from '@/assets/imgs/insert/success.png';
  6. import failPath from '@/assets/imgs/insert/fail.png';
  7. import { InsertStatusEnum } from './consts';
  8. import Box from '@mui/material/Box';
  9. const InsertStatus: FC<InsertStatusProps> = ({ status, failMsg }) => {
  10. const { t: insertTrans } = useTranslation('insert');
  11. const textSx = (theme: Theme) => ({ marginTop: theme.spacing(3) });
  12. const loadingTipSx = (theme: Theme) => ({ marginBottom: theme.spacing(6) });
  13. const loadingSvgSx = (theme: Theme) => ({
  14. color: theme.palette.primary.main,
  15. });
  16. const wrapperSx = (theme: Theme) => ({
  17. width: '75vw',
  18. height: status === InsertStatusEnum.loading ? '288px' : '200px',
  19. display: 'flex',
  20. flexDirection: 'column',
  21. alignItems: 'center',
  22. justifyContent: 'center',
  23. });
  24. const InsertSuccess = () => (
  25. <>
  26. <img src={successPath} alt="insert success" />
  27. <Typography variant="h4" sx={textSx}>
  28. {insertTrans('statusSuccess')}
  29. </Typography>
  30. </>
  31. );
  32. const InsertLoading = () => (
  33. <>
  34. <CircularProgress size={64} thickness={5} sx={loadingSvgSx} />
  35. <Typography variant="h4" sx={textSx}>
  36. {insertTrans('statusLoading')}
  37. </Typography>
  38. <Typography
  39. variant="h5"
  40. sx={theme => ({ ...textSx(theme), ...loadingTipSx(theme) })}
  41. >
  42. {insertTrans('statusLoadingTip')}
  43. </Typography>
  44. </>
  45. );
  46. const InsertError = () => (
  47. <>
  48. <img src={failPath} alt="insert error" />
  49. <Typography variant="h4" sx={textSx}>
  50. {insertTrans('statusError')}
  51. </Typography>
  52. {failMsg && <Typography sx={textSx}>{failMsg}</Typography>}
  53. </>
  54. );
  55. const generateStatus = (status: InsertStatusEnum) => {
  56. switch (status) {
  57. case InsertStatusEnum.loading:
  58. return <InsertLoading />;
  59. case InsertStatusEnum.success:
  60. return <InsertSuccess />;
  61. // status error or init as default
  62. default:
  63. return <InsertError />;
  64. }
  65. };
  66. return (
  67. <Box component="section" sx={wrapperSx}>
  68. {generateStatus(status)}
  69. </Box>
  70. );
  71. };
  72. export default InsertStatus;