monitorCheckIns.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import styled from '@emotion/styled';
  2. import {Button} from 'sentry/components/button';
  3. import Duration from 'sentry/components/duration';
  4. import {PanelBody, PanelItem} from 'sentry/components/panels';
  5. import TimeSince from 'sentry/components/timeSince';
  6. import {Tooltip} from 'sentry/components/tooltip';
  7. import {IconDownload} from 'sentry/icons';
  8. import {tct} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import {defined} from 'sentry/utils';
  11. import useApiRequests from 'sentry/utils/useApiRequests';
  12. import {CheckInStatus, Monitor} from 'sentry/views/monitors/types';
  13. import CheckInIcon from './checkInIcon';
  14. type CheckIn = {
  15. dateCreated: string;
  16. duration: number;
  17. id: string;
  18. status: CheckInStatus;
  19. attachmentId?: number;
  20. };
  21. type Props = {
  22. monitor: Monitor;
  23. orgId: string;
  24. };
  25. type State = {
  26. checkInList: CheckIn[];
  27. };
  28. const MonitorCheckIns = ({monitor, orgId}: Props) => {
  29. const {data, hasError, renderComponent} = useApiRequests<State>({
  30. endpoints: [
  31. [
  32. 'checkInList',
  33. `/organizations/${orgId}/monitors/${monitor.id}/checkins/`,
  34. {query: {per_page: '10'}},
  35. ],
  36. ],
  37. });
  38. const generateDownloadUrl = (checkin: CheckIn) =>
  39. `/api/0/organizations/${orgId}/monitors/${monitor.id}/checkins/${checkin.id}/attachment/`;
  40. const renderedComponent = renderComponent(
  41. <PanelBody>
  42. {data.checkInList?.map(checkIn => (
  43. <PanelItem key={checkIn.id}>
  44. <CheckInIconWrapper>
  45. <Tooltip
  46. title={tct('Check In Status: [status]', {
  47. status: checkIn.status,
  48. })}
  49. >
  50. <CheckInIcon status={checkIn.status} size={16} />
  51. </Tooltip>
  52. </CheckInIconWrapper>
  53. <TimeSinceWrapper>
  54. <TimeSince date={checkIn.dateCreated} />
  55. </TimeSinceWrapper>
  56. <DurationWrapper>
  57. {defined(checkIn.duration) && <Duration seconds={checkIn.duration / 1000} />}
  58. </DurationWrapper>
  59. <AttachmentWrapper>
  60. {checkIn.attachmentId && (
  61. <Button
  62. size="xs"
  63. icon={<IconDownload size="xs" />}
  64. href={generateDownloadUrl(checkIn)}
  65. >
  66. Attachment
  67. </Button>
  68. )}
  69. </AttachmentWrapper>
  70. </PanelItem>
  71. ))}
  72. </PanelBody>
  73. );
  74. return hasError ? <ErrorWrapper>{renderedComponent}</ErrorWrapper> : renderedComponent;
  75. };
  76. export default MonitorCheckIns;
  77. const DivMargin = styled('div')`
  78. margin-right: ${space(2)};
  79. `;
  80. const CheckInIconWrapper = styled(DivMargin)`
  81. display: flex;
  82. align-items: center;
  83. `;
  84. const TimeSinceWrapper = styled(DivMargin)`
  85. font-variant-numeric: tabular-nums;
  86. `;
  87. const DurationWrapper = styled('div')`
  88. font-variant-numeric: tabular-nums;
  89. `;
  90. const ErrorWrapper = styled('div')`
  91. margin: ${space(3)} ${space(3)} 0;
  92. `;
  93. const AttachmentWrapper = styled('div')`
  94. margin-left: auto;
  95. `;