datePageFilter.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {Fragment} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {withRouter, WithRouterProps} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import {updateDateTime} from 'sentry/actionCreators/pageFilters';
  6. import Datetime from 'sentry/components/dateTime';
  7. import PageFilterDropdownButton from 'sentry/components/organizations/pageFilters/pageFilterDropdownButton';
  8. import PageFilterPinIndicator from 'sentry/components/organizations/pageFilters/pageFilterPinIndicator';
  9. import TimeRangeSelector, {
  10. ChangeData,
  11. } from 'sentry/components/organizations/timeRangeSelector';
  12. import {IconCalendar} from 'sentry/icons';
  13. import space from 'sentry/styles/space';
  14. import {
  15. DEFAULT_DAY_END_TIME,
  16. DEFAULT_DAY_START_TIME,
  17. getFormattedDate,
  18. } from 'sentry/utils/dates';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import usePageFilters from 'sentry/utils/usePageFilters';
  21. type Props = Omit<
  22. React.ComponentProps<typeof TimeRangeSelector>,
  23. 'organization' | 'start' | 'end' | 'utc' | 'relative' | 'onUpdate'
  24. > &
  25. WithRouterProps & {
  26. /**
  27. * Reset these URL params when we fire actions (custom routing only)
  28. */
  29. resetParamsOnChange?: string[];
  30. };
  31. function DatePageFilter({router, resetParamsOnChange, disabled, ...props}: Props) {
  32. const {selection, desyncedFilters} = usePageFilters();
  33. const organization = useOrganization();
  34. const {start, end, period, utc} = selection.datetime;
  35. const handleUpdate = (timePeriodUpdate: ChangeData) => {
  36. const {relative, ...startEndUtc} = timePeriodUpdate;
  37. const newTimePeriod = {
  38. period: relative,
  39. ...startEndUtc,
  40. };
  41. updateDateTime(newTimePeriod, router, {save: true, resetParams: resetParamsOnChange});
  42. };
  43. const customDropdownButton = ({getActorProps, isOpen}) => {
  44. let label;
  45. if (start && end) {
  46. const startTimeFormatted = getFormattedDate(start, 'HH:mm:ss', {local: true});
  47. const endTimeFormatted = getFormattedDate(end, 'HH:mm:ss', {local: true});
  48. const showDateOnly =
  49. startTimeFormatted === DEFAULT_DAY_START_TIME &&
  50. endTimeFormatted === DEFAULT_DAY_END_TIME;
  51. label = (
  52. <Fragment>
  53. <Datetime date={start} dateOnly={showDateOnly} />
  54. {' – '}
  55. <Datetime date={end} dateOnly={showDateOnly} />
  56. </Fragment>
  57. );
  58. } else {
  59. label = period?.toUpperCase();
  60. }
  61. return (
  62. <PageFilterDropdownButton
  63. detached
  64. disabled={disabled}
  65. hideBottomBorder={false}
  66. isOpen={isOpen}
  67. highlighted={desyncedFilters.has('datetime')}
  68. data-test-id="page-filter-timerange-selector"
  69. {...getActorProps()}
  70. >
  71. <DropdownTitle>
  72. <PageFilterPinIndicator filter="datetime">
  73. <IconCalendar />
  74. </PageFilterPinIndicator>
  75. <TitleContainer>{label}</TitleContainer>
  76. </DropdownTitle>
  77. </PageFilterDropdownButton>
  78. );
  79. };
  80. return (
  81. <TimeRangeSelector
  82. organization={organization}
  83. start={start}
  84. end={end}
  85. relative={period}
  86. utc={utc}
  87. onUpdate={handleUpdate}
  88. customDropdownButton={customDropdownButton}
  89. disabled={disabled}
  90. showPin
  91. detached
  92. {...props}
  93. />
  94. );
  95. }
  96. const TitleContainer = styled('div')`
  97. overflow: hidden;
  98. white-space: nowrap;
  99. text-overflow: ellipsis;
  100. flex: 1 1 0%;
  101. margin-left: ${space(1)};
  102. text-align: left;
  103. `;
  104. const DropdownTitle = styled('div')`
  105. display: flex;
  106. align-items: center;
  107. flex: 1;
  108. width: 100%;
  109. `;
  110. export default withRouter(DatePageFilter);