monitorCheckIns.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, MonitorEnvironment} 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. monitorEnv: MonitorEnvironment;
  26. orgId: string;
  27. };
  28. type State = {
  29. checkInList: CheckIn[];
  30. };
  31. function MonitorCheckIns({monitor, monitorEnv, orgId}: Props) {
  32. const {data, hasError, renderComponent} = useApiRequests<State>({
  33. endpoints: [
  34. [
  35. 'checkInList',
  36. `/organizations/${orgId}/monitors/${monitor.slug}/checkins/`,
  37. {query: {per_page: '10', environment: monitorEnv.name}},
  38. {paginate: true},
  39. ],
  40. ],
  41. });
  42. const generateDownloadUrl = (checkin: CheckIn) =>
  43. `/api/0/organizations/${orgId}/monitors/${monitor.slug}/checkins/${checkin.id}/attachment/`;
  44. const renderedComponent = renderComponent(
  45. <React.Fragment>
  46. <Panel>
  47. <PanelHeader>{t('Recent Check-ins')}</PanelHeader>
  48. <PanelBody>
  49. {data.checkInList?.map(checkIn => (
  50. <PanelItem key={checkIn.id}>
  51. <CheckInIconWrapper>
  52. <Tooltip
  53. title={tct('Check In Status: [status]', {
  54. status: checkIn.status,
  55. })}
  56. >
  57. <CheckInIcon status={checkIn.status} size={16} />
  58. </Tooltip>
  59. </CheckInIconWrapper>
  60. <TimeSinceWrapper>
  61. <TimeSince date={checkIn.dateCreated} />
  62. </TimeSinceWrapper>
  63. <DurationWrapper>
  64. {defined(checkIn.duration) && (
  65. <Duration seconds={checkIn.duration / 1000} />
  66. )}
  67. </DurationWrapper>
  68. <AttachmentWrapper>
  69. {checkIn.attachmentId && (
  70. <Button
  71. size="xs"
  72. icon={<IconDownload size="xs" />}
  73. href={generateDownloadUrl(checkIn)}
  74. >
  75. Attachment
  76. </Button>
  77. )}
  78. </AttachmentWrapper>
  79. </PanelItem>
  80. ))}
  81. </PanelBody>
  82. </Panel>
  83. <Pagination pageLinks={data.checkInListPageLinks} />
  84. </React.Fragment>
  85. );
  86. return hasError ? <ErrorWrapper>{renderedComponent}</ErrorWrapper> : renderedComponent;
  87. }
  88. export default MonitorCheckIns;
  89. const DivMargin = styled('div')`
  90. margin-right: ${space(2)};
  91. `;
  92. const CheckInIconWrapper = styled(DivMargin)`
  93. display: flex;
  94. align-items: center;
  95. `;
  96. const TimeSinceWrapper = styled(DivMargin)`
  97. font-variant-numeric: tabular-nums;
  98. `;
  99. const DurationWrapper = styled('div')`
  100. font-variant-numeric: tabular-nums;
  101. `;
  102. const ErrorWrapper = styled('div')`
  103. margin: ${space(3)} ${space(3)} 0;
  104. `;
  105. const AttachmentWrapper = styled('div')`
  106. margin-left: auto;
  107. `;