index.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 {DateNavigator} from '../timeline/dateNavigator';
  19. import {GridLineLabels, GridLineOverlay} from '../timeline/gridLines';
  20. import {useDateNavigation} from '../timeline/hooks/useDateNavigation';
  21. import {useMonitorStats} from '../timeline/hooks/useMonitorStats';
  22. import {useTimeWindowConfig} from '../timeline/hooks/useTimeWindowConfig';
  23. import {OverviewRow} from './overviewRow';
  24. import {SortSelector} from './sortSelector';
  25. interface Props {
  26. monitorList: Monitor[];
  27. }
  28. export function OverviewTimeline({monitorList}: Props) {
  29. const organization = useOrganization();
  30. const api = useApi();
  31. const queryClient = useQueryClient();
  32. const router = useRouter();
  33. const location = router.location;
  34. const elementRef = useRef<HTMLDivElement>(null);
  35. const {width: timelineWidth} = useDimensions<HTMLDivElement>({elementRef});
  36. const timeWindowConfig = useTimeWindowConfig({timelineWidth});
  37. const dateNavigation = useDateNavigation();
  38. const {data: monitorStats, isPending} = useMonitorStats({
  39. monitors: monitorList.map(m => m.id),
  40. timeWindowConfig,
  41. });
  42. const handleDeleteEnvironment = async (monitor: Monitor, env: string) => {
  43. const success = await deleteMonitorEnvironment(api, organization.slug, monitor, env);
  44. if (!success) {
  45. return;
  46. }
  47. const queryKey = makeMonitorListQueryKey(organization, location.query);
  48. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  49. const oldMonitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  50. if (oldMonitorIdx < 0) {
  51. return oldMonitorList;
  52. }
  53. const oldMonitor = oldMonitorList[oldMonitorIdx];
  54. const newEnvList = oldMonitor.environments.filter(e => e.name !== env);
  55. const updatedMonitor = {
  56. ...oldMonitor,
  57. environments: newEnvList,
  58. };
  59. const left = oldMonitorList.slice(0, oldMonitorIdx);
  60. const right = oldMonitorList.slice(oldMonitorIdx + 1);
  61. if (newEnvList.length === 0) {
  62. return [...left, ...right];
  63. }
  64. return [...left, updatedMonitor, ...right];
  65. });
  66. };
  67. const handleToggleMuteEnvironment = async (
  68. monitor: Monitor,
  69. env: string,
  70. isMuted: boolean
  71. ) => {
  72. const resp = await setEnvironmentIsMuted(
  73. api,
  74. organization.slug,
  75. monitor,
  76. env,
  77. isMuted
  78. );
  79. if (resp === null) {
  80. return;
  81. }
  82. const queryKey = makeMonitorListQueryKey(organization, location.query);
  83. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  84. const monitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  85. // TODO(davidenwang): in future only change the specifically modified environment for optimistic updates
  86. oldMonitorList[monitorIdx] = resp;
  87. return oldMonitorList;
  88. });
  89. };
  90. const handleToggleStatus = async (monitor: Monitor) => {
  91. const status = monitor.status === 'active' ? 'disabled' : 'active';
  92. const resp = await updateMonitor(api, organization.slug, monitor, {status});
  93. if (resp === null) {
  94. return;
  95. }
  96. const queryKey = makeMonitorListQueryKey(organization, location.query);
  97. setApiQueryData(queryClient, queryKey, (oldMonitorList: Monitor[]) => {
  98. const monitorIdx = oldMonitorList.findIndex(m => m.slug === monitor.slug);
  99. oldMonitorList[monitorIdx] = {...oldMonitorList[monitorIdx], status: resp.status};
  100. return oldMonitorList;
  101. });
  102. };
  103. return (
  104. <MonitorListPanel role="region">
  105. <TimelineWidthTracker ref={elementRef} />
  106. <Header>
  107. <HeaderControlsLeft>
  108. <SortSelector size="xs" />
  109. <DateNavigator
  110. dateNavigation={dateNavigation}
  111. direction="back"
  112. size="xs"
  113. borderless
  114. />
  115. </HeaderControlsLeft>
  116. <AlignedGridLineLabels timeWindowConfig={timeWindowConfig} />
  117. <HeaderControlsRight>
  118. <DateNavigator
  119. dateNavigation={dateNavigation}
  120. direction="forward"
  121. size="xs"
  122. borderless
  123. />
  124. </HeaderControlsRight>
  125. </Header>
  126. <AlignedGridLineOverlay
  127. stickyCursor
  128. allowZoom
  129. showCursor={!isPending}
  130. showIncidents={!isPending}
  131. timeWindowConfig={timeWindowConfig}
  132. />
  133. <MonitorRows>
  134. {monitorList.map(monitor => (
  135. <OverviewRow
  136. key={monitor.id}
  137. monitor={monitor}
  138. timeWindowConfig={timeWindowConfig}
  139. bucketedData={monitorStats?.[monitor.id]}
  140. onDeleteEnvironment={env => handleDeleteEnvironment(monitor, env)}
  141. onToggleMuteEnvironment={(env, isMuted) =>
  142. handleToggleMuteEnvironment(monitor, env, isMuted)
  143. }
  144. onToggleStatus={handleToggleStatus}
  145. />
  146. ))}
  147. </MonitorRows>
  148. </MonitorListPanel>
  149. );
  150. }
  151. const Header = styled(Sticky)`
  152. display: grid;
  153. grid-column: 1/-1;
  154. grid-template-columns: subgrid;
  155. z-index: 1;
  156. background: ${p => p.theme.background};
  157. border-top-left-radius: ${p => p.theme.panelBorderRadius};
  158. border-top-right-radius: ${p => p.theme.panelBorderRadius};
  159. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  160. &[data-stuck] {
  161. border-radius: 0;
  162. border-left: 1px solid ${p => p.theme.border};
  163. border-right: 1px solid ${p => p.theme.border};
  164. margin: 0 -1px;
  165. }
  166. `;
  167. const TimelineWidthTracker = styled('div')`
  168. position: absolute;
  169. width: 100%;
  170. grid-row: 1;
  171. grid-column: 3/-1;
  172. `;
  173. const AlignedGridLineOverlay = styled(GridLineOverlay)`
  174. grid-row: 1;
  175. grid-column: 3/-1;
  176. `;
  177. const AlignedGridLineLabels = styled(GridLineLabels)`
  178. grid-row: 1;
  179. grid-column: 3/-1;
  180. `;
  181. const MonitorListPanel = styled(Panel)`
  182. display: grid;
  183. grid-template-columns: 350px 135px 1fr max-content;
  184. `;
  185. const MonitorRows = styled('ul')`
  186. display: grid;
  187. grid-template-columns: subgrid;
  188. grid-column: 1 / -1;
  189. list-style: none;
  190. padding: 0;
  191. margin: 0;
  192. `;
  193. const HeaderControlsLeft = styled('div')`
  194. grid-column: 1/3;
  195. display: flex;
  196. justify-content: space-between;
  197. gap: ${space(0.5)};
  198. padding: ${space(1.5)} ${space(2)};
  199. `;
  200. const HeaderControlsRight = styled('div')`
  201. grid-row: 1;
  202. grid-column: -1;
  203. padding: ${space(1.5)} ${space(2)};
  204. `;