import { useCallback } from 'react' ; import { useSWRConfig } from 'swr' ; import { Nft } from 'types/Nft' ; import { useAuthenticatedUser } from '../users/useUser' ; import useGet from '../_rest/useGet' ; type GetAllNFtsResponse = { nfts : Nft [ ] ; } ; type QueryProps = { userId : string ; } ; const getAllNftsAction = 'fetch all nfts' ; function getAllNftsCacheKey ( { userId } ) { return [ `/nfts/user_get?user_id=${ userId }` , getAllNftsAction ] ; } export default function useAllNfts ( ) { const user = useAuthenticatedUser ( ) ; const data = useGet < GetAllNFtsResponse > ( `/nfts/user_get?user_id=${ user . id }` , getAllNftsAction ) ; if ( ! data ) { throw new Error ( `No NFTs were found for user id: ${ user . id }` ) ; } return data . nfts || [ ] ; } export function useMutateAllNftsCache ( ) { const { id : userId } = useAuthenticatedUser ( ) ; const { mutate } = useSWRConfig ( ) ; return useCallback ( async ( ) => { await mutate ( getAllNftsCacheKey ( { userId } ) ) ; } , [ mutate , userId ] ) ; }