123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import {Component, Fragment} from 'react';
- // eslint-disable-next-line no-restricted-imports
- import {browserHistory, withRouter, WithRouterProps} from 'react-router';
- import isEqual from 'lodash/isEqual';
- import omit from 'lodash/omit';
- import * as qs from 'query-string';
- import {fetchOrgMembers, indexMembersByProject} from 'sentry/actionCreators/members';
- import {Client} from 'sentry/api';
- import EmptyStateWarning from 'sentry/components/emptyStateWarning';
- import LoadingError from 'sentry/components/loadingError';
- import LoadingIndicator from 'sentry/components/loadingIndicator';
- import Pagination from 'sentry/components/pagination';
- import {Panel, PanelBody} from 'sentry/components/panels';
- import {parseSearch, Token} from 'sentry/components/searchSyntax/parser';
- import {treeResultLocator} from 'sentry/components/searchSyntax/utils';
- import StreamGroup, {
- DEFAULT_STREAM_GROUP_STATS_PERIOD,
- } from 'sentry/components/stream/group';
- import {t} from 'sentry/locale';
- import GroupStore from 'sentry/stores/groupStore';
- import {Group} from 'sentry/types';
- import {callIfFunction} from 'sentry/utils/callIfFunction';
- import StreamManager from 'sentry/utils/streamManager';
- import withApi from 'sentry/utils/withApi';
- import {TimePeriodType} from 'sentry/views/alerts/rules/metric/details/constants';
- import {RELATED_ISSUES_BOOLEAN_QUERY_ERROR} from 'sentry/views/alerts/rules/metric/details/relatedIssuesNotAvailable';
- import GroupListHeader from './groupListHeader';
- const defaultProps = {
- canSelectGroups: true,
- withChart: true,
- withPagination: true,
- useFilteredStats: true,
- useTintRow: true,
- narrowGroups: false,
- };
- type Props = WithRouterProps & {
- api: Client;
- endpointPath: string;
- orgId: string;
- query: string;
- customStatsPeriod?: TimePeriodType;
- onFetchSuccess?: (
- groupListState: State,
- onCursor: (
- cursor: string,
- path: string,
- query: Record<string, any>,
- pageDiff: number
- ) => void
- ) => void;
- queryFilterDescription?: string;
- queryParams?: Record<string, number | string | string[] | undefined | null>;
- renderEmptyMessage?: () => React.ReactNode;
- renderErrorMessage?: ({detail: string}, retry: () => void) => React.ReactNode;
- } & Partial<typeof defaultProps>;
- type State = {
- error: boolean;
- errorData: {detail: string} | null;
- groups: Group[];
- loading: boolean;
- pageLinks: string | null;
- memberList?: ReturnType<typeof indexMembersByProject>;
- };
- class GroupList extends Component<Props, State> {
- static defaultProps = defaultProps;
- state: State = {
- loading: true,
- error: false,
- errorData: null,
- groups: [],
- pageLinks: null,
- };
- componentDidMount() {
- this.fetchData();
- }
- shouldComponentUpdate(nextProps: Props, nextState: State) {
- return (
- !isEqual(this.state, nextState) ||
- nextProps.endpointPath !== this.props.endpointPath ||
- nextProps.query !== this.props.query ||
- !isEqual(nextProps.queryParams, this.props.queryParams)
- );
- }
- componentDidUpdate(prevProps: Props) {
- const ignoredQueryParams = ['end'];
- if (
- prevProps.orgId !== this.props.orgId ||
- prevProps.endpointPath !== this.props.endpointPath ||
- prevProps.query !== this.props.query ||
- !isEqual(
- omit(prevProps.queryParams, ignoredQueryParams),
- omit(this.props.queryParams, ignoredQueryParams)
- )
- ) {
- this.fetchData();
- }
- }
- componentWillUnmount() {
- GroupStore.reset();
- callIfFunction(this.listener);
- }
- listener = GroupStore.listen(() => this.onGroupChange(), undefined);
- private _streamManager = new StreamManager(GroupStore);
- fetchData = async () => {
- GroupStore.loadInitialData([]);
- const {api, orgId, queryParams} = this.props;
- api.clear();
- this.setState({loading: true, error: false, errorData: null});
- fetchOrgMembers(api, orgId).then(members => {
- this.setState({memberList: indexMembersByProject(members)});
- });
- const endpoint = this.getGroupListEndpoint();
- const parsedQuery = parseSearch((queryParams ?? this.getQueryParams()).query);
- const hasLogicBoolean = parsedQuery
- ? treeResultLocator<boolean>({
- tree: parsedQuery,
- noResultValue: false,
- visitorTest: ({token, returnResult}) => {
- return token.type === Token.LogicBoolean ? returnResult(true) : null;
- },
- })
- : false;
- // Check if the alert rule query has AND or OR
- // logic queries haven't been implemented for issue search yet
- if (hasLogicBoolean) {
- this.setState({
- error: true,
- errorData: {detail: RELATED_ISSUES_BOOLEAN_QUERY_ERROR},
- loading: false,
- });
- return;
- }
- try {
- const [data, , jqXHR] = await api.requestPromise(endpoint, {
- includeAllArgs: true,
- });
- this._streamManager.push(data);
- this.setState(
- {
- error: false,
- errorData: null,
- loading: false,
- pageLinks: jqXHR?.getResponseHeader('Link') ?? null,
- },
- () => {
- this.props.onFetchSuccess?.(this.state, this.handleCursorChange);
- }
- );
- } catch (error) {
- this.setState({error: true, errorData: error.responseJSON, loading: false});
- }
- };
- getGroupListEndpoint() {
- const {orgId, endpointPath, queryParams} = this.props;
- const path = endpointPath ?? `/organizations/${orgId}/issues/`;
- const queryParameters = queryParams ?? this.getQueryParams();
- return `${path}?${qs.stringify(queryParameters)}`;
- }
- getQueryParams() {
- const {location, query} = this.props;
- const queryParams = location.query;
- queryParams.limit = 50;
- queryParams.sort = 'new';
- queryParams.query = query;
- return queryParams;
- }
- handleCursorChange(
- cursor: string | undefined,
- path: string,
- query: Record<string, any>,
- pageDiff: number
- ) {
- const queryPageInt = parseInt(query.page, 10);
- let nextPage: number | undefined = isNaN(queryPageInt)
- ? pageDiff
- : queryPageInt + pageDiff;
- // unset cursor and page when we navigate back to the first page
- // also reset cursor if somehow the previous button is enabled on
- // first page and user attempts to go backwards
- if (nextPage <= 0) {
- cursor = undefined;
- nextPage = undefined;
- }
- browserHistory.push({
- pathname: path,
- query: {...query, cursor, page: nextPage},
- });
- }
- onGroupChange() {
- const groups = this._streamManager.getAllItems();
- if (!isEqual(groups, this.state.groups)) {
- this.setState({groups});
- }
- }
- render() {
- const {
- canSelectGroups,
- withChart,
- renderEmptyMessage,
- renderErrorMessage,
- withPagination,
- useFilteredStats,
- useTintRow,
- customStatsPeriod,
- queryParams,
- queryFilterDescription,
- narrowGroups,
- } = this.props;
- const {loading, error, errorData, groups, memberList, pageLinks} = this.state;
- if (loading) {
- return <LoadingIndicator />;
- }
- if (error) {
- if (typeof renderErrorMessage === 'function' && errorData) {
- return renderErrorMessage(errorData, this.fetchData);
- }
- return <LoadingError onRetry={this.fetchData} />;
- }
- if (groups.length === 0) {
- if (typeof renderEmptyMessage === 'function') {
- return renderEmptyMessage();
- }
- return (
- <Panel>
- <PanelBody>
- <EmptyStateWarning>
- <p>{t("There don't seem to be any events fitting the query.")}</p>
- </EmptyStateWarning>
- </PanelBody>
- </Panel>
- );
- }
- const statsPeriod =
- queryParams?.groupStatsPeriod === 'auto'
- ? queryParams?.groupStatsPeriod
- : DEFAULT_STREAM_GROUP_STATS_PERIOD;
- return (
- <Fragment>
- <Panel>
- <GroupListHeader withChart={!!withChart} narrowGroups={narrowGroups} />
- <PanelBody>
- {groups.map(({id, project}) => {
- const members = memberList?.hasOwnProperty(project.slug)
- ? memberList[project.slug]
- : undefined;
- return (
- <StreamGroup
- key={id}
- id={id}
- canSelect={canSelectGroups}
- withChart={withChart}
- memberList={members}
- useFilteredStats={useFilteredStats}
- useTintRow={useTintRow}
- customStatsPeriod={customStatsPeriod}
- statsPeriod={statsPeriod}
- queryFilterDescription={queryFilterDescription}
- narrowGroups={narrowGroups}
- />
- );
- })}
- </PanelBody>
- </Panel>
- {withPagination && (
- <Pagination pageLinks={pageLinks} onCursor={this.handleCursorChange} />
- )}
- </Fragment>
- );
- }
- }
- export {GroupList};
- export default withApi(withRouter(GroupList));
|