useMonitorDates.tsx 840 B

12345678910111213141516171819202122232425262728
  1. import {useRef} from 'react';
  2. import moment from 'moment-timezone';
  3. import {intervalToMilliseconds} from 'sentry/utils/duration/intervalToMilliseconds';
  4. import usePageFilters from 'sentry/utils/usePageFilters';
  5. /**
  6. * Computes since and until values from the current page filters
  7. */
  8. export function useMonitorDates() {
  9. const nowRef = useRef<Date>(moment().startOf('minute').add(1, 'minutes').toDate());
  10. const {selection} = usePageFilters();
  11. const {start, end, period} = selection.datetime;
  12. let since: Date;
  13. let until: Date;
  14. if (!start || !end) {
  15. const periodMs = intervalToMilliseconds(period ?? '24h');
  16. until = nowRef.current;
  17. since = moment(nowRef.current).subtract(periodMs, 'milliseconds').toDate();
  18. } else {
  19. since = new Date(start);
  20. until = new Date(end);
  21. }
  22. return {since, until, nowRef};
  23. }