import { useQuery, UseQueryOptions, UseQueryResult, } from '@tanstack/react-query'; import { useDataProvider } from '../context/tushan'; import type { GetOneParams, BasicRecord } from './types'; export function useGetOne( resource: string, { id, meta }: GetOneParams, options?: UseQueryOptions ): UseGetOneHookValue { const dataProvider = useDataProvider(); return useQuery( // Sometimes the id comes as a string (e.g. when read from the URL in a Show view). // Sometimes the id comes as a number (e.g. when read from a Record in useGetList response). // As the react-query cache is type-sensitive, we always stringify the identifier to get a match [resource, 'getOne', { id: String(id), meta }], () => dataProvider .getOne(resource, { id, meta }) .then(({ data }) => data), options ); } export type UseGetOneHookValue = UseQueryResult;