index.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import {Component} from 'react';
  2. import type {Range} from 'react-date-range';
  3. import {WithRouterProps} from 'react-router';
  4. import {Theme, withTheme} from '@emotion/react';
  5. import styled from '@emotion/styled';
  6. import moment from 'moment';
  7. import {DateRangePicker} from 'sentry/components/calendar';
  8. import Checkbox from 'sentry/components/checkbox';
  9. import TimePicker from 'sentry/components/organizations/timeRangeSelector/timePicker';
  10. import {MAX_PICKABLE_DAYS} from 'sentry/constants';
  11. import {t} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {Organization} from 'sentry/types';
  14. import {analytics} from 'sentry/utils/analytics';
  15. import {
  16. getEndOfDay,
  17. getStartOfPeriodAgo,
  18. isValidTime,
  19. setDateToTime,
  20. } from 'sentry/utils/dates';
  21. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  22. // eslint-disable-next-line no-restricted-imports
  23. import withSentryRouter from 'sentry/utils/withSentryRouter';
  24. const getTimeStringFromDate = (date: Date) => moment(date).local().format('HH:mm');
  25. type ChangeData = {end?: Date; hasDateRangeErrors?: boolean; start?: Date};
  26. const defaultProps = {
  27. showAbsolute: true,
  28. showRelative: false,
  29. /**
  30. * The maximum number of days in the past you can pick
  31. */
  32. maxPickableDays: MAX_PICKABLE_DAYS,
  33. };
  34. type Props = WithRouterProps & {
  35. /**
  36. * End date value for absolute date selector
  37. */
  38. end: Date | null;
  39. /**
  40. * Callback when value changes
  41. */
  42. onChange: (data: ChangeData) => void;
  43. /**
  44. * handle UTC checkbox change
  45. */
  46. onChangeUtc: () => void;
  47. /**
  48. * Just used for metrics
  49. */
  50. organization: Organization;
  51. /**
  52. * Start date value for absolute date selector
  53. */
  54. start: Date | null;
  55. theme: Theme;
  56. className?: string;
  57. /**
  58. * Should we have a time selector?
  59. */
  60. showTimePicker?: boolean;
  61. /**
  62. * Use UTC
  63. */
  64. utc?: boolean | null;
  65. } & Partial<typeof defaultProps>;
  66. type State = {
  67. hasEndErrors: boolean;
  68. hasStartErrors: boolean;
  69. };
  70. class BaseDateRange extends Component<Props, State> {
  71. static defaultProps = defaultProps;
  72. state: State = {
  73. hasStartErrors: false,
  74. hasEndErrors: false,
  75. };
  76. handleSelectDateRange = (range: Range) => {
  77. const {onChange} = this.props;
  78. const {startDate, endDate} = range;
  79. const end = endDate ? getEndOfDay(endDate) : endDate;
  80. onChange({
  81. start: startDate,
  82. end,
  83. });
  84. };
  85. handleChangeStart = (e: React.ChangeEvent<HTMLInputElement>) => {
  86. // Safari does not support "time" inputs, so we don't have access to
  87. // `e.target.valueAsDate`, must parse as string
  88. //
  89. // Time will be in 24hr e.g. "21:00"
  90. const start = this.props.start ?? '';
  91. const end = this.props.end ?? undefined;
  92. const {onChange, organization, router} = this.props;
  93. const startTime = e.target.value;
  94. if (!startTime || !isValidTime(startTime)) {
  95. this.setState({hasStartErrors: true});
  96. onChange({hasDateRangeErrors: true});
  97. return;
  98. }
  99. const newTime = setDateToTime(start, startTime, {local: true});
  100. analytics('dateselector.time_changed', {
  101. field_changed: 'start',
  102. time: startTime,
  103. path: getRouteStringFromRoutes(router.routes),
  104. org_id: parseInt(organization.id, 10),
  105. });
  106. onChange({
  107. start: newTime,
  108. end,
  109. hasDateRangeErrors: this.state.hasEndErrors,
  110. });
  111. this.setState({hasStartErrors: false});
  112. };
  113. handleChangeEnd = (e: React.ChangeEvent<HTMLInputElement>) => {
  114. const start = this.props.start ?? undefined;
  115. const end = this.props.end ?? '';
  116. const {organization, onChange, router} = this.props;
  117. const endTime = e.target.value;
  118. if (!endTime || !isValidTime(endTime)) {
  119. this.setState({hasEndErrors: true});
  120. onChange({hasDateRangeErrors: true});
  121. return;
  122. }
  123. const newTime = setDateToTime(end, endTime, {local: true});
  124. analytics('dateselector.time_changed', {
  125. field_changed: 'end',
  126. time: endTime,
  127. path: getRouteStringFromRoutes(router.routes),
  128. org_id: parseInt(organization.id, 10),
  129. });
  130. onChange({
  131. start,
  132. end: newTime,
  133. hasDateRangeErrors: this.state.hasStartErrors,
  134. });
  135. this.setState({hasEndErrors: false});
  136. };
  137. render() {
  138. const {className, maxPickableDays, utc, showTimePicker, onChangeUtc} = this.props;
  139. const start = this.props.start ?? '';
  140. const end = this.props.end ?? '';
  141. const startTime = getTimeStringFromDate(new Date(start));
  142. const endTime = getTimeStringFromDate(new Date(end));
  143. // Restraints on the time range that you can select
  144. // Can't select dates in the future b/c we're not fortune tellers (yet)
  145. //
  146. // We want `maxPickableDays` - 1 (if today is Jan 5, max is 3 days, the minDate should be Jan 3)
  147. // Subtract additional day because we force the end date to be inclusive,
  148. // so when you pick Jan 1 the time becomes Jan 1 @ 23:59:59,
  149. // (or really, Jan 2 @ 00:00:00 - 1 second), while the start time is at 00:00
  150. const minDate = getStartOfPeriodAgo(
  151. 'days',
  152. (maxPickableDays ?? MAX_PICKABLE_DAYS) - 2
  153. );
  154. const maxDate = new Date();
  155. return (
  156. <div className={className} data-test-id="date-range">
  157. <DateRangePicker
  158. startDate={moment(start).local().toDate()}
  159. endDate={moment(end).local().toDate()}
  160. minDate={minDate}
  161. maxDate={maxDate}
  162. onChange={this.handleSelectDateRange}
  163. />
  164. {showTimePicker && (
  165. <TimeAndUtcPicker>
  166. <TimePicker
  167. start={startTime}
  168. end={endTime}
  169. onChangeStart={this.handleChangeStart}
  170. onChangeEnd={this.handleChangeEnd}
  171. />
  172. <UtcPicker>
  173. {t('Use UTC')}
  174. <Checkbox onChange={onChangeUtc} checked={utc || false} />
  175. </UtcPicker>
  176. </TimeAndUtcPicker>
  177. )}
  178. </div>
  179. );
  180. }
  181. }
  182. const DateRange = styled(withTheme(withSentryRouter(BaseDateRange)))`
  183. display: flex;
  184. flex-direction: column;
  185. border-left: 1px solid ${p => p.theme.border};
  186. `;
  187. const TimeAndUtcPicker = styled('div')`
  188. display: flex;
  189. align-items: center;
  190. padding: ${space(0.25)} ${space(2)};
  191. border-top: 1px solid ${p => p.theme.innerBorder};
  192. `;
  193. const UtcPicker = styled('div')`
  194. color: ${p => p.theme.gray300};
  195. white-space: nowrap;
  196. display: flex;
  197. align-items: center;
  198. justify-content: flex-end;
  199. flex: 1;
  200. gap: ${space(1)};
  201. `;
  202. export default DateRange;