Skip to content

Commit 44da84c

Browse files
committed
separate out useClient and useGateway
1 parent 661d04b commit 44da84c

10 files changed

Lines changed: 81 additions & 62 deletions

File tree

src/Sample.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import { assertEx } from '@xylabs/assert'
55
import type { Hash } from '@xylabs/hex'
66
import { isHash } from '@xylabs/hex'
77
import { isUndefined } from '@xylabs/typeof'
8+
import type { GatewayName } from '@xyo-network/xl1-protocol'
89
import { useState } from 'react'
910

1011
import {
1112
Onboarding, SubmitTransactionButton, TxConfirmedAlert, WelcomeStack,
1213
} from './components/index.ts'
13-
import { buildSamplePayloads } from './helpers/index.ts'
14-
import { useDefaultGateway } from './hooks/index.ts'
14+
import { buildSamplePayloads, LocalGatewayName } from './helpers/index.ts'
15+
import { useGateway } from './hooks/index.ts'
1516

1617
export const XL1BrowserSample = () => {
1718
const [error, setError] = useState<Error>()
1819
const [confirmed, setConfirmed] = useState<Hash>()
19-
const { gateway, error: gatewayError } = useDefaultGateway()
20+
const { gateway, error: gatewayError } = useGateway(LocalGatewayName)
2021

2122
const submitTransaction = async () => {
2223
setError(undefined)

src/components/buttons/SubmitTransactionButton.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import type { ButtonProps } from '@mui/material'
22
import { Button } from '@mui/material'
33
import { isUndefined } from '@xylabs/typeof'
44

5-
import { useDefaultGateway, useOnBoarding } from '../../hooks/index.ts'
5+
import { LocalGatewayName } from '../../helpers/index.ts'
6+
import { useGateway, useOnBoarding } from '../../hooks/index.ts'
67

78
export const SubmitTransactionButton: React.FC<ButtonProps> = (props) => {
8-
const { gateway } = useDefaultGateway()
9+
const { gateway } = useGateway(LocalGatewayName)
910
const { showSubmitTransaction } = useOnBoarding()
1011

1112
return showSubmitTransaction

src/helpers/LocalGatewayName.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type { GatewayName } from '@xyo-network/xl1-protocol'
2+
3+
export const LocalGatewayName = 'local' as GatewayName

src/helpers/getXyoClient.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { XyoClient } from '@xyo-network/xl1-protocol'
2+
3+
const CLIENT_LISTENER_TIMEOUT = 5000
4+
5+
const hasXyoClient = () => {
6+
return 'client' in globalThis.xyo
7+
}
8+
9+
export const listenForClientInjection = (onClientReady: () => void, onTimeout: () => void) => {
10+
let resolved = false
11+
const listener: EventListener = () => {
12+
onClientReady()
13+
resolved = true
14+
}
15+
globalThis.addEventListener('xyo:plugin-ready', listener)
16+
setTimeout(() => {
17+
if (!resolved) {
18+
onTimeout()
19+
}
20+
}, CLIENT_LISTENER_TIMEOUT)
21+
}
22+
23+
export async function getXyoClient(): Promise<XyoClient | undefined> {
24+
return hasXyoClient()
25+
? globalThis.xyo.client
26+
// listen for the XyoWallet to be injected
27+
: await new Promise<XyoClient | undefined>((resolve, reject) => {
28+
listenForClientInjection(
29+
() => {
30+
resolve(globalThis.xyo.client)
31+
},
32+
() => {
33+
reject(new Error('XYO Client not installed'))
34+
},
35+
)
36+
})
37+
}

src/helpers/getXyoGateway.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/helpers/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './buildSamplePayloads.ts'
2-
export * from './getXyoGateway.ts'
2+
export * from './getXyoClient.ts'
3+
export * from './LocalGatewayName.ts'

src/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './useDefaultGateway.ts'
1+
export * from './useGateway.ts'
22
export * from './useOnBoarding.ts'
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import type { XyoGatewayProvider } from '@xyo-network/xl1-protocol'
1+
import type { XyoClient } from '@xyo-network/xl1-protocol'
22
import { useSyncExternalStore } from 'react'
33

4-
import { getXyoGateway } from '../helpers/index.ts'
4+
import { getXyoClient } from '../helpers/index.ts'
55

6-
interface GatewayState {
6+
interface ClientState {
7+
client?: XyoClient
78
error: Error | null
8-
gateway?: XyoGatewayProvider
99
isLoading: boolean
1010
}
1111

12-
let currentState: GatewayState = {
13-
gateway: undefined,
12+
let currentState: ClientState = {
13+
client: undefined,
1414
error: null,
1515
isLoading: false,
1616
}
@@ -21,20 +21,20 @@ const emitChange = () => {
2121
for (const listener of listeners) listener()
2222
}
2323

24-
const updateState = (newState: Partial<GatewayState>) => {
24+
const updateState = (newState: Partial<ClientState>) => {
2525
currentState = { ...currentState, ...newState }
2626
emitChange()
2727
}
2828

29-
const initializeGateway = async () => {
30-
if (currentState.isLoading || currentState.gateway) return
29+
const initializeClient = async () => {
30+
if (currentState.isLoading || currentState.client) return
3131

3232
updateState({ isLoading: true, error: null })
3333

3434
try {
35-
const gateway = await getXyoGateway()
35+
const client = await getXyoClient()
3636
updateState({
37-
gateway, isLoading: false, error: null,
37+
client, isLoading: false, error: null,
3838
})
3939
} catch (error) {
4040
updateState({ error: error as Error, isLoading: false })
@@ -44,15 +44,15 @@ const initializeGateway = async () => {
4444
const subscribe = (listener: () => void) => {
4545
listeners.add(listener)
4646

47-
void initializeGateway()
47+
void initializeClient()
4848

4949
return () => {
5050
listeners.delete(listener)
5151
}
5252
}
5353

54-
const getSnapshot = (): GatewayState => currentState
54+
const getSnapshot = (): ClientState => currentState
5555

56-
export const useDefaultGateway = () => {
56+
export const useClient = () => {
5757
return useSyncExternalStore(subscribe, getSnapshot)
5858
}

src/hooks/useGateway.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { GatewayName } from '@xyo-network/xl1-protocol'
2+
3+
import { useClient } from './useClient.ts'
4+
5+
export const useGateway = (gatewayName?: GatewayName) => {
6+
const {
7+
client, isLoading, error,
8+
} = useClient()
9+
return {
10+
gateway: gatewayName ? client?.gateways?.[gatewayName] : undefined,
11+
isLoading,
12+
error,
13+
}
14+
}

src/hooks/useOnBoarding.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { isDefined, isUndefined } from '@xylabs/typeof'
22
import { useCheckLocalRpc } from '@xyo-network/react-chain-provider'
33

4-
import { useDefaultGateway } from './useDefaultGateway.ts'
4+
import { LocalGatewayName } from '../helpers/index.ts'
5+
import { useGateway } from './useGateway.ts'
56

67
export const useOnBoarding = () => {
7-
const { gateway } = useDefaultGateway()
8+
const { gateway } = useGateway(LocalGatewayName)
89
const { isLocalProducer } = useCheckLocalRpc()
910

1011
const producerIsReachable = isLocalProducer

0 commit comments

Comments
 (0)