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