( runId: string | undefined, options?: RunStatusesOptions )
| 40 | } & GetRunStatuses); |
| 41 | |
| 42 | export function useRunStatuses( |
| 43 | runId: string | undefined, |
| 44 | options?: RunStatusesOptions |
| 45 | ): UseRunStatusesResult { |
| 46 | const { apiUrl, publicApiKey, queryClient } = useTriggerProvider(); |
| 47 | |
| 48 | const queryResult = useQuery( |
| 49 | { |
| 50 | queryKey: [`triggerdotdev-run-statuses-${runId}`], |
| 51 | queryFn: async () => { |
| 52 | return await zodfetch(GetRunStatusesSchema, `${apiUrl}/api/v2/runs/${runId}/statuses`, { |
| 53 | method: "GET", |
| 54 | headers: { |
| 55 | Authorization: `Bearer ${publicApiKey}`, |
| 56 | }, |
| 57 | }); |
| 58 | }, |
| 59 | enabled: !!runId, |
| 60 | refetchInterval: (data) => { |
| 61 | if (data?.run.status && runResolvedStatuses.includes(data.run.status)) { |
| 62 | return false; |
| 63 | } |
| 64 | if (options?.refreshIntervalMs !== undefined) { |
| 65 | return Math.max(options.refreshIntervalMs, 500); |
| 66 | } |
| 67 | |
| 68 | return defaultRefreshInterval; |
| 69 | }, |
| 70 | }, |
| 71 | queryClient |
| 72 | ); |
| 73 | |
| 74 | switch (queryResult.status) { |
| 75 | case "pending": { |
| 76 | return { |
| 77 | fetchStatus: "loading", |
| 78 | error: undefined, |
| 79 | statuses: undefined, |
| 80 | run: undefined, |
| 81 | }; |
| 82 | } |
| 83 | case "error": { |
| 84 | return { |
| 85 | fetchStatus: "error", |
| 86 | error: queryResult.error, |
| 87 | statuses: undefined, |
| 88 | run: undefined, |
| 89 | }; |
| 90 | } |
| 91 | case "success": { |
| 92 | return { |
| 93 | fetchStatus: "success", |
| 94 | error: undefined, |
| 95 | run: queryResult.data.run, |
| 96 | statuses: queryResult.data.statuses, |
| 97 | }; |
| 98 | } |
| 99 | } |
no test coverage detected
searching dependent graphs…