-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuse-query.tsx
More file actions
221 lines (197 loc) · 7.85 KB
/
Copy pathuse-query.tsx
File metadata and controls
221 lines (197 loc) · 7.85 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { type Entity, Type, Utils } from '@graphprotocol/hypergraph';
import type * as Schema from 'effect/Schema';
import { useQueryLocal } from './HypergraphSpaceContext.js';
import { useQueryPublic } from './internal/use-query-public.js';
import type { DiffEntry } from './types.js';
type QueryParams<S extends Entity.AnyNoContext> = {
mode: 'public' | 'private';
filter?: { [K in keyof Schema.Schema.Type<S>]?: Entity.EntityFieldFilter<Schema.Schema.Type<S>[K]> } | undefined;
// TODO: for multi-level nesting it should only allow the allowed properties instead of Record<string, Record<string, never>>
include?: { [K in keyof Schema.Schema.Type<S>]?: Record<string, Record<string, never>> } | undefined;
space?: string | undefined;
first?: number | undefined;
};
// @ts-expect-error TODO: remove this function
const mergeEntities = <S extends Entity.AnyNoContext>(
publicEntities: Entity.Entity<S>[],
localEntities: Entity.Entity<S>[],
localDeletedEntities: Entity.Entity<S>[],
) => {
const mergedData: Entity.Entity<S>[] = [];
for (const entity of publicEntities) {
const deletedEntity = localDeletedEntities.find((e) => e.id === entity.id);
if (deletedEntity) {
continue;
}
const localEntity = localEntities.find((e) => e.id === entity.id);
if (localEntity) {
const mergedEntity = { ...entity };
for (const key in entity) {
mergedEntity[key] = localEntity[key];
}
mergedData.push(mergedEntity);
} else {
mergedData.push(entity);
}
}
// find all local entities that are not in the public result
const localEntitiesNotInPublic = localEntities.filter((e) => !publicEntities.some((p) => p.id === e.id));
mergedData.push(...localEntitiesNotInPublic);
return mergedData;
};
// @ts-expect-error TODO: remove this function
const getDiff = <S extends Entity.AnyNoContext>(
type: S,
publicEntities: Entity.Entity<S>[],
localEntities: Entity.Entity<S>[],
localDeletedEntities: Entity.Entity<S>[],
) => {
const deletedEntities: Entity.Entity<S>[] = [];
const updatedEntities: { id: string; current: Entity.Entity<S>; new: Entity.Entity<S>; diff: DiffEntry }[] = [];
for (const entity of publicEntities) {
const deletedEntity = localDeletedEntities.find((e) => e.id === entity.id);
if (deletedEntity) {
deletedEntities.push(deletedEntity);
continue;
}
const localEntity = localEntities.find((e) => e.id === entity.id);
if (localEntity) {
const diff: DiffEntry = {};
for (const [key, field] of Object.entries(type.fields)) {
if (key === '__version' || key === '__deleted') {
continue;
}
if (Utils.isRelationField(field)) {
const relationIds: string[] = entity[key].map((e: Entity.Entity<S>) => e.id);
const localRelationIds: string[] = localEntity[key].map((e: Entity.Entity<S>) => e.id);
if (
relationIds.length !== localRelationIds.length ||
relationIds.some((id) => !localRelationIds.includes(id))
) {
const removedIds = relationIds.filter((id) => !localRelationIds.includes(id));
const addedIds = localRelationIds.filter((id) => !relationIds.includes(id));
// get a list of the ids that didn't get added or removed
const unchangedIds = localRelationIds.filter((id) => !addedIds.includes(id) && !removedIds.includes(id));
diff[key] = {
type: 'relation',
current: entity[key],
new: localEntity[key],
addedIds,
removedIds,
unchangedIds,
};
}
} else {
if (field === Type.Date) {
if (entity[key].getTime() !== localEntity[key].getTime()) {
diff[key] = {
type: 'property',
current: entity[key],
new: localEntity[key],
};
}
} else if (field === Type.Url) {
if (entity[key].toString() !== localEntity[key].toString()) {
diff[key] = {
type: 'property',
current: entity[key],
new: localEntity[key],
};
}
} else if (field === Type.Point) {
if (entity[key].join(',') !== localEntity[key].join(',')) {
diff[key] = {
type: 'property',
current: entity[key],
new: localEntity[key],
};
}
} else if (entity[key] !== localEntity[key]) {
diff[key] = {
type: 'property',
current: entity[key],
new: localEntity[key],
};
}
}
}
if (Object.keys(diff).length > 0) {
updatedEntities.push({ id: entity.id, current: entity, new: localEntity, diff });
}
} else {
// TODO update the local entity in this place?
}
}
const newEntities = localEntities.filter((e) => !publicEntities.some((p) => p.id === e.id));
return {
newEntities,
deletedEntities,
updatedEntities,
};
};
const preparePublishDummy = () => undefined;
export function useQuery<const S extends Entity.AnyNoContext>(type: S, params: QueryParams<S>) {
const { mode, filter, include, space, first } = params;
const publicResult = useQueryPublic(type, { enabled: mode === 'public', include, first, space });
const localResult = useQueryLocal(type, { enabled: mode === 'private', filter, include, space });
// const mapping = useSelector(store, (state) => state.context.mapping);
// const generateUpdateOps = useGenerateUpdateOps(type, mode === 'merged');
// const mergedData = useMemo(() => {
// if (mode !== 'merged' || publicResult.isLoading) {
// return localResult.entities;
// }
// return mergeEntities(publicResult.data, localResult.entities, localResult.deletedEntities);
// }, [mode, publicResult.isLoading, publicResult.data, localResult.entities, localResult.deletedEntities]);
if (mode === 'public') {
return {
...publicResult,
deleted: [],
preparePublish: preparePublishDummy,
};
}
return {
...publicResult,
data: localResult.entities,
deleted: localResult.deletedEntities,
preparePublish: preparePublishDummy,
};
// const preparePublish = async (): Promise<PublishDiffInfo> => {
// // @ts-expect-error TODO should use the actual type instead of the name in the mapping
// const typeName = type.name;
// const mappingEntry = mapping?.[typeName];
// if (!mappingEntry) {
// throw new Error(`Mapping entry for ${typeName} not found`);
// }
// const result = await publicResult.refetch();
// if (!result.data) {
// throw new Error('No data found');
// }
// const diff = getDiff(
// type,
// parseResult(result.data, type, mappingEntry, mapping).data,
// localResult.entities,
// localResult.deletedEntities,
// );
// const newEntities = diff.newEntities.map((entity) => {
// const { ops: createOps } = generateCreateOps(entity);
// return { id: entity.id, entity, ops: createOps };
// });
// const updatedEntities = diff.updatedEntities.map((updatedEntityInfo) => {
// const { ops: updateOps } = generateUpdateOps({ id: updatedEntityInfo.id, diff: updatedEntityInfo.diff });
// return { ...updatedEntityInfo, ops: updateOps };
// });
// const deletedEntities = await Promise.all(
// diff.deletedEntities.map(async (entity) => {
// const deleteOps = await generateDeleteOps(entity);
// return { id: entity.id, entity, ops: deleteOps };
// }),
// );
// return { newEntities, updatedEntities, deletedEntities };
// };
// return {
// ...publicResult,
// data: mergedData,
// deleted: localResult.deletedEntities,
// preparePublish: !publicResult.isLoading ? preparePublish : preparePublishDummy,
// };
}