@xyo-network/react-chain-provider 1.20.2 → 1.20.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/browser/contexts/gateway/Provider.d.ts +0 -2
  2. package/dist/browser/contexts/gateway/Provider.d.ts.map +1 -1
  3. package/dist/browser/contexts/gateway/state.d.ts +1 -2
  4. package/dist/browser/contexts/gateway/state.d.ts.map +1 -1
  5. package/dist/browser/contexts/gateway/story/GatewayStats.d.ts.map +1 -1
  6. package/dist/browser/contexts/in-page-gateways/Provider.d.ts +8 -0
  7. package/dist/browser/contexts/in-page-gateways/Provider.d.ts.map +1 -0
  8. package/dist/browser/contexts/in-page-gateways/context.d.ts +8 -0
  9. package/dist/browser/contexts/in-page-gateways/context.d.ts.map +1 -0
  10. package/dist/browser/contexts/in-page-gateways/index.d.ts +5 -0
  11. package/dist/browser/contexts/in-page-gateways/index.d.ts.map +1 -0
  12. package/dist/browser/contexts/in-page-gateways/state.d.ts +10 -0
  13. package/dist/browser/contexts/in-page-gateways/state.d.ts.map +1 -0
  14. package/dist/browser/contexts/in-page-gateways/use.d.ts +4 -0
  15. package/dist/browser/contexts/in-page-gateways/use.d.ts.map +1 -0
  16. package/dist/browser/contexts/index.d.ts +1 -0
  17. package/dist/browser/contexts/index.d.ts.map +1 -1
  18. package/dist/browser/hooks/client/helpers/index.d.ts +0 -1
  19. package/dist/browser/hooks/client/helpers/index.d.ts.map +1 -1
  20. package/dist/browser/hooks/client/index.d.ts +0 -1
  21. package/dist/browser/hooks/client/index.d.ts.map +1 -1
  22. package/dist/browser/index.d.ts +1 -0
  23. package/dist/browser/index.d.ts.map +1 -1
  24. package/dist/browser/index.mjs +177 -132
  25. package/dist/browser/index.mjs.map +1 -1
  26. package/dist/browser/lib/buildGateway.d.ts.map +1 -0
  27. package/dist/browser/lib/index.d.ts +1 -0
  28. package/dist/browser/lib/index.d.ts.map +1 -1
  29. package/dist/browser/types/ContextGatewayType.d.ts +3 -0
  30. package/dist/browser/types/ContextGatewayType.d.ts.map +1 -0
  31. package/dist/browser/types/index.d.ts +1 -0
  32. package/dist/browser/types/index.d.ts.map +1 -1
  33. package/package.json +15 -15
  34. package/src/contexts/gateway/Provider.tsx +26 -17
  35. package/src/contexts/gateway/state.ts +1 -3
  36. package/src/contexts/gateway/story/GatewayStats.tsx +14 -11
  37. package/src/contexts/gateway/story/Provider.stories.tsx +1 -1
  38. package/src/contexts/gateway/story/ProviderWithWallet.stories.tsx +1 -1
  39. package/src/contexts/in-page-gateways/Provider.tsx +82 -0
  40. package/src/contexts/in-page-gateways/context.ts +5 -0
  41. package/src/contexts/in-page-gateways/index.ts +4 -0
  42. package/src/contexts/in-page-gateways/state.ts +12 -0
  43. package/src/contexts/in-page-gateways/use.ts +5 -0
  44. package/src/contexts/index.ts +1 -0
  45. package/src/hooks/client/helpers/index.ts +0 -1
  46. package/src/hooks/client/index.ts +0 -1
  47. package/src/index.ts +1 -0
  48. package/src/lib/index.ts +1 -0
  49. package/src/types/ContextGatewayType.ts +4 -0
  50. package/src/types/index.ts +1 -0
  51. package/dist/browser/hooks/client/helpers/buildGateway.d.ts.map +0 -1
  52. package/dist/browser/hooks/client/useGatewayFromConfig.d.ts +0 -9
  53. package/dist/browser/hooks/client/useGatewayFromConfig.d.ts.map +0 -1
  54. package/src/hooks/client/useGatewayFromConfig.ts +0 -43
  55. /package/dist/browser/{hooks/client/helpers → lib}/buildGateway.d.ts +0 -0
  56. /package/src/{hooks/client/helpers → lib}/buildGateway.ts +0 -0
@@ -1,25 +1,24 @@
1
- /* eslint-disable react-hooks-extra/no-direct-set-state-in-use-effect */
2
1
  /* eslint-disable react-hooks/set-state-in-effect */
2
+ /* eslint-disable react-hooks-extra/no-direct-set-state-in-use-effect */
3
3
  import { isDefinedNotNull, isNull } from '@xylabs/sdk-js'
4
- import type { AccountInstance } from '@xyo-network/sdk-js'
4
+ import { ErrorRender } from '@xyo-network/react-error'
5
5
  import type { GatewayName } from '@xyo-network/xl1-sdk'
6
6
  import type { PropsWithChildren } from 'react'
7
7
  import React, {
8
8
  useEffect, useMemo, useState,
9
9
  } from 'react'
10
10
 
11
- import { useGatewayFromConfig, useGatewayFromWallet } from '../../hooks/index.ts'
11
+ import { useGatewayFromWallet } from '../../hooks/index.ts'
12
+ import type { ContextGatewayType } from '../../types/index.ts'
13
+ import { useProvidedInPageGateways } from '../in-page-gateways/index.ts'
12
14
  import { GatewayContext } from './context.ts'
13
- import type { ContextGatewayType, GatewayState } from './state.ts'
15
+ import type { GatewayState } from './state.ts'
14
16
 
15
17
  export interface GatewayProviderProps extends PropsWithChildren {
16
- account?: AccountInstance
17
18
  gatewayName?: GatewayName
18
19
  }
19
20
 
20
- export const GatewayProvider: React.FC<GatewayProviderProps> = ({
21
- account, gatewayName, children,
22
- }) => {
21
+ export const GatewayProvider: React.FC<GatewayProviderProps> = ({ gatewayName, children }) => {
23
22
  const [defaultGateway, setDefaultGateway] = useState<ContextGatewayType | null | undefined>()
24
23
  const [gateways, setGateways] = useState<GatewayState['gateways']>({
25
24
  inPageGateway: undefined,
@@ -32,10 +31,13 @@ export const GatewayProvider: React.FC<GatewayProviderProps> = ({
32
31
  } = useGatewayFromWallet(gatewayName)
33
32
 
34
33
  const {
35
- gateway: gatewayFromConfig,
36
- error: gatewayFromConfigError,
37
- clearGateways: clearGatewaysFromConfig,
38
- } = useGatewayFromConfig(gatewayName, account)
34
+ gateways: allGateways,
35
+ errors: allGatewayErrors,
36
+ clearAll,
37
+ } = useProvidedInPageGateways(true)
38
+
39
+ const gatewayFromConfig = gatewayName ? allGateways[gatewayName] : undefined
40
+ const gatewayFromConfigError = gatewayName ? allGatewayErrors[gatewayName] : undefined
39
41
 
40
42
  useEffect(() => {
41
43
  if (isNull(gatewayFromWallet)) {
@@ -56,18 +58,25 @@ export const GatewayProvider: React.FC<GatewayProviderProps> = ({
56
58
 
57
59
  const value = useMemo(() => {
58
60
  const value: GatewayState = {
59
- defaultGateway: defaultGateway,
61
+ defaultGateway,
60
62
  error: gatewayFromWalletError || gatewayFromConfigError,
61
63
  gateways,
62
64
  provided: true,
63
- resetGatewaysFromConfig: clearGatewaysFromConfig,
65
+ resetGatewaysFromConfig: clearAll,
64
66
  }
65
67
  return value
66
- }, [defaultGateway,
68
+ }, [
69
+ defaultGateway,
67
70
  gatewayFromWalletError,
68
71
  gatewayFromConfigError,
69
72
  gateways,
70
- clearGatewaysFromConfig])
73
+ clearAll,
74
+ ])
71
75
 
72
- return <GatewayContext value={value}>{children}</GatewayContext>
76
+ return (
77
+ <GatewayContext value={value}>
78
+ <ErrorRender error={gatewayFromConfigError} />
79
+ {children}
80
+ </GatewayContext>
81
+ )
73
82
  }
@@ -1,8 +1,6 @@
1
1
  import type { ContextExState } from '@xylabs/react-shared'
2
- import type { XyoGateway, XyoGatewayRunner } from '@xyo-network/xl1-sdk'
3
2
 
4
- // Can be removed once there is a gateway type with optional runner properties
5
- export type ContextGatewayType = XyoGateway | XyoGatewayRunner
3
+ import type { ContextGatewayType } from '../../types/index.ts'
6
4
 
7
5
  export interface GatewayStateFields {
8
6
  defaultGateway: ContextGatewayType | undefined | null
@@ -2,6 +2,7 @@ import { Stack, Typography } from '@mui/material'
2
2
  import type { AccountInstance } from '@xyo-network/sdk-js'
3
3
  import type { GatewayName } from '@xyo-network/xl1-sdk'
4
4
 
5
+ import { InPageGatewaysProvider } from '../../in-page-gateways/index.ts'
5
6
  import { GatewayProvider } from '../Provider.tsx'
6
7
  import { GatewayStatus } from './GatewayStatus.tsx'
7
8
 
@@ -15,16 +16,18 @@ export const GatewayStats: React.FC<GatewayStatsProps> = ({
15
16
  account, id, url,
16
17
  }) => {
17
18
  return (
18
- <GatewayProvider key={id} gatewayName={id} account={account}>
19
- <Stack gap={1}>
20
- <Typography variant="subtitle1">
21
- <strong>{id}</strong>
22
- </Typography>
23
- <Typography variant="caption" color="text.secondary">
24
- {url}
25
- </Typography>
26
- <GatewayStatus />
27
- </Stack>
28
- </GatewayProvider>
19
+ <InPageGatewaysProvider account={account}>
20
+ <GatewayProvider key={id} gatewayName={id}>
21
+ <Stack gap={1}>
22
+ <Typography variant="subtitle1">
23
+ <strong>{id}</strong>
24
+ </Typography>
25
+ <Typography variant="caption" color="text.secondary">
26
+ {url}
27
+ </Typography>
28
+ <GatewayStatus />
29
+ </Stack>
30
+ </GatewayProvider>
31
+ </InPageGatewaysProvider>
29
32
  )
30
33
  }
@@ -10,7 +10,7 @@ import { GatewayStats } from './GatewayStats.tsx'
10
10
  const meta: Meta<GatewayProviderProps> = {
11
11
  title: 'provider/GatewayProvider',
12
12
  component: GatewayProvider,
13
- // eslint-disable-next-line @stylistic/max-len
13
+
14
14
  parameters: { docs: { description: { component: 'Wraps children with a `GatewayContext` populated from the given `gatewayName` and show the gateways from the default networks if available.' } } },
15
15
  }
16
16
 
@@ -13,7 +13,7 @@ const meta: Meta<GatewayProviderProps> = {
13
13
  title: 'provider/GatewayProvider',
14
14
  component: GatewayProvider,
15
15
  decorators: [IframeWalletWarningDecorator],
16
- // eslint-disable-next-line @stylistic/max-len
16
+
17
17
  parameters: { docs: { description: { component: 'Wraps children with a `GatewayContext` populated from the given `gatewayName` and show the wallet-provided gateways if available.' } } },
18
18
  }
19
19
 
@@ -0,0 +1,82 @@
1
+ import type { AccountInstance } from '@xyo-network/sdk-js'
2
+ import type { GatewayName } from '@xyo-network/xl1-sdk'
3
+ import { DefaultNetworks } from '@xyo-network/xl1-sdk'
4
+ import type { PropsWithChildren } from 'react'
5
+ import React, {
6
+ startTransition,
7
+ useCallback, useEffect, useMemo, useState,
8
+ } from 'react'
9
+
10
+ import { buildGateway } from '../../lib/index.ts'
11
+ import type { ContextGatewayType } from '../../types/index.ts'
12
+ import { InPageGatewaysContext } from './context.ts'
13
+ import type { InPageGatewaysState } from './state.ts'
14
+
15
+ export interface InPageGatewaysProviderProps extends PropsWithChildren {
16
+ account?: AccountInstance
17
+ }
18
+
19
+ export const InPageGatewaysProvider: React.FC<InPageGatewaysProviderProps> = ({ account, children }) => {
20
+ const [gateways, setGateways] = useState<Partial<Record<GatewayName, ContextGatewayType>>>({})
21
+ const [errors, setErrors] = useState<Partial<Record<GatewayName, Error>>>({})
22
+ const [previousAccount, setPreviousAccount] = useState<AccountInstance | undefined>(account)
23
+
24
+ const clearAll = useCallback(() => {
25
+ setGateways({})
26
+ setErrors({})
27
+ }, [])
28
+
29
+ if (previousAccount !== account) {
30
+ clearAll()
31
+ setPreviousAccount(account)
32
+ }
33
+
34
+ useEffect(() => {
35
+ let cancelled = false
36
+
37
+ const buildAll = async () => {
38
+ const results = await Promise.allSettled(
39
+ DefaultNetworks.map(async (network) => {
40
+ const gateway = await buildGateway(network.id as GatewayName, account)
41
+ return { id: network.id as GatewayName, gateway }
42
+ }),
43
+ )
44
+
45
+ if (cancelled) return
46
+
47
+ const nextGateways: Partial<Record<GatewayName, ContextGatewayType>> = {}
48
+ const nextErrors: Partial<Record<GatewayName, Error>> = {}
49
+
50
+ for (const [index, result] of results.entries()) {
51
+ const networkId = DefaultNetworks[index].id as GatewayName
52
+ if (result.status === 'fulfilled') {
53
+ nextGateways[networkId] = result.value.gateway
54
+ } else {
55
+ nextErrors[networkId] = result.reason instanceof Error ? result.reason : new Error(String(result.reason))
56
+ }
57
+ }
58
+
59
+ startTransition(() => {
60
+ setGateways(nextGateways)
61
+ setErrors(nextErrors)
62
+ })
63
+ }
64
+
65
+ void buildAll()
66
+ return () => {
67
+ cancelled = true
68
+ }
69
+ }, [account])
70
+
71
+ const value = useMemo(() => {
72
+ const value: InPageGatewaysState = {
73
+ clearAll,
74
+ errors,
75
+ gateways,
76
+ provided: true,
77
+ }
78
+ return value
79
+ }, [clearAll, errors, gateways])
80
+
81
+ return <InPageGatewaysContext value={value}>{children}</InPageGatewaysContext>
82
+ }
@@ -0,0 +1,5 @@
1
+ import { createContextEx } from '@xylabs/react-shared'
2
+
3
+ import type { InPageGatewaysState } from './state.ts'
4
+
5
+ export const InPageGatewaysContext = createContextEx<InPageGatewaysState>()
@@ -0,0 +1,4 @@
1
+ export * from './context.ts'
2
+ export * from './Provider.tsx'
3
+ export * from './state.ts'
4
+ export * from './use.ts'
@@ -0,0 +1,12 @@
1
+ import type { ContextExState } from '@xylabs/react-shared'
2
+ import type { GatewayName } from '@xyo-network/xl1-sdk'
3
+
4
+ import type { ContextGatewayType } from '../../types/index.ts'
5
+
6
+ export interface InPageGatewaysStateFields {
7
+ clearAll: () => void
8
+ errors: Partial<Record<GatewayName, Error>>
9
+ gateways: Partial<Record<GatewayName, ContextGatewayType>>
10
+ }
11
+
12
+ export type InPageGatewaysState = ContextExState<InPageGatewaysStateFields>
@@ -0,0 +1,5 @@
1
+ import { useContextEx } from '@xylabs/react-shared'
2
+
3
+ import { InPageGatewaysContext } from './context.ts'
4
+
5
+ export const useProvidedInPageGateways = (required = true) => useContextEx(InPageGatewaysContext, 'InPageGateways', required)
@@ -1,2 +1,3 @@
1
1
  export * from './current-block/index.ts'
2
2
  export * from './gateway/index.ts'
3
+ export * from './in-page-gateways/index.ts'
@@ -1,2 +1 @@
1
- export * from './buildGateway.ts'
2
1
  export * from './findCaveat.ts'
@@ -1,4 +1,3 @@
1
1
  export * from './permissions/index.ts'
2
2
  export * from './useClientFromWallet.ts'
3
- export * from './useGatewayFromConfig.ts'
4
3
  export * from './useGatewayFromWallet.ts'
package/src/index.ts CHANGED
@@ -2,3 +2,4 @@ export * from './components/index.ts'
2
2
  export * from './contexts/index.ts'
3
3
  export * from './hooks/index.ts'
4
4
  export * from './lib/index.ts'
5
+ export * from './types/index.ts'
package/src/lib/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './balanceForRange.ts'
2
+ export * from './buildGateway.ts'
2
3
  export * from './findMinimumBlock.ts'
@@ -0,0 +1,4 @@
1
+ import type { XyoGateway, XyoGatewayRunner } from '@xyo-network/xl1-sdk'
2
+
3
+ // Can be removed once there is a gateway type with optional runner properties
4
+ export type ContextGatewayType = XyoGateway | XyoGatewayRunner
@@ -1 +1,2 @@
1
+ export * from './ContextGatewayType.ts'
1
2
  export * from './GatewayFromWallet.ts'
@@ -1 +0,0 @@
1
- {"version":3,"file":"buildGateway.d.ts","sourceRoot":"","sources":["../../../../../src/hooks/client/helpers/buildGateway.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAC1D,OAAO,KAAK,EACV,WAAW,EAAgB,gBAAgB,EAC5C,MAAM,sBAAsB,CAAA;AAK7B,eAAO,MAAM,YAAY,GAAU,aAAa,WAAW,EAAE,UAAU,eAAe,wFAkBrF,CAAA"}
@@ -1,9 +0,0 @@
1
- import type { AccountInstance } from '@xyo-network/sdk-js';
2
- import type { GatewayName } from '@xyo-network/xl1-sdk';
3
- export declare const useGatewayFromConfig: (gatewayName?: GatewayName, account?: AccountInstance) => {
4
- gateway: import("@xyo-network/xl1-sdk").XyoGatewayRunner | import("@xyo-network/xl1-sdk").CreatableProviderInstance<import("@xyo-network/xl1-sdk").SimpleXyoGateway> | import("@xyo-network/xl1-sdk").XyoGateway | undefined;
5
- error: Error | undefined;
6
- state: import("@xylabs/react-promise").UsePromiseState | undefined;
7
- clearGateways: () => void;
8
- };
9
- //# sourceMappingURL=useGatewayFromConfig.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useGatewayFromConfig.d.ts","sourceRoot":"","sources":["../../../../src/hooks/client/useGatewayFromConfig.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAMvD,eAAO,MAAM,oBAAoB,GAAI,cAAc,WAAW,EAAE,UAAU,eAAe;;;;;CAiCxF,CAAA"}
@@ -1,43 +0,0 @@
1
- import { usePromise } from '@xylabs/react-promise'
2
- import { isDefined } from '@xylabs/sdk-js'
3
- import type { AccountInstance } from '@xyo-network/sdk-js'
4
- import type { GatewayName } from '@xyo-network/xl1-sdk'
5
- import { useState } from 'react'
6
-
7
- import type { ContextGatewayType } from '../../contexts/index.ts'
8
- import { buildGateway } from './helpers/index.ts'
9
-
10
- export const useGatewayFromConfig = (gatewayName?: GatewayName, account?: AccountInstance) => {
11
- const [gatewayCache, setGatewayCache] = useState<Record<GatewayName, ContextGatewayType>>({})
12
- const clearGateways = () => setGatewayCache({})
13
-
14
- // if the account changes, we should clear the gateway cache since the gateway is built with the account
15
- const [previousAccount, setPreviousAccount] = useState<AccountInstance | undefined>(account)
16
- if (previousAccount !== account) {
17
- clearGateways()
18
- setPreviousAccount(account)
19
- }
20
-
21
- const builtGatewayResults = usePromise(async () => {
22
- if (isDefined(gatewayName)) {
23
- // if gateway already built and cached, return it
24
- if (isDefined(gatewayCache[gatewayName])) {
25
- return gatewayCache[gatewayName]
26
- } else {
27
- // otherwise build the gateway and cache it
28
- const gateway = await buildGateway(gatewayName, account)
29
- setGatewayCache(prev => ({ ...prev, [gatewayName]: gateway }))
30
- return gateway
31
- }
32
- }
33
- }, [gatewayName, account, gatewayCache])
34
-
35
- const [gateway, error, state] = builtGatewayResults
36
-
37
- return {
38
- gateway,
39
- error,
40
- state,
41
- clearGateways,
42
- }
43
- }