1- import { type Entity , Type , Utils } from '@graphprotocol/hypergraph' ;
1+ import type { Entity } from '@graphprotocol/hypergraph' ;
22import type * as Schema from 'effect/Schema' ;
33import { useQueryLocal } from './HypergraphSpaceContext.js' ;
44import { useQueryPublic } from './internal/use-query-public.js' ;
5- import type { DiffEntry } from './types.js' ;
65
76type QueryParams < S extends Entity . AnyNoContext > = {
87 mode : 'public' | 'private' ;
@@ -13,142 +12,12 @@ type QueryParams<S extends Entity.AnyNoContext> = {
1312 first ?: number | undefined ;
1413} ;
1514
16- // @ts -expect-error TODO: remove this function
17- const _mergeEntities = < S extends Entity . AnyNoContext > (
18- publicEntities : Entity . Entity < S > [ ] ,
19- localEntities : Entity . Entity < S > [ ] ,
20- localDeletedEntities : Entity . Entity < S > [ ] ,
21- ) => {
22- const mergedData : Entity . Entity < S > [ ] = [ ] ;
23-
24- for ( const entity of publicEntities ) {
25- const deletedEntity = localDeletedEntities . find ( ( e ) => e . id === entity . id ) ;
26- if ( deletedEntity ) {
27- continue ;
28- }
29- const localEntity = localEntities . find ( ( e ) => e . id === entity . id ) ;
30- if ( localEntity ) {
31- const mergedEntity = { ...entity } ;
32- for ( const key in entity ) {
33- mergedEntity [ key ] = localEntity [ key ] ;
34- }
35- mergedData . push ( mergedEntity ) ;
36- } else {
37- mergedData . push ( entity ) ;
38- }
39- }
40-
41- // find all local entities that are not in the public result
42- const localEntitiesNotInPublic = localEntities . filter ( ( e ) => ! publicEntities . some ( ( p ) => p . id === e . id ) ) ;
43-
44- mergedData . push ( ...localEntitiesNotInPublic ) ;
45- return mergedData ;
46- } ;
47-
48- // @ts -expect-error TODO: remove this function
49- const _getDiff = < S extends Entity . AnyNoContext > (
50- type : S ,
51- publicEntities : Entity . Entity < S > [ ] ,
52- localEntities : Entity . Entity < S > [ ] ,
53- localDeletedEntities : Entity . Entity < S > [ ] ,
54- ) => {
55- const deletedEntities : Entity . Entity < S > [ ] = [ ] ;
56- const updatedEntities : { id : string ; current : Entity . Entity < S > ; new : Entity . Entity < S > ; diff : DiffEntry } [ ] = [ ] ;
57-
58- for ( const entity of publicEntities ) {
59- const deletedEntity = localDeletedEntities . find ( ( e ) => e . id === entity . id ) ;
60- if ( deletedEntity ) {
61- deletedEntities . push ( deletedEntity ) ;
62- continue ;
63- }
64- const localEntity = localEntities . find ( ( e ) => e . id === entity . id ) ;
65- if ( localEntity ) {
66- const diff : DiffEntry = { } ;
67-
68- for ( const [ key , field ] of Object . entries ( type . fields ) ) {
69- if ( key === '__version' || key === '__deleted' ) {
70- continue ;
71- }
72-
73- if ( Utils . isRelationField ( field ) ) {
74- const relationIds : string [ ] = entity [ key ] . map ( ( e : Entity . Entity < S > ) => e . id ) ;
75- const localRelationIds : string [ ] = localEntity [ key ] . map ( ( e : Entity . Entity < S > ) => e . id ) ;
76- if (
77- relationIds . length !== localRelationIds . length ||
78- relationIds . some ( ( id ) => ! localRelationIds . includes ( id ) )
79- ) {
80- const removedIds = relationIds . filter ( ( id ) => ! localRelationIds . includes ( id ) ) ;
81- const addedIds = localRelationIds . filter ( ( id ) => ! relationIds . includes ( id ) ) ;
82- // get a list of the ids that didn't get added or removed
83- const unchangedIds = localRelationIds . filter ( ( id ) => ! addedIds . includes ( id ) && ! removedIds . includes ( id ) ) ;
84- diff [ key ] = {
85- type : 'relation' ,
86- current : entity [ key ] ,
87- new : localEntity [ key ] ,
88- addedIds,
89- removedIds,
90- unchangedIds,
91- } ;
92- }
93- } else {
94- if ( field === Type . Date ) {
95- if ( entity [ key ] . getTime ( ) !== localEntity [ key ] . getTime ( ) ) {
96- diff [ key ] = {
97- type : 'property' ,
98- current : entity [ key ] ,
99- new : localEntity [ key ] ,
100- } ;
101- }
102- } else if ( field === Type . Point ) {
103- if ( entity [ key ] . join ( ',' ) !== localEntity [ key ] . join ( ',' ) ) {
104- diff [ key ] = {
105- type : 'property' ,
106- current : entity [ key ] ,
107- new : localEntity [ key ] ,
108- } ;
109- }
110- } else if ( entity [ key ] !== localEntity [ key ] ) {
111- diff [ key ] = {
112- type : 'property' ,
113- current : entity [ key ] ,
114- new : localEntity [ key ] ,
115- } ;
116- }
117- }
118- }
119-
120- if ( Object . keys ( diff ) . length > 0 ) {
121- updatedEntities . push ( { id : entity . id , current : entity , new : localEntity , diff } ) ;
122- }
123- } else {
124- // TODO update the local entity in this place?
125- }
126- }
127-
128- const newEntities = localEntities . filter ( ( e ) => ! publicEntities . some ( ( p ) => p . id === e . id ) ) ;
129-
130- return {
131- newEntities,
132- deletedEntities,
133- updatedEntities,
134- } ;
135- } ;
136-
13715const preparePublishDummy = ( ) => undefined ;
13816
13917export function useQuery < const S extends Entity . AnyNoContext > ( type : S , params : QueryParams < S > ) {
14018 const { mode, filter, include, space, first } = params ;
14119 const publicResult = useQueryPublic ( type , { enabled : mode === 'public' , include, first, space } ) ;
14220 const localResult = useQueryLocal ( type , { enabled : mode === 'private' , filter, include, space } ) ;
143- // const mapping = useSelector(store, (state) => state.context.mapping);
144- // const generateUpdateOps = useGenerateUpdateOps(type, mode === 'merged');
145-
146- // const mergedData = useMemo(() => {
147- // if (mode !== 'merged' || publicResult.isLoading) {
148- // return localResult.entities;
149- // }
150- // return mergeEntities(publicResult.data, localResult.entities, localResult.deletedEntities);
151- // }, [mode, publicResult.isLoading, publicResult.data, localResult.entities, localResult.deletedEntities]);
15221
15322 if ( mode === 'public' ) {
15423 return {
@@ -164,50 +33,4 @@ export function useQuery<const S extends Entity.AnyNoContext>(type: S, params: Q
16433 deleted : localResult . deletedEntities ,
16534 preparePublish : preparePublishDummy ,
16635 } ;
167-
168- // const preparePublish = async (): Promise<PublishDiffInfo> => {
169- // // @ts -expect-error TODO should use the actual type instead of the name in the mapping
170- // const typeName = type.name;
171- // const mappingEntry = mapping?.[typeName];
172- // if (!mappingEntry) {
173- // throw new Error(`Mapping entry for ${typeName} not found`);
174- // }
175-
176- // const result = await publicResult.refetch();
177- // if (!result.data) {
178- // throw new Error('No data found');
179- // }
180- // const diff = getDiff(
181- // type,
182- // parseResult(result.data, type, mappingEntry, mapping).data,
183- // localResult.entities,
184- // localResult.deletedEntities,
185- // );
186-
187- // const newEntities = diff.newEntities.map((entity) => {
188- // const { ops: createOps } = generateCreateOps(entity);
189- // return { id: entity.id, entity, ops: createOps };
190- // });
191-
192- // const updatedEntities = diff.updatedEntities.map((updatedEntityInfo) => {
193- // const { ops: updateOps } = generateUpdateOps({ id: updatedEntityInfo.id, diff: updatedEntityInfo.diff });
194- // return { ...updatedEntityInfo, ops: updateOps };
195- // });
196-
197- // const deletedEntities = await Promise.all(
198- // diff.deletedEntities.map(async (entity) => {
199- // const deleteOps = await generateDeleteOps(entity);
200- // return { id: entity.id, entity, ops: deleteOps };
201- // }),
202- // );
203-
204- // return { newEntities, updatedEntities, deletedEntities };
205- // };
206-
207- // return {
208- // ...publicResult,
209- // data: mergedData,
210- // deleted: localResult.deletedEntities,
211- // preparePublish: !publicResult.isLoading ? preparePublish : preparePublishDummy,
212- // };
21336}
0 commit comments