From 8d1a1853bcfbb39262556d9fdc1ed7402cd7f6fc Mon Sep 17 00:00:00 2001 From: Marc-Antoine Parent Date: Mon, 8 Dec 2025 15:41:02 -0500 Subject: [PATCH 1/4] 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 007ab6e279a12ea767655f610c9ccb609a55742a Mon Sep 17 00:00:00 2001 From: Marc-Antoine Parent Date: Tue, 9 Dec 2025 09:19:16 -0500 Subject: [PATCH 2/4] 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", From 22e40d8fa5245edbd564ea5bf289fb99986769cc Mon Sep 17 00:00:00 2001 From: Marc-Antoine Parent Date: Mon, 22 Dec 2025 11:04:57 -0500 Subject: [PATCH 3/4] use the new user setting constant --- apps/roam/src/components/SuggestionsBody.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/roam/src/components/SuggestionsBody.tsx b/apps/roam/src/components/SuggestionsBody.tsx index fb3f04acb..49d7f5610 100644 --- a/apps/roam/src/components/SuggestionsBody.tsx +++ b/apps/roam/src/components/SuggestionsBody.tsx @@ -29,6 +29,7 @@ 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 { USE_REIFIED_RELATIONS } from "~/data/userSettings"; import { createReifiedRelation } from "~/utils/createReifiedBlock"; export type DiscourseData = { @@ -308,7 +309,7 @@ const SuggestionsBody = ({ }; const handleCreateBlock = async (node: SuggestedNode) => { - if (getSetting("use-reified-relations")) { + if (getSetting(USE_REIFIED_RELATIONS, false)) { if (discourseNode === false) { renderToast({ id: "suggestions-create-block-error", From 1a4bd977ea207007e5af98deca28d8b18a0c8cee Mon Sep 17 00:00:00 2001 From: Marc-Antoine Parent Date: Mon, 22 Dec 2025 11:35:03 -0500 Subject: [PATCH 4/4] node.type is already given --- apps/roam/src/components/SuggestionsBody.tsx | 24 ++++---------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/apps/roam/src/components/SuggestionsBody.tsx b/apps/roam/src/components/SuggestionsBody.tsx index 49d7f5610..3ba716471 100644 --- a/apps/roam/src/components/SuggestionsBody.tsx +++ b/apps/roam/src/components/SuggestionsBody.tsx @@ -19,9 +19,7 @@ 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, { - findDiscourseNodeByTitleAndUid, -} from "~/utils/findDiscourseNode"; +import findDiscourseNode from "~/utils/findDiscourseNode"; import getDiscourseRelations from "~/utils/getDiscourseRelations"; import getDiscourseNodes from "~/utils/getDiscourseNodes"; import normalizePageTitle from "roamjs-components/queries/normalizePageTitle"; @@ -319,25 +317,11 @@ const SuggestionsBody = ({ }); return; } - const selectedNodeType = findDiscourseNodeByTitleAndUid({ - uid: node.uid, - title: node.text, - }); - 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.source === node.type && rel.destination === discourseNode.type) || - (rel.destination === selectedNodeType.type && - rel.source === discourseNode.type), + (rel.destination === node.type && rel.source === discourseNode.type), ); if (relevantRelns.length) { if (relevantRelns.length > 1) { @@ -347,7 +331,7 @@ const SuggestionsBody = ({ } const rel = relevantRelns[0]; try { - if (rel.destination === selectedNodeType.type) + if (rel.destination === node.type) await createReifiedRelation({ sourceUid: tagUid, destinationUid: node.uid,