@xyo-network/react-schema 2.26.36 → 2.26.39

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/docs.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "fileName": "index.ts",
11
11
  "line": 1,
12
12
  "character": 0,
13
- "url": "https://github.com/XYOracleNetwork/sdk-xyo-react-js/blob/3b6a925/packages/schema/src/index.ts#L1"
13
+ "url": "https://github.com/XYOracleNetwork/sdk-xyo-react-js/blob/413a933/packages/schema/src/index.ts#L1"
14
14
  }
15
15
  ]
16
16
  }
package/package.json CHANGED
@@ -19,10 +19,10 @@
19
19
  "@xylabs/react-shared": "^2.14.10",
20
20
  "@xyo-network/api": "^2.22.15",
21
21
  "@xyo-network/payload": "^2.22.15",
22
- "@xyo-network/react-archive": "^2.26.36",
23
- "@xyo-network/react-archivist-api": "^2.26.36",
24
- "@xyo-network/react-property": "^2.26.36",
25
- "@xyo-network/react-shared": "^2.26.36",
22
+ "@xyo-network/react-archive": "^2.26.39",
23
+ "@xyo-network/react-archivist-api": "^2.26.39",
24
+ "@xyo-network/react-property": "^2.26.39",
25
+ "@xyo-network/react-shared": "^2.26.39",
26
26
  "@xyo-network/utils": "^2.22.15",
27
27
  "react": "^18.2.0",
28
28
  "react-dom": "^18.2.0",
@@ -85,5 +85,5 @@
85
85
  },
86
86
  "sideEffects": false,
87
87
  "types": "dist/esm/index.d.ts",
88
- "version": "2.26.36"
88
+ "version": "2.26.39"
89
89
  }
@@ -0,0 +1,36 @@
1
+ import { ComponentStory, Meta } from '@storybook/react'
2
+
3
+ import { appThemeDecorator } from '../../../../../.storybook'
4
+ import { SchemaProperty } from './SchemaProperty'
5
+
6
+ const StorybookEntry: Meta = {
7
+ argTypes: {},
8
+ args: {},
9
+ component: SchemaProperty,
10
+ decorators: [appThemeDecorator],
11
+ parameters: {
12
+ docs: {
13
+ page: null,
14
+ },
15
+ },
16
+ title: 'Schema/SchemaProperty',
17
+ }
18
+
19
+ const Template: ComponentStory<typeof SchemaProperty> = (args) => <SchemaProperty {...args} />
20
+
21
+ const Default = Template.bind({})
22
+ Default.args = {}
23
+
24
+ const WithVerfiedValue = Template.bind({})
25
+ WithVerfiedValue.args = { value: 'network.xyo.schema' }
26
+
27
+ const WithVerfiedValuePaper = Template.bind({})
28
+ WithVerfiedValuePaper.args = { paper: true, value: 'network.xyo.schema' }
29
+
30
+ const WithUnverfiedValue = Template.bind({})
31
+ WithUnverfiedValue.args = { value: 'network.xyo.blahblah' }
32
+
33
+ export { Default, WithUnverfiedValue, WithVerfiedValue, WithVerfiedValuePaper }
34
+
35
+ // eslint-disable-next-line import/no-default-export
36
+ export default StorybookEntry
@@ -0,0 +1,64 @@
1
+ import NewReleasesIcon from '@mui/icons-material/NewReleases'
2
+ import OpenInNewIcon from '@mui/icons-material/OpenInNew'
3
+ import VerifiedIcon from '@mui/icons-material/Verified'
4
+ import { IconButton } from '@mui/material'
5
+ import { LinkEx } from '@xylabs/react-link'
6
+ import { useAsyncEffect } from '@xylabs/react-shared'
7
+ import { Property, PropertyProps, PropertyValue } from '@xyo-network/react-property'
8
+ import { XyoSchemaCache, XyoSchemaCacheEntry } from '@xyo-network/utils'
9
+ import { useState } from 'react'
10
+
11
+ export type SchemaPropertyProps = PropertyProps & {
12
+ value?: string
13
+ viewSchemaUrl?: string
14
+ }
15
+
16
+ const useResolveSchema = (schema?: string) => {
17
+ const [entry, setEntry] = useState<XyoSchemaCacheEntry | null>()
18
+ useAsyncEffect(
19
+ // eslint-disable-next-line react-hooks/exhaustive-deps
20
+ async (mounted) => {
21
+ if (schema) {
22
+ const entry = await XyoSchemaCache.instance.get(schema)
23
+ if (mounted()) {
24
+ setEntry(entry)
25
+ }
26
+ }
27
+ },
28
+ [schema]
29
+ )
30
+ return entry
31
+ }
32
+
33
+ export const SchemaProperty: React.FC<SchemaPropertyProps> = ({ titleProps, value, viewSchemaUrl = 'https://explore.xyo.network/', ...props }) => {
34
+ const resolvedSchema = useResolveSchema(value)
35
+ return (
36
+ <Property title="Schema" value={value} tip="Schema sent with the payload" titleProps={titleProps} {...props}>
37
+ {value ? (
38
+ resolvedSchema === null ? (
39
+ <IconButton rel="noopener noreferrer" size="small" target="_blank" href={`${viewSchemaUrl}${value}`}>
40
+ <NewReleasesIcon color="warning" fontSize="inherit" />
41
+ </IconButton>
42
+ ) : resolvedSchema === undefined ? (
43
+ <IconButton rel="noopener noreferrer" size="small" target="_blank" href={`${viewSchemaUrl}${value}`}>
44
+ <NewReleasesIcon color="disabled" fontSize="inherit" />
45
+ </IconButton>
46
+ ) : (
47
+ <IconButton rel="noopener noreferrer" size="small" target="_blank" href={resolvedSchema?.huri?.href ?? ''}>
48
+ <VerifiedIcon color="success" fontSize="inherit" />
49
+ </IconButton>
50
+ )
51
+ ) : null}
52
+ {value ? (
53
+ <LinkEx flexGrow={1} alignItems="center" href={`${viewSchemaUrl}${value}`} rel="noopener noreferrer" display="flex">
54
+ <PropertyValue value={value} title="view schema in new window" />
55
+ </LinkEx>
56
+ ) : null}
57
+ {value ? (
58
+ <LinkEx marginX={1} alignItems="center" href={`${viewSchemaUrl}${value}`} target="_blank" rel="noopener noreferrer" display="flex">
59
+ <OpenInNewIcon fontSize="inherit" />
60
+ </LinkEx>
61
+ ) : null}
62
+ </Property>
63
+ )
64
+ }
@@ -0,0 +1 @@
1
+ export * from './SchemaProperty'
@@ -0,0 +1,62 @@
1
+ /* eslint-disable import/no-internal-modules */
2
+ import { ComponentMeta, ComponentStory } from '@storybook/react'
3
+ import { SelectExProps } from '@xylabs/react-common'
4
+
5
+ import { authDecorator, WrappedArgs } from '../../../../../.storybook'
6
+ import { ArchivistApiProvider } from '../../../../archivist-api/src'
7
+ import { SchemaMemoryProvider } from '../../contexts'
8
+ import { SchemaSelectEx } from './SchemaSelectEx'
9
+
10
+ const StorybookEntry = {
11
+ argTypes: {},
12
+ component: SchemaSelectEx,
13
+ decorators: [authDecorator],
14
+ parameters: {
15
+ docs: {
16
+ page: null,
17
+ },
18
+ },
19
+ title: 'schema/SchemaSelectEx',
20
+ } as ComponentMeta<typeof SchemaSelectEx>
21
+
22
+ const Template: ComponentStory<typeof SchemaSelectEx> = (args) => {
23
+ return <SchemaSelectEx {...args}></SchemaSelectEx>
24
+ }
25
+
26
+ const TemplateWithProvider: ComponentStory<typeof SchemaSelectEx> = (args) => {
27
+ const combinedArgs = args as WrappedArgs & SelectExProps<string>
28
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
29
+ const { authState, ...props } = combinedArgs
30
+ return (
31
+ <SchemaMemoryProvider knownSchemaList={['network.xyo.schema', 'network.xyo.domain']}>
32
+ <SchemaSelectEx {...props}></SchemaSelectEx>
33
+ </SchemaMemoryProvider>
34
+ )
35
+ }
36
+
37
+ const TemplateWithApi: ComponentStory<typeof SchemaSelectEx> = (args) => {
38
+ const combinedArgs = args as WrappedArgs & SelectExProps<string>
39
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
40
+ const { authState, ...props } = combinedArgs
41
+ return (
42
+ <ArchivistApiProvider apiDomain="https://beta.api.archivist.xyo.network">
43
+ <SchemaMemoryProvider>
44
+ <SchemaSelectEx {...props}></SchemaSelectEx>
45
+ </SchemaMemoryProvider>
46
+ </ArchivistApiProvider>
47
+ )
48
+ }
49
+
50
+ const Default = Template.bind({})
51
+ Default.args = {}
52
+
53
+ const WithProvider = TemplateWithProvider.bind({})
54
+ WithProvider.args = {}
55
+
56
+ const WithApi = TemplateWithApi.bind({})
57
+ WithApi.args = {}
58
+
59
+ export { Default, WithApi, WithProvider }
60
+
61
+ // eslint-disable-next-line import/no-default-export
62
+ export default StorybookEntry
@@ -0,0 +1,39 @@
1
+ import { MenuItem, Typography } from '@mui/material'
2
+ import { SelectEx, SelectExProps } from '@xylabs/react-common'
3
+
4
+ import { useSchema } from '../../contexts'
5
+
6
+ export type SchemaSelectExProps = SelectExProps<string>
7
+
8
+ export const SchemaSelectEx: React.FC<SchemaSelectExProps> = ({ onChange, ...props }) => {
9
+ const { schema, setSchema, schemaList } = useSchema(false)
10
+
11
+ return (
12
+ <SelectEx
13
+ variant="outlined"
14
+ size="small"
15
+ value={schema ?? 'none'}
16
+ onChange={(event, child) => {
17
+ if (event.target.value !== schema) {
18
+ onChange?.(event, child)
19
+ setSchema?.(event.target.value)
20
+ }
21
+ }}
22
+ renderValue={(value) => {
23
+ return <Typography>{value === 'none' ? '- None -' : value}</Typography>
24
+ }}
25
+ {...props}
26
+ >
27
+ {schemaList?.map((schema, index) => {
28
+ return (
29
+ <MenuItem key={index} value={schema}>
30
+ {schema}
31
+ </MenuItem>
32
+ )
33
+ })}
34
+ <MenuItem key="none" value="none">
35
+ - None -
36
+ </MenuItem>
37
+ </SelectEx>
38
+ )
39
+ }
@@ -0,0 +1 @@
1
+ export * from './SchemaSelectEx'
@@ -0,0 +1,2 @@
1
+ export * from './Property'
2
+ export * from './SelectEx'
@@ -0,0 +1,5 @@
1
+ import { createContextEx } from '@xyo-network/react-shared'
2
+
3
+ import { SchemaContextState } from './State'
4
+
5
+ export const SchemaContext = createContextEx<SchemaContextState>()
@@ -0,0 +1,3 @@
1
+ export * from './useSchemaDefinitions'
2
+ export * from './useSchemaList'
3
+ export * from './useSchemaStats'
@@ -0,0 +1,28 @@
1
+ import { useAsyncEffect } from '@xylabs/react-shared'
2
+ import { XyoSchemaPayload } from '@xyo-network/payload'
3
+ import { XyoSchemaCache } from '@xyo-network/utils'
4
+ import { useState } from 'react'
5
+
6
+ import { useSchemaList } from './useSchemaList'
7
+
8
+ export const useSchemaDefinitions = (): XyoSchemaPayload[] | undefined => {
9
+ const [schemaList] = useSchemaList()
10
+ const [schemaPayloads, setSchemaPayloads] = useState<XyoSchemaPayload[]>()
11
+ useAsyncEffect(
12
+ // eslint-disable-next-line react-hooks/exhaustive-deps
13
+ async (mounted) => {
14
+ if (schemaList) {
15
+ const promiseResults = await Promise.allSettled(schemaList?.map((schema) => XyoSchemaCache.instance.get(schema)))
16
+ if (mounted()) {
17
+ setSchemaPayloads(
18
+ promiseResults
19
+ .map((result) => (result.status === 'fulfilled' ? result.value?.payload : undefined))
20
+ .filter((item) => item !== undefined && item !== null) as XyoSchemaPayload[]
21
+ )
22
+ }
23
+ }
24
+ },
25
+ [schemaList]
26
+ )
27
+ return schemaPayloads
28
+ }
@@ -0,0 +1,25 @@
1
+ import { useAsyncEffect } from '@xylabs/react-shared'
2
+ import { useArchive } from '@xyo-network/react-archive'
3
+ import { useArchivistApi } from '@xyo-network/react-archivist-api'
4
+ import { Dispatch, useState } from 'react'
5
+
6
+ export const useSchemaList = (knownSchemaList?: string[]): [string[] | undefined, Dispatch<string[]>] => {
7
+ const { api } = useArchivistApi(false)
8
+ const { archive = 'temp' } = useArchive(false)
9
+ const [schemaList, setSchemaList] = useState<string[] | undefined>(knownSchemaList)
10
+
11
+ useAsyncEffect(
12
+ // eslint-disable-next-line react-hooks/exhaustive-deps
13
+ async (mounted) => {
14
+ if (api && archive) {
15
+ const list = await api?.archive(archive).payload.schema.get()
16
+ if (mounted()) {
17
+ setSchemaList(list)
18
+ }
19
+ }
20
+ },
21
+ [api, archive]
22
+ )
23
+
24
+ return [schemaList, setSchemaList]
25
+ }
@@ -0,0 +1,26 @@
1
+ import { useAsyncEffect } from '@xylabs/react-shared'
2
+ import { XyoArchivistArchivePayloadSchemaStats } from '@xyo-network/api'
3
+ import { useArchive } from '@xyo-network/react-archive'
4
+ import { useArchivistApi } from '@xyo-network/react-archivist-api'
5
+ import { useState } from 'react'
6
+
7
+ export const useSchemaStats = (): XyoArchivistArchivePayloadSchemaStats | undefined => {
8
+ const { api } = useArchivistApi(false)
9
+ const { archive = 'temp' } = useArchive(false)
10
+ const [stats, setStats] = useState<XyoArchivistArchivePayloadSchemaStats>()
11
+
12
+ useAsyncEffect(
13
+ // eslint-disable-next-line react-hooks/exhaustive-deps
14
+ async (mounted) => {
15
+ if (api && archive) {
16
+ const stats = await api?.archive(archive).payload.schema.stats.get()
17
+ if (mounted()) {
18
+ setStats(stats)
19
+ }
20
+ }
21
+ },
22
+ [api, archive]
23
+ )
24
+
25
+ return stats
26
+ }
@@ -0,0 +1,13 @@
1
+ import { WithChildren } from '@xylabs/react-shared'
2
+ import { useState } from 'react'
3
+
4
+ import { SchemaContext } from '../Context'
5
+ import { useSchemaList } from '../Hooks'
6
+ import { SchemaProviderProps } from './Props'
7
+
8
+ export const SchemaMemoryProvider: React.FC<WithChildren<SchemaProviderProps>> = ({ defaultSchema, knownSchemaList = [], ...props }) => {
9
+ const [schema, setSchema] = useState(defaultSchema)
10
+ const [schemaList, setSchemaList] = useSchemaList(knownSchemaList)
11
+
12
+ return <SchemaContext.Provider value={{ provided: true, schema, schemaList, setSchema, setSchemaList }} {...props} />
13
+ }
@@ -0,0 +1,4 @@
1
+ export interface SchemaProviderProps {
2
+ defaultSchema?: string
3
+ knownSchemaList?: string[]
4
+ }
@@ -0,0 +1,62 @@
1
+ import { WithChildren } from '@xylabs/react-shared'
2
+ import { useCallback, useEffect } from 'react'
3
+ import { useSearchParams } from 'react-router-dom'
4
+
5
+ import { SchemaContext } from '../Context'
6
+ import { useSchema } from '../use'
7
+ import { SchemaMemoryProvider } from './Memory'
8
+ import { SchemaProviderProps } from './Props'
9
+
10
+ const SchemaRouteProviderInner: React.FC<WithChildren> = ({ children }) => {
11
+ const { schema, setSchema, schemaList } = useSchema()
12
+
13
+ const [params, setParams] = useSearchParams()
14
+
15
+ const routeSchema = params.get('schema')
16
+
17
+ //update the network stored in the route
18
+ const setSchemaParam = useCallback(
19
+ (schema?: string) => {
20
+ if (schema) {
21
+ params.set('schema', schema)
22
+ setParams(params, { replace: true })
23
+ setSchema?.(schema)
24
+ } else {
25
+ params.delete('network')
26
+ }
27
+ },
28
+ [params, setParams, setSchema]
29
+ )
30
+
31
+ //if the network is actively changed, update both memory and route
32
+ const setSchemaLocal = useCallback(
33
+ (schema: string) => {
34
+ setSchemaParam(schema)
35
+ setSchema?.(schema)
36
+ },
37
+ [setSchemaParam, setSchema]
38
+ )
39
+
40
+ //sync memory and route storage of network
41
+ useEffect(() => {
42
+ if (routeSchema !== schema) {
43
+ if (routeSchema === undefined && schema !== undefined) {
44
+ //if the route does not have a network selected, use what is in the memory context
45
+ setSchemaLocal(schema)
46
+ } else if (routeSchema) {
47
+ //if the route has a selection and it is different from memory, update memory
48
+ setSchema?.(routeSchema)
49
+ }
50
+ }
51
+ }, [routeSchema, schema, setSchemaParam, setSchema, setSchemaLocal])
52
+
53
+ return <SchemaContext.Provider value={{ provided: true, schema, schemaList, setSchema: setSchemaLocal }}>{children}</SchemaContext.Provider>
54
+ }
55
+
56
+ export const SchemaRouteProvider: React.FC<WithChildren<SchemaProviderProps>> = ({ knownSchemaList, defaultSchema, ...props }) => {
57
+ return (
58
+ <SchemaMemoryProvider knownSchemaList={knownSchemaList} defaultSchema={defaultSchema}>
59
+ <SchemaRouteProviderInner {...props} />
60
+ </SchemaMemoryProvider>
61
+ )
62
+ }
@@ -0,0 +1,3 @@
1
+ export * from './Memory'
2
+ export * from './Props'
3
+ export * from './Route'
@@ -0,0 +1,13 @@
1
+ import { ContextExState } from '@xyo-network/react-shared'
2
+ import { Dispatch } from 'react'
3
+
4
+ export interface SchemaContextState extends 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
+ }
@@ -0,0 +1,5 @@
1
+ export * from './Context'
2
+ export * from './Hooks'
3
+ export * from './Provider'
4
+ export * from './State'
5
+ export * from './use'
@@ -0,0 +1,8 @@
1
+ import { useContextEx } from '@xyo-network/react-shared'
2
+
3
+ import { SchemaContext } from './Context'
4
+ import { SchemaContextState } from './State'
5
+
6
+ export const useSchema = (required = false) => {
7
+ return useContextEx<SchemaContextState>(SchemaContext, 'Schema', required)
8
+ }
@@ -0,0 +1 @@
1
+ export * from './Schema'
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './components'
2
+ export * from './contexts'