1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-11-16 02:04:54 +01:00
market/src/providers/UrqlProvider.tsx
2021-07-06 01:21:35 +03:00

47 lines
1.2 KiB
TypeScript

import { createClient, Provider, Client } from 'urql'
import React, { useState, useEffect, ReactNode, ReactElement } from 'react'
import { useWeb3 } from './Web3'
import { Logger } from '@oceanprotocol/lib'
import { getOceanConfig } from '../utils/ocean'
let urqlClient: Client
function createUrqlClient(subgraphUri: string) {
const client = createClient({
url: `${subgraphUri}/subgraphs/name/oceanprotocol/ocean-subgraph`
})
return client
}
export function getUrqlClientInstance(): Client {
return urqlClient
}
export default function UrqlClientProvider({
children
}: {
children: ReactNode
}): ReactElement {
const { networkId } = useWeb3()
const [client, setClient] = useState<Client>()
useEffect(() => {
const oceanConfig = getOceanConfig(networkId || 1)
if (!oceanConfig?.subgraphUri) {
Logger.error(
'No subgraphUri defined, preventing UrqlProvider from initialization.'
)
return
}
const newClient = createUrqlClient(oceanConfig.subgraphUri)
urqlClient = newClient
setClient(newClient)
Logger.log(`[URQL] Client connected to ${oceanConfig.subgraphUri}`)
}, [networkId])
return client ? <Provider value={client}>{children}</Provider> : <></>
}
export { UrqlClientProvider }