index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import {useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import {
  4. deleteMonitorEnvironment,
  5. setEnvironmentIsMuted,
  6. updateMonitor,
  7. } from 'sentry/actionCreators/monitors';
  8. import Panel from 'sentry/components/panels/panel';
  9. import {Sticky} from 'sentry/components/sticky';
  10. import {space} from 'sentry/styles/space';
  11. import {setApiQueryData, useQueryClient} from 'sentry/utils/queryClient';
  12. import useApi from 'sentry/utils/useApi';
  13. import {useDimensions} from 'sentry/utils/useDimensions';
  14. import useOrganization from 'sentry/utils/useOrganization';
  15. import useRouter from 'sentry/utils/useRouter';
  16. import type {Monitor} from 'sentry/views/monitors/types';
  17. import {makeMonitorListQueryKey} from 'sentry/views/monitors/utils';
  18. import {GridLineOverlay, GridLineTimeLabels} from './gridLines';
  19. import {SortSelector} from './sortSelector';
  20. import {TimelineTableRow} from './timelineTableRow';
  21. import {useMonitorStats} from './useMonitorStats';
  22. import {useTimeWindowConfig} from './useTimeWindowConfig';
  23. interface Props {
  24. monitorList: Monitor[];
  25. }
  26. export function OverviewTimeline({monitorList}: Props) {
  27. const organization = useOrganization();
  28. const api = useApi();
  29. const queryClient = useQueryClient();
  30. const router = useRouter();
  31. const location = router.location;
  32. const elementRef = useRef<HTMLDivElement>(null);
  33. const {width: timelineWidth} = useDimensions<HTMLDivElement>({elementRef});
  34. const timeWindowConfig = useTimeWindowConfig({timelineWidth});
  35. const {data: monitorStats, isLoading} = useMonitorStats({
  36. monitors: monitorList.map(m => m.id),
  37. timeWindowConfig,
  38. });
  39. const handleDeleteEnvironment = async (monitor: Monitor, env: string) => {
  40. const success = await deleteMonitorEnvironment(api, organization.slug, monitor, env);
  41. if (!success) {
  42. return;
  43. }
  44. const queryKey = makeMonitorListQueryKey(organization, location.query);
  45. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  46. const oldMonitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  47. if (oldMonitorIdx < 0) {
  48. return oldMonitorList;
  49. }
  50. const oldMonitor = oldMonitorList[oldMonitorIdx];
  51. const newEnvList = oldMonitor.environments.filter(e => e.name !== env);
  52. const updatedMonitor = {
  53. ...oldMonitor,
  54. environments: newEnvList,
  55. };
  56. const left = oldMonitorList.slice(0, oldMonitorIdx);
  57. const right = oldMonitorList.slice(oldMonitorIdx + 1);
  58. if (newEnvList.length === 0) {
  59. return [...left, ...right];
  60. }
  61. return [...left, updatedMonitor, ...right];
  62. });
  63. };
  64. const handleToggleMuteEnvironment = async (
  65. monitor: Monitor,
  66. env: string,
  67. isMuted: boolean
  68. ) => {
  69. const resp = await setEnvironmentIsMuted(
  70. api,
  71. organization.slug,
  72. monitor,
  73. env,
  74. isMuted
  75. );
  76. if (resp === null) {
  77. return;
  78. }
  79. const queryKey = makeMonitorListQueryKey(organization, location.query);
  80. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  81. const monitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  82. // TODO(davidenwang): in future only change the specifically modified environment for optimistic updates
  83. oldMonitorList[monitorIdx] = resp;
  84. return oldMonitorList;
  85. });
  86. };
  87. const handleToggleStatus = async (monitor: Monitor) => {
  88. const status = monitor.status === 'active' ? 'disabled' : 'active';
  89. const resp = await updateMonitor(api, organization.slug, monitor, {status});
  90. if (resp === null) {
  91. return;
  92. }
  93. const queryKey = makeMonitorListQueryKey(organization, location.query);
  94. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  95. const monitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  96. oldMonitorList[monitorIdx] = {...oldMonitorList[monitorIdx], status: resp.status};
  97. return oldMonitorList;
  98. });
  99. };
  100. return (
  101. <MonitorListPanel>
  102. <TimelineWidthTracker ref={elementRef} />
  103. <Header>
  104. <HeaderControls>
  105. <SortSelector size="xs" />
  106. </HeaderControls>
  107. <GridLineTimeLabels timeWindowConfig={timeWindowConfig} width={timelineWidth} />
  108. </Header>
  109. <GridLineOverlay
  110. stickyCursor
  111. allowZoom
  112. showCursor={!isLoading}
  113. timeWindowConfig={timeWindowConfig}
  114. width={timelineWidth}
  115. />
  116. {monitorList.map(monitor => (
  117. <TimelineTableRow
  118. key={monitor.id}
  119. monitor={monitor}
  120. timeWindowConfig={timeWindowConfig}
  121. bucketedData={monitorStats?.[monitor.id]}
  122. width={timelineWidth}
  123. onDeleteEnvironment={env => handleDeleteEnvironment(monitor, env)}
  124. onToggleMuteEnvironment={(env, isMuted) =>
  125. handleToggleMuteEnvironment(monitor, env, isMuted)
  126. }
  127. onToggleStatus={handleToggleStatus}
  128. />
  129. ))}
  130. </MonitorListPanel>
  131. );
  132. }
  133. const MonitorListPanel = styled(Panel)`
  134. display: grid;
  135. grid-template-columns: 350px 135px 1fr;
  136. `;
  137. const Header = styled(Sticky)`
  138. display: grid;
  139. grid-column: 1/-1;
  140. grid-template-columns: subgrid;
  141. z-index: 1;
  142. background: ${p => p.theme.background};
  143. border-top-left-radius: ${p => p.theme.panelBorderRadius};
  144. border-top-right-radius: ${p => p.theme.panelBorderRadius};
  145. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  146. &[data-stuck] {
  147. border-radius: 0;
  148. border-left: 1px solid ${p => p.theme.border};
  149. border-right: 1px solid ${p => p.theme.border};
  150. margin: 0 -1px;
  151. }
  152. `;
  153. const HeaderControls = styled('div')`
  154. grid-column: 1/3;
  155. display: flex;
  156. gap: ${space(0.5)};
  157. padding: ${space(1.5)} ${space(2)};
  158. `;
  159. const TimelineWidthTracker = styled('div')`
  160. position: absolute;
  161. width: 100%;
  162. grid-row: 1;
  163. grid-column: 3;
  164. `;