timelineTableRow.tsx 6.7 KB

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