index.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import {Fragment, useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import {promptsCheck, promptsUpdate} from 'sentry/actionCreators/prompts';
  4. import Feature from 'sentry/components/acl/feature';
  5. import {Alert} from 'sentry/components/alert';
  6. import {LinkButton} from 'sentry/components/button';
  7. import CreateAlertButton from 'sentry/components/createAlertButton';
  8. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  12. import Pagination from 'sentry/components/pagination';
  13. import {PanelTable} from 'sentry/components/panels/panelTable';
  14. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  18. import type {Organization} from 'sentry/types/organization';
  19. import type {Project} from 'sentry/types/project';
  20. import {trackAnalytics} from 'sentry/utils/analytics';
  21. import Projects from 'sentry/utils/projects';
  22. import FilterBar from '../../filterBar';
  23. import type {Incident} from '../../types';
  24. import {getQueryStatus, getTeamParams} from '../../utils';
  25. import AlertHeader from '../header';
  26. import Onboarding from '../onboarding';
  27. import AlertListRow from './row';
  28. const DOCS_URL =
  29. 'https://docs.sentry.io/workflow/alerts-notifications/alerts/?_ga=2.21848383.580096147.1592364314-1444595810.1582160976';
  30. type Props = RouteComponentProps<{}, {}> & {
  31. organization: Organization;
  32. };
  33. type State = {
  34. incidentList: Incident[];
  35. /**
  36. * User has not yet seen the 'alert_stream' welcome prompt for this
  37. * organization.
  38. */
  39. firstVisitShown?: boolean;
  40. /**
  41. * Is there at least one alert rule configured for the currently selected
  42. * projects?
  43. */
  44. hasAlertRule?: boolean;
  45. };
  46. class IncidentsList extends DeprecatedAsyncComponent<
  47. Props,
  48. State & DeprecatedAsyncComponent['state']
  49. > {
  50. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  51. const {organization, location} = this.props;
  52. const {query} = location;
  53. const status = getQueryStatus(query.status);
  54. return [
  55. [
  56. 'incidentList',
  57. `/organizations/${organization.slug}/incidents/`,
  58. {
  59. query: {
  60. ...query,
  61. status: status === 'all' ? undefined : status,
  62. team: getTeamParams(query.team),
  63. expand: ['original_alert_rule'],
  64. },
  65. },
  66. ],
  67. ];
  68. }
  69. /**
  70. * If our incidentList is empty, determine if we've configured alert rules or
  71. * if the user has seen the welcome prompt.
  72. */
  73. async onLoadAllEndpointsSuccess() {
  74. const {incidentList} = this.state;
  75. if (!incidentList || incidentList.length !== 0) {
  76. this.setState({hasAlertRule: true, firstVisitShown: false});
  77. return;
  78. }
  79. this.setState({loading: true});
  80. // Check if they have rules or not, to know which empty state message to
  81. // display
  82. const {location, organization} = this.props;
  83. const alertRules = await this.api.requestPromise(
  84. `/organizations/${organization.slug}/alert-rules/`,
  85. {
  86. method: 'GET',
  87. query: location.query,
  88. }
  89. );
  90. const hasAlertRule = alertRules.length > 0;
  91. // We've already configured alert rules, no need to check if we should show
  92. // the "first time welcome" prompt
  93. if (hasAlertRule) {
  94. this.setState({hasAlertRule, firstVisitShown: false, loading: false});
  95. return;
  96. }
  97. // Check if they have already seen the prompt for the alert stream
  98. const prompt = await promptsCheck(this.api, {
  99. organization,
  100. feature: 'alert_stream',
  101. });
  102. const firstVisitShown = !prompt?.dismissedTime;
  103. if (firstVisitShown) {
  104. // Prompt has not been seen, mark the prompt as seen immediately so they
  105. // don't see it again
  106. promptsUpdate(this.api, {
  107. organization,
  108. feature: 'alert_stream',
  109. status: 'dismissed',
  110. });
  111. }
  112. this.setState({hasAlertRule, firstVisitShown, loading: false});
  113. }
  114. get projectsFromIncidents() {
  115. const {incidentList} = this.state;
  116. return [...new Set(incidentList?.flatMap(({projects}) => projects))];
  117. }
  118. handleChangeSearch = (title: string) => {
  119. const {router, location} = this.props;
  120. const {cursor: _cursor, page: _page, ...currentQuery} = location.query;
  121. router.push({
  122. pathname: location.pathname,
  123. query: {
  124. ...currentQuery,
  125. title,
  126. },
  127. });
  128. };
  129. handleChangeFilter = (activeFilters: string[]) => {
  130. const {router, location} = this.props;
  131. const {cursor: _cursor, page: _page, ...currentQuery} = location.query;
  132. router.push({
  133. pathname: location.pathname,
  134. query: {
  135. ...currentQuery,
  136. // Preserve empty team query parameter
  137. team: activeFilters.length > 0 ? activeFilters : '',
  138. },
  139. });
  140. };
  141. handleChangeStatus = (value: string): void => {
  142. const {router, location} = this.props;
  143. const {cursor: _cursor, page: _page, ...currentQuery} = location.query;
  144. router.push({
  145. pathname: location.pathname,
  146. query: {
  147. ...currentQuery,
  148. status: value === 'all' ? undefined : value,
  149. },
  150. });
  151. };
  152. tryRenderOnboarding() {
  153. const {firstVisitShown} = this.state;
  154. const {organization} = this.props;
  155. if (!firstVisitShown) {
  156. return null;
  157. }
  158. const actions = (
  159. <Fragment>
  160. <LinkButton size="sm" external href={DOCS_URL}>
  161. {t('View Features')}
  162. </LinkButton>
  163. <CreateAlertButton
  164. organization={organization}
  165. iconProps={{size: 'xs'}}
  166. size="sm"
  167. priority="primary"
  168. referrer="alert_stream"
  169. >
  170. {t('Create Alert')}
  171. </CreateAlertButton>
  172. </Fragment>
  173. );
  174. return <Onboarding actions={actions} />;
  175. }
  176. renderLoading() {
  177. return this.renderBody();
  178. }
  179. renderList() {
  180. const {loading, incidentList, incidentListPageLinks, hasAlertRule} = this.state;
  181. const {organization} = this.props;
  182. const checkingForAlertRules =
  183. incidentList?.length === 0 && hasAlertRule === undefined;
  184. const showLoadingIndicator = loading || checkingForAlertRules;
  185. return (
  186. <Fragment>
  187. {this.tryRenderOnboarding() ?? (
  188. <StyledPanelTable
  189. isLoading={showLoadingIndicator}
  190. isEmpty={incidentList?.length === 0}
  191. emptyMessage={t('No incidents exist for the current query.')}
  192. emptyAction={
  193. <EmptyStateAction>
  194. {tct('Learn more about [link:Metric Alerts]', {
  195. link: <ExternalLink href={DOCS_URL} />,
  196. })}
  197. </EmptyStateAction>
  198. }
  199. headers={[
  200. t('Alert Rule'),
  201. t('Triggered'),
  202. t('Duration'),
  203. t('Project'),
  204. t('Alert ID'),
  205. t('Team'),
  206. ]}
  207. >
  208. <Projects orgId={organization.slug} slugs={this.projectsFromIncidents}>
  209. {({initiallyLoaded, projects}) =>
  210. incidentList.map(incident => (
  211. <AlertListRow
  212. key={incident.id}
  213. projectsLoaded={initiallyLoaded}
  214. projects={projects as Project[]}
  215. incident={incident}
  216. organization={organization}
  217. />
  218. ))
  219. }
  220. </Projects>
  221. </StyledPanelTable>
  222. )}
  223. <Pagination pageLinks={incidentListPageLinks} />
  224. </Fragment>
  225. );
  226. }
  227. renderBody() {
  228. const {organization, router, location} = this.props;
  229. return (
  230. <SentryDocumentTitle title={t('Alerts')} orgSlug={organization.slug}>
  231. <PageFiltersContainer>
  232. <AlertHeader router={router} activeTab="stream" />
  233. <Layout.Body>
  234. <Layout.Main fullWidth>
  235. {!this.tryRenderOnboarding() && (
  236. <Fragment>
  237. <StyledAlert showIcon>
  238. {t('This page only shows metric alerts.')}
  239. </StyledAlert>
  240. <FilterBar
  241. location={location}
  242. onChangeFilter={this.handleChangeFilter}
  243. onChangeSearch={this.handleChangeSearch}
  244. onChangeStatus={this.handleChangeStatus}
  245. hasStatusFilters
  246. />
  247. </Fragment>
  248. )}
  249. {this.renderList()}
  250. </Layout.Main>
  251. </Layout.Body>
  252. </PageFiltersContainer>
  253. </SentryDocumentTitle>
  254. );
  255. }
  256. }
  257. function IncidentsListContainer(props: Props) {
  258. useEffect(() => {
  259. trackAnalytics('alert_stream.viewed', {
  260. organization: props.organization,
  261. });
  262. // eslint-disable-next-line react-hooks/exhaustive-deps
  263. }, []);
  264. const renderDisabled = () => (
  265. <Layout.Body>
  266. <Layout.Main fullWidth>
  267. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  268. </Layout.Main>
  269. </Layout.Body>
  270. );
  271. return (
  272. <Feature
  273. features="incidents"
  274. hookName="feature-disabled:alerts-page"
  275. renderDisabled={renderDisabled}
  276. >
  277. <IncidentsList {...props} />
  278. </Feature>
  279. );
  280. }
  281. const StyledPanelTable = styled(PanelTable)`
  282. font-size: ${p => p.theme.fontSizeMedium};
  283. & > div {
  284. padding: ${space(1.5)} ${space(2)};
  285. }
  286. `;
  287. const StyledAlert = styled(Alert)`
  288. margin-bottom: ${space(1.5)};
  289. `;
  290. const EmptyStateAction = styled('p')`
  291. font-size: ${p => p.theme.fontSizeLarge};
  292. `;
  293. export default IncidentsListContainer;