import { useQuery } from 'react-query' ; import * as db from '../db' ; import * as api from '../api' ; export type CurrentOwner = db . Owner & api . OwnerRes ; async function fetchOwner ( ) { let ownerRes = await api . getOwner ( ) ; let owner = await db . getOwner ( ownerRes . id ) ; if ( ! owner ) { owner = await db . addOwner ( ownerRes ) ; } if ( owner . publicKey && ! ownerRes . publicKey ) { ownerRes = await api . patchOwner ( { publicKey : owner . publicKey , } ) ; } if ( ! owner . publicKey && ownerRes . publicKey ) { owner . publicKey = ownerRes . publicKey ; owner = await db . updateOwner ( owner ) ; } return { ... owner , ... ownerRes } ; } export function useOwner ( ) { return useQuery ( 'owner' , fetchOwner , { retry : 1 , refetchInterval : false , refetchIntervalInBackground : false , refetchOnWindowFocus : false , } ) ; }