index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, isLoading} = 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
  117. timeWindowConfig={timeWindowConfig}
  118. width={timelineWidth}
  119. />
  120. <HeaderControlsRight>
  121. <DateNavigator
  122. dateNavigation={dateNavigation}
  123. direction="forward"
  124. size="xs"
  125. borderless
  126. />
  127. </HeaderControlsRight>
  128. </Header>
  129. <AlignedGridLineOverlay
  130. stickyCursor
  131. allowZoom
  132. showCursor={!isLoading}
  133. timeWindowConfig={timeWindowConfig}
  134. width={timelineWidth}
  135. />
  136. <MonitorRows>
  137. {monitorList.map(monitor => (
  138. <OverviewRow
  139. key={monitor.id}
  140. monitor={monitor}
  141. timeWindowConfig={timeWindowConfig}
  142. bucketedData={monitorStats?.[monitor.id]}
  143. width={timelineWidth}
  144. onDeleteEnvironment={env => handleDeleteEnvironment(monitor, env)}
  145. onToggleMuteEnvironment={(env, isMuted) =>
  146. handleToggleMuteEnvironment(monitor, env, isMuted)
  147. }
  148. onToggleStatus={handleToggleStatus}
  149. />
  150. ))}
  151. </MonitorRows>
  152. </MonitorListPanel>
  153. );
  154. }
  155. const Header = styled(Sticky)`
  156. display: grid;
  157. grid-column: 1/-1;
  158. grid-template-columns: subgrid;
  159. z-index: 1;
  160. background: ${p => p.theme.background};
  161. border-top-left-radius: ${p => p.theme.panelBorderRadius};
  162. border-top-right-radius: ${p => p.theme.panelBorderRadius};
  163. box-shadow: 0 1px ${p => p.theme.translucentBorder};
  164. &[data-stuck] {
  165. border-radius: 0;
  166. border-left: 1px solid ${p => p.theme.border};
  167. border-right: 1px solid ${p => p.theme.border};
  168. margin: 0 -1px;
  169. }
  170. `;
  171. const TimelineWidthTracker = styled('div')`
  172. position: absolute;
  173. width: 100%;
  174. grid-row: 1;
  175. grid-column: 3/-1;
  176. `;
  177. const AlignedGridLineOverlay = styled(GridLineOverlay)`
  178. grid-row: 1;
  179. grid-column: 3/-1;
  180. `;
  181. const AlignedGridLineLabels = styled(GridLineLabels)`
  182. grid-row: 1;
  183. grid-column: 3/-1;
  184. `;
  185. const MonitorListPanel = styled(Panel)`
  186. display: grid;
  187. grid-template-columns: 350px 135px 1fr max-content;
  188. `;
  189. const MonitorRows = styled('ul')`
  190. display: grid;
  191. grid-template-columns: subgrid;
  192. grid-column: 1 / -1;
  193. list-style: none;
  194. padding: 0;
  195. margin: 0;
  196. `;
  197. const HeaderControlsLeft = styled('div')`
  198. grid-column: 1/3;
  199. display: flex;
  200. justify-content: space-between;
  201. gap: ${space(0.5)};
  202. padding: ${space(1.5)} ${space(2)};
  203. `;
  204. const HeaderControlsRight = styled('div')`
  205. grid-row: 1;
  206. grid-column: -1;
  207. padding: ${space(1.5)} ${space(2)};
  208. `;