Skip to content

Commit f359d63

Browse files
authored
fix(autofix): Improve the try again experience (#67025)
Previously, Autofix would flash the old state after triggering a new run. Now it should work as expected and go straight into the new run with a blank slate. Because the overwiteData state was a bit janky I rewrote this hook so that it sets to the query cache instead.
1 parent 3ae4a10 commit f359d63

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

static/app/components/events/autofix/useAutofix.tsx

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,68 @@
1-
import {useCallback, useEffect, useState} from 'react';
1+
import {useCallback, useState} from 'react';
22

33
import type {AutofixData, GroupWithAutofix} from 'sentry/components/events/autofix/types';
44
import type {Event} from 'sentry/types';
5-
import {useApiQuery} from 'sentry/utils/queryClient';
5+
import {
6+
type ApiQueryKey,
7+
setApiQueryData,
8+
useApiQuery,
9+
useQueryClient,
10+
} from 'sentry/utils/queryClient';
611
import useApi from 'sentry/utils/useApi';
712

13+
type AutofixResponse = {
14+
autofix: AutofixData | null;
15+
};
16+
817
const POLL_INTERVAL = 2500;
918

19+
const makeAutofixQueryKey = (groupId: string): ApiQueryKey => [
20+
`/issues/${groupId}/ai-autofix/`,
21+
];
22+
23+
const isPolling = (autofixData?: AutofixData | null) =>
24+
autofixData?.status === 'PROCESSING';
25+
1026
export const useAiAutofix = (group: GroupWithAutofix, event: Event) => {
1127
const api = useApi();
28+
const queryClient = useQueryClient();
1229

13-
const [overwriteData, setOverwriteData] = useState<AutofixData | 'reset' | null>(null);
14-
const autofixData =
15-
(overwriteData === 'reset' ? null : overwriteData ?? group.metadata?.autofix) ?? null;
16-
const isPolling = autofixData?.status === 'PROCESSING';
30+
const [isReset, setIsReset] = useState<boolean>(false);
31+
const initialAutofixData = group.metadata?.autofix ?? null;
1732

1833
const {
1934
data: apiData,
2035
isError,
21-
refetch: dataRefetch,
2236
error,
23-
} = useApiQuery<{autofix: AutofixData | null}>([`/issues/${group.id}/ai-autofix/`], {
37+
} = useApiQuery<AutofixResponse>(makeAutofixQueryKey(group.id), {
2438
staleTime: Infinity,
2539
retry: false,
26-
enabled: !autofixData?.status || autofixData.status === 'PROCESSING',
40+
initialData: [{autofix: initialAutofixData}, undefined, undefined],
2741
refetchInterval: data => {
28-
if (data?.[0]?.autofix?.status === 'PROCESSING') {
42+
if (isPolling(data?.[0]?.autofix)) {
2943
return POLL_INTERVAL;
3044
}
3145
return false;
3246
},
3347
});
3448

35-
useEffect(() => {
36-
if (overwriteData !== 'reset' && apiData?.autofix) {
37-
setOverwriteData(apiData.autofix);
38-
}
39-
}, [apiData?.autofix, overwriteData]);
40-
4149
const triggerAutofix = useCallback(
4250
async (instruction: string) => {
43-
setOverwriteData({
44-
status: 'PROCESSING',
45-
steps: [
46-
{
47-
id: '1',
48-
index: 0,
49-
status: 'PROCESSING',
50-
title: 'Starting Autofix...',
51-
progress: [],
52-
},
53-
],
54-
created_at: new Date().toISOString(),
51+
setIsReset(false);
52+
setApiQueryData<AutofixResponse>(queryClient, makeAutofixQueryKey(group.id), {
53+
autofix: {
54+
status: 'PROCESSING',
55+
steps: [
56+
{
57+
id: '1',
58+
index: 0,
59+
status: 'PROCESSING',
60+
title: 'Starting Autofix...',
61+
progress: [],
62+
},
63+
],
64+
created_at: new Date().toISOString(),
65+
},
5566
});
5667

5768
try {
@@ -65,21 +76,21 @@ export const useAiAutofix = (group: GroupWithAutofix, event: Event) => {
6576
} catch (e) {
6677
// Don't need to do anything, error should be in the metadata
6778
}
68-
69-
dataRefetch();
7079
},
71-
[api, group.id, event.id, dataRefetch]
80+
[queryClient, group.id, api, event.id]
7281
);
7382

7483
const reset = useCallback(() => {
75-
setOverwriteData('reset');
84+
setIsReset(true);
7685
}, []);
7786

87+
const autofixData = isReset ? null : apiData?.autofix ?? null;
88+
7889
return {
7990
autofixData,
8091
error,
8192
isError,
82-
isPolling,
93+
isPolling: isPolling(autofixData),
8394
triggerAutofix,
8495
reset,
8596
};

0 commit comments

Comments
 (0)