-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuse-entities-public-infinite.ts
More file actions
53 lines (50 loc) · 1.84 KB
/
Copy pathuse-entities-public-infinite.ts
File metadata and controls
53 lines (50 loc) · 1.84 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
import { Constants, Entity } from '@graphprotocol/hypergraph';
import { useInfiniteQuery as useInfiniteQueryTanstack } from '@tanstack/react-query';
import * as Option from 'effect/Option';
import type * as Schema from 'effect/Schema';
import * as SchemaAST from 'effect/SchemaAST';
import { useHypergraphApp } from '../HypergraphAppContext.js';
import type { QueryPublicParams } from '../internal/types.js';
import { useHypergraphSpaceInternal } from '../internal/use-hypergraph-space-internal.js';
export const useEntitiesPublicInfinite = <S extends Schema.Schema.AnyNoContext>(
type: S,
params?: QueryPublicParams<S>,
) => {
const {
enabled = true,
filter,
include,
space: spaceFromParams,
spaces,
first = 2,
offset = 0,
logInvalidResults: logInvalidResultsParam,
} = params ?? {};
const { space: spaceFromContext } = useHypergraphSpaceInternal();
const { logInvalidResults: contextLogInvalidResults = true } = useHypergraphApp();
const logInvalidResults = logInvalidResultsParam ?? contextLogInvalidResults ?? true;
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 = useInfiniteQueryTanstack({
queryKey: ['hypergraph-public-entities', spaceSelectionKey, typeIds, include, filter, 'infinite'],
queryFn: async ({ pageParam }) => {
return Entity.findManyPublic(type, {
filter,
include,
...(spaces ? { spaces } : { space }),
first,
offset: pageParam,
logInvalidResults,
});
},
getNextPageParam: (_lastPage, pages) => {
return offset + pages.length * first;
},
initialPageParam: offset,
enabled,
});
return result;
};