-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuse-entities.tsx
More file actions
60 lines (55 loc) · 2 KB
/
Copy pathuse-entities.tsx
File metadata and controls
60 lines (55 loc) · 2 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
import type { Entity } from '@graphprotocol/hypergraph';
import type * as Schema from 'effect/Schema';
import { useEntitiesPrivate } from '../internal/use-entities-private.js';
import { useEntitiesPublic } from '../internal/use-entities-public.js';
import { useHypergraphSpaceInternal } from '../internal/use-hypergraph-space-internal.js';
type SpaceSelectionInputOptionInContext = {
space?: never;
spaces?: never;
};
type UseEntitiesParams<S extends Schema.Schema.AnyNoContext> = (
| Entity.SpaceSelectionInput
| SpaceSelectionInputOptionInContext
) & {
mode: 'public' | 'private';
filter?: Entity.EntityFilter<Schema.Schema.Type<S>> | undefined;
// TODO: restrict multi-level nesting to the actual relation keys
include?: Entity.EntityInclude<S> | undefined;
first?: number | undefined;
offset?: number | undefined;
orderBy?:
| {
property: keyof Schema.Schema.Type<S>;
direction: 'asc' | 'desc';
}
| undefined;
backlinksTotalCountsTypeId1?: string | undefined;
};
export function useEntities<const S extends Schema.Schema.AnyNoContext>(type: S, params: UseEntitiesParams<S>) {
const { mode, filter, include, space, spaces, first, offset, orderBy, backlinksTotalCountsTypeId1 } = params;
const { space: spaceFromContext } = useHypergraphSpaceInternal();
const resolvedSpace = space ?? spaceFromContext;
const publicSpaceParams = spaces ? { spaces } : { space: resolvedSpace };
const publicResult = useEntitiesPublic(type, {
enabled: mode === 'public',
filter,
include,
first,
offset,
orderBy,
backlinksTotalCountsTypeId1,
...publicSpaceParams,
});
const localResult = useEntitiesPrivate(type, { enabled: mode === 'private', filter, include, space: resolvedSpace });
if (mode === 'public') {
return {
...publicResult,
deleted: [],
};
}
return {
...publicResult,
data: localResult.entities as (Entity.Entity<S> & { backlinksTotalCountsTypeId1?: number })[],
deleted: localResult.deletedEntities,
};
}