timelineTableRow.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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, 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 === MonitorStatus.DISABLED ? MonitorStatus.DISABLED : status;
  51. const {label, icon} = statusIconColorMap[envStatus];
  52. return (
  53. <EnvWithStatus key={name}>
  54. {onDeleteEnvironment && (
  55. <DropdownMenu
  56. size="sm"
  57. trigger={triggerProps => (
  58. <EnvActionButton
  59. {...triggerProps}
  60. aria-label={t('Monitor environment actions')}
  61. size="zero"
  62. icon={<IconEllipsis size="sm" />}
  63. />
  64. )}
  65. items={[
  66. {
  67. label: t('Delete Environment'),
  68. key: 'delete',
  69. onAction: () => {
  70. openConfirmModal({
  71. onConfirm: () => onDeleteEnvironment(name),
  72. header: t('Delete Environment?'),
  73. message: tct(
  74. 'Are you sure you want to permanently delete the "[envName]" environment?',
  75. {envName: name}
  76. ),
  77. confirmText: t('Delete'),
  78. priority: 'danger',
  79. });
  80. },
  81. },
  82. ]}
  83. />
  84. )}
  85. <MonitorEnvLabel status={envStatus}>{name}</MonitorEnvLabel>
  86. <Tooltip title={label} skipWrapper>
  87. {icon}
  88. </Tooltip>
  89. </EnvWithStatus>
  90. );
  91. })}
  92. {!isExpanded && (
  93. <Button size="xs" onClick={() => setExpanded(true)}>
  94. {tct('Show [num] More', {
  95. num: monitor.environments.length - MAX_SHOWN_ENVIRONMENTS,
  96. })}
  97. </Button>
  98. )}
  99. </MonitorEnvContainer>
  100. <TimelineContainer>
  101. {environments.map(({name}) => {
  102. return (
  103. <TimelineEnvOuterContainer key={name}>
  104. {!bucketedData ? (
  105. <TimelinePlaceholder />
  106. ) : (
  107. <TimelineEnvContainer>
  108. <CheckInTimeline
  109. {...timelineProps}
  110. bucketedData={bucketedData}
  111. environment={name}
  112. />
  113. </TimelineEnvContainer>
  114. )}
  115. </TimelineEnvOuterContainer>
  116. );
  117. })}
  118. </TimelineContainer>
  119. </TimelineRow>
  120. );
  121. }
  122. function MonitorDetails({monitor}: {monitor: Monitor}) {
  123. const organization = useOrganization();
  124. const schedule = scheduleAsText(monitor.config);
  125. const monitorDetailUrl = `/organizations/${organization.slug}/crons/${monitor.slug}/`;
  126. return (
  127. <DetailsContainer to={monitorDetailUrl}>
  128. <Name>{monitor.name}</Name>
  129. <Schedule>{schedule}</Schedule>
  130. </DetailsContainer>
  131. );
  132. }
  133. const TimelineRow = styled('div')<{singleMonitorView?: boolean}>`
  134. display: contents;
  135. ${p =>
  136. !p.singleMonitorView &&
  137. css`
  138. &:nth-child(odd) > * {
  139. background: ${p.theme.backgroundSecondary};
  140. }
  141. &:hover > * {
  142. background: ${p.theme.backgroundTertiary};
  143. }
  144. `}
  145. > * {
  146. transition: background 50ms ease-in-out;
  147. }
  148. `;
  149. const DetailsContainer = styled(Link)`
  150. color: ${p => p.theme.textColor};
  151. padding: ${space(3)};
  152. border-right: 1px solid ${p => p.theme.border};
  153. border-radius: 0;
  154. `;
  155. const Name = styled('h3')`
  156. font-size: ${p => p.theme.fontSizeLarge};
  157. margin-bottom: ${space(0.25)};
  158. `;
  159. const Schedule = styled('small')`
  160. color: ${p => p.theme.subText};
  161. font-size: ${p => p.theme.fontSizeSmall};
  162. `;
  163. const MonitorEnvContainer = styled('div')`
  164. display: flex;
  165. padding: ${space(3)} ${space(2)};
  166. gap: ${space(4)};
  167. flex-direction: column;
  168. border-right: 1px solid ${p => p.theme.innerBorder};
  169. text-align: right;
  170. `;
  171. const EnvActionButton = styled(Button)`
  172. padding: ${space(0.5)} ${space(1)};
  173. display: none;
  174. `;
  175. const EnvWithStatus = styled('div')`
  176. display: flex;
  177. gap: ${space(0.5)};
  178. align-items: center;
  179. height: calc(${p => p.theme.fontSizeLarge} * ${p => p.theme.text.lineHeightHeading});
  180. &:hover ${EnvActionButton} {
  181. display: block;
  182. }
  183. `;
  184. const MonitorEnvLabel = styled('div')<{status: MonitorStatus}>`
  185. text-overflow: ellipsis;
  186. overflow: hidden;
  187. white-space: nowrap;
  188. flex: 1;
  189. color: ${p => p.theme[statusIconColorMap[p.status].color]};
  190. `;
  191. const TimelineContainer = styled('div')`
  192. display: flex;
  193. padding: ${space(3)} 0;
  194. flex-direction: column;
  195. gap: ${space(4)};
  196. contain: content;
  197. `;
  198. const TimelineEnvOuterContainer = styled('div')`
  199. position: relative;
  200. height: calc(${p => p.theme.fontSizeLarge} * ${p => p.theme.text.lineHeightHeading});
  201. `;
  202. const TimelineEnvContainer = styled('div')`
  203. position: absolute;
  204. inset: 0;
  205. opacity: 0;
  206. animation: ${fadeIn} 1.5s ease-out forwards;
  207. contain: content;
  208. `;