Status.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { FC, useMemo } from 'react';
  2. import StatusIcon, { LoadingType } from '@/components/status/StatusIcon';
  3. import { useTranslation } from 'react-i18next';
  4. import { Theme, Typography, useTheme } from '@mui/material';
  5. import { LOADING_STATE } from '@/consts';
  6. import { makeStyles } from '@mui/styles';
  7. export type StatusType = {
  8. status: LOADING_STATE;
  9. percentage?: number;
  10. };
  11. const useStyles = makeStyles((theme: Theme) => ({
  12. root: {
  13. display: 'flex',
  14. alignItems: 'center',
  15. },
  16. label: {
  17. color: theme.palette.text.secondary,
  18. textTransform: 'capitalize',
  19. },
  20. circle: {
  21. backgroundColor: (props: any) => props.color,
  22. borderRadius: '50%',
  23. width: '10px',
  24. height: '10px',
  25. marginRight: theme.spacing(0.5),
  26. },
  27. loading: {
  28. marginRight: '10px',
  29. },
  30. flash: {
  31. animation: '$bgColorChange 1.5s infinite',
  32. },
  33. '@keyframes bgColorChange': {
  34. '0%': {
  35. backgroundColor: (props: any) => props.color,
  36. },
  37. '50%': {
  38. backgroundColor: 'transparent',
  39. },
  40. '100%': {
  41. backgroundColor: (props: any) => props.color,
  42. },
  43. },
  44. }));
  45. const Status: FC<StatusType> = props => {
  46. const { status, percentage = 0 } = props;
  47. const { t: commonTrans } = useTranslation();
  48. const theme = useTheme();
  49. const statusTrans = commonTrans('status');
  50. const { label, color } = useMemo(() => {
  51. switch (status) {
  52. case LOADING_STATE.UNLOADED:
  53. return {
  54. label: statusTrans.unloaded,
  55. color: theme.palette.primary.main,
  56. };
  57. case LOADING_STATE.LOADED:
  58. return {
  59. label: statusTrans.loaded,
  60. color: '#06f3af',
  61. };
  62. case LOADING_STATE.LOADING:
  63. return {
  64. label: `${percentage}% ${statusTrans.loading}`,
  65. color: '#f25c06',
  66. };
  67. default:
  68. return {
  69. label: statusTrans.error,
  70. color: '#f25c06',
  71. };
  72. }
  73. }, [status, statusTrans, percentage]);
  74. const classes = useStyles({ color });
  75. return (
  76. <div className={classes.root}>
  77. {status === LOADING_STATE.LOADED && (
  78. <div className={classes.circle}></div>
  79. )}
  80. {status === LOADING_STATE.LOADING && (
  81. <StatusIcon type={LoadingType.CREATING} className={classes.loading} />
  82. )}
  83. <Typography variant="body2" className={classes.label}>
  84. {label}
  85. </Typography>
  86. </div>
  87. );
  88. };
  89. export default Status;