import type { UserIntentsResponse, Address } from '@sodax/sdk'; import { useQuery, type UseQueryResult } from '@tanstack/react-query'; import { useSodaxContext } from '../shared/useSodaxContext.js'; import { unwrapResult } from './unwrapResult.js'; import type { ReadHookParams } from '../shared/types.js'; export type UseBackendUserIntentsParams = ReadHookParams< UserIntentsResponse | undefined, { userAddress: Address | undefined; startDate?: number; endDate?: number; } >; /** * React hook for fetching user-created intents from the backend API for a given user address. * * @example * const { data: userIntents } = useBackendUserIntents({ * params: { userAddress: '0x123...' }, * }); */ export const useBackendUserIntents = ({ params, queryOptions, }: UseBackendUserIntentsParams = {}): UseQueryResult => { const { sodax } = useSodaxContext(); const userAddress = params?.userAddress; const startDate = params?.startDate; const endDate = params?.endDate; return useQuery({ queryKey: ['backend', 'intent', 'user', userAddress, startDate, endDate], queryFn: async (): Promise => { if (!userAddress) return undefined; return unwrapResult(await sodax.backendApi.getUserIntents({ userAddress, startDate, endDate })); }, enabled: !!userAddress && userAddress.length > 0, retry: 3, ...queryOptions, }); };