From babcea3e931b0ed4543bbeda3fa4c5b5e421e70c Mon Sep 17 00:00:00 2001 From: Marc-Antoine Parent Date: Mon, 8 Dec 2025 15:41:02 -0500 Subject: [PATCH 1/2] eng-1101 create relation from suggestion mode --- apps/roam/src/components/SuggestionsBody.tsx | 74 ++++++++++++++++++-- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/apps/roam/src/components/SuggestionsBody.tsx b/apps/roam/src/components/SuggestionsBody.tsx index 2c4a542d3..6f8c5a132 100644 --- a/apps/roam/src/components/SuggestionsBody.tsx +++ b/apps/roam/src/components/SuggestionsBody.tsx @@ -19,12 +19,17 @@ import { performHydeSearch } from "../utils/hyde"; import { createBlock } from "roamjs-components/writes"; import getDiscourseContextResults from "~/utils/getDiscourseContextResults"; import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle"; -import findDiscourseNode from "~/utils/findDiscourseNode"; +import findDiscourseNode, { + findDiscourseNodeByTitleAndUid, +} from "~/utils/findDiscourseNode"; import getDiscourseRelations from "~/utils/getDiscourseRelations"; import getDiscourseNodes from "~/utils/getDiscourseNodes"; import normalizePageTitle from "roamjs-components/queries/normalizePageTitle"; import { type RelationDetails } from "~/utils/hyde"; import { getFormattedConfigTree } from "~/utils/discourseConfigRef"; +import { render as renderToast } from "roamjs-components/components/Toast"; +import { getSetting } from "~/utils/extensionSettings"; +import { createReifiedRelation } from "~/utils/createReifiedBlock"; export type DiscourseData = { results: Awaited>; @@ -303,10 +308,69 @@ const SuggestionsBody = ({ }; const handleCreateBlock = async (node: SuggestedNode) => { - await createBlock({ - parentUid: blockUid, - node: { text: `[[${node.text}]]` }, - }); + if (getSetting("use-reified-relations")) { + const selectedNodeType = findDiscourseNodeByTitleAndUid({ + uid: node.uid, + title: node.title as string, + }); + if (discourseNode === false) { + renderToast({ + id: "suggestions-create-block-error", + content: "Could not identify type of source", + intent: "danger", + timeout: 5000, + }); + return; + } + if (selectedNodeType === false) { + renderToast({ + id: "suggestions-create-block-error", + content: "Could not identify type of target", + intent: "danger", + timeout: 5000, + }); + return; + } + const relevantRelns = validRelations.filter( + (rel) => + (rel.source === selectedNodeType.type && + rel.destination === discourseNode.type) || + (rel.destination === selectedNodeType.type && + rel.source === discourseNode.type), + ); + if (relevantRelns.length) { + if (relevantRelns.length > 1) { + // I don't want to panick the user with this. + // TODO: Maybe think of adding a relation type picker? + console.warn("Picking an arbitrary relation"); + } + const rel = relevantRelns[0]; + if (rel.destination === selectedNodeType.type) + await createReifiedRelation({ + sourceUid: tagUid, + destinationUid: node.uid, + relationBlockUid: rel.id, + }); + else + await createReifiedRelation({ + sourceUid: node.uid, + destinationUid: tagUid, + relationBlockUid: rel.id, + }); + } else { + renderToast({ + id: "suggestions-create-block-error", + content: "Could not identify a relevant relation", + intent: "danger", + timeout: 5000, + }); + } + } else { + await createBlock({ + parentUid: blockUid, + node: { text: `[[${node.text}]]` }, + }); + } setHydeFilteredNodes((prev) => prev.filter((n) => n.uid !== node.uid)); }; From d7733c65046066ab4fab30faf35e9b488fe7a1c5 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Parent Date: Tue, 9 Dec 2025 09:19:16 -0500 Subject: [PATCH 2/2] coderabbit suggestions and corrections --- apps/roam/src/components/SuggestionsBody.tsx | 41 +++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/apps/roam/src/components/SuggestionsBody.tsx b/apps/roam/src/components/SuggestionsBody.tsx index 6f8c5a132..fb3f04acb 100644 --- a/apps/roam/src/components/SuggestionsBody.tsx +++ b/apps/roam/src/components/SuggestionsBody.tsx @@ -309,10 +309,6 @@ const SuggestionsBody = ({ const handleCreateBlock = async (node: SuggestedNode) => { if (getSetting("use-reified-relations")) { - const selectedNodeType = findDiscourseNodeByTitleAndUid({ - uid: node.uid, - title: node.title as string, - }); if (discourseNode === false) { renderToast({ id: "suggestions-create-block-error", @@ -322,6 +318,10 @@ const SuggestionsBody = ({ }); return; } + const selectedNodeType = findDiscourseNodeByTitleAndUid({ + uid: node.uid, + title: node.text, + }); if (selectedNodeType === false) { renderToast({ id: "suggestions-create-block-error", @@ -345,18 +345,29 @@ const SuggestionsBody = ({ console.warn("Picking an arbitrary relation"); } const rel = relevantRelns[0]; - if (rel.destination === selectedNodeType.type) - await createReifiedRelation({ - sourceUid: tagUid, - destinationUid: node.uid, - relationBlockUid: rel.id, - }); - else - await createReifiedRelation({ - sourceUid: node.uid, - destinationUid: tagUid, - relationBlockUid: rel.id, + try { + if (rel.destination === selectedNodeType.type) + await createReifiedRelation({ + sourceUid: tagUid, + destinationUid: node.uid, + relationBlockUid: rel.id, + }); + else + await createReifiedRelation({ + sourceUid: node.uid, + destinationUid: tagUid, + relationBlockUid: rel.id, + }); + } catch (error) { + console.error("Failed to create reified relation:", error); + renderToast({ + id: "suggestions-create-block-error", + content: "Failed to create relation", + intent: "danger", + timeout: 5000, }); + return; + } } else { renderToast({ id: "suggestions-create-block-error",