mockTimelineVisualization.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {Fragment, useContext, useEffect, useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import FormContext from 'sentry/components/forms/formContext';
  4. import type {FieldValue} from 'sentry/components/forms/model';
  5. import Panel from 'sentry/components/panels/panel';
  6. import Placeholder from 'sentry/components/placeholder';
  7. import {t} from 'sentry/locale';
  8. import {useApiQuery} from 'sentry/utils/queryClient';
  9. import {useDimensions} from 'sentry/utils/useDimensions';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import {ScheduleType} from 'sentry/views/monitors/types';
  12. import {CheckInPlaceholder} from './timeline/checkInPlaceholder';
  13. import {MockCheckInTimeline} from './timeline/checkInTimeline';
  14. import {GridLineLabels, GridLineOverlay} from './timeline/gridLines';
  15. import {getConfigFromTimeRange} from './timeline/utils/getConfigFromTimeRange';
  16. interface ScheduleConfig {
  17. cronSchedule?: FieldValue;
  18. intervalFrequency?: FieldValue;
  19. intervalUnit?: FieldValue;
  20. scheduleType?: FieldValue;
  21. timezone?: FieldValue;
  22. }
  23. const NUM_SAMPLE_TICKS = 9;
  24. function isValidConfig(schedule: ScheduleConfig) {
  25. const {scheduleType, cronSchedule, intervalFrequency, intervalUnit} = schedule;
  26. return !!(
  27. (scheduleType === ScheduleType.CRONTAB && cronSchedule) ||
  28. (scheduleType === ScheduleType.INTERVAL && intervalFrequency && intervalUnit)
  29. );
  30. }
  31. interface Props {
  32. schedule: ScheduleConfig;
  33. }
  34. export function MockTimelineVisualization({schedule}: Props) {
  35. const {scheduleType, cronSchedule, timezone, intervalFrequency, intervalUnit} =
  36. schedule;
  37. const organization = useOrganization();
  38. const {form} = useContext(FormContext);
  39. const query = {
  40. num_ticks: NUM_SAMPLE_TICKS,
  41. schedule_type: scheduleType,
  42. timezone,
  43. schedule:
  44. scheduleType === 'interval' ? [intervalFrequency, intervalUnit] : cronSchedule,
  45. };
  46. const elementRef = useRef<HTMLDivElement>(null);
  47. const {width: timelineWidth} = useDimensions<HTMLDivElement>({elementRef});
  48. const sampleDataQueryKey = [
  49. `/organizations/${organization.slug}/monitors-schedule-data/`,
  50. {query},
  51. ] as const;
  52. const {data, isLoading, isError, error} = useApiQuery<number[]>(sampleDataQueryKey, {
  53. staleTime: 0,
  54. enabled: isValidConfig(schedule),
  55. retry: false,
  56. });
  57. const errorMessage =
  58. isError || !isValidConfig(schedule)
  59. ? error?.responseJSON?.schedule?.[0] ?? t('Invalid Schedule')
  60. : null;
  61. useEffect(() => {
  62. if (!form) {
  63. return;
  64. }
  65. if (scheduleType === ScheduleType.INTERVAL) {
  66. form.setError('config.schedule.frequency', errorMessage);
  67. } else if (scheduleType === ScheduleType.CRONTAB) {
  68. form.setError('config.schedule', errorMessage);
  69. }
  70. }, [errorMessage, form, scheduleType]);
  71. const mockTimestamps = data?.map(ts => new Date(ts * 1000));
  72. const start = mockTimestamps?.[0];
  73. const end = mockTimestamps?.[mockTimestamps.length - 1];
  74. const timeWindowConfig =
  75. start && end ? getConfigFromTimeRange(start, end, timelineWidth) : undefined;
  76. return (
  77. <TimelineContainer>
  78. <TimelineWidthTracker ref={elementRef} />
  79. {isLoading || !start || !end || !timeWindowConfig || !mockTimestamps ? (
  80. <Fragment>
  81. <Placeholder height="50px" />
  82. {errorMessage ? null : <CheckInPlaceholder />}
  83. </Fragment>
  84. ) : (
  85. <Fragment>
  86. <AlignedGridLineLabels
  87. timeWindowConfig={timeWindowConfig}
  88. width={timelineWidth}
  89. />
  90. <AlignedGridLineOverlay
  91. showCursor={!isLoading}
  92. timeWindowConfig={timeWindowConfig}
  93. width={timelineWidth}
  94. />
  95. <MockCheckInTimeline
  96. width={timelineWidth}
  97. mockTimestamps={mockTimestamps.slice(1, mockTimestamps.length - 1)}
  98. timeWindowConfig={timeWindowConfig}
  99. />
  100. </Fragment>
  101. )}
  102. </TimelineContainer>
  103. );
  104. }
  105. const TimelineContainer = styled(Panel)`
  106. display: grid;
  107. grid-template-columns: 1fr;
  108. grid-template-rows: auto 60px;
  109. align-items: center;
  110. `;
  111. const AlignedGridLineLabels = styled(GridLineLabels)`
  112. grid-column: 0;
  113. border-bottom: 1px solid ${p => p.theme.border};
  114. `;
  115. const AlignedGridLineOverlay = styled(GridLineOverlay)`
  116. grid-column: 0;
  117. `;
  118. const TimelineWidthTracker = styled('div')`
  119. position: absolute;
  120. width: 100%;
  121. grid-row: 1;
  122. grid-column: 0;
  123. `;