projectIssues.tsx 9.3 KB

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