Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Aug 2, 2024
1 parent f95f4fa commit f89a9b3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/dapp-connector/src/dapp-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ export type DappConnectorManager = {
trustedUrl: string,
sendMessage: (id: string, result: unknown, error?: Error) => void,
): Promise<void>
readonly chainId: number
}

export const dappConnectorMaker = (storage: Storage, wallet: ResolverWallet, api: Api): DappConnector => {
return new DappConnector(storage, wallet, api)
}

export class DappConnector implements DappConnectorManager {
constructor(private storage: Storage, private wallet: ResolverWallet, private api: Api) {}
chainId: number
constructor(private storage: Storage, private wallet: ResolverWallet, private api: Api) {
this.chainId = wallet.networkId
}

async getDAppList() {
return this.api.getDApps({chainId: this.wallet.networkId})
Expand Down
1 change: 1 addition & 0 deletions packages/dapp-connector/src/manager.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {DappConnectorManager} from './dapp-connector'
import {DappListResponse} from './adapters/api'

export const managerMock: DappConnectorManager = {
chainId: 1,
getDAppList(): Promise<DappListResponse> {
return Promise.resolve(mockedDAppList)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('useDappList', () => {
</QueryClientProvider>
)

const {result} = renderHook(() => useDappList({suspense: false}), {wrapper})
const {result} = renderHook(() => useDappList(), {wrapper})
await waitFor(() => expect(result.current.data?.dapps).toBeDefined())
await waitFor(() => expect(result.current.data?.filters).toBeDefined())
client.clear()
Expand Down
12 changes: 8 additions & 4 deletions packages/dapp-connector/src/translators/reactjs/useDappList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import {useQuery, UseQueryOptions} from 'react-query'
import {DappListResponse} from '../../adapters/api'
import {useDappConnector} from './DappConnectorProvider'

export const useDappList = (options?: UseQueryOptions<DappListResponse, Error, DappListResponse, ['dappList']>) => {
export const useDappList = (
options?: UseQueryOptions<DappListResponse, Error, DappListResponse, ['dappList', string]>,
) => {
const {manager} = useDappConnector()
return useQuery(['dappList'], {
return useQuery(['dappList', String(manager.chainId)], {
queryFn: () => manager.getDAppList(),
refetchOnMount: true,
suspense: true,
refetchOnMount: false,
refetchInterval: ONE_DAY,
...options,
})
}

const ONE_DAY = 1000 * 60 * 60 * 24

0 comments on commit f89a9b3

Please sign in to comment.