TimeToRetain.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React from 'react';
  2. import prettyMilliseconds from 'pretty-ms';
  3. import { useFormContext } from 'react-hook-form';
  4. import { ErrorMessage } from '@hookform/error-message';
  5. import { MILLISECONDS_IN_WEEK, MILLISECONDS_IN_SECOND } from 'lib/constants';
  6. import { InputLabel } from 'components/common/Input/InputLabel.styled';
  7. import Input from 'components/common/Input/Input';
  8. import { FormError } from 'components/common/Input/Input.styled';
  9. import * as S from './TopicForm.styled';
  10. import TimeToRetainBtns from './TimeToRetainBtns';
  11. interface Props {
  12. isSubmitting: boolean;
  13. }
  14. const TimeToRetain: React.FC<Props> = ({ isSubmitting }) => {
  15. const {
  16. watch,
  17. formState: { errors },
  18. } = useFormContext();
  19. const defaultValue = MILLISECONDS_IN_WEEK;
  20. const name = 'retentionMs';
  21. const watchedValue = watch(name, defaultValue.toString());
  22. const valueHint = React.useMemo(() => {
  23. const value = parseInt(watchedValue, 10);
  24. return value >= MILLISECONDS_IN_SECOND ? prettyMilliseconds(value) : false;
  25. }, [watchedValue]);
  26. return (
  27. <>
  28. <S.Label>
  29. <InputLabel htmlFor="timeToRetain">
  30. Time to retain data (in ms)
  31. </InputLabel>
  32. {valueHint && <span>{valueHint}</span>}
  33. </S.Label>
  34. <Input
  35. id="timeToRetain"
  36. type="number"
  37. placeholder=" Time to retain data (in ms)"
  38. name={name}
  39. hookFormOptions={{
  40. min: { value: -1, message: 'must be greater than or equal to -1' },
  41. }}
  42. disabled={isSubmitting}
  43. />
  44. <FormError>
  45. <ErrorMessage errors={errors} name={name} />
  46. </FormError>
  47. <TimeToRetainBtns name={name} value={watchedValue} />
  48. </>
  49. );
  50. };
  51. export default TimeToRetain;