Skip to content

Commit a409855

Browse files
committed
ref(tsc): move automationFireHistory to apiOptions
1 parent d4ad198 commit a409855

File tree

2 files changed

+48
-53
lines changed

2 files changed

+48
-53
lines changed

static/app/views/automations/components/automationHistoryList.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Fragment, useMemo} from 'react';
22
import styled from '@emotion/styled';
3+
import {useQuery} from '@tanstack/react-query';
34
import {PlatformIcon} from 'platformicons';
45

56
import {Flex} from '@sentry/scraps/layout';
@@ -11,11 +12,12 @@ import {Pagination} from 'sentry/components/pagination';
1112
import {Placeholder} from 'sentry/components/placeholder';
1213
import {SimpleTable} from 'sentry/components/tables/simpleTable';
1314
import {t, tct} from 'sentry/locale';
15+
import {selectJsonWithHeaders} from 'sentry/utils/api/apiOptions';
1416
import {parseCursor} from 'sentry/utils/cursor';
1517
import {useLocation} from 'sentry/utils/useLocation';
1618
import {useNavigate} from 'sentry/utils/useNavigate';
1719
import {useOrganization} from 'sentry/utils/useOrganization';
18-
import {useAutomationFireHistoryQuery} from 'sentry/views/automations/hooks';
20+
import {automationFireHistoryApiOptions} from 'sentry/views/automations/hooks';
1921
import {makeMonitorDetailsPathname} from 'sentry/views/detectors/pathnames';
2022

2123
const DEFAULT_HISTORY_PER_PAGE = 10;
@@ -63,36 +65,37 @@ export function AutomationHistoryList({
6365
const cursor =
6466
typeof location.query.cursor === 'string' ? location.query.cursor : undefined;
6567

66-
const {
67-
data: fireHistory = [],
68-
isLoading,
69-
isError,
70-
getResponseHeader,
71-
} = useAutomationFireHistoryQuery(
72-
{automationId, limit, cursor, query},
73-
{enabled: !!automationId}
74-
);
75-
76-
const pageLinks = getResponseHeader?.('Link');
77-
const totalCount = getResponseHeader?.('X-Hits');
78-
const totalCountInt = totalCount ? parseInt(totalCount, 10) : 0;
68+
const {data, isLoading, isError} = useQuery({
69+
...automationFireHistoryApiOptions({
70+
organization: org,
71+
automationId,
72+
cursor,
73+
limit,
74+
query,
75+
}),
76+
select: selectJsonWithHeaders,
77+
});
78+
79+
const fireHistory = data?.json ?? [];
80+
const pageLinks = data?.headers.Link;
81+
const totalCountInt = data?.headers['X-Hits'] ?? 0;
7982

8083
const paginationCaption = useMemo(() => {
81-
if (isLoading || !fireHistory || fireHistory?.length === 0 || limit === null) {
84+
if (isLoading || !data?.json || data.json.length === 0 || limit === null) {
8285
return undefined;
8386
}
8487

8588
const currentCursor = parseCursor(cursor);
8689
const offset = currentCursor?.offset ?? 0;
8790
const startCount = offset * limit + 1;
88-
const endCount = startCount + fireHistory.length - 1;
91+
const endCount = startCount + data.json.length - 1;
8992

9093
return tct('[start]-[end] of [total]', {
9194
start: startCount.toLocaleString(),
9295
end: endCount.toLocaleString(),
9396
total: totalCountInt.toLocaleString(),
9497
});
95-
}, [fireHistory, isLoading, cursor, limit, totalCountInt]);
98+
}, [data?.json, isLoading, cursor, limit, totalCountInt]);
9699

97100
return (
98101
<Fragment>

static/app/views/automations/hooks/index.tsx

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import {queryOptions, skipToken} from '@tanstack/react-query';
2+
13
import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
24
import {t, tn} from 'sentry/locale';
5+
import type {Organization} from 'sentry/types/organization';
36
import type {Action, ActionHandler} from 'sentry/types/workflowEngine/actions';
47
import type {
58
Automation,
@@ -10,6 +13,7 @@ import type {
1013
DataConditionHandler,
1114
DataConditionHandlerGroupType,
1215
} from 'sentry/types/workflowEngine/dataConditions';
16+
import {apiOptions} from 'sentry/utils/api/apiOptions';
1317
import {getApiUrl} from 'sentry/utils/api/getApiUrl';
1418
import type {
1519
ApiQueryKey,
@@ -82,14 +86,14 @@ interface UseAutomationsQueryOptions {
8286
}
8387
export function useAutomationsQuery(
8488
options: UseAutomationsQueryOptions = {},
85-
queryOptions: Partial<UseApiQueryOptions<Automation[]>> = {}
89+
useApiQueryOptions: Partial<UseApiQueryOptions<Automation[]>> = {}
8690
) {
8791
const {slug: orgSlug} = useOrganization();
8892

8993
return useApiQuery<Automation[]>(makeAutomationsQueryKey({orgSlug, ...options}), {
9094
staleTime: 0,
9195
retry: false,
92-
...queryOptions,
96+
...useApiQueryOptions,
9397
});
9498
}
9599

@@ -102,45 +106,33 @@ export function useAutomationQuery(automationId: string) {
102106
});
103107
}
104108

105-
const makeAutomationFireHistoryQueryKey = ({
106-
orgSlug,
107-
automationId,
108-
cursor,
109-
limit,
110-
query = {},
111-
}: {
112-
automationId: string;
113-
orgSlug: string;
114-
cursor?: string;
115-
limit?: number;
116-
query?: Record<string, any>;
117-
}): ApiQueryKey => [
118-
getApiUrl('/organizations/$organizationIdOrSlug/workflows/$workflowId/group-history/', {
119-
path: {organizationIdOrSlug: orgSlug, workflowId: automationId},
120-
}),
121-
{query: {...query, per_page: limit, cursor}},
122-
];
123-
124-
interface UseAutomationFireHistoryQueryOptions {
109+
interface AutomationFireHistoryApiOptionsParams {
125110
automationId: string;
111+
organization: Organization;
126112
cursor?: string;
127113
limit?: number;
128114
query?: Record<string, any>;
129115
}
130-
export function useAutomationFireHistoryQuery(
131-
options: UseAutomationFireHistoryQueryOptions,
132-
queryOptions: Partial<UseApiQueryOptions<AutomationFireHistory[]>> = {}
133-
) {
134-
const {slug} = useOrganization();
135-
136-
return useApiQuery<AutomationFireHistory[]>(
137-
makeAutomationFireHistoryQueryKey({orgSlug: slug, ...options}),
138-
{
139-
staleTime: 5 * 60 * 1000, // 5 minutes
140-
retry: false,
141-
...queryOptions,
142-
}
143-
);
116+
export function automationFireHistoryApiOptions({
117+
automationId,
118+
cursor,
119+
limit,
120+
organization,
121+
query = {},
122+
}: AutomationFireHistoryApiOptionsParams) {
123+
return queryOptions({
124+
...apiOptions.as<AutomationFireHistory[]>()(
125+
'/organizations/$organizationIdOrSlug/workflows/$workflowId/group-history/',
126+
{
127+
path: automationId
128+
? {organizationIdOrSlug: organization.slug, workflowId: automationId}
129+
: skipToken,
130+
query: {...query, per_page: limit, cursor},
131+
staleTime: 5 * 60 * 1000, // 5 minutes
132+
}
133+
),
134+
retry: false,
135+
});
144136
}
145137

146138
export function useDataConditionsQuery(groupType: DataConditionHandlerGroupType) {

0 commit comments

Comments
 (0)