monitors.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {Fragment} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {withRouter, WithRouterProps} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import * as qs from 'query-string';
  6. import Access from 'sentry/components/acl/access';
  7. import Button from 'sentry/components/button';
  8. import FeatureBadge from 'sentry/components/featureBadge';
  9. import Link from 'sentry/components/links/link';
  10. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  11. import PageHeading from 'sentry/components/pageHeading';
  12. import Pagination from 'sentry/components/pagination';
  13. import {Panel, PanelBody, PanelItem} from 'sentry/components/panels';
  14. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  15. import SearchBar from 'sentry/components/searchBar';
  16. import TimeSince from 'sentry/components/timeSince';
  17. import {t} from 'sentry/locale';
  18. import {PageHeader} from 'sentry/styles/organization';
  19. import space from 'sentry/styles/space';
  20. import {Organization} from 'sentry/types';
  21. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  22. import {decodeScalar} from 'sentry/utils/queryString';
  23. import withOrganization from 'sentry/utils/withOrganization';
  24. import AsyncView from 'sentry/views/asyncView';
  25. import MonitorIcon from './monitorIcon';
  26. import {Monitor} from './types';
  27. type Props = AsyncView['props'] &
  28. WithRouterProps<{orgId: string}> & {
  29. organization: Organization;
  30. };
  31. type State = AsyncView['state'] & {
  32. monitorList: Monitor[] | null;
  33. };
  34. class Monitors extends AsyncView<Props, State> {
  35. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  36. const {params, location} = this.props;
  37. return [
  38. [
  39. 'monitorList',
  40. `/organizations/${params.orgId}/monitors/`,
  41. {
  42. query: location.query,
  43. },
  44. ],
  45. ];
  46. }
  47. getTitle() {
  48. return `Monitors - ${this.props.params.orgId}`;
  49. }
  50. componentDidMount() {
  51. trackAdvancedAnalyticsEvent('monitors.page_viewed', {
  52. organization: this.props.organization.id,
  53. });
  54. }
  55. handleSearch = (query: string) => {
  56. const {location, router} = this.props;
  57. router.push({
  58. pathname: location.pathname,
  59. query: normalizeDateTimeParams({
  60. ...(location.query || {}),
  61. query,
  62. }),
  63. });
  64. };
  65. renderBody() {
  66. const {monitorList, monitorListPageLinks} = this.state;
  67. const {organization} = this.props;
  68. return (
  69. <Fragment>
  70. <PageHeader>
  71. <HeaderTitle>
  72. <div>
  73. {t('Monitors')} <FeatureBadge type="beta" />
  74. </div>
  75. <Access organization={organization} access={['project:write']}>
  76. {({hasAccess}) => (
  77. <Button
  78. to={`/organizations/${organization.slug}/monitors/create/`}
  79. priority="primary"
  80. size="sm"
  81. disabled={!hasAccess}
  82. tooltipProps={{
  83. disabled: hasAccess,
  84. }}
  85. title={t('You must be an organization admin to create a new monitor')}
  86. >
  87. {t('New Monitor')}
  88. </Button>
  89. )}
  90. </Access>
  91. </HeaderTitle>
  92. </PageHeader>
  93. <Filters>
  94. <ProjectPageFilter resetParamsOnChange={['cursor']} />
  95. <SearchBar
  96. query={decodeScalar(qs.parse(location.search)?.query, '')}
  97. placeholder={t('Search for monitors.')}
  98. onSearch={this.handleSearch}
  99. />
  100. </Filters>
  101. <Panel>
  102. <PanelBody>
  103. {monitorList?.map(monitor => (
  104. <PanelItemCentered key={monitor.id}>
  105. <MonitorIcon status={monitor.status} size={16} />
  106. <StyledLink
  107. to={`/organizations/${organization.slug}/monitors/${monitor.id}/`}
  108. >
  109. {monitor.name}
  110. </StyledLink>
  111. {monitor.nextCheckIn ? (
  112. <StyledTimeSince date={monitor.lastCheckIn} />
  113. ) : (
  114. t('n/a')
  115. )}
  116. </PanelItemCentered>
  117. ))}
  118. </PanelBody>
  119. </Panel>
  120. {monitorListPageLinks && (
  121. <Pagination pageLinks={monitorListPageLinks} {...this.props} />
  122. )}
  123. </Fragment>
  124. );
  125. }
  126. }
  127. const HeaderTitle = styled(PageHeading)`
  128. display: flex;
  129. align-items: center;
  130. justify-content: space-between;
  131. flex: 1;
  132. `;
  133. const PanelItemCentered = styled(PanelItem)`
  134. align-items: center;
  135. padding: 0;
  136. padding-left: ${space(2)};
  137. padding-right: ${space(2)};
  138. `;
  139. const StyledLink = styled(Link)`
  140. flex: 1;
  141. padding: ${space(2)};
  142. `;
  143. const StyledTimeSince = styled(TimeSince)`
  144. font-variant-numeric: tabular-nums;
  145. `;
  146. const Filters = styled('div')`
  147. display: grid;
  148. grid-template-columns: minmax(auto, 300px) 1fr;
  149. gap: ${space(1.5)};
  150. margin-bottom: ${space(2)};
  151. `;
  152. export default withRouter(withOrganization(Monitors));