|
| 1 | +import { type GeoSmartAccount, Graph, Id, type PropertiesParam, type RelationsParam } from '@graphprotocol/grc-20'; |
| 2 | +import type { Entity } from '@graphprotocol/hypergraph'; |
| 3 | +import { Type, store } from '@graphprotocol/hypergraph'; |
| 4 | +import { useQueryClient } from '@tanstack/react-query'; |
| 5 | +import { useSelector } from '@xstate/store/react'; |
| 6 | +import type * as Schema from 'effect/Schema'; |
| 7 | +import { publishOps } from '../publish-ops.js'; |
| 8 | + |
| 9 | +type CreateEntityPublicParams = { |
| 10 | + space: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export function useCreateEntityPublic<const S extends Entity.AnyNoContext>( |
| 14 | + type: S, |
| 15 | + { space }: CreateEntityPublicParams, |
| 16 | +) { |
| 17 | + const mapping = useSelector(store, (state) => state.context.mapping); |
| 18 | + const queryClient = useQueryClient(); |
| 19 | + |
| 20 | + return async ( |
| 21 | + data: Readonly<Schema.Schema.Type<Entity.Insert<S>>>, |
| 22 | + { walletClient }: { walletClient: GeoSmartAccount }, |
| 23 | + // TODO: return the entity with this type: Promise<Entity.Entity<S>> |
| 24 | + ) => { |
| 25 | + try { |
| 26 | + // @ts-expect-error TODO should use the actual type instead of the name in the mapping |
| 27 | + const typeName = type.name; |
| 28 | + const mappingEntry = mapping?.[typeName]; |
| 29 | + if (!mappingEntry) { |
| 30 | + throw new Error(`Mapping entry for ${typeName} not found`); |
| 31 | + } |
| 32 | + |
| 33 | + const fields = type.fields; |
| 34 | + const values: PropertiesParam = []; |
| 35 | + for (const [key, value] of Object.entries(mappingEntry.properties || {})) { |
| 36 | + let serializedValue: string = data[key]; |
| 37 | + if (fields[key] === Type.Checkbox) { |
| 38 | + serializedValue = Graph.serializeCheckbox(data[key]); |
| 39 | + } else if (fields[key] === Type.Date) { |
| 40 | + serializedValue = Graph.serializeDate(data[key]); |
| 41 | + } else if (fields[key] === Type.Point) { |
| 42 | + serializedValue = Graph.serializePoint(data[key]); |
| 43 | + } else if (fields[key] === Type.Number) { |
| 44 | + serializedValue = Graph.serializeNumber(data[key]); |
| 45 | + } |
| 46 | + |
| 47 | + values.push({ |
| 48 | + property: Id.Id(value), |
| 49 | + value: serializedValue, |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + const relations: RelationsParam = {}; |
| 54 | + for (const [key, relationId] of Object.entries(mappingEntry.relations || {})) { |
| 55 | + const toIds: { toEntity: Id.Id }[] = []; |
| 56 | + |
| 57 | + if (data[key]) { |
| 58 | + // @ts-expect-error - TODO: fix the types error |
| 59 | + for (const entity of data[key]) { |
| 60 | + if (typeof entity === 'string') { |
| 61 | + toIds.push({ toEntity: Id.Id(entity) }); |
| 62 | + } else { |
| 63 | + toIds.push({ toEntity: Id.Id(entity.id) }); |
| 64 | + } |
| 65 | + } |
| 66 | + relations[Id.Id(relationId)] = toIds; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + const { ops } = Graph.createEntity({ |
| 71 | + types: mappingEntry.typeIds, |
| 72 | + id: data.id, |
| 73 | + values, |
| 74 | + relations, |
| 75 | + }); |
| 76 | + |
| 77 | + const { cid, txResult } = await publishOps({ |
| 78 | + ops, |
| 79 | + space, |
| 80 | + name: `Create entity ${data.id}`, |
| 81 | + walletClient, |
| 82 | + network: 'TESTNET', |
| 83 | + }); |
| 84 | + // TODO: temporary fix until we get the information from the API when a transaction is confirmed |
| 85 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 86 | + queryClient.invalidateQueries({ |
| 87 | + queryKey: [ |
| 88 | + 'hypergraph-public-entities', |
| 89 | + // @ts-expect-error - TODO: find a better way to access the type.name |
| 90 | + type.name, |
| 91 | + space, |
| 92 | + ], |
| 93 | + }); |
| 94 | + return { success: true, cid, txResult }; |
| 95 | + } catch (error) { |
| 96 | + console.error(error); |
| 97 | + return { success: false, error: 'Failed to create entity' }; |
| 98 | + } |
| 99 | + }; |
| 100 | +} |
0 commit comments