import { useQuery } from '@tanstack/react-query'; import { refundsApi } from '../apis'; import { refundKeys } from '../keys'; import { REFUND_STATUS_GC_TIME, REFUND_STATUS_POLL_INTERVAL_MS, REFUND_STATUS_STALE_TIME, } from '../constants'; import { isTerminalRefundStatus } from '../types'; /** * The customer's read-only refund status for a booking. Polls (`refetchInterval`) **only while the refund * is non-terminal** (`requested`/`approved`/`processing`) and **stops** at `succeeded`/`failed`/`rejected` * — and never polls the empty state (no refund → `null`). A modest `staleTime`/`gcTime` means re-entering * the screen doesn't re-hit the network needlessly; the cancel mutation primes this key so the first render * is warm. `data` is `null` when the booking has no refund. */ export function useRefundStatus(bookingId: number | undefined, options?: { enabled?: boolean }) { return useQuery({ queryKey: refundKeys.byBooking(bookingId ?? -1), queryFn: () => refundsApi.getRefundByBooking(bookingId as number), enabled: (options?.enabled ?? true) && bookingId != null && bookingId > 0, staleTime: REFUND_STATUS_STALE_TIME, gcTime: REFUND_STATUS_GC_TIME, refetchInterval: (query) => { const refund = query.state.data; if (!refund) return false; return isTerminalRefundStatus(refund.refundStatus) ? false : REFUND_STATUS_POLL_INTERVAL_MS; }, }); }