|
| 1 | +import { useCallback, useEffect, useState } from 'react'; |
| 2 | + |
| 3 | +import type { |
| 4 | + EnterpriseConnectionTestRunResource, |
| 5 | + GetEnterpriseConnectionTestRunsParams, |
| 6 | +} from '../../types/enterpriseConnectionTestRun'; |
| 7 | +import { useClerkInstanceContext } from '../contexts'; |
| 8 | +import { useClerkQueryClient } from '../query/use-clerk-query-client'; |
| 9 | +import { useClerkQuery } from '../query/useQuery'; |
| 10 | +import { useUserBase } from './base/useUserBase'; |
| 11 | +import { useClearQueriesOnSignOut } from './useClearQueriesOnSignOut'; |
| 12 | +import { useEnterpriseConnectionTestRunsCacheKeys } from './useEnterpriseConnectionTestRuns.shared'; |
| 13 | + |
| 14 | +const DEFAULT_POLL_INTERVAL_MS = 2_000; |
| 15 | + |
| 16 | +export type UseEnterpriseConnectionTestRunsParams = { |
| 17 | + enterpriseConnectionId: string | null; |
| 18 | + /** |
| 19 | + * Pass-through fetch parameters (pagination, status filter). |
| 20 | + * Defaults to `{ initialPage: 1, pageSize: 10 }`. |
| 21 | + */ |
| 22 | + params?: GetEnterpriseConnectionTestRunsParams; |
| 23 | + /** |
| 24 | + * Polling interval (ms) applied between `revalidate()` and the moment the |
| 25 | + * first record arrives in the response. |
| 26 | + * |
| 27 | + * @default 2000 |
| 28 | + */ |
| 29 | + pollIntervalMs?: number; |
| 30 | + /** |
| 31 | + * If `false`, the hook is dormant — no fetch, no polling. |
| 32 | + * |
| 33 | + * @default true |
| 34 | + */ |
| 35 | + enabled?: boolean; |
| 36 | +}; |
| 37 | + |
| 38 | +export type UseEnterpriseConnectionTestRunsReturn = { |
| 39 | + data: EnterpriseConnectionTestRunResource[] | undefined; |
| 40 | + totalCount: number | undefined; |
| 41 | + error: Error | null; |
| 42 | + isLoading: boolean; |
| 43 | + isFetching: boolean; |
| 44 | + /** |
| 45 | + * `true` while the hook is actively polling for the first record to appear |
| 46 | + */ |
| 47 | + isPolling: boolean; |
| 48 | + /** |
| 49 | + * Force a refetch and (if the list is currently empty) arm polling |
| 50 | + */ |
| 51 | + revalidate: () => Promise<void>; |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Subscribes to the list of enterprise-connection test runs for the signed-in user |
| 56 | + * |
| 57 | + * @internal |
| 58 | + */ |
| 59 | +function useEnterpriseConnectionTestRuns( |
| 60 | + params: UseEnterpriseConnectionTestRunsParams, |
| 61 | +): UseEnterpriseConnectionTestRunsReturn { |
| 62 | + const { |
| 63 | + enterpriseConnectionId, |
| 64 | + params: fetchParams = { initialPage: 1, pageSize: 10 }, |
| 65 | + pollIntervalMs = DEFAULT_POLL_INTERVAL_MS, |
| 66 | + enabled = true, |
| 67 | + } = params; |
| 68 | + |
| 69 | + const clerk = useClerkInstanceContext(); |
| 70 | + const user = useUserBase(); |
| 71 | + const [queryClient] = useClerkQueryClient(); |
| 72 | + |
| 73 | + const { queryKey, invalidationKey, stableKey, authenticated } = useEnterpriseConnectionTestRunsCacheKeys({ |
| 74 | + userId: user?.id ?? null, |
| 75 | + enterpriseConnectionId, |
| 76 | + args: fetchParams, |
| 77 | + }); |
| 78 | + |
| 79 | + useClearQueriesOnSignOut({ |
| 80 | + isSignedOut: user === null, |
| 81 | + authenticated, |
| 82 | + stableKeys: stableKey, |
| 83 | + }); |
| 84 | + |
| 85 | + const queryEnabled = enabled && clerk.loaded && Boolean(user) && Boolean(enterpriseConnectionId); |
| 86 | + |
| 87 | + const [shouldPoll, setShouldPoll] = useState(false); |
| 88 | + |
| 89 | + const query = useClerkQuery({ |
| 90 | + queryKey, |
| 91 | + queryFn: () => { |
| 92 | + if (!enterpriseConnectionId) { |
| 93 | + throw new Error('enterpriseConnectionId is required to fetch test runs'); |
| 94 | + } |
| 95 | + return user?.getEnterpriseConnectionTestRuns(enterpriseConnectionId, fetchParams); |
| 96 | + }, |
| 97 | + refetchInterval: q => { |
| 98 | + if (!shouldPoll) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + const hasRows = (q.state.data?.data?.length ?? 0) > 0; |
| 103 | + return hasRows ? false : pollIntervalMs; |
| 104 | + }, |
| 105 | + enabled: queryEnabled, |
| 106 | + refetchIntervalInBackground: false, |
| 107 | + }); |
| 108 | + |
| 109 | + const hasRows = (query.data?.data?.length ?? 0) > 0; |
| 110 | + |
| 111 | + useEffect(() => { |
| 112 | + if (shouldPoll && hasRows) { |
| 113 | + setShouldPoll(false); |
| 114 | + } |
| 115 | + }, [shouldPoll, hasRows]); |
| 116 | + |
| 117 | + const revalidate = useCallback(async () => { |
| 118 | + // Only arm polling when there is nothing in the list yet — once any record |
| 119 | + // has been seen, this is a one-shot refetch. |
| 120 | + if (!hasRows) { |
| 121 | + setShouldPoll(true); |
| 122 | + } |
| 123 | + await queryClient.invalidateQueries({ queryKey: invalidationKey }); |
| 124 | + }, [queryClient, invalidationKey, hasRows]); |
| 125 | + |
| 126 | + const isPolling = queryEnabled && shouldPoll && !hasRows; |
| 127 | + |
| 128 | + return { |
| 129 | + data: query.data?.data, |
| 130 | + totalCount: query.data?.total_count, |
| 131 | + error: query.error ?? null, |
| 132 | + isLoading: query.isLoading, |
| 133 | + isFetching: query.isFetching, |
| 134 | + isPolling, |
| 135 | + revalidate, |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +export { useEnterpriseConnectionTestRuns as __internal_useEnterpriseConnectionTestRuns }; |
0 commit comments