TimeToRetain.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React from 'react';
  2. import prettyMilliseconds from 'pretty-ms';
  3. import { useFormContext, ErrorMessage } from 'react-hook-form';
  4. import { MILLISECONDS_IN_WEEK, MILLISECONDS_IN_SECOND } from 'lib/constants';
  5. import TimeToRetainBtns from './TimeToRetainBtns';
  6. interface Props {
  7. isSubmitting: boolean;
  8. }
  9. const TimeToRetain: React.FC<Props> = ({ isSubmitting }) => {
  10. const { register, errors, watch } = useFormContext();
  11. const defaultValue = MILLISECONDS_IN_WEEK;
  12. const name = 'retentionMs';
  13. const watchedValue = watch(name, defaultValue.toString());
  14. const valueHint = React.useMemo(() => {
  15. const value = parseInt(watchedValue, 10);
  16. return value >= MILLISECONDS_IN_SECOND ? prettyMilliseconds(value) : false;
  17. }, [watchedValue]);
  18. return (
  19. <>
  20. <label
  21. className="label is-flex"
  22. style={{ justifyContent: 'space-between' }}
  23. >
  24. <div>Time to retain data (in ms)</div>
  25. {valueHint && <span className="has-text-info">{valueHint}</span>}
  26. </label>
  27. <input
  28. className="input"
  29. id="timeToRetain"
  30. type="number"
  31. defaultValue={defaultValue}
  32. name={name}
  33. ref={register({
  34. min: { value: -1, message: 'must be greater than or equal to -1' },
  35. })}
  36. disabled={isSubmitting}
  37. />
  38. <p className="help is-danger">
  39. <ErrorMessage errors={errors} name={name} />
  40. </p>
  41. <TimeToRetainBtns name={name} value={watchedValue} />
  42. </>
  43. );
  44. };
  45. export default TimeToRetain;