timelineTableRow.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 ProjectBadge from 'sentry/components/idBadge/projectBadge';
  9. import {Tag} from 'sentry/components/tag';
  10. import {IconEllipsis} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import {fadeIn} from 'sentry/styles/animations';
  13. import {space} from 'sentry/styles/space';
  14. import type {ObjectStatus} from 'sentry/types';
  15. import {trimSlug} from 'sentry/utils/trimSlug';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  18. import MonitorEnvironmentLabel from 'sentry/views/monitors/components/overviewTimeline/monitorEnvironmentLabel';
  19. import {StatusToggleButton} from 'sentry/views/monitors/components/statusToggleButton';
  20. import type {Monitor} from 'sentry/views/monitors/types';
  21. import {scheduleAsText} from 'sentry/views/monitors/utils';
  22. import type {CheckInTimelineProps} from './checkInTimeline';
  23. import {CheckInTimeline} from './checkInTimeline';
  24. import {TimelinePlaceholder} from './timelinePlaceholder';
  25. import type {MonitorBucket} from './types';
  26. interface Props extends Omit<CheckInTimelineProps, 'bucketedData' | 'environment'> {
  27. monitor: Monitor;
  28. bucketedData?: MonitorBucket[];
  29. onDeleteEnvironment?: (env: string) => Promise<void>;
  30. onToggleMuteEnvironment?: (env: string, isMuted: boolean) => Promise<void>;
  31. onToggleStatus?: (monitor: Monitor, status: ObjectStatus) => Promise<void>;
  32. /**
  33. * Whether only one monitor is being rendered in a larger view with this component
  34. * turns off things like zebra striping, hover effect, and showing monitor name
  35. */
  36. singleMonitorView?: boolean;
  37. }
  38. const MAX_SHOWN_ENVIRONMENTS = 4;
  39. export function TimelineTableRow({
  40. monitor,
  41. bucketedData,
  42. singleMonitorView,
  43. onDeleteEnvironment,
  44. onToggleMuteEnvironment,
  45. onToggleStatus,
  46. ...timelineProps
  47. }: Props) {
  48. const organization = useOrganization();
  49. const [isExpanded, setExpanded] = useState(
  50. monitor.environments.length <= MAX_SHOWN_ENVIRONMENTS
  51. );
  52. const environments = isExpanded
  53. ? monitor.environments
  54. : monitor.environments.slice(0, MAX_SHOWN_ENVIRONMENTS);
  55. const isDisabled = monitor.status === 'disabled';
  56. const monitorDetails = singleMonitorView ? null : (
  57. <DetailsArea>
  58. <DetailsLink
  59. to={normalizeUrl(
  60. `/organizations/${organization.slug}/crons/${monitor.project.slug}/${monitor.slug}/`
  61. )}
  62. >
  63. <DetailsHeadline>
  64. <Name>{monitor.name}</Name>
  65. </DetailsHeadline>
  66. <ProjectScheduleDetails>
  67. <DetailsText>{scheduleAsText(monitor.config)}</DetailsText>
  68. <ProjectDetails>
  69. <ProjectBadge
  70. project={monitor.project}
  71. avatarSize={12}
  72. disableLink
  73. hideName
  74. />
  75. <DetailsText>{trimSlug(monitor.project.slug)}</DetailsText>
  76. </ProjectDetails>
  77. </ProjectScheduleDetails>
  78. <MonitorStatuses>
  79. {monitor.isMuted && <Tag>{t('Muted')}</Tag>}
  80. {isDisabled && <Tag>{t('Disabled')}</Tag>}
  81. </MonitorStatuses>
  82. </DetailsLink>
  83. <DetailsActions>
  84. {onToggleStatus && (
  85. <StatusToggleButton
  86. monitor={monitor}
  87. size="xs"
  88. onToggleStatus={status => onToggleStatus(monitor, status)}
  89. />
  90. )}
  91. </DetailsActions>
  92. </DetailsArea>
  93. );
  94. const environmentActionCreators = [
  95. (env: string) => ({
  96. label: t('View Environment'),
  97. key: 'view',
  98. to: normalizeUrl(
  99. `/organizations/${organization.slug}/crons/${monitor.project.slug}/${monitor.slug}/?environment=${env}`
  100. ),
  101. }),
  102. ...(onToggleMuteEnvironment
  103. ? [
  104. (env: string, isMuted: boolean) => ({
  105. label:
  106. isMuted && !monitor.isMuted
  107. ? t('Unmute Environment')
  108. : t('Mute Environment'),
  109. key: 'mute',
  110. details: monitor.isMuted ? t('Monitor is muted') : undefined,
  111. disabled: monitor.isMuted,
  112. onAction: () => onToggleMuteEnvironment(env, !isMuted),
  113. }),
  114. ]
  115. : []),
  116. ...(onDeleteEnvironment
  117. ? [
  118. (env: string) => ({
  119. label: t('Delete Environment'),
  120. key: 'delete',
  121. onAction: () => {
  122. openConfirmModal({
  123. onConfirm: () => onDeleteEnvironment(env),
  124. header: t('Delete Environment?'),
  125. message: tct(
  126. 'Are you sure you want to permanently delete the "[envName]" environment?',
  127. {envName: env}
  128. ),
  129. confirmText: t('Delete'),
  130. priority: 'danger',
  131. });
  132. },
  133. }),
  134. ]
  135. : []),
  136. ];
  137. return (
  138. <TimelineRow
  139. key={monitor.id}
  140. isDisabled={isDisabled}
  141. singleMonitorView={singleMonitorView}
  142. >
  143. {monitorDetails}
  144. <MonitorEnvContainer>
  145. {environments.map(env => {
  146. const {name, isMuted} = env;
  147. return (
  148. <EnvRow key={name}>
  149. <DropdownMenu
  150. size="sm"
  151. trigger={triggerProps => (
  152. <EnvActionButton
  153. {...triggerProps}
  154. aria-label={t('Monitor environment actions')}
  155. size="xs"
  156. icon={<IconEllipsis />}
  157. />
  158. )}
  159. items={environmentActionCreators.map(actionCreator =>
  160. actionCreator(name, isMuted)
  161. )}
  162. />
  163. <MonitorEnvironmentLabel monitorEnv={env} />
  164. </EnvRow>
  165. );
  166. })}
  167. {!isExpanded && (
  168. <Button size="xs" onClick={() => setExpanded(true)}>
  169. {tct('Show [num] More', {
  170. num: monitor.environments.length - MAX_SHOWN_ENVIRONMENTS,
  171. })}
  172. </Button>
  173. )}
  174. </MonitorEnvContainer>
  175. <TimelineContainer>
  176. {environments.map(({name}) => {
  177. return (
  178. <TimelineEnvOuterContainer key={name}>
  179. {!bucketedData ? (
  180. <TimelinePlaceholder />
  181. ) : (
  182. <TimelineEnvContainer>
  183. <CheckInTimeline
  184. {...timelineProps}
  185. bucketedData={bucketedData}
  186. environment={name}
  187. />
  188. </TimelineEnvContainer>
  189. )}
  190. </TimelineEnvOuterContainer>
  191. );
  192. })}
  193. </TimelineContainer>
  194. </TimelineRow>
  195. );
  196. }
  197. const DetailsLink = styled(Link)`
  198. display: block;
  199. padding: ${space(3)};
  200. color: ${p => p.theme.textColor};
  201. `;
  202. const DetailsArea = styled('div')`
  203. border-right: 1px solid ${p => p.theme.border};
  204. border-radius: 0;
  205. position: relative;
  206. `;
  207. const DetailsActions = styled('div')`
  208. position: absolute;
  209. top: 0;
  210. right: 0;
  211. display: none;
  212. align-items: center;
  213. /* Align to the center of the heading text */
  214. height: calc(${p => p.theme.fontSizeLarge} * ${p => p.theme.text.lineHeightHeading});
  215. margin: ${space(3)};
  216. `;
  217. const DetailsHeadline = styled('div')`
  218. display: grid;
  219. gap: ${space(1)};
  220. grid-template-columns: 1fr minmax(30px, max-content);
  221. `;
  222. const ProjectScheduleDetails = styled('div')`
  223. display: flex;
  224. gap: ${space(1)};
  225. flex-wrap: wrap;
  226. `;
  227. const ProjectDetails = styled('div')`
  228. display: flex;
  229. gap: ${space(0.5)};
  230. `;
  231. const MonitorStatuses = styled('div')`
  232. display: flex;
  233. gap: ${space(0.5)};
  234. margin-top: ${space(1)};
  235. `;
  236. const Name = styled('h3')`
  237. font-size: ${p => p.theme.fontSizeLarge};
  238. margin-bottom: ${space(0.25)};
  239. word-break: break-word;
  240. `;
  241. const DetailsText = styled('small')`
  242. color: ${p => p.theme.subText};
  243. font-size: ${p => p.theme.fontSizeSmall};
  244. `;
  245. interface TimelineRowProps {
  246. isDisabled?: boolean;
  247. singleMonitorView?: boolean;
  248. }
  249. const TimelineRow = styled('div')<TimelineRowProps>`
  250. grid-column: 1/-1;
  251. display: grid;
  252. grid-template-columns: subgrid;
  253. ${p =>
  254. !p.singleMonitorView &&
  255. css`
  256. transition: background 50ms ease-in-out;
  257. &:nth-child(odd) {
  258. background: ${p.theme.backgroundSecondary};
  259. }
  260. &:hover {
  261. background: ${p.theme.backgroundTertiary};
  262. }
  263. `}
  264. /* Show detail actions on hover */
  265. &:hover ${DetailsActions} {
  266. display: flex;
  267. }
  268. /* Hide trailing items on hover */
  269. &:hover ${DetailsHeadline} ${Tag} {
  270. visibility: hidden;
  271. }
  272. /* Disabled monitors become more opaque */
  273. --disabled-opacity: ${p => (p.isDisabled ? '0.6' : 'unset')};
  274. &:last-child {
  275. border-bottom-left-radius: ${p => p.theme.borderRadius};
  276. border-bottom-right-radius: ${p => p.theme.borderRadius};
  277. }
  278. `;
  279. const MonitorEnvContainer = styled('div')`
  280. display: flex;
  281. padding: ${space(3)} ${space(2)};
  282. gap: ${space(4)};
  283. flex-direction: column;
  284. border-right: 1px solid ${p => p.theme.innerBorder};
  285. text-align: right;
  286. `;
  287. const EnvActionButton = styled(Button)`
  288. padding: ${space(0.5)} ${space(1)};
  289. display: none;
  290. `;
  291. const EnvRow = styled('div')`
  292. &:hover ${EnvActionButton} {
  293. display: block;
  294. }
  295. display: flex;
  296. gap: ${space(0.5)};
  297. justify-content: space-between;
  298. align-items: center;
  299. height: calc(${p => p.theme.fontSizeLarge} * ${p => p.theme.text.lineHeightHeading});
  300. `;
  301. const TimelineContainer = styled('div')`
  302. display: flex;
  303. padding: ${space(3)} 0;
  304. flex-direction: column;
  305. gap: ${space(4)};
  306. contain: content;
  307. `;
  308. const TimelineEnvOuterContainer = styled('div')`
  309. position: relative;
  310. height: calc(${p => p.theme.fontSizeLarge} * ${p => p.theme.text.lineHeightHeading});
  311. opacity: var(--disabled-opacity);
  312. `;
  313. const TimelineEnvContainer = styled('div')`
  314. position: absolute;
  315. inset: 0;
  316. opacity: 0;
  317. animation: ${fadeIn} 1.5s ease-out forwards;
  318. contain: content;
  319. `;