useAutofix.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {useCallback, useEffect, useState} from 'react';
  2. import type {AutofixData, GroupWithAutofix} from 'sentry/components/events/autofix/types';
  3. import type {Event} from 'sentry/types';
  4. import {useApiQuery} from 'sentry/utils/queryClient';
  5. import useApi from 'sentry/utils/useApi';
  6. const POLL_INTERVAL = 2500;
  7. export const useAiAutofix = (group: GroupWithAutofix, event: Event) => {
  8. const api = useApi();
  9. const [overwriteData, setOverwriteData] = useState<AutofixData | 'reset' | null>(null);
  10. const autofixData =
  11. (overwriteData === 'reset' ? null : overwriteData ?? group.metadata?.autofix) ?? null;
  12. const isPolling = autofixData?.status === 'PROCESSING';
  13. const {
  14. data: apiData,
  15. isError,
  16. refetch: dataRefetch,
  17. error,
  18. } = useApiQuery<{autofix: AutofixData | null}>([`/issues/${group.id}/ai-autofix/`], {
  19. staleTime: Infinity,
  20. retry: false,
  21. enabled: !autofixData?.status || autofixData.status === 'PROCESSING',
  22. refetchInterval: data => {
  23. if (data?.[0]?.autofix?.status === 'PROCESSING') {
  24. return POLL_INTERVAL;
  25. }
  26. return false;
  27. },
  28. });
  29. useEffect(() => {
  30. if (overwriteData !== 'reset' && apiData?.autofix) {
  31. setOverwriteData(apiData.autofix);
  32. }
  33. }, [apiData?.autofix, overwriteData]);
  34. const triggerAutofix = useCallback(
  35. async (instruction: string) => {
  36. setOverwriteData({
  37. status: 'PROCESSING',
  38. steps: [
  39. {
  40. id: '1',
  41. index: 0,
  42. status: 'PROCESSING',
  43. title: 'Starting Autofix...',
  44. progress: [],
  45. },
  46. ],
  47. created_at: new Date().toISOString(),
  48. });
  49. try {
  50. await api.requestPromise(`/issues/${group.id}/ai-autofix/`, {
  51. method: 'POST',
  52. data: {
  53. event_id: event.id,
  54. instruction,
  55. },
  56. });
  57. } catch (e) {
  58. // Don't need to do anything, error should be in the metadata
  59. }
  60. dataRefetch();
  61. },
  62. [api, group.id, event.id, dataRefetch]
  63. );
  64. const reset = useCallback(() => {
  65. setOverwriteData('reset');
  66. }, []);
  67. return {
  68. autofixData,
  69. error,
  70. isError,
  71. isPolling,
  72. triggerAutofix,
  73. reset,
  74. };
  75. };