|
| 1 | +import { type GeoSmartAccount, Graph, type Op } from '@graphprotocol/grc-20'; |
| 2 | +import type { Entity } from '@graphprotocol/hypergraph'; |
| 3 | +import { useQueryClient } from '@tanstack/react-query'; |
| 4 | +import request, { gql } from 'graphql-request'; |
| 5 | +import { publishOps } from '../publish-ops.js'; |
| 6 | +import { GEO_API_TESTNET_ENDPOINT } from './constants.js'; |
| 7 | + |
| 8 | +type DeleteEntityPublicParams = { |
| 9 | + space: string; |
| 10 | +}; |
| 11 | + |
| 12 | +const deleteEntityQueryDocument = gql` |
| 13 | +query entityToDelete($entityId: String!, $spaceId: String!) { |
| 14 | + entity(id: $entityId, spaceId: $spaceId) { |
| 15 | + values { |
| 16 | + propertyId |
| 17 | + } |
| 18 | + relations { |
| 19 | + id |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | +`; |
| 24 | + |
| 25 | +type EntityToDeleteQueryResult = { |
| 26 | + entity: { |
| 27 | + values: { |
| 28 | + propertyId: string; |
| 29 | + }[]; |
| 30 | + relations: { |
| 31 | + id: string; |
| 32 | + }[]; |
| 33 | + }; |
| 34 | +} | null; |
| 35 | + |
| 36 | +export const useDeleteEntityPublic = <S extends Entity.AnyNoContext>(type: S, { space }: DeleteEntityPublicParams) => { |
| 37 | + const queryClient = useQueryClient(); |
| 38 | + |
| 39 | + return async ({ id, walletClient }: { id: string; walletClient: GeoSmartAccount }) => { |
| 40 | + try { |
| 41 | + const result = await request<EntityToDeleteQueryResult>(GEO_API_TESTNET_ENDPOINT, deleteEntityQueryDocument, { |
| 42 | + spaceId: space, |
| 43 | + entityId: id, |
| 44 | + }); |
| 45 | + if (!result) { |
| 46 | + return { success: false, error: 'Entity not found' }; |
| 47 | + } |
| 48 | + const { values, relations } = result.entity; |
| 49 | + const ops: Op[] = []; |
| 50 | + const { ops: unsetEntityValuesOps } = Graph.unsetEntityValues({ |
| 51 | + id, |
| 52 | + properties: values.map(({ propertyId }) => propertyId), |
| 53 | + }); |
| 54 | + ops.push(...unsetEntityValuesOps); |
| 55 | + for (const relation of relations) { |
| 56 | + const { ops: deleteRelationOps } = Graph.deleteRelation({ id: relation.id }); |
| 57 | + ops.push(...deleteRelationOps); |
| 58 | + } |
| 59 | + |
| 60 | + const { cid, txResult } = await publishOps({ |
| 61 | + ops, |
| 62 | + space, |
| 63 | + name: `Delete entity ${id}`, |
| 64 | + walletClient, |
| 65 | + network: 'TESTNET', |
| 66 | + }); |
| 67 | + // TODO: temporary fix until we get the information from the API when a transaction is confirmed |
| 68 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 69 | + queryClient.invalidateQueries({ |
| 70 | + queryKey: [ |
| 71 | + 'hypergraph-public-entities', |
| 72 | + // @ts-expect-error - TODO: find a better way to access the type.name |
| 73 | + type.name, |
| 74 | + space, |
| 75 | + ], |
| 76 | + }); |
| 77 | + |
| 78 | + return { success: true, cid, txResult }; |
| 79 | + } catch (error) { |
| 80 | + return { success: false, error: 'Failed to delete entity' }; |
| 81 | + } |
| 82 | + }; |
| 83 | +}; |
0 commit comments