ThresholdSetting.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {
  2. Button,
  3. Dialog,
  4. DialogTitle,
  5. Input,
  6. List,
  7. ListItem,
  8. makeStyles,
  9. Theme,
  10. Typography,
  11. } from '@material-ui/core';
  12. import { Dispatch, SetStateAction, useMemo, useState } from 'react';
  13. import CustomInput from '../../components/customInput/CustomInput';
  14. import { ITextfieldConfig } from '../../components/customInput/Types';
  15. import { useFormValidation } from '../../hooks/Form';
  16. import { formatForm } from '../../utils/Form';
  17. import { HEALTHY_STATUS_COLORS } from './consts';
  18. import { EHealthyStatus, IThreshold } from './Types';
  19. import SettingsOutlinedIcon from '@material-ui/icons/SettingsOutlined';
  20. import CustomButton from '../../components/customButton/CustomButton';
  21. export interface SimpleDialogProps {
  22. open: boolean;
  23. selectedValue: string;
  24. onClose: (value: string) => void;
  25. }
  26. const getStyles = makeStyles((theme: Theme) => ({
  27. root: {
  28. padding: '24px 32px',
  29. width: '360px',
  30. display: 'flex',
  31. flexDirection: 'column',
  32. },
  33. note: {
  34. fontWeight: 500,
  35. color: '#444',
  36. fontSize: '14px',
  37. margin: theme.spacing(1, 0, 2),
  38. },
  39. input: {
  40. margin: theme.spacing(0.5, 0, 0),
  41. },
  42. button: {
  43. alignSelf: 'flex-end',
  44. },
  45. }));
  46. function ThresholdSettingDialog({
  47. onClose,
  48. open,
  49. threshold,
  50. setThreshold,
  51. }: {
  52. open: boolean;
  53. onClose: () => void;
  54. threshold: IThreshold;
  55. setThreshold: (threshold: IThreshold) => void;
  56. }) {
  57. const classes = getStyles();
  58. const handleClose = () => {
  59. setThreshold({ ...form, memory: form.memory * 1024 * 1024 * 1024 });
  60. onClose();
  61. };
  62. const [form, setForm] = useState<IThreshold>({
  63. cpu: threshold.cpu,
  64. memory: threshold.memory / 1024 / 1024 / 1024,
  65. });
  66. const handleFormChange = (key: 'cpu' | 'memory', value: number) => {
  67. setForm(v => ({ ...v, [key]: value }));
  68. };
  69. const checkedForm = useMemo(() => {
  70. return formatForm(form);
  71. }, [form]);
  72. const { validation, checkIsValid } = useFormValidation(checkedForm);
  73. const inputConfigs: ITextfieldConfig[] = useMemo(
  74. () => [
  75. {
  76. label: `CPU (Core)`,
  77. key: 'prometheus_address',
  78. onChange: (v: string) => handleFormChange('cpu', +v),
  79. variant: 'filled',
  80. className: classes.input,
  81. placeholder: 'CPU',
  82. fullWidth: true,
  83. defaultValue: form.cpu,
  84. },
  85. {
  86. label: `Memory (GB)`,
  87. key: 'prometheus_address',
  88. onChange: (v: string) => handleFormChange('memory', +v),
  89. variant: 'filled',
  90. className: classes.input,
  91. placeholder: 'Memory',
  92. fullWidth: true,
  93. defaultValue: form.memory,
  94. },
  95. ],
  96. [form]
  97. );
  98. return (
  99. <Dialog onClose={handleClose} open={open}>
  100. <div className={classes.root}>
  101. <div className={classes.note}>
  102. {`Exceeding any threshold will result in a `}
  103. <span
  104. style={{
  105. color: HEALTHY_STATUS_COLORS[EHealthyStatus.warning],
  106. fontWeight: 600,
  107. fontSize: 18,
  108. }}
  109. >
  110. warning
  111. </span>
  112. .
  113. </div>
  114. {inputConfigs.map(v => (
  115. <CustomInput
  116. type="text"
  117. textConfig={v}
  118. key={v.label}
  119. checkValid={checkIsValid}
  120. validInfo={validation}
  121. />
  122. ))}
  123. <div className={classes.button}>
  124. <CustomButton variant="contained" onClick={handleClose}>
  125. Confirm
  126. </CustomButton>
  127. </div>
  128. </div>
  129. </Dialog>
  130. );
  131. }
  132. const ThresholdSetting = ({
  133. threshold,
  134. setThreshold,
  135. }: {
  136. threshold: IThreshold;
  137. setThreshold: (threshold: IThreshold) => void;
  138. }) => {
  139. const [open, setOpen] = useState(false);
  140. const handleClickOpen = () => {
  141. setOpen(true);
  142. };
  143. const handleClose = () => {
  144. setOpen(false);
  145. };
  146. return (
  147. <>
  148. <SettingsOutlinedIcon
  149. onClick={handleClickOpen}
  150. style={{
  151. cursor: 'pointer',
  152. opacity: 0.8,
  153. }}
  154. />
  155. <ThresholdSettingDialog
  156. threshold={threshold}
  157. setThreshold={setThreshold}
  158. open={open}
  159. onClose={handleClose}
  160. />
  161. </>
  162. );
  163. };
  164. export default ThresholdSetting;