@@ -25,6 +25,10 @@ import getDiscourseNodes from "~/utils/getDiscourseNodes";
2525import normalizePageTitle from "roamjs-components/queries/normalizePageTitle" ;
2626import { type RelationDetails } from "~/utils/hyde" ;
2727import { getFormattedConfigTree } from "~/utils/discourseConfigRef" ;
28+ import { render as renderToast } from "roamjs-components/components/Toast" ;
29+ import { getSetting } from "~/utils/extensionSettings" ;
30+ import { USE_REIFIED_RELATIONS } from "~/data/userSettings" ;
31+ import { createReifiedRelation } from "~/utils/createReifiedBlock" ;
2832
2933export type DiscourseData = {
3034 results : Awaited < ReturnType < typeof getDiscourseContextResults > > ;
@@ -303,10 +307,66 @@ const SuggestionsBody = ({
303307 } ;
304308
305309 const handleCreateBlock = async ( node : SuggestedNode ) => {
306- await createBlock ( {
307- parentUid : blockUid ,
308- node : { text : `[[${ node . text } ]]` } ,
309- } ) ;
310+ if ( getSetting < boolean > ( USE_REIFIED_RELATIONS , false ) ) {
311+ if ( discourseNode === false ) {
312+ renderToast ( {
313+ id : "suggestions-create-block-error" ,
314+ content : "Could not identify type of source" ,
315+ intent : "danger" ,
316+ timeout : 5000 ,
317+ } ) ;
318+ return ;
319+ }
320+ const relevantRelns = validRelations . filter (
321+ ( rel ) =>
322+ ( rel . source === node . type &&
323+ rel . destination === discourseNode . type ) ||
324+ ( rel . destination === node . type && rel . source === discourseNode . type ) ,
325+ ) ;
326+ if ( relevantRelns . length ) {
327+ if ( relevantRelns . length > 1 ) {
328+ // I don't want to panick the user with this.
329+ // TODO: Maybe think of adding a relation type picker?
330+ console . warn ( "Picking an arbitrary relation" ) ;
331+ }
332+ const rel = relevantRelns [ 0 ] ;
333+ try {
334+ if ( rel . destination === node . type )
335+ await createReifiedRelation ( {
336+ sourceUid : tagUid ,
337+ destinationUid : node . uid ,
338+ relationBlockUid : rel . id ,
339+ } ) ;
340+ else
341+ await createReifiedRelation ( {
342+ sourceUid : node . uid ,
343+ destinationUid : tagUid ,
344+ relationBlockUid : rel . id ,
345+ } ) ;
346+ } catch ( error ) {
347+ console . error ( "Failed to create reified relation:" , error ) ;
348+ renderToast ( {
349+ id : "suggestions-create-block-error" ,
350+ content : "Failed to create relation" ,
351+ intent : "danger" ,
352+ timeout : 5000 ,
353+ } ) ;
354+ return ;
355+ }
356+ } else {
357+ renderToast ( {
358+ id : "suggestions-create-block-error" ,
359+ content : "Could not identify a relevant relation" ,
360+ intent : "danger" ,
361+ timeout : 5000 ,
362+ } ) ;
363+ }
364+ } else {
365+ await createBlock ( {
366+ parentUid : blockUid ,
367+ node : { text : `[[${ node . text } ]]` } ,
368+ } ) ;
369+ }
310370 setHydeFilteredNodes ( ( prev ) => prev . filter ( ( n ) => n . uid !== node . uid ) ) ;
311371 } ;
312372
0 commit comments