timelineTableRow.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import {useState} from 'react';
  2. import {Link} from 'react-router';
  3. import {css} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import {Button} from 'sentry/components/button';
  6. import {openConfirmModal} from 'sentry/components/confirm';
  7. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  8. import {Tooltip} from 'sentry/components/tooltip';
  9. import {IconEllipsis} from 'sentry/icons';
  10. import {t, tct} from 'sentry/locale';
  11. import {fadeIn} from 'sentry/styles/animations';
  12. import {space} from 'sentry/styles/space';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. import {Monitor, MonitorObjectStatus, MonitorStatus} from 'sentry/views/monitors/types';
  15. import {scheduleAsText} from 'sentry/views/monitors/utils';
  16. import {statusIconColorMap} from 'sentry/views/monitors/utils/constants';
  17. import {CheckInTimeline, CheckInTimelineProps} from './checkInTimeline';
  18. import {TimelinePlaceholder} from './timelinePlaceholder';
  19. import {MonitorBucket} from './types';
  20. interface Props extends Omit<CheckInTimelineProps, 'bucketedData' | 'environment'> {
  21. monitor: Monitor;
  22. bucketedData?: MonitorBucket[];
  23. onDeleteEnvironment?: (env: string) => void;
  24. /**
  25. * Whether only one monitor is being rendered in a larger view with this component
  26. * turns off things like zebra striping, hover effect, and showing monitor name
  27. */
  28. singleMonitorView?: boolean;
  29. }
  30. const MAX_SHOWN_ENVIRONMENTS = 4;
  31. export function TimelineTableRow({
  32. monitor,
  33. bucketedData,
  34. singleMonitorView,
  35. onDeleteEnvironment,
  36. ...timelineProps
  37. }: Props) {
  38. const [isExpanded, setExpanded] = useState(
  39. monitor.environments.length <= MAX_SHOWN_ENVIRONMENTS
  40. );
  41. const environments = isExpanded
  42. ? monitor.environments
  43. : monitor.environments.slice(0, MAX_SHOWN_ENVIRONMENTS);
  44. return (
  45. <TimelineRow key={monitor.id} singleMonitorView={singleMonitorView}>
  46. {!singleMonitorView && <MonitorDetails monitor={monitor} />}
  47. <MonitorEnvContainer>
  48. {environments.map(({name, status}) => {
  49. const envStatus =
  50. monitor.status === MonitorObjectStatus.MUTED
  51. ? MonitorStatus.DISABLED
  52. : status;
  53. const {label, icon} = statusIconColorMap[envStatus];
  54. return (
  55. <EnvWithStatus key={name}>
  56. {onDeleteEnvironment && (
  57. <DropdownMenu
  58. size="sm"
  59. trigger={triggerProps => (
  60. <EnvActionButton
  61. {...triggerProps}
  62. aria-label={t('Monitor environment actions')}
  63. size="zero"
  64. icon={<IconEllipsis size="sm" />}
  65. />
  66. )}
  67. items={[
  68. {
  69. label: t('Delete Environment'),
  70. key: 'delete',
  71. onAction: () => {
  72. openConfirmModal({
  73. onConfirm: () => onDeleteEnvironment(name),
  74. header: t('Delete Environment?'),
  75. message: tct(
  76. 'Are you sure you want to permanently delete the "[envName]" environment?',
  77. {envName: name}
  78. ),
  79. confirmText: t('Delete'),
  80. priority: 'danger',
  81. });
  82. },
  83. },
  84. ]}
  85. />
  86. )}
  87. <MonitorEnvLabel status={envStatus}>{name}</MonitorEnvLabel>
  88. <Tooltip title={label} skipWrapper>
  89. {icon}
  90. </Tooltip>
  91. </EnvWithStatus>
  92. );
  93. })}
  94. {!isExpanded && (
  95. <Button size="xs" onClick={() => setExpanded(true)}>
  96. {tct('Show [num] More', {
  97. num: monitor.environments.length - MAX_SHOWN_ENVIRONMENTS,
  98. })}
  99. </Button>
  100. )}
  101. </MonitorEnvContainer>
  102. <TimelineContainer>
  103. {environments.map(({name}) => {
  104. return (
  105. <TimelineEnvOuterContainer key={name}>
  106. {!bucketedData ? (
  107. <TimelinePlaceholder />
  108. ) : (
  109. <TimelineEnvContainer>
  110. <CheckInTimeline
  111. {...timelineProps}
  112. bucketedData={bucketedData}
  113. environment={name}
  114. />
  115. </TimelineEnvContainer>
  116. )}
  117. </TimelineEnvOuterContainer>
  118. );
  119. })}
  120. </TimelineContainer>
  121. </TimelineRow>
  122. );
  123. }
  124. function MonitorDetails({monitor}: {monitor: Monitor}) {
  125. const organization = useOrganization();
  126. const schedule = scheduleAsText(monitor.config);
  127. const monitorDetailUrl = `/organizations/${organization.slug}/crons/${monitor.slug}/`;
  128. return (
  129. <DetailsContainer to={monitorDetailUrl}>
  130. <Name>{monitor.name}</Name>
  131. <Schedule>{schedule}</Schedule>
  132. </DetailsContainer>
  133. );
  134. }
  135. const TimelineRow = styled('div')<{singleMonitorView?: boolean}>`
  136. display: contents;
  137. ${p =>
  138. !p.singleMonitorView &&
  139. css`
  140. &:nth-child(odd) > * {
  141. background: ${p.theme.backgroundSecondary};
  142. }
  143. &:hover > * {
  144. background: ${p.theme.backgroundTertiary};
  145. }
  146. `}
  147. &:last-child > *:first-child {
  148. border-bottom-left-radius: ${p => p.theme.borderRadius};
  149. }
  150. &:last-child > *:last-child {
  151. border-bottom-right-radius: ${p => p.theme.borderRadius};
  152. }
  153. > * {
  154. transition: background 50ms ease-in-out;
  155. }
  156. `;
  157. const DetailsContainer = styled(Link)`
  158. color: ${p => p.theme.textColor};
  159. padding: ${space(3)};
  160. border-right: 1px solid ${p => p.theme.border};
  161. border-radius: 0;
  162. `;
  163. const Name = styled('h3')`
  164. font-size: ${p => p.theme.fontSizeLarge};
  165. margin-bottom: ${space(0.25)};
  166. `;
  167. const Schedule = styled('small')`
  168. color: ${p => p.theme.subText};
  169. font-size: ${p => p.theme.fontSizeSmall};
  170. `;
  171. const MonitorEnvContainer = styled('div')`
  172. display: flex;
  173. padding: ${space(3)} ${space(2)};
  174. gap: ${space(4)};
  175. flex-direction: column;
  176. border-right: 1px solid ${p => p.theme.innerBorder};
  177. text-align: right;
  178. `;
  179. const EnvActionButton = styled(Button)`
  180. padding: ${space(0.5)} ${space(1)};
  181. display: none;
  182. `;
  183. const EnvWithStatus = styled('div')`
  184. display: flex;
  185. gap: ${space(0.5)};
  186. align-items: center;
  187. height: calc(${p => p.theme.fontSizeLarge} * ${p => p.theme.text.lineHeightHeading});
  188. &:hover ${EnvActionButton} {
  189. display: block;
  190. }
  191. `;
  192. const MonitorEnvLabel = styled('div')<{status: MonitorStatus}>`
  193. text-overflow: ellipsis;
  194. overflow: hidden;
  195. white-space: nowrap;
  196. flex: 1;
  197. color: ${p => p.theme[statusIconColorMap[p.status].color]};
  198. `;
  199. const TimelineContainer = styled('div')`
  200. display: flex;
  201. padding: ${space(3)} 0;
  202. flex-direction: column;
  203. gap: ${space(4)};
  204. contain: content;
  205. `;
  206. const TimelineEnvOuterContainer = styled('div')`
  207. position: relative;
  208. height: calc(${p => p.theme.fontSizeLarge} * ${p => p.theme.text.lineHeightHeading});
  209. `;
  210. const TimelineEnvContainer = styled('div')`
  211. position: absolute;
  212. inset: 0;
  213. opacity: 0;
  214. animation: ${fadeIn} 1.5s ease-out forwards;
  215. contain: content;
  216. `;