|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Entity, type Id, store } from '@graphprotocol/hypergraph'; |
| 4 | +import { useSelector } from '@xstate/store/react'; |
| 5 | +import * as Schema from 'effect/Schema'; |
| 6 | +import { useEffect, useRef, useSyncExternalStore } from 'react'; |
| 7 | +import { useHypergraphApp } from '../HypergraphAppContext.js'; |
| 8 | +import { useHypergraphSpaceInternal } from '../HypergraphSpaceContext.js'; |
| 9 | + |
| 10 | +const subscribeToSpaceCache = new Map<string, boolean>(); |
| 11 | + |
| 12 | +function useSubscribeToSpaceAndGetHandle({ spaceId, enabled }: { spaceId: string; enabled: boolean }) { |
| 13 | + const handle = useSelector(store, (state) => { |
| 14 | + const space = state.context.spaces.find((space) => space.id === spaceId); |
| 15 | + if (!space) { |
| 16 | + return undefined; |
| 17 | + } |
| 18 | + return space.automergeDocHandle; |
| 19 | + }); |
| 20 | + |
| 21 | + const { subscribeToSpace, isConnecting } = useHypergraphApp(); |
| 22 | + useEffect(() => { |
| 23 | + if (!isConnecting && enabled) { |
| 24 | + if (subscribeToSpaceCache.has(spaceId)) { |
| 25 | + return; |
| 26 | + } |
| 27 | + subscribeToSpaceCache.set(spaceId, true); |
| 28 | + subscribeToSpace({ spaceId }); |
| 29 | + } |
| 30 | + return () => { |
| 31 | + // TODO: unsubscribe from space in case the space ID changes |
| 32 | + subscribeToSpaceCache.delete(spaceId); |
| 33 | + }; |
| 34 | + }, [isConnecting, subscribeToSpace, spaceId, enabled]); |
| 35 | + |
| 36 | + return handle; |
| 37 | +} |
| 38 | + |
| 39 | +export function useEntityPrivate<const S extends Entity.AnyNoContext>( |
| 40 | + type: S, |
| 41 | + params: { |
| 42 | + id: string | Id; |
| 43 | + enabled?: boolean; |
| 44 | + space?: string; |
| 45 | + include?: { [K in keyof Schema.Schema.Type<S>]?: Record<string, Record<string, never>> } | undefined; |
| 46 | + }, |
| 47 | +) { |
| 48 | + const { space: spaceFromContext } = useHypergraphSpaceInternal(); |
| 49 | + const { space: spaceFromParams, include, id, enabled = true } = params; |
| 50 | + const handle = useSubscribeToSpaceAndGetHandle({ spaceId: spaceFromParams ?? spaceFromContext, enabled }); |
| 51 | + const prevEntityRef = useRef<{ |
| 52 | + data: Entity.Entity<S> | undefined; |
| 53 | + invalidEntity: Record<string, string | boolean | number | Date> | undefined; |
| 54 | + isPending: boolean; |
| 55 | + isError: boolean; |
| 56 | + }>({ data: undefined, invalidEntity: undefined, isPending: false, isError: false }); |
| 57 | + const equals = Schema.equivalence(type); |
| 58 | + |
| 59 | + const subscribe = (callback: () => void) => { |
| 60 | + if (!handle || !enabled) { |
| 61 | + return () => {}; |
| 62 | + } |
| 63 | + const handleChange = () => { |
| 64 | + callback(); |
| 65 | + }; |
| 66 | + |
| 67 | + const handleDelete = () => { |
| 68 | + callback(); |
| 69 | + }; |
| 70 | + |
| 71 | + handle.on('change', handleChange); |
| 72 | + handle.on('delete', handleDelete); |
| 73 | + |
| 74 | + return () => { |
| 75 | + handle.off('change', handleChange); |
| 76 | + handle.off('delete', handleDelete); |
| 77 | + }; |
| 78 | + }; |
| 79 | + |
| 80 | + return useSyncExternalStore(subscribe, () => { |
| 81 | + if (!handle || !enabled) { |
| 82 | + return prevEntityRef.current; |
| 83 | + } |
| 84 | + const doc = handle.doc(); |
| 85 | + if (doc === undefined) { |
| 86 | + return prevEntityRef.current; |
| 87 | + } |
| 88 | + |
| 89 | + const found = Entity.findOne(handle, type, include)(id); |
| 90 | + if (found === undefined && prevEntityRef.current.data !== undefined) { |
| 91 | + // entity was maybe deleted, delete from the ref |
| 92 | + prevEntityRef.current = { data: undefined, invalidEntity: undefined, isPending: false, isError: false }; |
| 93 | + } else if (found !== undefined && prevEntityRef.current.data === undefined) { |
| 94 | + prevEntityRef.current = { data: found, invalidEntity: undefined, isPending: false, isError: false }; |
| 95 | + } else if ( |
| 96 | + found !== undefined && |
| 97 | + prevEntityRef.current.data !== undefined && |
| 98 | + !equals(found, prevEntityRef.current.data) |
| 99 | + ) { |
| 100 | + // found and ref have a value, compare for equality, if they are not equal, update the ref and return |
| 101 | + prevEntityRef.current = { data: found, invalidEntity: undefined, isPending: false, isError: false }; |
| 102 | + } |
| 103 | + |
| 104 | + return prevEntityRef.current; |
| 105 | + }); |
| 106 | +} |
0 commit comments