index.tsx 6.5 KB

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