monitorCheckIns.tsx 3.4 KB

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