1- import { type Entity , Type , Utils , store } from '@graphprotocol/hypergraph' ;
2- import { useSelector } from '@xstate/store/react' ;
1+ import { type Entity , Type , Utils } from '@graphprotocol/hypergraph' ;
32import type * as Schema from 'effect/Schema' ;
4- import { useMemo } from 'react' ;
53import { useQueryLocal } from './HypergraphSpaceContext.js' ;
6- import { generateDeleteOps } from './internal/generate-delete-ops.js' ;
7- import { useGenerateCreateOps } from './internal/use-generate-create-ops.js' ;
8- import { useGenerateUpdateOps } from './internal/use-generate-update-ops.js' ;
9- import { parseResult , useQueryPublic } from './internal/use-query-public.js' ;
10- import type { DiffEntry , PublishDiffInfo } from './types.js' ;
4+ import { useQueryPublic } from './internal/use-query-public.js' ;
5+ import type { DiffEntry } from './types.js' ;
116
127type QueryParams < S extends Entity . AnyNoContext > = {
13- mode ?: 'merged' | 'public' | 'private' ;
8+ mode : 'public' | 'private' ;
149 filter ?: { [ K in keyof Schema . Schema . Type < S > ] ?: Entity . EntityFieldFilter < Schema . Schema . Type < S > [ K ] > } | undefined ;
1510 // TODO: for multi-level nesting it should only allow the allowed properties instead of Record<string, Record<string, never>>
1611 include ?: { [ K in keyof Schema . Schema . Type < S > ] ?: Record < string , Record < string , never > > } | undefined ;
1712} ;
1813
14+ // @ts -expect-error TODO: remove this function
1915const mergeEntities = < S extends Entity . AnyNoContext > (
2016 publicEntities : Entity . Entity < S > [ ] ,
2117 localEntities : Entity . Entity < S > [ ] ,
@@ -47,6 +43,7 @@ const mergeEntities = <S extends Entity.AnyNoContext>(
4743 return mergedData ;
4844} ;
4945
46+ // @ts -expect-error TODO: remove this function
5047const getDiff = < S extends Entity . AnyNoContext > (
5148 type : S ,
5249 publicEntities : Entity . Entity < S > [ ] ,
@@ -145,20 +142,20 @@ const getDiff = <S extends Entity.AnyNoContext>(
145142
146143const preparePublishDummy = ( ) => undefined ;
147144
148- export function useQuery < const S extends Entity . AnyNoContext > ( type : S , params ? : QueryParams < S > ) {
149- const { mode = 'merged' , filter, include } = params ?? { } ;
150- const publicResult = useQueryPublic ( type , { enabled : mode === 'public' || mode === 'merged' , include } ) ;
151- const localResult = useQueryLocal ( type , { enabled : mode === 'private' || mode === 'merged' , filter, include } ) ;
152- const mapping = useSelector ( store , ( state ) => state . context . mapping ) ;
153- const generateCreateOps = useGenerateCreateOps ( type , mode === 'merged' ) ;
154- const generateUpdateOps = useGenerateUpdateOps ( type , mode === 'merged' ) ;
155-
156- const mergedData = useMemo ( ( ) => {
157- if ( mode !== 'merged' || publicResult . isLoading ) {
158- return localResult . entities ;
159- }
160- return mergeEntities ( publicResult . data , localResult . entities , localResult . deletedEntities ) ;
161- } , [ mode , publicResult . isLoading , publicResult . data , localResult . entities , localResult . deletedEntities ] ) ;
145+ export function useQuery < const S extends Entity . AnyNoContext > ( type : S , params : QueryParams < S > ) {
146+ const { mode, filter, include } = params ;
147+ const publicResult = useQueryPublic ( type , { enabled : mode === 'public' , include } ) ;
148+ const localResult = useQueryLocal ( type , { enabled : mode === 'private' , filter, include } ) ;
149+ // const mapping = useSelector(store, (state) => state.context.mapping);
150+ // const generateCreateOps = useGenerateCreateOps(type, mode === 'merged');
151+ // const generateUpdateOps = useGenerateUpdateOps(type, mode === 'merged');
152+
153+ // const mergedData = useMemo(() => {
154+ // if (mode !== 'merged' || publicResult.isLoading) {
155+ // return localResult.entities;
156+ // }
157+ // return mergeEntities(publicResult.data, localResult.entities, localResult.deletedEntities);
158+ // }, [mode, publicResult.isLoading, publicResult.data, localResult.entities, localResult.deletedEntities]);
162159
163160 if ( mode === 'public' ) {
164161 return {
@@ -168,58 +165,56 @@ export function useQuery<const S extends Entity.AnyNoContext>(type: S, params?:
168165 } ;
169166 }
170167
171- if ( mode === 'private' ) {
172- return {
173- ...publicResult ,
174- data : localResult . entities ,
175- deleted : localResult . deletedEntities ,
176- preparePublish : preparePublishDummy ,
177- } ;
178- }
179-
180- const preparePublish = async ( ) : Promise < PublishDiffInfo > => {
181- // @ts -expect-error TODO should use the actual type instead of the name in the mapping
182- const typeName = type . name ;
183- const mappingEntry = mapping ?. [ typeName ] ;
184- if ( ! mappingEntry ) {
185- throw new Error ( `Mapping entry for ${ typeName } not found` ) ;
186- }
187-
188- const result = await publicResult . refetch ( ) ;
189- if ( ! result . data ) {
190- throw new Error ( 'No data found' ) ;
191- }
192- const diff = getDiff (
193- type ,
194- parseResult ( result . data , type , mappingEntry , mapping ) . data ,
195- localResult . entities ,
196- localResult . deletedEntities ,
197- ) ;
198-
199- const newEntities = diff . newEntities . map ( ( entity ) => {
200- const { ops : createOps } = generateCreateOps ( entity ) ;
201- return { id : entity . id , entity, ops : createOps } ;
202- } ) ;
203-
204- const updatedEntities = diff . updatedEntities . map ( ( updatedEntityInfo ) => {
205- const { ops : updateOps } = generateUpdateOps ( { id : updatedEntityInfo . id , diff : updatedEntityInfo . diff } ) ;
206- return { ...updatedEntityInfo , ops : updateOps } ;
207- } ) ;
208-
209- const deletedEntities = await Promise . all (
210- diff . deletedEntities . map ( async ( entity ) => {
211- const deleteOps = await generateDeleteOps ( entity ) ;
212- return { id : entity . id , entity, ops : deleteOps } ;
213- } ) ,
214- ) ;
215-
216- return { newEntities, updatedEntities, deletedEntities } ;
217- } ;
218-
219168 return {
220169 ...publicResult ,
221- data : mergedData ,
170+ data : localResult . entities ,
222171 deleted : localResult . deletedEntities ,
223- preparePublish : ! publicResult . isLoading ? preparePublish : preparePublishDummy ,
172+ preparePublish : preparePublishDummy ,
224173 } ;
174+
175+ // const preparePublish = async (): Promise<PublishDiffInfo> => {
176+ // // @ts -expect-error TODO should use the actual type instead of the name in the mapping
177+ // const typeName = type.name;
178+ // const mappingEntry = mapping?.[typeName];
179+ // if (!mappingEntry) {
180+ // throw new Error(`Mapping entry for ${typeName} not found`);
181+ // }
182+
183+ // const result = await publicResult.refetch();
184+ // if (!result.data) {
185+ // throw new Error('No data found');
186+ // }
187+ // const diff = getDiff(
188+ // type,
189+ // parseResult(result.data, type, mappingEntry, mapping).data,
190+ // localResult.entities,
191+ // localResult.deletedEntities,
192+ // );
193+
194+ // const newEntities = diff.newEntities.map((entity) => {
195+ // const { ops: createOps } = generateCreateOps(entity);
196+ // return { id: entity.id, entity, ops: createOps };
197+ // });
198+
199+ // const updatedEntities = diff.updatedEntities.map((updatedEntityInfo) => {
200+ // const { ops: updateOps } = generateUpdateOps({ id: updatedEntityInfo.id, diff: updatedEntityInfo.diff });
201+ // return { ...updatedEntityInfo, ops: updateOps };
202+ // });
203+
204+ // const deletedEntities = await Promise.all(
205+ // diff.deletedEntities.map(async (entity) => {
206+ // const deleteOps = await generateDeleteOps(entity);
207+ // return { id: entity.id, entity, ops: deleteOps };
208+ // }),
209+ // );
210+
211+ // return { newEntities, updatedEntities, deletedEntities };
212+ // };
213+
214+ // return {
215+ // ...publicResult,
216+ // data: mergedData,
217+ // deleted: localResult.deletedEntities,
218+ // preparePublish: !publicResult.isLoading ? preparePublish : preparePublishDummy,
219+ // };
225220}
0 commit comments