Status.tsx 2.3 KB

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