-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuse-entities-public.tsx
More file actions
62 lines (59 loc) · 1.76 KB
/
Copy pathuse-entities-public.tsx
File metadata and controls
62 lines (59 loc) · 1.76 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
import { Constants, Entity } from '@graphprotocol/hypergraph';
import { useQuery as useQueryTanstack } from '@tanstack/react-query';
import * as Option from 'effect/Option';
import type * as Schema from 'effect/Schema';
import * as SchemaAST from 'effect/SchemaAST';
import type { QueryPublicParams } from './types.js';
import { useHypergraphSpaceInternal } from './use-hypergraph-space-internal.js';
export const useEntitiesPublic = <S extends Schema.Schema.AnyNoContext>(type: S, params?: QueryPublicParams<S>) => {
const {
enabled = true,
filter,
include,
space: spaceFromParams,
spaces,
first = 100,
offset,
orderBy,
backlinksTotalCountsTypeId1,
logInvalidResults = true,
} = params ?? {};
const { space: spaceFromContext } = useHypergraphSpaceInternal();
const space = spaceFromParams ?? spaceFromContext;
const spaceSelectionKey = spaces ?? space;
const typeIds = SchemaAST.getAnnotation<string[]>(Constants.TypeIdsSymbol)(type.ast as SchemaAST.TypeLiteral).pipe(
Option.getOrElse(() => []),
);
const result = useQueryTanstack({
queryKey: [
'hypergraph-public-entities',
spaceSelectionKey,
typeIds,
include,
filter,
first,
offset,
orderBy,
backlinksTotalCountsTypeId1,
],
queryFn: async () => {
return Entity.findManyPublic(type, {
filter,
include,
...(spaces ? { spaces } : { space }),
first,
offset,
orderBy,
backlinksTotalCountsTypeId1,
logInvalidResults,
});
},
enabled,
});
return {
...result,
data: result.data?.data || [],
invalidEntities: result.data?.invalidEntities || [],
invalidRelationEntities: result.data?.invalidRelationEntities || [],
};
};