|
@@ -1,4 +1,5 @@
|
|
|
import {Component, useContext} from 'react';
|
|
|
+import {useQuery} from '@tanstack/react-query';
|
|
|
import {Location} from 'history';
|
|
|
|
|
|
import {EventQuery} from 'sentry/actionCreators/events';
|
|
@@ -13,6 +14,8 @@ import {QueryBatching} from 'sentry/utils/performance/contexts/genericQueryBatch
|
|
|
import {PerformanceEventViewContext} from 'sentry/utils/performance/contexts/performanceEventViewContext';
|
|
|
import {OrganizationContext} from 'sentry/views/organizationContext';
|
|
|
|
|
|
+import useApi from '../useApi';
|
|
|
+
|
|
|
export class QueryError {
|
|
|
message: string;
|
|
|
private originalError: any; // For debugging in case parseError picks a value that doesn't make sense.
|
|
@@ -51,7 +54,6 @@ type OptionalContextProps = {
|
|
|
};
|
|
|
|
|
|
type BaseDiscoverQueryProps = {
|
|
|
- api: Client;
|
|
|
/**
|
|
|
* Used as the default source for cursor values.
|
|
|
*/
|
|
@@ -142,6 +144,7 @@ type Props<T, P> = InnerRequestProps<P> & ReactProps<T> & ComponentProps<T, P>;
|
|
|
type OuterProps<T, P> = OuterRequestProps<P> & ReactProps<T> & ComponentProps<T, P>;
|
|
|
|
|
|
type State<T> = {
|
|
|
+ api: Client;
|
|
|
tableFetchID: symbol | undefined;
|
|
|
} & GenericChildrenProps<T>;
|
|
|
|
|
@@ -156,6 +159,7 @@ class _GenericDiscoverQuery<T, P> extends Component<Props<T, P>, State<T>> {
|
|
|
|
|
|
tableData: null,
|
|
|
pageLinks: null,
|
|
|
+ api: new Client(),
|
|
|
};
|
|
|
|
|
|
componentDidMount() {
|
|
@@ -179,36 +183,9 @@ class _GenericDiscoverQuery<T, P> extends Component<Props<T, P>, State<T>> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- getPayload(props: Props<T, P>) {
|
|
|
- const {cursor, limit, noPagination, referrer} = props;
|
|
|
- const payload = this.props.getRequestPayload
|
|
|
- ? this.props.getRequestPayload(props)
|
|
|
- : props.eventView.getEventsAPIPayload(
|
|
|
- props.location,
|
|
|
- props.forceAppendRawQueryString
|
|
|
- );
|
|
|
-
|
|
|
- if (cursor) {
|
|
|
- payload.cursor = cursor;
|
|
|
- }
|
|
|
- if (limit) {
|
|
|
- payload.per_page = limit;
|
|
|
- }
|
|
|
- if (noPagination) {
|
|
|
- payload.noPagination = noPagination;
|
|
|
- }
|
|
|
- if (referrer) {
|
|
|
- payload.referrer = referrer;
|
|
|
- }
|
|
|
-
|
|
|
- Object.assign(payload, props.queryExtras ?? {});
|
|
|
-
|
|
|
- return payload;
|
|
|
- }
|
|
|
-
|
|
|
_shouldRefetchData = (prevProps: Props<T, P>): boolean => {
|
|
|
- const thisAPIPayload = this.getPayload(this.props);
|
|
|
- const otherAPIPayload = this.getPayload(prevProps);
|
|
|
+ const thisAPIPayload = getPayload(this.props);
|
|
|
+ const otherAPIPayload = getPayload(prevProps);
|
|
|
|
|
|
return (
|
|
|
!isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload) ||
|
|
@@ -246,7 +223,6 @@ class _GenericDiscoverQuery<T, P> extends Component<Props<T, P>, State<T>> {
|
|
|
|
|
|
fetchData = async () => {
|
|
|
const {
|
|
|
- api,
|
|
|
queryBatching,
|
|
|
beforeFetch,
|
|
|
afterFetch,
|
|
@@ -256,6 +232,7 @@ class _GenericDiscoverQuery<T, P> extends Component<Props<T, P>, State<T>> {
|
|
|
route,
|
|
|
setError,
|
|
|
} = this.props;
|
|
|
+ const {api} = this.state;
|
|
|
|
|
|
if (!eventView.isValid()) {
|
|
|
return;
|
|
@@ -263,7 +240,7 @@ class _GenericDiscoverQuery<T, P> extends Component<Props<T, P>, State<T>> {
|
|
|
|
|
|
const url = `/organizations/${orgSlug}/${route}/`;
|
|
|
const tableFetchID = Symbol(`tableFetchID`);
|
|
|
- const apiPayload: Partial<EventQuery & LocationQuery> = this.getPayload(this.props);
|
|
|
+ const apiPayload: Partial<EventQuery & LocationQuery> = getPayload(this.props);
|
|
|
|
|
|
this.setState({isLoading: true, tableFetchID});
|
|
|
|
|
@@ -371,4 +348,49 @@ export function doDiscoverQuery<T>(
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+function getPayload<T, P>(props: Props<T, P>) {
|
|
|
+ const {
|
|
|
+ cursor,
|
|
|
+ limit,
|
|
|
+ noPagination,
|
|
|
+ referrer,
|
|
|
+ getRequestPayload,
|
|
|
+ eventView,
|
|
|
+ location,
|
|
|
+ forceAppendRawQueryString,
|
|
|
+ } = props;
|
|
|
+ const payload = getRequestPayload
|
|
|
+ ? getRequestPayload(props)
|
|
|
+ : eventView.getEventsAPIPayload(location, forceAppendRawQueryString);
|
|
|
+
|
|
|
+ if (cursor) {
|
|
|
+ payload.cursor = cursor;
|
|
|
+ }
|
|
|
+ if (limit) {
|
|
|
+ payload.per_page = limit;
|
|
|
+ }
|
|
|
+ if (noPagination) {
|
|
|
+ payload.noPagination = noPagination;
|
|
|
+ }
|
|
|
+ if (referrer) {
|
|
|
+ payload.referrer = referrer;
|
|
|
+ }
|
|
|
+
|
|
|
+ Object.assign(payload, props.queryExtras ?? {});
|
|
|
+
|
|
|
+ return payload;
|
|
|
+}
|
|
|
+
|
|
|
+export function useGenericDiscoverQuery<T, P>(props: Props<T, P>) {
|
|
|
+ const api = useApi();
|
|
|
+ const {orgSlug, route} = props;
|
|
|
+ const url = `/organizations/${orgSlug}/${route}/`;
|
|
|
+ const apiPayload = getPayload<T, P>(props);
|
|
|
+
|
|
|
+ return useQuery<T, QueryError>([route, apiPayload], async () => {
|
|
|
+ const [resp] = await doDiscoverQuery<T>(api, url, apiPayload, props.queryBatching);
|
|
|
+ return resp;
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
export default GenericDiscoverQuery;
|