row.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import cronstrue from 'cronstrue';
  4. import {deleteMonitor} from 'sentry/actionCreators/monitors';
  5. import {openConfirmModal} from 'sentry/components/confirm';
  6. import {DropdownMenu, MenuItemProps} from 'sentry/components/dropdownMenu';
  7. import IdBadge from 'sentry/components/idBadge';
  8. import Link from 'sentry/components/links/link';
  9. import TextOverflow from 'sentry/components/textOverflow';
  10. import TimeSince from 'sentry/components/timeSince';
  11. import {IconEllipsis} from 'sentry/icons';
  12. import {t, tct, tn} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {Organization} from 'sentry/types';
  15. import {shouldUse24Hours} from 'sentry/utils/dates';
  16. import useApi from 'sentry/utils/useApi';
  17. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  18. import {MonitorBadge} from './components/monitorBadge';
  19. import {Monitor, MonitorConfig, MonitorStatus, ScheduleType} from './types';
  20. interface MonitorRowProps {
  21. monitor: Monitor;
  22. onDelete: () => void;
  23. organization: Organization;
  24. }
  25. function scheduleAsText(config: MonitorConfig) {
  26. // Crontab format uses cronstrue
  27. if (config.schedule_type === ScheduleType.CRONTAB) {
  28. return cronstrue.toString(config.schedule, {
  29. verbose: true,
  30. throwExceptionOnParseError: false,
  31. use24HourTimeFormat: shouldUse24Hours(),
  32. });
  33. }
  34. // Interval format is simpler
  35. const [value, timeUnit] = config.schedule;
  36. if (timeUnit === 'minute') {
  37. return tn('Every minute', 'Every %s minutes', value);
  38. }
  39. if (timeUnit === 'hour') {
  40. return tn('Every hour', 'Every %s hours', value);
  41. }
  42. if (timeUnit === 'day') {
  43. return tn('Every day', 'Every %s days', value);
  44. }
  45. if (timeUnit === 'week') {
  46. return tn('Every week', 'Every %s weeks', value);
  47. }
  48. if (timeUnit === 'month') {
  49. return tn('Every month', 'Every %s months', value);
  50. }
  51. return t('Unknown schedule');
  52. }
  53. function MonitorRow({monitor, organization, onDelete}: MonitorRowProps) {
  54. const api = useApi();
  55. const lastCheckin = <TimeSince unitStyle="regular" date={monitor.lastCheckIn} />;
  56. const actions: MenuItemProps[] = [
  57. {
  58. key: 'edit',
  59. label: t('Edit'),
  60. to: normalizeUrl(`/organizations/${organization.slug}/crons/${monitor.slug}/edit/`),
  61. },
  62. {
  63. key: 'delete',
  64. label: t('Delete'),
  65. priority: 'danger',
  66. onAction: () => {
  67. openConfirmModal({
  68. onConfirm: async () => {
  69. await deleteMonitor(api, organization.slug, monitor.slug);
  70. onDelete();
  71. },
  72. header: t('Delete Monitor?'),
  73. message: tct('Are you sure you want to permanently delete [name]?', {
  74. name: monitor.name,
  75. }),
  76. confirmText: t('Delete Monitor'),
  77. priority: 'danger',
  78. });
  79. },
  80. },
  81. ];
  82. return (
  83. <Fragment>
  84. <MonitorName>
  85. <MonitorBadge status={monitor.status} />
  86. <Link to={`/organizations/${organization.slug}/crons/${monitor.slug}/`}>
  87. {monitor.name}
  88. </Link>
  89. </MonitorName>
  90. <StatusColumn>
  91. <TextOverflow>
  92. {monitor.status === MonitorStatus.DISABLED
  93. ? t('Paused')
  94. : monitor.status === MonitorStatus.ACTIVE
  95. ? t('Waiting for first check-in')
  96. : monitor.status === MonitorStatus.OK
  97. ? tct('Check-in [lastCheckin]', {lastCheckin})
  98. : monitor.status === MonitorStatus.MISSED_CHECKIN
  99. ? tct('Missed [lastCheckin]', {lastCheckin})
  100. : monitor.status === MonitorStatus.ERROR
  101. ? tct('Failed [lastCheckin]', {lastCheckin})
  102. : null}
  103. </TextOverflow>
  104. </StatusColumn>
  105. <ScheduleColumn>
  106. <TextOverflow>{scheduleAsText(monitor.config)}</TextOverflow>
  107. </ScheduleColumn>
  108. <NextCheckin>
  109. {monitor.nextCheckIn &&
  110. monitor.status !== MonitorStatus.DISABLED &&
  111. monitor.status !== MonitorStatus.ACTIVE ? (
  112. <TimeSince unitStyle="regular" date={monitor.nextCheckIn} />
  113. ) : (
  114. '\u2014'
  115. )}
  116. </NextCheckin>
  117. <ProjectColumn>
  118. <IdBadge
  119. project={monitor.project}
  120. avatarSize={18}
  121. avatarProps={{hasTooltip: true, tooltip: monitor.project.slug}}
  122. />
  123. </ProjectColumn>
  124. <ActionsColumn>
  125. <DropdownMenu
  126. items={actions}
  127. position="bottom-end"
  128. triggerProps={{
  129. 'aria-label': t('Actions'),
  130. size: 'xs',
  131. icon: <IconEllipsis size="xs" />,
  132. showChevron: false,
  133. }}
  134. />
  135. </ActionsColumn>
  136. </Fragment>
  137. );
  138. }
  139. export {MonitorRow};
  140. const MonitorName = styled('div')`
  141. display: flex;
  142. align-items: center;
  143. gap: ${space(2)};
  144. font-size: ${p => p.theme.fontSizeLarge};
  145. `;
  146. const StatusColumn = styled('div')`
  147. display: flex;
  148. align-items: center;
  149. `;
  150. const ScheduleColumn = styled('div')`
  151. display: flex;
  152. align-items: center;
  153. `;
  154. const NextCheckin = styled('div')`
  155. display: flex;
  156. align-items: center;
  157. `;
  158. const ProjectColumn = styled('div')`
  159. display: flex;
  160. align-items: center;
  161. `;
  162. const ActionsColumn = styled('div')`
  163. display: flex;
  164. align-items: center;
  165. justify-content: center;
  166. `;