-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuse-delete-entity-public.tsx
More file actions
52 lines (45 loc) · 1.67 KB
/
use-delete-entity-public.tsx
File metadata and controls
52 lines (45 loc) · 1.67 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
import { Graph } from '@geoprotocol/geo-sdk';
import type { Connect } from '@graphprotocol/hypergraph';
import { Constants } from '@graphprotocol/hypergraph';
import { useQueryClient } from '@tanstack/react-query';
import * as Option from 'effect/Option';
import type * as Schema from 'effect/Schema';
import * as SchemaAST from 'effect/SchemaAST';
import { publishOps } from '../publish-ops.js';
type DeleteEntityPublicParams = {
space: string;
};
export const useDeleteEntityPublic = <S extends Schema.Schema.AnyNoContext>(
type: S,
{ space }: DeleteEntityPublicParams,
) => {
const queryClient = useQueryClient();
return async ({ id, walletClient }: { id: string; walletClient: Connect.SmartSessionClient }) => {
try {
const { ops } = Graph.deleteEntity({ id });
const { cid, txResult } = await publishOps({
ops,
space,
name: `Delete entity ${id}`,
walletClient,
});
const typeIds = SchemaAST.getAnnotation<string[]>(Constants.TypeIdsSymbol)(
type.ast as SchemaAST.TypeLiteral,
).pipe(Option.getOrElse(() => []));
// TODO: temporary fix until we get the information from the API when a transaction is confirmed
await new Promise((resolve) => setTimeout(resolve, 2000));
if (typeIds.length > 0) {
queryClient.invalidateQueries({
queryKey: ['hypergraph-public-entities', space, typeIds],
});
} else {
queryClient.invalidateQueries({
queryKey: ['hypergraph-public-entities', space],
});
}
return { success: true, cid, txResult };
} catch (_error) {
return { success: false, error: 'Failed to delete entity' };
}
};
};