@@ -19,7 +19,7 @@ import nanoid from "nanoid";
1919import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle" ;
2020import getDiscourseContextResults from "~/utils/getDiscourseContextResults" ;
2121import findDiscourseNode from "~/utils/findDiscourseNode" ;
22- import getDiscourseNodes from "~/utils/getDiscourseNodes" ;
22+ import getDiscourseNodes , { DiscourseNode } from "~/utils/getDiscourseNodes" ;
2323import getDiscourseRelations , {
2424 DiscourseRelation ,
2525} from "~/utils/getDiscourseRelations" ;
@@ -96,67 +96,6 @@ const getAllReferencesOnPage = (pageTitle: string) => {
9696 } ) ) as Result [ ] ;
9797} ;
9898
99- type SelfNodeType = {
100- type : string ;
101- text : string ;
102- format : string ;
103- } ;
104-
105- type AllNodesType = ReadonlyArray < SelfNodeType > ;
106-
107- const getUniqueLabelTypeTriplets = (
108- connectingRelations : DiscourseRelation [ ] ,
109- selfNode : SelfNodeType ,
110- allNodes : AllNodesType ,
111- ) : RelationDetails [ ] => {
112- const relatedNodeType = selfNode . type ;
113-
114- const mappedItems = connectingRelations . flatMap ( ( relation ) => {
115- let targetNodeIdentifier : string ;
116- let targetNodeDetails : { text : string ; format : string } | undefined ;
117- let finalRelationLabel : string ;
118-
119- if ( relation . source === relatedNodeType ) {
120- targetNodeIdentifier = relation . destination ;
121- finalRelationLabel = relation . label ;
122- const destinationNode = allNodes . find (
123- ( n ) => n . type === targetNodeIdentifier ,
124- ) ;
125- if ( destinationNode && destinationNode . text && destinationNode . format ) {
126- targetNodeDetails = {
127- text : destinationNode . text ,
128- format : destinationNode . format ,
129- } ;
130- }
131- } else if ( relation . destination === relatedNodeType ) {
132- targetNodeIdentifier = relation . source ;
133- finalRelationLabel = relation . complement ;
134- const sourceNode = allNodes . find ( ( n ) => n . type === targetNodeIdentifier ) ;
135- if ( sourceNode && sourceNode . text && sourceNode . format ) {
136- targetNodeDetails = {
137- text : sourceNode . text ,
138- format : sourceNode . format ,
139- } ;
140- }
141- } else {
142- return [ ] ;
143- }
144-
145- if ( ! targetNodeDetails ) {
146- return [ ] ;
147- }
148-
149- const mappedItem : RelationDetails = {
150- relationLabel : finalRelationLabel ,
151- relatedNodeText : targetNodeDetails . text ,
152- relatedNodeFormat : targetNodeDetails . format ,
153- } ;
154- return [ mappedItem ] ;
155- } ) ;
156-
157- return mappedItems ;
158- } ;
159-
16099const DiscourseContextOverlay = ( {
161100 tag,
162101 id,
@@ -218,8 +157,8 @@ const DiscourseContextOverlay = ({
218157
219158 if ( ! selfNode || ! selfNode . text || ! selfNode . format ) {
220159 return {
221- validTypes : [ ] as string [ ] ,
222- uniqueRelationTypeTriplets : [ ] as RelationDetails [ ] ,
160+ validTypes : [ ] ,
161+ uniqueRelationTypeTriplets : [ ] ,
223162 } ;
224163 }
225164 const selfType = selfNode . type ;
@@ -228,11 +167,43 @@ const DiscourseContextOverlay = ({
228167 ( relation ) =>
229168 relation . source === selfType || relation . destination === selfType ,
230169 ) ;
231- const uniqueTriplets = getUniqueLabelTypeTriplets (
232- relationsConnectingToSelf ,
233- selfNode ,
234- allNodes ,
235- ) ;
170+
171+ const uniqueTriplets = useMemo ( ( ) => {
172+ const relatedNodeType = selfNode . type ;
173+
174+ return relationsConnectingToSelf . flatMap ( ( relation ) => {
175+ const isSelfSource = relation . source === relatedNodeType ;
176+ const isSelfDestination = relation . destination === relatedNodeType ;
177+
178+ let targetNodeType : string ;
179+ let currentRelationLabel : string ;
180+
181+ if ( isSelfSource ) {
182+ targetNodeType = relation . destination ;
183+ currentRelationLabel = relation . label ;
184+ } else if ( isSelfDestination ) {
185+ targetNodeType = relation . source ;
186+ currentRelationLabel = relation . complement ;
187+ } else {
188+ return [ ] ;
189+ }
190+
191+ const identifiedTargetNode = allNodes . find (
192+ ( node ) => node . type === targetNodeType ,
193+ ) ;
194+
195+ if ( ! identifiedTargetNode ) {
196+ return [ ] ;
197+ }
198+
199+ const mappedItem : RelationDetails = {
200+ relationLabel : currentRelationLabel ,
201+ relatedNodeText : identifiedTargetNode . text ,
202+ relatedNodeFormat : identifiedTargetNode . format ,
203+ } ;
204+ return [ mappedItem ] ;
205+ } ) ;
206+ } , [ relationsConnectingToSelf , selfNode . type , allNodes ] ) ;
236207
237208 const relationsInvolvingSelfBroadly = relations . filter ( ( relation ) =>
238209 [ relation . source , relation . destination , relation . label ] . includes (
@@ -348,15 +319,11 @@ const DiscourseContextOverlay = ({
348319 }
349320
350321 try {
351- const candidateNodesForHyde = currentSuggestions . map ( ( node ) => {
352- const nodeWithEmbedding = {
353- uid : node . uid ,
354- text : node . text ,
355- type : node . type ,
356- embedding : [ ] as number [ ] ,
357- } ;
358- return nodeWithEmbedding as CandidateNodeWithEmbedding ;
359- } ) ;
322+ const candidateNodesForHyde = currentSuggestions . map ( ( node ) => ( {
323+ uid : node . uid ,
324+ text : node . text ,
325+ type : node . type ,
326+ } ) ) ;
360327
361328 const foundNodes : SuggestedNode [ ] = await findSimilarNodesUsingHyde ( {
362329 candidateNodes : candidateNodesForHyde ,
0 commit comments