groupList.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import {Component} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {browserHistory, withRouter, WithRouterProps} from 'react-router';
  4. import isEqual from 'lodash/isEqual';
  5. import omit from 'lodash/omit';
  6. import * as qs from 'query-string';
  7. import {fetchOrgMembers, indexMembersByProject} from 'sentry/actionCreators/members';
  8. import {Client} from 'sentry/api';
  9. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  10. import LoadingError from 'sentry/components/loadingError';
  11. import LoadingIndicator from 'sentry/components/loadingIndicator';
  12. import Pagination from 'sentry/components/pagination';
  13. import {Panel, PanelBody} from 'sentry/components/panels';
  14. import IssuesReplayCountProvider from 'sentry/components/replays/issuesReplayCountProvider';
  15. import {parseSearch, Token} from 'sentry/components/searchSyntax/parser';
  16. import {treeResultLocator} from 'sentry/components/searchSyntax/utils';
  17. import StreamGroup, {
  18. DEFAULT_STREAM_GROUP_STATS_PERIOD,
  19. } from 'sentry/components/stream/group';
  20. import {t} from 'sentry/locale';
  21. import GroupStore from 'sentry/stores/groupStore';
  22. import {Group} from 'sentry/types';
  23. import withApi from 'sentry/utils/withApi';
  24. import {TimePeriodType} from 'sentry/views/alerts/rules/metric/details/constants';
  25. import {RELATED_ISSUES_BOOLEAN_QUERY_ERROR} from 'sentry/views/alerts/rules/metric/details/relatedIssuesNotAvailable';
  26. import GroupListHeader from './groupListHeader';
  27. const defaultProps = {
  28. canSelectGroups: true,
  29. withChart: true,
  30. withPagination: true,
  31. useFilteredStats: true,
  32. useTintRow: true,
  33. narrowGroups: false,
  34. };
  35. type Props = WithRouterProps & {
  36. api: Client;
  37. endpointPath: string;
  38. orgId: string;
  39. query: string;
  40. customStatsPeriod?: TimePeriodType;
  41. onFetchSuccess?: (
  42. groupListState: State,
  43. onCursor: (
  44. cursor: string,
  45. path: string,
  46. query: Record<string, any>,
  47. pageDiff: number
  48. ) => void
  49. ) => void;
  50. queryFilterDescription?: string;
  51. queryParams?: Record<string, number | string | string[] | undefined | null>;
  52. renderEmptyMessage?: () => React.ReactNode;
  53. renderErrorMessage?: (props: {detail: string}, retry: () => void) => React.ReactNode;
  54. // where the group list is rendered
  55. source?: string;
  56. } & Partial<typeof defaultProps>;
  57. type State = {
  58. error: boolean;
  59. errorData: {detail: string} | null;
  60. groups: Group[];
  61. loading: boolean;
  62. pageLinks: string | null;
  63. memberList?: ReturnType<typeof indexMembersByProject>;
  64. };
  65. class GroupList extends Component<Props, State> {
  66. static defaultProps = defaultProps;
  67. state: State = {
  68. loading: true,
  69. error: false,
  70. errorData: null,
  71. groups: [],
  72. pageLinks: null,
  73. };
  74. componentDidMount() {
  75. this.fetchData();
  76. }
  77. shouldComponentUpdate(nextProps: Props, nextState: State) {
  78. return (
  79. !isEqual(this.state, nextState) ||
  80. nextProps.endpointPath !== this.props.endpointPath ||
  81. nextProps.query !== this.props.query ||
  82. !isEqual(nextProps.queryParams, this.props.queryParams)
  83. );
  84. }
  85. componentDidUpdate(prevProps: Props) {
  86. const ignoredQueryParams = ['end'];
  87. if (
  88. prevProps.orgId !== this.props.orgId ||
  89. prevProps.endpointPath !== this.props.endpointPath ||
  90. prevProps.query !== this.props.query ||
  91. !isEqual(
  92. omit(prevProps.queryParams, ignoredQueryParams),
  93. omit(this.props.queryParams, ignoredQueryParams)
  94. )
  95. ) {
  96. this.fetchData();
  97. }
  98. }
  99. componentWillUnmount() {
  100. GroupStore.reset();
  101. this.listener?.();
  102. }
  103. listener = GroupStore.listen(() => this.onGroupChange(), undefined);
  104. fetchData = async () => {
  105. GroupStore.loadInitialData([]);
  106. const {api, orgId, queryParams} = this.props;
  107. api.clear();
  108. this.setState({loading: true, error: false, errorData: null});
  109. fetchOrgMembers(api, orgId).then(members => {
  110. this.setState({memberList: indexMembersByProject(members)});
  111. });
  112. const endpoint = this.getGroupListEndpoint();
  113. const parsedQuery = parseSearch((queryParams ?? this.getQueryParams()).query);
  114. const hasLogicBoolean = parsedQuery
  115. ? treeResultLocator<boolean>({
  116. tree: parsedQuery,
  117. noResultValue: false,
  118. visitorTest: ({token, returnResult}) => {
  119. return token.type === Token.LogicBoolean ? returnResult(true) : null;
  120. },
  121. })
  122. : false;
  123. // Check if the alert rule query has AND or OR
  124. // logic queries haven't been implemented for issue search yet
  125. if (hasLogicBoolean) {
  126. this.setState({
  127. error: true,
  128. errorData: {detail: RELATED_ISSUES_BOOLEAN_QUERY_ERROR},
  129. loading: false,
  130. });
  131. return;
  132. }
  133. try {
  134. const [data, , jqXHR] = await api.requestPromise(endpoint, {
  135. includeAllArgs: true,
  136. });
  137. GroupStore.add(data);
  138. this.setState(
  139. {
  140. error: false,
  141. errorData: null,
  142. loading: false,
  143. pageLinks: jqXHR?.getResponseHeader('Link') ?? null,
  144. },
  145. () => {
  146. this.props.onFetchSuccess?.(this.state, this.handleCursorChange);
  147. }
  148. );
  149. } catch (error) {
  150. this.setState({error: true, errorData: error.responseJSON, loading: false});
  151. }
  152. };
  153. getGroupListEndpoint() {
  154. const {orgId, endpointPath, queryParams} = this.props;
  155. const path = endpointPath ?? `/organizations/${orgId}/issues/`;
  156. const queryParameters = queryParams ?? this.getQueryParams();
  157. return `${path}?${qs.stringify(queryParameters)}`;
  158. }
  159. getQueryParams() {
  160. const {location, query} = this.props;
  161. const queryParams = location.query;
  162. queryParams.limit = 50;
  163. queryParams.sort = 'new';
  164. queryParams.query = query;
  165. return queryParams;
  166. }
  167. handleCursorChange(
  168. cursor: string | undefined,
  169. path: string,
  170. query: Record<string, any>,
  171. pageDiff: number
  172. ) {
  173. const queryPageInt = parseInt(query.page, 10);
  174. let nextPage: number | undefined = isNaN(queryPageInt)
  175. ? pageDiff
  176. : queryPageInt + pageDiff;
  177. // unset cursor and page when we navigate back to the first page
  178. // also reset cursor if somehow the previous button is enabled on
  179. // first page and user attempts to go backwards
  180. if (nextPage <= 0) {
  181. cursor = undefined;
  182. nextPage = undefined;
  183. }
  184. browserHistory.push({
  185. pathname: path,
  186. query: {...query, cursor, page: nextPage},
  187. });
  188. }
  189. onGroupChange() {
  190. const groups = GroupStore.getAllItems() as Group[];
  191. if (!isEqual(groups, this.state.groups)) {
  192. this.setState({groups});
  193. }
  194. }
  195. render() {
  196. const {
  197. canSelectGroups,
  198. withChart,
  199. renderEmptyMessage,
  200. renderErrorMessage,
  201. withPagination,
  202. useFilteredStats,
  203. useTintRow,
  204. customStatsPeriod,
  205. queryParams,
  206. queryFilterDescription,
  207. narrowGroups,
  208. source,
  209. } = this.props;
  210. const {loading, error, errorData, groups, memberList, pageLinks} = this.state;
  211. if (loading) {
  212. return <LoadingIndicator />;
  213. }
  214. if (error) {
  215. if (typeof renderErrorMessage === 'function' && errorData) {
  216. return renderErrorMessage(errorData, this.fetchData);
  217. }
  218. return <LoadingError onRetry={this.fetchData} />;
  219. }
  220. if (groups.length === 0) {
  221. if (typeof renderEmptyMessage === 'function') {
  222. return renderEmptyMessage();
  223. }
  224. return (
  225. <Panel>
  226. <PanelBody>
  227. <EmptyStateWarning>
  228. <p>{t("There don't seem to be any events fitting the query.")}</p>
  229. </EmptyStateWarning>
  230. </PanelBody>
  231. </Panel>
  232. );
  233. }
  234. const statsPeriod =
  235. queryParams?.groupStatsPeriod === 'auto'
  236. ? queryParams?.groupStatsPeriod
  237. : DEFAULT_STREAM_GROUP_STATS_PERIOD;
  238. return (
  239. <IssuesReplayCountProvider groupIds={groups.map(({id}) => id)}>
  240. <Panel>
  241. <GroupListHeader withChart={!!withChart} narrowGroups={narrowGroups} />
  242. <PanelBody>
  243. {groups.map(({id, project}) => {
  244. const members = memberList?.hasOwnProperty(project.slug)
  245. ? memberList[project.slug]
  246. : undefined;
  247. return (
  248. <StreamGroup
  249. key={id}
  250. id={id}
  251. canSelect={canSelectGroups}
  252. withChart={withChart}
  253. memberList={members}
  254. useFilteredStats={useFilteredStats}
  255. useTintRow={useTintRow}
  256. customStatsPeriod={customStatsPeriod}
  257. statsPeriod={statsPeriod}
  258. queryFilterDescription={queryFilterDescription}
  259. narrowGroups={narrowGroups}
  260. source={source}
  261. />
  262. );
  263. })}
  264. </PanelBody>
  265. </Panel>
  266. {withPagination && (
  267. <Pagination pageLinks={pageLinks} onCursor={this.handleCursorChange} />
  268. )}
  269. </IssuesReplayCountProvider>
  270. );
  271. }
  272. }
  273. export {GroupList};
  274. export default withApi(withRouter(GroupList));