@xyo-network/react-schema 7.5.8 → 7.5.12
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.
- package/dist/browser/contexts/Schema/Provider/Memory.d.ts.map +1 -1
- package/dist/browser/hooks/useGetSchema.d.ts +8 -6
- package/dist/browser/hooks/useGetSchema.d.ts.map +1 -1
- package/dist/browser/hooks/useSchemaDefinitions.d.ts +2 -2
- package/dist/browser/hooks/useSchemaDefinitions.d.ts.map +1 -1
- package/dist/browser/hooks/useSchemaList.d.ts +1 -1
- package/dist/browser/hooks/useSchemaList.d.ts.map +1 -1
- package/dist/browser/hooks/useSchemaStats.d.ts +1 -1
- package/dist/browser/hooks/useSchemaStats.d.ts.map +1 -1
- package/dist/browser/index.mjs +309 -298
- package/dist/browser/index.mjs.map +1 -1
- package/package.json +160 -57
- package/src/components/Property/SchemaProperty.stories.tsx +0 -41
- package/src/components/Property/SchemaProperty.tsx +0 -154
- package/src/components/Property/index.ts +0 -1
- package/src/components/SelectEx/SchemaSelectEx.tsx +0 -45
- package/src/components/SelectEx/index.ts +0 -1
- package/src/components/index.ts +0 -2
- package/src/contexts/Schema/Context.ts +0 -5
- package/src/contexts/Schema/Provider/Memory.tsx +0 -31
- package/src/contexts/Schema/Provider/Props.ts +0 -4
- package/src/contexts/Schema/Provider/Route.tsx +0 -73
- package/src/contexts/Schema/Provider/index.ts +0 -3
- package/src/contexts/Schema/State.ts +0 -13
- package/src/contexts/Schema/index.ts +0 -4
- package/src/contexts/Schema/use.ts +0 -8
- package/src/contexts/index.ts +0 -1
- package/src/hooks/index.ts +0 -4
- package/src/hooks/stories/TestSchemaHooks.stories.tsx +0 -102
- package/src/hooks/useGetSchema.stories.tsx +0 -65
- package/src/hooks/useGetSchema.tsx +0 -57
- package/src/hooks/useSchemaDefinitions.tsx +0 -26
- package/src/hooks/useSchemaList.tsx +0 -35
- package/src/hooks/useSchemaStats.tsx +0 -62
- package/src/index.ts +0 -3
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { exists } from '@xylabs/sdk-js'
|
|
2
|
-
import type { PropsWithChildren } from 'react'
|
|
3
|
-
import React, {
|
|
4
|
-
useEffect, useMemo, useState,
|
|
5
|
-
} from 'react'
|
|
6
|
-
|
|
7
|
-
import { useSchemaStats } from '../../../hooks/index.ts'
|
|
8
|
-
import { SchemaContext } from '../Context.ts'
|
|
9
|
-
import type { SchemaContextState } from '../State.ts'
|
|
10
|
-
import type { SchemaProviderProps } from './Props.ts'
|
|
11
|
-
|
|
12
|
-
export const SchemaMemoryProvider: React.FC<PropsWithChildren<SchemaProviderProps>> = ({
|
|
13
|
-
defaultSchema, knownSchemaList, ...props
|
|
14
|
-
}) => {
|
|
15
|
-
const [schema, setSchema] = useState(defaultSchema)
|
|
16
|
-
const [schemaList, setSchemaList] = useState<string[] | undefined>(knownSchemaList)
|
|
17
|
-
const [fetchedSchemaStats] = useSchemaStats()
|
|
18
|
-
|
|
19
|
-
useEffect(() => {
|
|
20
|
-
if (fetchedSchemaStats) {
|
|
21
|
-
const schemaList = (fetchedSchemaStats.map(({ name }) => name)).filter(exists)
|
|
22
|
-
setSchemaList(schemaList)
|
|
23
|
-
}
|
|
24
|
-
}, [fetchedSchemaStats])
|
|
25
|
-
|
|
26
|
-
const value: SchemaContextState = useMemo(() => ({
|
|
27
|
-
provided: true, schema, schemaList: knownSchemaList ?? schemaList, setSchema, setSchemaList,
|
|
28
|
-
}), [schema, schemaList, knownSchemaList])
|
|
29
|
-
|
|
30
|
-
return <SchemaContext value={value} {...props} />
|
|
31
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import type { PropsWithChildren } from 'react'
|
|
2
|
-
import React, {
|
|
3
|
-
useCallback, useEffect, useMemo,
|
|
4
|
-
} from 'react'
|
|
5
|
-
import { useSearchParams } from 'react-router-dom'
|
|
6
|
-
|
|
7
|
-
import { SchemaContext } from '../Context.ts'
|
|
8
|
-
import type { SchemaContextState } from '../State.ts'
|
|
9
|
-
import { useSchema } from '../use.ts'
|
|
10
|
-
import { SchemaMemoryProvider } from './Memory.tsx'
|
|
11
|
-
import type { SchemaProviderProps } from './Props.ts'
|
|
12
|
-
|
|
13
|
-
const SchemaRouteProviderInner: React.FC<PropsWithChildren> = ({ children }) => {
|
|
14
|
-
const {
|
|
15
|
-
schema, setSchema, schemaList,
|
|
16
|
-
} = useSchema()
|
|
17
|
-
|
|
18
|
-
const [params, setParams] = useSearchParams()
|
|
19
|
-
|
|
20
|
-
const routeSchema = params.get('schema')
|
|
21
|
-
|
|
22
|
-
// update the network stored in the route
|
|
23
|
-
const setSchemaParam = useCallback(
|
|
24
|
-
(schema?: string) => {
|
|
25
|
-
if (schema) {
|
|
26
|
-
params.set('schema', schema)
|
|
27
|
-
setParams(params, { replace: true })
|
|
28
|
-
setSchema?.(schema)
|
|
29
|
-
} else {
|
|
30
|
-
params.delete('network')
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
[params, setParams, setSchema],
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
// if the network is actively changed, update both memory and route
|
|
37
|
-
const setSchemaLocal = useCallback(
|
|
38
|
-
(schema: string) => {
|
|
39
|
-
setSchemaParam(schema)
|
|
40
|
-
setSchema?.(schema)
|
|
41
|
-
},
|
|
42
|
-
[setSchemaParam, setSchema],
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
// sync memory and route storage of network
|
|
46
|
-
useEffect(() => {
|
|
47
|
-
if (routeSchema !== schema) {
|
|
48
|
-
if (routeSchema === undefined && schema !== undefined) {
|
|
49
|
-
// if the route does not have a network selected, use what is in the memory context
|
|
50
|
-
setSchemaLocal(schema)
|
|
51
|
-
} else if (routeSchema) {
|
|
52
|
-
// if the route has a selection and it is different from memory, update memory
|
|
53
|
-
setSchema?.(routeSchema)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}, [routeSchema, schema, setSchemaParam, setSchema, setSchemaLocal])
|
|
57
|
-
|
|
58
|
-
const value: SchemaContextState = useMemo(() => ({
|
|
59
|
-
provided: true, schema, schemaList, setSchema: setSchemaLocal,
|
|
60
|
-
}), [schema, schemaList, setSchemaLocal])
|
|
61
|
-
|
|
62
|
-
return <SchemaContext value={value}>{children}</SchemaContext>
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export const SchemaRouteProvider: React.FC<PropsWithChildren<SchemaProviderProps>> = ({
|
|
66
|
-
knownSchemaList, defaultSchema, ...props
|
|
67
|
-
}) => {
|
|
68
|
-
return (
|
|
69
|
-
<SchemaMemoryProvider knownSchemaList={knownSchemaList} defaultSchema={defaultSchema}>
|
|
70
|
-
<SchemaRouteProviderInner {...props} />
|
|
71
|
-
</SchemaMemoryProvider>
|
|
72
|
-
)
|
|
73
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { ContextExState } from '@xylabs/react-shared'
|
|
2
|
-
import type { Dispatch } from 'react'
|
|
3
|
-
|
|
4
|
-
export type SchemaContextState = ContextExState<{
|
|
5
|
-
/** @field The currently selected XYO Schema */
|
|
6
|
-
schema?: string
|
|
7
|
-
/** @field The list of known available schema */
|
|
8
|
-
schemaList?: string[]
|
|
9
|
-
/** @field Function to set the selected Schema */
|
|
10
|
-
setSchema?: Dispatch<string>
|
|
11
|
-
/** @field Function to set the list of known available schema */
|
|
12
|
-
setSchemaList?: Dispatch<string[]>
|
|
13
|
-
}>
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { useContextEx } from '@xylabs/react-shared'
|
|
2
|
-
|
|
3
|
-
import { SchemaContext } from './Context.ts'
|
|
4
|
-
import type { SchemaContextState } from './State.ts'
|
|
5
|
-
|
|
6
|
-
export const useSchema = (required = false) => {
|
|
7
|
-
return useContextEx<SchemaContextState>(SchemaContext, 'Schema', required)
|
|
8
|
-
}
|
package/src/contexts/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './Schema/index.ts'
|
package/src/hooks/index.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Alert, Button, TextField, Typography,
|
|
3
|
-
} from '@mui/material'
|
|
4
|
-
import type {
|
|
5
|
-
Decorator, Meta, StoryFn,
|
|
6
|
-
} from '@storybook/react-vite'
|
|
7
|
-
import { useAsyncEffect } from '@xylabs/react-async-effect'
|
|
8
|
-
import { FlexGrowRow } from '@xylabs/react-flexbox'
|
|
9
|
-
import type { Address } from '@xylabs/sdk-js'
|
|
10
|
-
import { HttpBridge, HttpBridgeConfigSchema } from '@xyo-network/bridge-http'
|
|
11
|
-
import { MemoryNode } from '@xyo-network/node-memory'
|
|
12
|
-
import { NodeConfigSchema } from '@xyo-network/node-model'
|
|
13
|
-
import { NodeProvider } from '@xyo-network/react-node'
|
|
14
|
-
import { DefaultSeedPhrase } from '@xyo-network/react-storybook'
|
|
15
|
-
import { useWallet, WalletProvider } from '@xyo-network/react-wallet'
|
|
16
|
-
import { SchemaCache } from '@xyo-network/schema-cache'
|
|
17
|
-
import React, { useState } from 'react'
|
|
18
|
-
|
|
19
|
-
import { useSchemaDefinitions } from '../useSchemaDefinitions.tsx'
|
|
20
|
-
import { useSchemaList } from '../useSchemaList.tsx'
|
|
21
|
-
import { useSchemaStats } from '../useSchemaStats.tsx'
|
|
22
|
-
|
|
23
|
-
const apiConfig = { apiDomain: 'https://api.archivist.xyo.network' }
|
|
24
|
-
const client = { url: 'http://localhost:8080/node' }
|
|
25
|
-
|
|
26
|
-
const MemoryNodeDecorator: Decorator = (Story, args) => {
|
|
27
|
-
const [node, setNode] = useState<MemoryNode>()
|
|
28
|
-
|
|
29
|
-
useAsyncEffect(
|
|
30
|
-
async () => {
|
|
31
|
-
const node = await MemoryNode.create({ config: { schema: NodeConfigSchema } })
|
|
32
|
-
const bridge = await HttpBridge.create({
|
|
33
|
-
config: {
|
|
34
|
-
client, schema: HttpBridgeConfigSchema, security: { allowAnonymous: true },
|
|
35
|
-
},
|
|
36
|
-
})
|
|
37
|
-
await node.register(bridge)
|
|
38
|
-
await node.attach(bridge.address, true)
|
|
39
|
-
setNode(node)
|
|
40
|
-
},
|
|
41
|
-
[],
|
|
42
|
-
)
|
|
43
|
-
|
|
44
|
-
const [wallet] = useWallet({ mnemonic: DefaultSeedPhrase })
|
|
45
|
-
|
|
46
|
-
return (
|
|
47
|
-
<WalletProvider rootWallet={wallet}>
|
|
48
|
-
<NodeProvider node={node}>
|
|
49
|
-
<Story {...args} />
|
|
50
|
-
</NodeProvider>
|
|
51
|
-
</WalletProvider>
|
|
52
|
-
)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export default {
|
|
56
|
-
decorators: [MemoryNodeDecorator],
|
|
57
|
-
title: 'schema/Hooks',
|
|
58
|
-
} as Meta
|
|
59
|
-
|
|
60
|
-
const Template: StoryFn<React.FC> = () => {
|
|
61
|
-
// eslint-disable-next-line react-hooks/immutability
|
|
62
|
-
SchemaCache.instance.proxy = `${apiConfig.apiDomain}/domain`
|
|
63
|
-
const [addressText, setAddressText] = useState<Address>('' as Address)
|
|
64
|
-
const [address, setAddress] = useState<Address>()
|
|
65
|
-
const [schemaStats, schemaStatsError] = useSchemaStats(address)
|
|
66
|
-
const [schemaList, schemaListError] = useSchemaList(address)
|
|
67
|
-
const mappedSchemaList = schemaList?.schemas?.map(name => ({ name })) as { name: string }[]
|
|
68
|
-
const schemaDefinitions = useSchemaDefinitions(mappedSchemaList)
|
|
69
|
-
|
|
70
|
-
return (
|
|
71
|
-
<div style={{
|
|
72
|
-
display: 'flex', flexDirection: 'column', rowGap: '16px',
|
|
73
|
-
}}
|
|
74
|
-
>
|
|
75
|
-
{schemaStatsError
|
|
76
|
-
? <Alert severity="error">{schemaStatsError.message ?? schemaListError?.message}</Alert>
|
|
77
|
-
: null}
|
|
78
|
-
<FlexGrowRow columnGap={4}>
|
|
79
|
-
<TextField fullWidth size="small" value={address} label="Address" onChange={event => setAddressText(event.target.value as Address)} />
|
|
80
|
-
<Button variant="contained" onClick={() => setAddress(addressText)} sx={{ whiteSpace: 'nowrap' }}>
|
|
81
|
-
Get Stats
|
|
82
|
-
</Button>
|
|
83
|
-
</FlexGrowRow>
|
|
84
|
-
<Typography variant="h2">Schema Stats</Typography>
|
|
85
|
-
<code>
|
|
86
|
-
<pre>{JSON.stringify(schemaStats, null, 2)}</pre>
|
|
87
|
-
</code>
|
|
88
|
-
<Typography variant="h2">Schema List</Typography>
|
|
89
|
-
<code>
|
|
90
|
-
<pre>{JSON.stringify(schemaList, null, 2)}</pre>
|
|
91
|
-
</code>
|
|
92
|
-
<Typography variant="h2">Schema Definitions</Typography>
|
|
93
|
-
<pre>
|
|
94
|
-
<code>{JSON.stringify(schemaDefinitions, null, 2)}</code>
|
|
95
|
-
</pre>
|
|
96
|
-
</div>
|
|
97
|
-
)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const Default = Template.bind({})
|
|
101
|
-
|
|
102
|
-
export { Default }
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FormControl, TextField, Typography,
|
|
3
|
-
} from '@mui/material'
|
|
4
|
-
import type { Meta, StoryFn } from '@storybook/react-vite'
|
|
5
|
-
import { FlexRow } from '@xylabs/react-flexbox'
|
|
6
|
-
import { useResetState } from '@xylabs/react-hooks'
|
|
7
|
-
import { JsonViewerEx } from '@xyo-network/react-payload-raw-info'
|
|
8
|
-
import { SchemaCache } from '@xyo-network/schema-cache'
|
|
9
|
-
import React from 'react'
|
|
10
|
-
|
|
11
|
-
import { useGetSchemaPayload } from './useGetSchema.tsx'
|
|
12
|
-
|
|
13
|
-
SchemaCache.instance.proxy = 'https://beta.api.archivist.xyo.network/domain'
|
|
14
|
-
|
|
15
|
-
const UseGetSchemaComponent: React.FC<{ schema: string }> = ({ schema }) => {
|
|
16
|
-
const exampleSchemas = ['network.xyo.domain', 'network.xyo.payload', 'network.xyo.schema']
|
|
17
|
-
const [schemaFieldValue, setSchemaFieldValue] = useResetState(schema)
|
|
18
|
-
const { schemaPayload } = useGetSchemaPayload(schemaFieldValue)
|
|
19
|
-
|
|
20
|
-
return (
|
|
21
|
-
<>
|
|
22
|
-
<Typography variant="body1" fontWeight="bold" mb={2}>
|
|
23
|
-
Example schemas to test:
|
|
24
|
-
{exampleSchemas.map(schema => (
|
|
25
|
-
<Typography
|
|
26
|
-
component="span"
|
|
27
|
-
mx={1}
|
|
28
|
-
key={exampleSchemas.indexOf(schema)}
|
|
29
|
-
onClick={() => setSchemaFieldValue(schema)}
|
|
30
|
-
sx={{ cursor: 'pointer', textDecoration: 'underline' }}
|
|
31
|
-
>
|
|
32
|
-
{schema}
|
|
33
|
-
</Typography>
|
|
34
|
-
))}
|
|
35
|
-
</Typography>
|
|
36
|
-
<FormControl>
|
|
37
|
-
<TextField value={schemaFieldValue} label="Schema Name" onChange={e => setSchemaFieldValue(e.target.value)} />
|
|
38
|
-
</FormControl>
|
|
39
|
-
<FlexRow my={3} justifyContent="start">
|
|
40
|
-
<JsonViewerEx value={schemaPayload || {}} />
|
|
41
|
-
</FlexRow>
|
|
42
|
-
</>
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const StorybookEntry: Meta = {
|
|
47
|
-
argTypes: {},
|
|
48
|
-
component: UseGetSchemaComponent,
|
|
49
|
-
parameters: { docs: { page: null } },
|
|
50
|
-
title: 'payload/useGetSchema',
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const Template: StoryFn<typeof UseGetSchemaComponent> = ({ schema }) => {
|
|
54
|
-
return <UseGetSchemaComponent schema={schema} />
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const Default = Template.bind({})
|
|
58
|
-
Default.args = { schema: 'network.xyo.schema' }
|
|
59
|
-
|
|
60
|
-
const Domain = Template.bind({})
|
|
61
|
-
Domain.args = { schema: 'network.xyo.domain' }
|
|
62
|
-
|
|
63
|
-
export { Default, Domain }
|
|
64
|
-
|
|
65
|
-
export default StorybookEntry
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { useAsyncEffect } from '@xylabs/react-async-effect'
|
|
2
|
-
import { PayloadBuilder } from '@xyo-network/payload-builder'
|
|
3
|
-
import type { ModuleError, WithSources } from '@xyo-network/payload-model'
|
|
4
|
-
import { ModuleErrorSchema } from '@xyo-network/payload-model'
|
|
5
|
-
import type { SchemaCacheEntry } from '@xyo-network/schema-cache'
|
|
6
|
-
import { SchemaCache } from '@xyo-network/schema-cache'
|
|
7
|
-
import type { SchemaPayload } from '@xyo-network/schema-payload-plugin'
|
|
8
|
-
import { useState } from 'react'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Gets a Huri and schema payload from a schema string
|
|
12
|
-
*/
|
|
13
|
-
const useGetSchemaPayload = (schema?: string) => {
|
|
14
|
-
const [notFound, setNotFound] = useState(false)
|
|
15
|
-
const [xyoError, setXyoError] = useState<WithSources<ModuleError>>()
|
|
16
|
-
const [schemaCacheEntry, setSchemaCacheEntry] = useState<SchemaCacheEntry | null | undefined>()
|
|
17
|
-
const [schemaLocal, setSchemaLocal] = useState<string>()
|
|
18
|
-
|
|
19
|
-
useAsyncEffect(
|
|
20
|
-
async (mounted) => {
|
|
21
|
-
const firstRequest = !notFound && !schemaCacheEntry && !xyoError
|
|
22
|
-
const schemaChanged = schema !== schemaLocal
|
|
23
|
-
|
|
24
|
-
if ((schema && firstRequest) || (schema && schemaChanged)) {
|
|
25
|
-
try {
|
|
26
|
-
const schemaCacheEntry = await SchemaCache.instance.get(schema)
|
|
27
|
-
if (mounted()) {
|
|
28
|
-
setSchemaCacheEntry(schemaCacheEntry)
|
|
29
|
-
setNotFound(schemaCacheEntry === null || schemaCacheEntry === undefined)
|
|
30
|
-
}
|
|
31
|
-
} catch (e) {
|
|
32
|
-
const error = e as Error
|
|
33
|
-
console.error(e)
|
|
34
|
-
if (mounted()) {
|
|
35
|
-
setXyoError({
|
|
36
|
-
message: error.message, schema: ModuleErrorSchema, $sources: [],
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (schemaChanged) {
|
|
42
|
-
setSchemaLocal(schema)
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
[xyoError, notFound, schema, schemaLocal, schemaCacheEntry],
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
notFound,
|
|
50
|
-
schemaHuri: schemaCacheEntry?.huri,
|
|
51
|
-
schemaPayload:
|
|
52
|
-
schemaCacheEntry ? new PayloadBuilder<SchemaPayload>(schemaCacheEntry?.payload).fields(schemaCacheEntry.payload).build() : schemaCacheEntry,
|
|
53
|
-
xyoError,
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export { useGetSchemaPayload }
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { useAsyncEffect } from '@xylabs/react-async-effect'
|
|
2
|
-
import { SchemaCache } from '@xyo-network/schema-cache'
|
|
3
|
-
import type { SchemaPayload } from '@xyo-network/schema-payload-plugin'
|
|
4
|
-
import { useState } from 'react'
|
|
5
|
-
|
|
6
|
-
export type SchemaList = { name: string }
|
|
7
|
-
|
|
8
|
-
export const useSchemaDefinitions = (schemaList?: SchemaList[]): SchemaPayload[] | undefined => {
|
|
9
|
-
const [schemaPayloads, setSchemaPayloads] = useState<SchemaPayload[]>()
|
|
10
|
-
useAsyncEffect(
|
|
11
|
-
async (mounted) => {
|
|
12
|
-
if (schemaList) {
|
|
13
|
-
const promiseResults = await Promise.allSettled(schemaList?.map(({ name }) => SchemaCache.instance.get(name)))
|
|
14
|
-
if (mounted()) {
|
|
15
|
-
setSchemaPayloads(
|
|
16
|
-
promiseResults
|
|
17
|
-
.map(result => (result.status === 'fulfilled' ? result.value?.payload : undefined))
|
|
18
|
-
.filter(item => item !== undefined && item !== null) as SchemaPayload[],
|
|
19
|
-
)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
[schemaList],
|
|
24
|
-
)
|
|
25
|
-
return schemaPayloads
|
|
26
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { Address } from '@xylabs/sdk-js'
|
|
3
|
-
import type { SchemaListPayload, SchemaListQueryPayload } from '@xyo-network/diviner-schema-list-model'
|
|
4
|
-
import { SchemaListQuerySchema } from '@xyo-network/diviner-schema-list-model'
|
|
5
|
-
import { useWeakDivinerFromNode } from '@xyo-network/react-diviner'
|
|
6
|
-
import { useMemo } from 'react'
|
|
7
|
-
|
|
8
|
-
export const useSchemaList = (address?: Address, nameOrAddress = 'SchemaListDiviner'): [SchemaListPayload | null | undefined, Error | undefined] => {
|
|
9
|
-
const [diviner, divinerError] = useWeakDivinerFromNode(nameOrAddress)
|
|
10
|
-
|
|
11
|
-
const query: SchemaListQueryPayload[] | undefined = useMemo(
|
|
12
|
-
() =>
|
|
13
|
-
address
|
|
14
|
-
? [
|
|
15
|
-
{
|
|
16
|
-
address,
|
|
17
|
-
schema: SchemaListQuerySchema,
|
|
18
|
-
},
|
|
19
|
-
]
|
|
20
|
-
: undefined,
|
|
21
|
-
[address],
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
const [schemaList, error] = usePromise(
|
|
25
|
-
async () => {
|
|
26
|
-
const divinerInstance = diviner?.deref()
|
|
27
|
-
if (divinerInstance) {
|
|
28
|
-
const response = (await divinerInstance.divine(query)) as SchemaListPayload[]
|
|
29
|
-
return response.at(0)
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
[diviner, divinerError, query],
|
|
33
|
-
)
|
|
34
|
-
return [schemaList, error]
|
|
35
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { useAsyncEffect } from '@xylabs/react-async-effect'
|
|
2
|
-
import { type Address, isDefined } from '@xylabs/sdk-js'
|
|
3
|
-
import type {
|
|
4
|
-
SchemaStatsPayload,
|
|
5
|
-
SchemaStatsQueryPayload,
|
|
6
|
-
} from '@xyo-network/diviner-schema-stats-model'
|
|
7
|
-
import {
|
|
8
|
-
SchemaStatsDivinerSchema,
|
|
9
|
-
SchemaStatsQuerySchema,
|
|
10
|
-
} from '@xyo-network/diviner-schema-stats-model'
|
|
11
|
-
import { TYPES } from '@xyo-network/node-core-types'
|
|
12
|
-
import type { WithSources } from '@xyo-network/payload-model'
|
|
13
|
-
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
|
|
14
|
-
import { useWeakDivinerFromNode } from '@xyo-network/react-diviner'
|
|
15
|
-
import type { Dispatch, SetStateAction } from 'react'
|
|
16
|
-
import { useState } from 'react'
|
|
17
|
-
|
|
18
|
-
export const useSchemaStats = (
|
|
19
|
-
statsAddress?: Address,
|
|
20
|
-
nameOrAddress = TYPES.SchemaStatsDiviner,
|
|
21
|
-
): [SchemaStatsPayload[] | undefined, Error | undefined, Dispatch<SetStateAction<number>>] => {
|
|
22
|
-
const [refresh, setRefresh] = useState(1)
|
|
23
|
-
const [diviner, divinerError] = useWeakDivinerFromNode(nameOrAddress)
|
|
24
|
-
const [error, setError] = useState<Error>()
|
|
25
|
-
const refreshHistory = () => setRefresh(previous => previous + 1)
|
|
26
|
-
|
|
27
|
-
const [schemaList, setSchemaList] = useState<WithSources<SchemaStatsPayload>[]>()
|
|
28
|
-
|
|
29
|
-
useAsyncEffect(
|
|
30
|
-
async (mounted) => {
|
|
31
|
-
const instance = diviner?.deref()
|
|
32
|
-
if (isDefined(instance) && isDefined(statsAddress)) {
|
|
33
|
-
if (divinerError) {
|
|
34
|
-
if (mounted()) {
|
|
35
|
-
setError(divinerError)
|
|
36
|
-
setSchemaList(undefined)
|
|
37
|
-
}
|
|
38
|
-
} else {
|
|
39
|
-
try {
|
|
40
|
-
const query = {
|
|
41
|
-
address: statsAddress,
|
|
42
|
-
schema: SchemaStatsQuerySchema,
|
|
43
|
-
}
|
|
44
|
-
const schemas = (await instance.divine([query])).filter(isPayloadOfSchemaType(SchemaStatsDivinerSchema)) as WithSources<
|
|
45
|
-
SchemaStatsPayload
|
|
46
|
-
>[]
|
|
47
|
-
if (mounted()) {
|
|
48
|
-
setSchemaList(schemas)
|
|
49
|
-
setError(undefined)
|
|
50
|
-
}
|
|
51
|
-
} catch (ex) {
|
|
52
|
-
setError(ex as Error)
|
|
53
|
-
setSchemaList(undefined)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
[diviner, refresh, divinerError, statsAddress],
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
return [schemaList, error, refreshHistory]
|
|
62
|
-
}
|
package/src/index.ts
DELETED