@xyo-network/react-payload 2.23.7

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 (179) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +69 -0
  3. package/babel.config.json +5 -0
  4. package/dist/cjs/components/Details/DataDetails.d.ts +10 -0
  5. package/dist/cjs/components/Details/DataDetails.js +39 -0
  6. package/dist/cjs/components/Details/DataDetails.js.map +1 -0
  7. package/dist/cjs/components/Details/Details.d.ts +15 -0
  8. package/dist/cjs/components/Details/Details.js +16 -0
  9. package/dist/cjs/components/Details/Details.js.map +1 -0
  10. package/dist/cjs/components/Details/HashSourceDetails.d.ts +8 -0
  11. package/dist/cjs/components/Details/HashSourceDetails.js +18 -0
  12. package/dist/cjs/components/Details/HashSourceDetails.js.map +1 -0
  13. package/dist/cjs/components/Details/JsonDetails.d.ts +9 -0
  14. package/dist/cjs/components/Details/JsonDetails.js +17 -0
  15. package/dist/cjs/components/Details/JsonDetails.js.map +1 -0
  16. package/dist/cjs/components/Details/MetaDetails.d.ts +8 -0
  17. package/dist/cjs/components/Details/MetaDetails.js +16 -0
  18. package/dist/cjs/components/Details/MetaDetails.js.map +1 -0
  19. package/dist/cjs/components/Details/ValidationDetails/SchemaProperty.d.ts +4 -0
  20. package/dist/cjs/components/Details/ValidationDetails/SchemaProperty.js +15 -0
  21. package/dist/cjs/components/Details/ValidationDetails/SchemaProperty.js.map +1 -0
  22. package/dist/cjs/components/Details/ValidationDetails/ValidationDetails.d.ts +3 -0
  23. package/dist/cjs/components/Details/ValidationDetails/ValidationDetails.js +23 -0
  24. package/dist/cjs/components/Details/ValidationDetails/ValidationDetails.js.map +1 -0
  25. package/dist/cjs/components/Details/ValidationDetails/ValidationDetailsProps.d.ts +8 -0
  26. package/dist/cjs/components/Details/ValidationDetails/ValidationDetailsProps.js +3 -0
  27. package/dist/cjs/components/Details/ValidationDetails/ValidationDetailsProps.js.map +1 -0
  28. package/dist/cjs/components/Details/ValidationDetails/index.d.ts +2 -0
  29. package/dist/cjs/components/Details/ValidationDetails/index.js +6 -0
  30. package/dist/cjs/components/Details/ValidationDetails/index.js.map +1 -0
  31. package/dist/cjs/components/Details/index.d.ts +5 -0
  32. package/dist/cjs/components/Details/index.js +9 -0
  33. package/dist/cjs/components/Details/index.js.map +1 -0
  34. package/dist/cjs/components/Table/PayloadTableColumnConfig.d.ts +10 -0
  35. package/dist/cjs/components/Table/PayloadTableColumnConfig.js +22 -0
  36. package/dist/cjs/components/Table/PayloadTableColumnConfig.js.map +1 -0
  37. package/dist/cjs/components/Table/Table.d.ts +11 -0
  38. package/dist/cjs/components/Table/Table.js +25 -0
  39. package/dist/cjs/components/Table/Table.js.map +1 -0
  40. package/dist/cjs/components/Table/TableRow.d.ts +11 -0
  41. package/dist/cjs/components/Table/TableRow.js +43 -0
  42. package/dist/cjs/components/Table/TableRow.js.map +1 -0
  43. package/dist/cjs/components/Table/index.d.ts +3 -0
  44. package/dist/cjs/components/Table/index.js +7 -0
  45. package/dist/cjs/components/Table/index.js.map +1 -0
  46. package/dist/cjs/components/index.d.ts +2 -0
  47. package/dist/cjs/components/index.js +6 -0
  48. package/dist/cjs/components/index.js.map +1 -0
  49. package/dist/cjs/hooks/index.d.ts +5 -0
  50. package/dist/cjs/hooks/index.js +9 -0
  51. package/dist/cjs/hooks/index.js.map +1 -0
  52. package/dist/cjs/hooks/lib/FetchHuriHashOptions.d.ts +3 -0
  53. package/dist/cjs/hooks/lib/FetchHuriHashOptions.js +3 -0
  54. package/dist/cjs/hooks/lib/FetchHuriHashOptions.js.map +1 -0
  55. package/dist/cjs/hooks/lib/findHuriNetwork.d.ts +3 -0
  56. package/dist/cjs/hooks/lib/findHuriNetwork.js +14 -0
  57. package/dist/cjs/hooks/lib/findHuriNetwork.js.map +1 -0
  58. package/dist/cjs/hooks/lib/index.d.ts +2 -0
  59. package/dist/cjs/hooks/lib/index.js +6 -0
  60. package/dist/cjs/hooks/lib/index.js.map +1 -0
  61. package/dist/cjs/hooks/useGetSchema.d.ts +11 -0
  62. package/dist/cjs/hooks/useGetSchema.js +49 -0
  63. package/dist/cjs/hooks/useGetSchema.js.map +1 -0
  64. package/dist/cjs/hooks/useHuriHash.d.ts +5 -0
  65. package/dist/cjs/hooks/useHuriHash.js +24 -0
  66. package/dist/cjs/hooks/useHuriHash.js.map +1 -0
  67. package/dist/cjs/hooks/usePayload.d.ts +3 -0
  68. package/dist/cjs/hooks/usePayload.js +42 -0
  69. package/dist/cjs/hooks/usePayload.js.map +1 -0
  70. package/dist/cjs/hooks/useResolveHuri.d.ts +5 -0
  71. package/dist/cjs/hooks/useResolveHuri.js +53 -0
  72. package/dist/cjs/hooks/useResolveHuri.js.map +1 -0
  73. package/dist/cjs/index.d.ts +2 -0
  74. package/dist/cjs/index.js +6 -0
  75. package/dist/cjs/index.js.map +1 -0
  76. package/dist/docs.json +144528 -0
  77. package/dist/esm/components/Details/DataDetails.d.ts +10 -0
  78. package/dist/esm/components/Details/DataDetails.js +32 -0
  79. package/dist/esm/components/Details/DataDetails.js.map +1 -0
  80. package/dist/esm/components/Details/Details.d.ts +15 -0
  81. package/dist/esm/components/Details/Details.js +10 -0
  82. package/dist/esm/components/Details/Details.js.map +1 -0
  83. package/dist/esm/components/Details/HashSourceDetails.d.ts +8 -0
  84. package/dist/esm/components/Details/HashSourceDetails.js +11 -0
  85. package/dist/esm/components/Details/HashSourceDetails.js.map +1 -0
  86. package/dist/esm/components/Details/JsonDetails.d.ts +9 -0
  87. package/dist/esm/components/Details/JsonDetails.js +11 -0
  88. package/dist/esm/components/Details/JsonDetails.js.map +1 -0
  89. package/dist/esm/components/Details/MetaDetails.d.ts +8 -0
  90. package/dist/esm/components/Details/MetaDetails.js +9 -0
  91. package/dist/esm/components/Details/MetaDetails.js.map +1 -0
  92. package/dist/esm/components/Details/ValidationDetails/SchemaProperty.d.ts +4 -0
  93. package/dist/esm/components/Details/ValidationDetails/SchemaProperty.js +11 -0
  94. package/dist/esm/components/Details/ValidationDetails/SchemaProperty.js.map +1 -0
  95. package/dist/esm/components/Details/ValidationDetails/ValidationDetails.d.ts +3 -0
  96. package/dist/esm/components/Details/ValidationDetails/ValidationDetails.js +16 -0
  97. package/dist/esm/components/Details/ValidationDetails/ValidationDetails.js.map +1 -0
  98. package/dist/esm/components/Details/ValidationDetails/ValidationDetailsProps.d.ts +8 -0
  99. package/dist/esm/components/Details/ValidationDetails/ValidationDetailsProps.js +2 -0
  100. package/dist/esm/components/Details/ValidationDetails/ValidationDetailsProps.js.map +1 -0
  101. package/dist/esm/components/Details/ValidationDetails/index.d.ts +2 -0
  102. package/dist/esm/components/Details/ValidationDetails/index.js +3 -0
  103. package/dist/esm/components/Details/ValidationDetails/index.js.map +1 -0
  104. package/dist/esm/components/Details/index.d.ts +5 -0
  105. package/dist/esm/components/Details/index.js +6 -0
  106. package/dist/esm/components/Details/index.js.map +1 -0
  107. package/dist/esm/components/Table/PayloadTableColumnConfig.d.ts +10 -0
  108. package/dist/esm/components/Table/PayloadTableColumnConfig.js +18 -0
  109. package/dist/esm/components/Table/PayloadTableColumnConfig.js.map +1 -0
  110. package/dist/esm/components/Table/Table.d.ts +11 -0
  111. package/dist/esm/components/Table/Table.js +18 -0
  112. package/dist/esm/components/Table/Table.js.map +1 -0
  113. package/dist/esm/components/Table/TableRow.d.ts +11 -0
  114. package/dist/esm/components/Table/TableRow.js +36 -0
  115. package/dist/esm/components/Table/TableRow.js.map +1 -0
  116. package/dist/esm/components/Table/index.d.ts +3 -0
  117. package/dist/esm/components/Table/index.js +4 -0
  118. package/dist/esm/components/Table/index.js.map +1 -0
  119. package/dist/esm/components/index.d.ts +2 -0
  120. package/dist/esm/components/index.js +3 -0
  121. package/dist/esm/components/index.js.map +1 -0
  122. package/dist/esm/hooks/index.d.ts +5 -0
  123. package/dist/esm/hooks/index.js +6 -0
  124. package/dist/esm/hooks/index.js.map +1 -0
  125. package/dist/esm/hooks/lib/FetchHuriHashOptions.d.ts +3 -0
  126. package/dist/esm/hooks/lib/FetchHuriHashOptions.js +2 -0
  127. package/dist/esm/hooks/lib/FetchHuriHashOptions.js.map +1 -0
  128. package/dist/esm/hooks/lib/findHuriNetwork.d.ts +3 -0
  129. package/dist/esm/hooks/lib/findHuriNetwork.js +9 -0
  130. package/dist/esm/hooks/lib/findHuriNetwork.js.map +1 -0
  131. package/dist/esm/hooks/lib/index.d.ts +2 -0
  132. package/dist/esm/hooks/lib/index.js +3 -0
  133. package/dist/esm/hooks/lib/index.js.map +1 -0
  134. package/dist/esm/hooks/useGetSchema.d.ts +11 -0
  135. package/dist/esm/hooks/useGetSchema.js +45 -0
  136. package/dist/esm/hooks/useGetSchema.js.map +1 -0
  137. package/dist/esm/hooks/useHuriHash.d.ts +5 -0
  138. package/dist/esm/hooks/useHuriHash.js +21 -0
  139. package/dist/esm/hooks/useHuriHash.js.map +1 -0
  140. package/dist/esm/hooks/usePayload.d.ts +3 -0
  141. package/dist/esm/hooks/usePayload.js +37 -0
  142. package/dist/esm/hooks/usePayload.js.map +1 -0
  143. package/dist/esm/hooks/useResolveHuri.d.ts +5 -0
  144. package/dist/esm/hooks/useResolveHuri.js +49 -0
  145. package/dist/esm/hooks/useResolveHuri.js.map +1 -0
  146. package/dist/esm/index.d.ts +2 -0
  147. package/dist/esm/index.js +3 -0
  148. package/dist/esm/index.js.map +1 -0
  149. package/package.json +139 -0
  150. package/src/components/Details/DataDetails.tsx +60 -0
  151. package/src/components/Details/Details.stories.tsx +46 -0
  152. package/src/components/Details/Details.tsx +33 -0
  153. package/src/components/Details/HashSourceDetails.tsx +40 -0
  154. package/src/components/Details/JsonDetails.tsx +31 -0
  155. package/src/components/Details/MetaDetails.tsx +30 -0
  156. package/src/components/Details/ValidationDetails/SchemaProperty.tsx +22 -0
  157. package/src/components/Details/ValidationDetails/ValidationDetails.stories.tsx +55 -0
  158. package/src/components/Details/ValidationDetails/ValidationDetails.tsx +43 -0
  159. package/src/components/Details/ValidationDetails/ValidationDetailsProps.ts +9 -0
  160. package/src/components/Details/ValidationDetails/index.ts +2 -0
  161. package/src/components/Details/index.ts +5 -0
  162. package/src/components/Table/PayloadTableColumnConfig.ts +28 -0
  163. package/src/components/Table/Table.stories.tsx +43 -0
  164. package/src/components/Table/Table.tsx +63 -0
  165. package/src/components/Table/TableRow.tsx +96 -0
  166. package/src/components/Table/index.ts +3 -0
  167. package/src/components/index.ts +2 -0
  168. package/src/hooks/index.ts +5 -0
  169. package/src/hooks/lib/FetchHuriHashOptions.ts +3 -0
  170. package/src/hooks/lib/findHuriNetwork.ts +13 -0
  171. package/src/hooks/lib/index.ts +2 -0
  172. package/src/hooks/useGetSchema.stories.tsx +70 -0
  173. package/src/hooks/useGetSchema.tsx +51 -0
  174. package/src/hooks/useHuriHash.stories.tsx +68 -0
  175. package/src/hooks/useHuriHash.tsx +27 -0
  176. package/src/hooks/usePayload.tsx +39 -0
  177. package/src/hooks/useResolveHuri.tsx +59 -0
  178. package/src/index.ts +2 -0
  179. package/src/types/images.d.ts +5 -0
@@ -0,0 +1,9 @@
1
+ import { FlexBoxProps } from '@xylabs/sdk-react'
2
+ import { XyoPayload } from '@xyo-network/core'
3
+
4
+ export interface PayloadValidationDetailsProps extends FlexBoxProps {
5
+ skipBody?: boolean
6
+ skipMeta?: boolean
7
+ value?: XyoPayload
8
+ viewSchemaUrl?: string
9
+ }
@@ -0,0 +1,2 @@
1
+ export * from './ValidationDetails'
2
+ export * from './ValidationDetailsProps'
@@ -0,0 +1,5 @@
1
+ export * from './DataDetails'
2
+ export * from './Details'
3
+ export * from './JsonDetails'
4
+ export * from './MetaDetails'
5
+ export * from './ValidationDetails'
@@ -0,0 +1,28 @@
1
+ export type PayloadTableColumnSlug = 'hash' | 'archive' | 'client' | 'date' | 'time' | 'schema' | 'valid'
2
+
3
+ export interface PayloadTableColumnConfig {
4
+ xs?: PayloadTableColumnSlug[]
5
+ sm?: PayloadTableColumnSlug[]
6
+ md?: PayloadTableColumnSlug[]
7
+ lg?: PayloadTableColumnSlug[]
8
+ xl?: PayloadTableColumnSlug[]
9
+ }
10
+
11
+ export const payloadColumnNames: Record<PayloadTableColumnSlug, string> = {
12
+ archive: 'Archive',
13
+ client: 'Client',
14
+ date: 'Date',
15
+ hash: 'Hash',
16
+ schema: 'Schema',
17
+ time: 'Time',
18
+ valid: 'Valid',
19
+ }
20
+
21
+ export const payloadTableColumnConfigDefaults = (): PayloadTableColumnConfig => {
22
+ const xs: PayloadTableColumnSlug[] = ['hash']
23
+ const sm: PayloadTableColumnSlug[] = ['hash', 'valid']
24
+ const md: PayloadTableColumnSlug[] = ['hash', 'schema', 'time', 'valid']
25
+ const lg: PayloadTableColumnSlug[] = ['hash', 'schema', 'archive', 'date', 'time', 'valid']
26
+ const xl: PayloadTableColumnSlug[] = ['hash', 'schema', 'archive', 'client', 'date', 'time', 'valid']
27
+ return { lg, md, sm, xl, xs }
28
+ }
@@ -0,0 +1,43 @@
1
+ import { ComponentMeta, ComponentStory } from '@storybook/react'
2
+ import { BrowserRouter } from 'react-router-dom'
3
+
4
+ import { appThemeDecorator, samplePayload } from '../../.storybook'
5
+ import { PayloadTable } from './Table'
6
+
7
+ const StorybookEntry = {
8
+ argTypes: {},
9
+ component: PayloadTable,
10
+ parameters: {
11
+ docs: {
12
+ page: null,
13
+ },
14
+ },
15
+ title: 'Payload/Table',
16
+ } as ComponentMeta<typeof PayloadTable>
17
+
18
+ const Template: ComponentStory<typeof PayloadTable> = (args) => (
19
+ <BrowserRouter>
20
+ <PayloadTable {...args}></PayloadTable>
21
+ </BrowserRouter>
22
+ )
23
+
24
+ const Default = Template.bind({})
25
+ Default.args = {}
26
+ Default.decorators = [appThemeDecorator]
27
+
28
+ const WithData = Template.bind({})
29
+ WithData.args = { payloads: [samplePayload, samplePayload] }
30
+ WithData.decorators = [appThemeDecorator]
31
+
32
+ const WithError = Template.bind({})
33
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
34
+ const { _hash, ...badPayload } = samplePayload
35
+
36
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
37
+ //@ts-ignore
38
+ WithError.args = { payloads: [samplePayload, badPayload] }
39
+
40
+ export { Default, WithData, WithError }
41
+
42
+ // eslint-disable-next-line import/no-default-export
43
+ export default StorybookEntry
@@ -0,0 +1,63 @@
1
+ import { Alert, Table, TableBody, TableCell, TableHead, TableProps, TableRow, Typography } from '@mui/material'
2
+ import { useBreakpoint } from '@xylabs/sdk-react'
3
+ import { XyoPayload } from '@xyo-network/core'
4
+ import { XyoApiThrownErrorBoundary } from '@xyo-network/react-auth-service'
5
+ import { ScrollTableOnSm } from '@xyo-network/react-shared'
6
+
7
+ import { payloadColumnNames, PayloadTableColumnConfig, payloadTableColumnConfigDefaults } from './PayloadTableColumnConfig'
8
+ import { PayloadTableRow } from './TableRow'
9
+
10
+ export interface PayloadTableProps extends TableProps {
11
+ exploreDomain?: string
12
+ onRowClick?: (value: XyoPayload) => void
13
+ payloads?: XyoPayload[] | null
14
+ columns?: PayloadTableColumnConfig
15
+ }
16
+
17
+ export const PayloadTable: React.FC<PayloadTableProps> = ({ exploreDomain, onRowClick, payloads, children, columns = payloadTableColumnConfigDefaults(), ...props }) => {
18
+ const breakPoint = useBreakpoint()
19
+ return breakPoint ? (
20
+ <ScrollTableOnSm>
21
+ <Table {...props}>
22
+ <TableHead>
23
+ <TableRow>
24
+ {columns[breakPoint]?.map((column, index) => {
25
+ return (
26
+ <TableCell key={index} width={index > 0 ? '10px' : undefined} align={index === 0 ? 'left' : 'center'}>
27
+ <Typography variant="caption" noWrap>
28
+ <strong>{payloadColumnNames[column]}</strong>
29
+ </Typography>
30
+ </TableCell>
31
+ )
32
+ })}
33
+ </TableRow>
34
+ </TableHead>
35
+ <TableBody>
36
+ {payloads?.map((payload, index) => (
37
+ <XyoApiThrownErrorBoundary
38
+ key={`${payload._hash}-${payload._timestamp}-${index}`}
39
+ errorComponent={(e) => (
40
+ <Alert severity="error">
41
+ Error Loading Payload: <Typography fontWeight="bold">{e.message}</Typography>
42
+ </Alert>
43
+ )}
44
+ >
45
+ <PayloadTableRow
46
+ onClick={
47
+ onRowClick
48
+ ? () => {
49
+ onRowClick(payload)
50
+ }
51
+ : undefined
52
+ }
53
+ exploreDomain={exploreDomain}
54
+ payload={payload}
55
+ />
56
+ </XyoApiThrownErrorBoundary>
57
+ ))}
58
+ {children}
59
+ </TableBody>
60
+ </Table>
61
+ </ScrollTableOnSm>
62
+ ) : null
63
+ }
@@ -0,0 +1,96 @@
1
+ import { TableCell, TableRow, TableRowProps, Typography } from '@mui/material'
2
+ import { useBreakpoint } from '@xylabs/sdk-react'
3
+ import { XyoPayload, XyoPayloadWrapper, XyoPayloadWrapperValidator } from '@xyo-network/core'
4
+ import { useNetwork } from '@xyo-network/react-network'
5
+ import { HashTableCell } from '@xyo-network/react-shared'
6
+ import { DateTime } from 'luxon'
7
+ import { ReactElement } from 'react'
8
+ import { MdClear, MdDone } from 'react-icons/md'
9
+
10
+ import { PayloadTableColumnConfig, payloadTableColumnConfigDefaults, PayloadTableColumnSlug } from './PayloadTableColumnConfig'
11
+
12
+ export interface PayloadTableRowProps extends TableRowProps {
13
+ payload?: XyoPayload
14
+ exploreDomain?: string
15
+ columns?: PayloadTableColumnConfig
16
+ network?: string
17
+ }
18
+
19
+ export const PayloadTableRow: React.FC<PayloadTableRowProps> = ({ exploreDomain, network: networkProp, payload, columns = payloadTableColumnConfigDefaults(), ...props }) => {
20
+ const breakPoint = useBreakpoint()
21
+ const timeStamp = payload?._timestamp ? DateTime.fromMillis(payload?._timestamp) : undefined
22
+ const wrapper = payload ? new XyoPayloadWrapper(payload) : undefined
23
+ const { network } = useNetwork()
24
+
25
+ const archive = (
26
+ <TableCell key="archive" align="center">
27
+ <Typography fontFamily="monospace" variant="body2" noWrap>
28
+ {payload?._archive}
29
+ </Typography>
30
+ </TableCell>
31
+ )
32
+
33
+ const client = (
34
+ <TableCell key="client" align="center">
35
+ <Typography fontFamily="monospace" variant="body2" noWrap>
36
+ {payload?._client}
37
+ </Typography>
38
+ </TableCell>
39
+ )
40
+
41
+ const date = (
42
+ <TableCell key="date" align="center">
43
+ <Typography fontFamily="monospace" variant="body2" noWrap>
44
+ {timeStamp?.toLocaleString(DateTime.DATE_SHORT)}
45
+ </Typography>
46
+ </TableCell>
47
+ )
48
+
49
+ const hash = (
50
+ <HashTableCell key="hash" value={payload?._hash} archive={payload?._archive} dataType="payload" exploreDomain={exploreDomain} network={networkProp ?? network?.slug} />
51
+ )
52
+
53
+ const schema = (
54
+ <TableCell key="payloads" align="center">
55
+ <Typography fontFamily="monospace" variant="body2" noWrap>
56
+ {payload?.schema}
57
+ </Typography>
58
+ </TableCell>
59
+ )
60
+
61
+ const time = (
62
+ <TableCell key="time" align="center">
63
+ <Typography fontFamily="monospace" variant="body2" noWrap>
64
+ {timeStamp?.toLocaleString(DateTime.TIME_SIMPLE)}
65
+ </Typography>
66
+ </TableCell>
67
+ )
68
+
69
+ const isValid = wrapper ? new XyoPayloadWrapperValidator(wrapper).all().length === 0 : undefined
70
+
71
+ const valid = (
72
+ <TableCell key="valid" align="center">
73
+ <Typography fontFamily="monospace" variant="body2" noWrap>
74
+ {isValid === undefined ? <MdDone fontSize={18} color="yellow" /> : isValid ? <MdDone fontSize={18} color="green" /> : <MdClear color="red" fontSize={18} />}
75
+ </Typography>
76
+ </TableCell>
77
+ )
78
+
79
+ const tableCells: Record<PayloadTableColumnSlug, ReactElement> = {
80
+ archive,
81
+ client,
82
+ date,
83
+ hash,
84
+ schema,
85
+ time,
86
+ valid,
87
+ }
88
+
89
+ return breakPoint ? (
90
+ <TableRow style={{ maxWidth: '100vw' }} {...props}>
91
+ {columns[breakPoint]?.map((column) => {
92
+ return tableCells[column]
93
+ })}
94
+ </TableRow>
95
+ ) : null
96
+ }
@@ -0,0 +1,3 @@
1
+ export * from './PayloadTableColumnConfig'
2
+ export * from './Table'
3
+ export * from './TableRow'
@@ -0,0 +1,2 @@
1
+ export * from './Details'
2
+ export * from './Table'
@@ -0,0 +1,5 @@
1
+ export * from './lib'
2
+ export * from './useGetSchema'
3
+ export * from './useHuriHash'
4
+ export * from './usePayload'
5
+ export * from './useResolveHuri'
@@ -0,0 +1,3 @@
1
+ export interface FetchHuriHashOptions {
2
+ changeActiveNetwork?: boolean
3
+ }
@@ -0,0 +1,13 @@
1
+ import { Huri } from '@xyo-network/core'
2
+ import { XyoNetworkPayload } from '@xyo-network/network'
3
+
4
+ export const findHuriNetwork = (huriInstance: Huri, networks?: XyoNetworkPayload[]) => {
5
+ // see if huri archivist matches any archivist in the network configs
6
+ return networks
7
+ ?.filter((network) =>
8
+ network.nodes?.find((node) => {
9
+ return node.type === 'archivist' && new URL(node.uri).hostname === huriInstance.archivist
10
+ })
11
+ )
12
+ .shift()
13
+ }
@@ -0,0 +1,2 @@
1
+ export * from './FetchHuriHashOptions'
2
+ export * from './findHuriNetwork'
@@ -0,0 +1,70 @@
1
+ import { FormControl, TextField, Typography } from '@mui/material'
2
+ import { ComponentStory, Meta } from '@storybook/react'
3
+ import { FlexCol, FlexRow } from '@xylabs/sdk-react'
4
+ import { XyoSchemaCache } from '@xyo-network/utils'
5
+ import { lazy, Suspense, useEffect, useState } from 'react'
6
+
7
+ import { useGetSchemaPayload } from './useGetSchema'
8
+
9
+ const JsonView = lazy(() => import(/* webpackChunkName: "jsonView" */ 'react-json-view'))
10
+
11
+ XyoSchemaCache.instance.proxy = 'https://beta.api.archivist.xyo.network/domain'
12
+
13
+ const UseGetSchemaComponent: React.FC<{ schema: string }> = ({ schema }) => {
14
+ const exampleSchemas = ['network.xyo.domain', 'network.xyo.payload', 'network.xyo.schema']
15
+ const [schemaFieldValue, setSchemaFieldValue] = useState('')
16
+ const { schemaPayload } = useGetSchemaPayload(schemaFieldValue)
17
+
18
+ useEffect(() => {
19
+ if (schema) {
20
+ setSchemaFieldValue(schema)
21
+ }
22
+ }, [schema])
23
+
24
+ return (
25
+ <>
26
+ <Typography variant="body1" fontWeight="bold" mb={2}>
27
+ Example schemas to test:
28
+ {exampleSchemas.map((schema, index) => (
29
+ <Typography component="span" mx={1} key={index} onClick={() => setSchemaFieldValue(schema)} sx={{ cursor: 'pointer', textDecoration: 'underline' }}>
30
+ {schema}
31
+ </Typography>
32
+ ))}
33
+ </Typography>
34
+ <FormControl>
35
+ <TextField value={schemaFieldValue} label="Schema Name" onChange={(e) => setSchemaFieldValue(e.target.value)} />
36
+ </FormControl>
37
+ <FlexRow my={3} justifyContent="start">
38
+ <Suspense fallback={<FlexCol busy />}>
39
+ <JsonView src={schemaPayload || {}} />
40
+ </Suspense>
41
+ </FlexRow>
42
+ </>
43
+ )
44
+ }
45
+
46
+ const StorybookEntry: Meta = {
47
+ argTypes: {},
48
+ component: UseGetSchemaComponent,
49
+ parameters: {
50
+ docs: {
51
+ page: null,
52
+ },
53
+ },
54
+ title: 'hooks/useGetSchema',
55
+ }
56
+
57
+ const Template: ComponentStory<typeof UseGetSchemaComponent> = ({ schema }) => {
58
+ return <UseGetSchemaComponent schema={schema} />
59
+ }
60
+
61
+ const Default = Template.bind({})
62
+ Default.args = { schema: 'network.xyo.schema' }
63
+
64
+ const Domain = Template.bind({})
65
+ Domain.args = { schema: 'network.xyo.domain' }
66
+
67
+ export { Default, Domain }
68
+
69
+ // eslint-disable-next-line import/no-default-export
70
+ export default StorybookEntry
@@ -0,0 +1,51 @@
1
+ import { useAsyncEffect } from '@xylabs/sdk-react'
2
+ import { XyoApiError } from '@xyo-network/api'
3
+ import { XyoPayloadBuilder } from '@xyo-network/core'
4
+ import { XyoSchemaCache, XyoSchemaCacheEntry } from '@xyo-network/utils'
5
+ import { useState } from 'react'
6
+
7
+ /**
8
+ * Gets a Huri and schema payload from a schema string
9
+ */
10
+ const useGetSchemaPayload = (schema?: string) => {
11
+ const [notFound, setNotFound] = useState(false)
12
+ const [apiError, setApiError] = useState<XyoApiError>()
13
+ const [schemaCacheEntry, setSchemaCacheEntry] = useState<XyoSchemaCacheEntry | null | undefined>()
14
+ const [schemaLocal, setSchemaLocal] = useState<string>()
15
+
16
+ useAsyncEffect(
17
+ // eslint-disable-next-line react-hooks/exhaustive-deps
18
+ async (mounted) => {
19
+ const firstRequest = !notFound && !schemaCacheEntry && !apiError
20
+ const schemaChanged = schema !== schemaLocal
21
+
22
+ if ((schema && firstRequest) || (schema && schemaChanged)) {
23
+ try {
24
+ const schemaCacheEntry = await XyoSchemaCache.instance.get(schema)
25
+ if (mounted()) {
26
+ setSchemaCacheEntry(schemaCacheEntry)
27
+ setNotFound(schemaCacheEntry === null || schemaCacheEntry === undefined)
28
+ }
29
+ } catch (e) {
30
+ console.error(e)
31
+ if (mounted()) {
32
+ setApiError(e as XyoApiError)
33
+ }
34
+ }
35
+ }
36
+ if (schemaChanged) {
37
+ setSchemaLocal(schema)
38
+ }
39
+ },
40
+ [apiError, notFound, schema, schemaLocal, schemaCacheEntry]
41
+ )
42
+
43
+ return {
44
+ apiError,
45
+ notFound,
46
+ schemaHuri: schemaCacheEntry?.huri,
47
+ schemaPayload: schemaCacheEntry ? new XyoPayloadBuilder(schemaCacheEntry?.payload).fields(schemaCacheEntry.payload).build() : schemaCacheEntry,
48
+ }
49
+ }
50
+
51
+ export { useGetSchemaPayload }
@@ -0,0 +1,68 @@
1
+ import { Typography } from '@mui/material'
2
+ import { ComponentStory, Meta } from '@storybook/react'
3
+ import { FlexCol } from '@xylabs/sdk-react'
4
+ import { Huri } from '@xyo-network/core'
5
+ import { ArchivistApiProvider } from '@xyo-network/react-archivist-api'
6
+ import { XyoSchemaCache } from '@xyo-network/utils'
7
+ import { lazy, Suspense } from 'react'
8
+
9
+ import { useHuriHash } from './useHuriHash'
10
+
11
+ const JsonView = lazy(() => import(/* webpackChunkName: "jsonView" */ 'react-json-view'))
12
+
13
+ interface UseHuriHashComponentProps {
14
+ huriOrHash: string | Huri
15
+ }
16
+
17
+ const apiDomain = 'https://beta.api.archivist.xyo.network'
18
+ const hash = '5605fabad11b10bb5fb86b309ca0970894eda8f22362dda1a489817723bca992'
19
+ XyoSchemaCache.instance.proxy = `${apiDomain}/domain`
20
+
21
+ const Wrapper: React.FC<UseHuriHashComponentProps> = ({ huriOrHash }) => (
22
+ <ArchivistApiProvider apiDomain={apiDomain}>
23
+ <UseHuriHashComponent huriOrHash={huriOrHash} />
24
+ </ArchivistApiProvider>
25
+ )
26
+
27
+ const UseHuriHashComponent: React.FC<UseHuriHashComponentProps> = ({ huriOrHash }) => {
28
+ const [payload] = useHuriHash(huriOrHash)
29
+
30
+ return (
31
+ <>
32
+ <Typography variant="body1" fontWeight="bold">
33
+ Fetches the payload for a huriOrHash.
34
+ </Typography>
35
+ <FlexCol my={3}>
36
+ <Suspense fallback={<FlexCol busy />}>
37
+ <JsonView src={payload || {}} />
38
+ </Suspense>
39
+ </FlexCol>
40
+ </>
41
+ )
42
+ }
43
+
44
+ const StorybookEntry: Meta = {
45
+ argTypes: {},
46
+ component: UseHuriHashComponent,
47
+ parameters: {
48
+ docs: {
49
+ page: null,
50
+ },
51
+ },
52
+ title: 'hooks/useHuriHash',
53
+ }
54
+
55
+ const Template: ComponentStory<typeof UseHuriHashComponent> = ({ huriOrHash }) => {
56
+ return <Wrapper huriOrHash={huriOrHash} />
57
+ }
58
+
59
+ const Default = Template.bind({})
60
+ Default.args = { huriOrHash: hash }
61
+
62
+ const WithHuri = Template.bind({})
63
+ WithHuri.args = { huriOrHash: new Huri(`${apiDomain}/${hash}`) }
64
+
65
+ export { Default, WithHuri }
66
+
67
+ // eslint-disable-next-line import/no-default-export
68
+ export default StorybookEntry
@@ -0,0 +1,27 @@
1
+ import { XyoApiError } from '@xyo-network/api'
2
+ import { Huri, XyoPayload } from '@xyo-network/core'
3
+ import { useCallback } from 'react'
4
+
5
+ import { FetchHuriHashOptions } from './lib'
6
+ import { usePayload } from './usePayload'
7
+ import { useResolveHuri } from './useResolveHuri'
8
+
9
+ const useHuriHash = (huriOrHash?: string | Huri, huriUri?: string, options?: FetchHuriHashOptions): [XyoPayload | undefined, boolean, XyoApiError | undefined] => {
10
+ const hash = useCallback((huriOrHash?: string | Huri) => {
11
+ if (huriOrHash) {
12
+ if (typeof huriOrHash === 'string') {
13
+ return huriOrHash
14
+ }
15
+ if (huriOrHash instanceof Huri) {
16
+ return huriOrHash.hash
17
+ }
18
+ }
19
+ }, [])
20
+
21
+ const [payload, notFound, apiError] = usePayload(hash(huriOrHash))
22
+ const [huriPayload, huriPayloadNotFound, huriApiError] = useResolveHuri(huriUri, notFound, options)
23
+
24
+ return [payload ?? huriPayload, huriPayloadNotFound, apiError ?? huriApiError]
25
+ }
26
+
27
+ export { useHuriHash }
@@ -0,0 +1,39 @@
1
+ import { useAsyncEffect } from '@xylabs/sdk-react'
2
+ import { XyoApiError } from '@xyo-network/api'
3
+ import { XyoPayload } from '@xyo-network/core'
4
+ import { useArchive } from '@xyo-network/react-archive'
5
+ import { useArchivistApi } from '@xyo-network/react-archivist-api'
6
+ import { useState } from 'react'
7
+
8
+ export const usePayload = (hash?: string): [XyoPayload | undefined, boolean, XyoApiError | undefined] => {
9
+ const { api } = useArchivistApi()
10
+ const { archive } = useArchive()
11
+ const [notFound, setNotFound] = useState(false)
12
+ const [apiError, setApiError] = useState<XyoApiError>()
13
+ const [payload, setPayload] = useState<XyoPayload>()
14
+ useAsyncEffect(
15
+ // eslint-disable-next-line react-hooks/exhaustive-deps
16
+ async (mounted) => {
17
+ if (hash && hash.length > 0) {
18
+ try {
19
+ const result = await api?.archive(archive).payload.hash(hash).get()
20
+ if (mounted()) {
21
+ setApiError(undefined)
22
+ if (result?.length) {
23
+ setPayload(result[0])
24
+ } else if (result) {
25
+ setNotFound(true)
26
+ setPayload(undefined)
27
+ }
28
+ }
29
+ } catch (e) {
30
+ setApiError(e as XyoApiError)
31
+ setPayload(undefined)
32
+ console.error(e)
33
+ }
34
+ }
35
+ },
36
+ [hash, api, archive]
37
+ )
38
+ return [payload, notFound, apiError]
39
+ }
@@ -0,0 +1,59 @@
1
+ import { useAsyncEffect } from '@xylabs/sdk-react'
2
+ import { XyoApiError } from '@xyo-network/api'
3
+ import { Huri, XyoPayload } from '@xyo-network/core'
4
+ import { XyoNetworkPayload } from '@xyo-network/network'
5
+ import { useNetwork } from '@xyo-network/react-network'
6
+ import { useState } from 'react'
7
+
8
+ import { FetchHuriHashOptions, findHuriNetwork } from './lib'
9
+
10
+ const useResolveHuri = (huriUri?: string, dependentNotFound?: boolean, options?: FetchHuriHashOptions): [XyoPayload | undefined, boolean, XyoApiError | undefined] => {
11
+ const { network, networks, setNetwork } = useNetwork()
12
+ const [huriNetwork, setHuriNetwork] = useState<XyoNetworkPayload>()
13
+ const [huriPayload, setHuriPayload] = useState<XyoPayload>()
14
+ const [huriPayloadNotFound, setHuriPayloadNotFound] = useState(false)
15
+ const [huriApiError, setHuriApiError] = useState<XyoApiError>()
16
+
17
+ const { changeActiveNetwork } = options ?? {}
18
+
19
+ useAsyncEffect(
20
+ // eslint-disable-next-line react-hooks/exhaustive-deps
21
+ async (mounted) => {
22
+ if ((dependentNotFound === undefined || dependentNotFound) && huriUri && network !== huriNetwork) {
23
+ const huriInstance = new Huri(huriUri)
24
+
25
+ const foundHuriNetwork = findHuriNetwork(huriInstance, networks)
26
+
27
+ // Update when we found a huri network and it doesn't match the current one
28
+ if (foundHuriNetwork && mounted()) {
29
+ setHuriNetwork(huriNetwork)
30
+ if (network !== foundHuriNetwork && changeActiveNetwork) {
31
+ setNetwork?.(foundHuriNetwork)
32
+ return
33
+ }
34
+
35
+ // If no matching network try to resolve the huri itself
36
+ try {
37
+ const huriPayload = await huriInstance.fetch()
38
+ if (mounted()) {
39
+ if (huriPayload) {
40
+ setHuriPayload(huriPayload)
41
+ } else {
42
+ setHuriPayloadNotFound(true)
43
+ }
44
+ }
45
+ } catch (e) {
46
+ if (mounted()) {
47
+ setHuriApiError(e as XyoApiError)
48
+ }
49
+ }
50
+ }
51
+ }
52
+ },
53
+ [huriNetwork, huriUri, network, networks, dependentNotFound, setNetwork, changeActiveNetwork]
54
+ )
55
+
56
+ return [huriPayload, huriPayloadNotFound, huriApiError]
57
+ }
58
+
59
+ export { useResolveHuri }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './components'
2
+ export * from './hooks'
@@ -0,0 +1,5 @@
1
+ declare module '*.png'
2
+ declare module '*.jpg'
3
+ declare module '*.svg'
4
+ declare module '*.gif'
5
+ declare module '*.webp'