projectIssues.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import {Fragment, useCallback, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import pick from 'lodash/pick';
  5. import * as qs from 'query-string';
  6. import type {Client} from 'sentry/api';
  7. import {LinkButton} from 'sentry/components/button';
  8. import ButtonBar from 'sentry/components/buttonBar';
  9. import DiscoverButton from 'sentry/components/discoverButton';
  10. import GroupList from 'sentry/components/issues/groupList';
  11. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  12. import Pagination from 'sentry/components/pagination';
  13. import Panel from 'sentry/components/panels/panel';
  14. import PanelBody from 'sentry/components/panels/panelBody';
  15. import QueryCount from 'sentry/components/queryCount';
  16. import {SegmentedControl} from 'sentry/components/segmentedControl';
  17. import {DEFAULT_RELATIVE_PERIODS, DEFAULT_STATS_PERIOD} from 'sentry/constants';
  18. import {URL_PARAM} from 'sentry/constants/pageFilters';
  19. import {t, tct} from 'sentry/locale';
  20. import {space} from 'sentry/styles/space';
  21. import type {Organization} from 'sentry/types/organization';
  22. import {trackAnalytics} from 'sentry/utils/analytics';
  23. import {browserHistory} from 'sentry/utils/browserHistory';
  24. import {SavedQueryDatasets} from 'sentry/utils/discover/types';
  25. import {decodeScalar} from 'sentry/utils/queryString';
  26. import {appendQueryDatasetParam} from 'sentry/views/dashboards/utils';
  27. import {makeDiscoverPathname} from 'sentry/views/discover/pathnames';
  28. import NoGroupsHandler from '../issueList/noGroupsHandler';
  29. enum IssuesType {
  30. NEW = 'new',
  31. UNHANDLED = 'unhandled',
  32. REGRESSED = 'regressed',
  33. RESOLVED = 'resolved',
  34. ALL = 'all',
  35. }
  36. enum IssuesQuery {
  37. NEW = 'is:unresolved is:for_review',
  38. UNHANDLED = 'error.unhandled:true is:unresolved',
  39. REGRESSED = 'regressed_in_release:latest',
  40. RESOLVED = 'is:resolved',
  41. ALL = '',
  42. }
  43. type Count = {
  44. all: number;
  45. new: number;
  46. regressed: number;
  47. resolved: number;
  48. unhandled: number;
  49. };
  50. type Props = {
  51. api: Client;
  52. location: Location;
  53. organization: Organization;
  54. projectId: number;
  55. query?: string;
  56. };
  57. function ProjectIssues({organization, location, projectId, query, api}: Props) {
  58. const [pageLinks, setPageLinks] = useState<string | undefined>();
  59. const [onCursor, setOnCursor] = useState<(() => void) | undefined>();
  60. const [issuesType, setIssuesType] = useState<IssuesType>(
  61. (location.query.issuesType as IssuesType) || IssuesType.UNHANDLED
  62. );
  63. const [issuesCount, setIssuesCount] = useState<Count>({
  64. all: 0,
  65. new: 0,
  66. regressed: 0,
  67. resolved: 0,
  68. unhandled: 0,
  69. });
  70. const fetchIssuesCount = useCallback(async () => {
  71. const getIssueCountEndpoint = (queryParameters: any) => {
  72. const issuesCountPath = `/organizations/${organization.slug}/issues-count/`;
  73. return `${issuesCountPath}?${qs.stringify(queryParameters)}`;
  74. };
  75. const params = [
  76. `${IssuesQuery.NEW}`,
  77. `${IssuesQuery.ALL}`,
  78. `${IssuesQuery.RESOLVED}`,
  79. `${IssuesQuery.UNHANDLED}`,
  80. `${IssuesQuery.REGRESSED}`,
  81. ];
  82. const queryParams = params.map(param => param);
  83. const queryParameters = {
  84. project: projectId,
  85. query: queryParams,
  86. ...(!location.query.start && {
  87. statsPeriod: location.query.statsPeriod || DEFAULT_STATS_PERIOD,
  88. }),
  89. start: location.query.start,
  90. end: location.query.end,
  91. environment: location.query.environment,
  92. cursor: location.query.cursor,
  93. };
  94. const issueCountEndpoint = getIssueCountEndpoint(queryParameters);
  95. try {
  96. const data = await api.requestPromise(issueCountEndpoint);
  97. setIssuesCount({
  98. all: data[`${IssuesQuery.ALL}`] || 0,
  99. new: data[`${IssuesQuery.NEW}`] || 0,
  100. resolved: data[`${IssuesQuery.RESOLVED}`] || 0,
  101. unhandled: data[`${IssuesQuery.UNHANDLED}`] || 0,
  102. regressed: data[`${IssuesQuery.REGRESSED}`] || 0,
  103. });
  104. } catch {
  105. // do nothing
  106. }
  107. }, [
  108. api,
  109. location.query.cursor,
  110. location.query.end,
  111. location.query.environment,
  112. location.query.start,
  113. location.query.statsPeriod,
  114. organization.slug,
  115. projectId,
  116. ]);
  117. useEffect(() => {
  118. fetchIssuesCount();
  119. }, [fetchIssuesCount]);
  120. function handleOpenInIssuesClick() {
  121. trackAnalytics('project_detail.open_issues', {organization});
  122. }
  123. function handleOpenInDiscoverClick() {
  124. trackAnalytics('project_detail.open_discover', {organization});
  125. }
  126. function handleFetchSuccess(groupListState: any, cursorHandler: any) {
  127. setPageLinks(groupListState.pageLinks);
  128. setOnCursor(() => cursorHandler);
  129. }
  130. const discoverQuery =
  131. issuesType === 'unhandled'
  132. ? ['event.type:error error.unhandled:true', query].join(' ').trim()
  133. : ['event.type:error', query].join(' ').trim();
  134. function getDiscoverUrl() {
  135. return {
  136. pathname: makeDiscoverPathname({
  137. path: `/results/`,
  138. organization,
  139. }),
  140. query: {
  141. name: t('Frequent Unhandled Issues'),
  142. field: ['issue', 'title', 'count()', 'count_unique(user)', 'project'],
  143. sort: '-count',
  144. query: discoverQuery,
  145. display: 'top5',
  146. ...normalizeDateTimeParams(pick(location.query, [...Object.values(URL_PARAM)])),
  147. ...appendQueryDatasetParam(organization, SavedQueryDatasets.ERRORS),
  148. },
  149. };
  150. }
  151. const endpointPath = `/organizations/${organization.slug}/issues/`;
  152. const issueQuery = (Object.values(IssuesType) as string[]).includes(issuesType)
  153. ? // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  154. [`${IssuesQuery[issuesType.toUpperCase()]}`, query].join(' ').trim()
  155. : [`${IssuesQuery.ALL}`, query].join(' ').trim();
  156. const queryParams = {
  157. limit: '5',
  158. ...normalizeDateTimeParams(
  159. pick(location.query, [...Object.values(URL_PARAM), 'cursor'])
  160. ),
  161. query: issueQuery,
  162. sort: 'freq',
  163. };
  164. const issueSearch = {
  165. pathname: endpointPath,
  166. query: queryParams,
  167. };
  168. function handleIssuesTypeSelection(issueType: IssuesType) {
  169. const to = {
  170. ...location,
  171. query: {
  172. ...location.query,
  173. issuesType: issueType,
  174. },
  175. };
  176. browserHistory.replace(to);
  177. setIssuesType(issueType);
  178. }
  179. function renderEmptyMessage() {
  180. const selectedTimePeriod = location.query.start
  181. ? null
  182. : // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  183. DEFAULT_RELATIVE_PERIODS[
  184. decodeScalar(location.query.statsPeriod, DEFAULT_STATS_PERIOD)
  185. ];
  186. const displayedPeriod = selectedTimePeriod
  187. ? selectedTimePeriod.toLowerCase()
  188. : t('given timeframe');
  189. return (
  190. <Panel>
  191. <PanelBody>
  192. <NoGroupsHandler
  193. api={api}
  194. organization={organization}
  195. query={issueQuery}
  196. selectedProjectIds={[projectId]}
  197. groupIds={[]}
  198. emptyMessage={tct('No [issuesType] issues for the [timePeriod].', {
  199. issuesType: issuesType === 'all' ? '' : issuesType,
  200. timePeriod: displayedPeriod,
  201. })}
  202. />
  203. </PanelBody>
  204. </Panel>
  205. );
  206. }
  207. const issuesTypes = [
  208. {value: IssuesType.ALL, label: t('All Issues'), issueCount: issuesCount.all},
  209. {value: IssuesType.NEW, label: t('New Issues'), issueCount: issuesCount.new},
  210. {
  211. value: IssuesType.UNHANDLED,
  212. label: t('Unhandled'),
  213. issueCount: issuesCount.unhandled,
  214. },
  215. {
  216. value: IssuesType.REGRESSED,
  217. label: t('Regressed'),
  218. issueCount: issuesCount.regressed,
  219. },
  220. {
  221. value: IssuesType.RESOLVED,
  222. label: t('Resolved'),
  223. issueCount: issuesCount.resolved,
  224. },
  225. ];
  226. return (
  227. <Fragment>
  228. <ControlsWrapper>
  229. <SegmentedControl
  230. aria-label={t('Issue type')}
  231. value={issuesType}
  232. onChange={value => handleIssuesTypeSelection(value)}
  233. size="xs"
  234. >
  235. {issuesTypes.map(({value, label, issueCount}) => (
  236. <SegmentedControl.Item key={value} textValue={label}>
  237. {label}&nbsp;
  238. <QueryCount count={issueCount} max={99} hideParens hideIfEmpty={false} />
  239. </SegmentedControl.Item>
  240. ))}
  241. </SegmentedControl>
  242. <OpenInButtonBar gap={1}>
  243. <LinkButton
  244. data-test-id="issues-open"
  245. size="xs"
  246. to={issueSearch}
  247. onClick={handleOpenInIssuesClick}
  248. >
  249. {t('Open in Issues')}
  250. </LinkButton>
  251. <DiscoverButton
  252. onClick={handleOpenInDiscoverClick}
  253. to={getDiscoverUrl()}
  254. size="xs"
  255. >
  256. {t('Open in Discover')}
  257. </DiscoverButton>
  258. <StyledPagination pageLinks={pageLinks} onCursor={onCursor} size="xs" />
  259. </OpenInButtonBar>
  260. </ControlsWrapper>
  261. <GroupList
  262. orgSlug={organization.slug}
  263. queryParams={queryParams}
  264. canSelectGroups={false}
  265. renderEmptyMessage={renderEmptyMessage}
  266. withChart={false}
  267. withPagination={false}
  268. onFetchSuccess={handleFetchSuccess}
  269. source="project"
  270. />
  271. </Fragment>
  272. );
  273. }
  274. const ControlsWrapper = styled('div')`
  275. display: flex;
  276. align-items: flex-end;
  277. justify-content: space-between;
  278. margin-bottom: ${space(1)};
  279. flex-wrap: wrap;
  280. `;
  281. const OpenInButtonBar = styled(ButtonBar)`
  282. margin-top: ${space(1)};
  283. @media (max-width: ${p => p.theme.breakpoints.small}) {
  284. width: 100%;
  285. }
  286. `;
  287. const StyledPagination = styled(Pagination)`
  288. margin: 0;
  289. `;
  290. export default ProjectIssues;