Skip to content

Commit ecdfdca

Browse files
authored
ENG-1182 extend findDiscourseNode to accept the title as an argument (#637)
* eng-1182: Use destructured input for findDiscourseNodes
1 parent cba1118 commit ecdfdca

File tree

12 files changed

+38
-44
lines changed

12 files changed

+38
-44
lines changed

apps/roam/src/components/CreateRelationDialog.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import getDiscourseRelations, {
1717
type DiscourseRelation,
1818
} from "~/utils/getDiscourseRelations";
1919
import { createReifiedRelation } from "~/utils/createReifiedBlock";
20-
import { findDiscourseNodeByTitleAndUid } from "~/utils/findDiscourseNode";
20+
import findDiscourseNode from "~/utils/findDiscourseNode";
2121
import { getDiscourseNodeFormatInnerExpression } from "~/utils/getDiscourseNodeFormatExpression";
2222
import type { DiscourseNode } from "~/utils/getDiscourseNodes";
2323
import type { Result } from "~/utils/types";
@@ -97,7 +97,7 @@ const CreateRelationDialog = ({
9797

9898
const identifyRelationMatch = (target: Result): RelWithDirection | null => {
9999
if (target.text.length === 0) return null;
100-
const selectedTargetType = findDiscourseNodeByTitleAndUid({
100+
const selectedTargetType = findDiscourseNode({
101101
uid: target.uid,
102102
title: target.text,
103103
nodes: discourseNodes,
@@ -280,7 +280,7 @@ const prepareRelData = (
280280
nodeTitle = nodeTitle || getPageTitleByPageUid(targetNodeUid).trim();
281281
const discourseNodeSchemas = getDiscourseNodes();
282282
const relations = getDiscourseRelations();
283-
const nodeSchema = findDiscourseNodeByTitleAndUid({
283+
const nodeSchema = findDiscourseNode({
284284
uid: targetNodeUid,
285285
title: nodeTitle,
286286
nodes: discourseNodeSchemas,
@@ -319,7 +319,7 @@ const extendProps = ({
319319
}: CreateRelationDialogProps): ExtendedCreateRelationDialogProps | null => {
320320
const nodeTitle = getPageTitleByPageUid(sourceNodeUid).trim();
321321
const relData = prepareRelData(sourceNodeUid, nodeTitle);
322-
const selectedSourceType = findDiscourseNodeByTitleAndUid({
322+
const selectedSourceType = findDiscourseNode({
323323
uid: sourceNodeUid,
324324
title: nodeTitle,
325325
});

apps/roam/src/components/DiscourseContextOverlay.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const DiscourseContextOverlay = ({
112112
() =>
113113
getOverlayInfo(tag ?? (uid ? (getPageTitleByPageUid(uid) ?? "") : ""))
114114
.then(({ refs, results }) => {
115-
const discourseNode = findDiscourseNode(tagUid);
115+
const discourseNode = findDiscourseNode({ uid: tagUid });
116116
if (discourseNode) {
117117
const attribute = getSettingValueFromTree({
118118
tree: getBasicTreeByParentUid(discourseNode.type),

apps/roam/src/components/Export.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ const ExportDialog: ExportDialogComponent = ({
328328
let nextShapeX = COMMON_BOUNDS_XOFFSET;
329329
let shapeY = commonBounds.top;
330330
for (const [i, r] of results.entries()) {
331-
const discourseNode = findDiscourseNode(r.uid);
331+
const discourseNode = findDiscourseNode({ uid: r.uid });
332332
const nodeType = discourseNode ? discourseNode.type : "page-node";
333333
const extensionAPI = getExtensionAPI();
334334
const { h, w, imageUrl } = await calcCanvasNodeSizeAndImg({

apps/roam/src/components/SuggestionsBody.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ const SuggestionsBody = ({
218218
>([]);
219219

220220
const tagUid = useMemo(() => getPageUidByPageTitle(tag), [tag]);
221-
const discourseNode = useMemo(() => findDiscourseNode(tagUid), [tagUid]);
221+
const discourseNode = useMemo(
222+
() => findDiscourseNode({ uid: tagUid }),
223+
[tagUid],
224+
);
222225
const allRelations = useMemo(() => getDiscourseRelations(), []);
223226
const allNodes = useMemo(() => getDiscourseNodes(), []);
224227

apps/roam/src/components/canvas/Clipboard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ const ClipboardPageSection = ({
587587
async (node: { uid: string; text: string }, pagePoint: Vec) => {
588588
if (!extensionAPI) return;
589589

590-
const nodeType = findDiscourseNode(node.uid);
590+
const nodeType = findDiscourseNode({ uid: node.uid });
591591
if (!nodeType) {
592592
internalError({
593593
error: new Error("Canvas Clipboard: Node type not found"),
@@ -726,7 +726,7 @@ const ClipboardPageSection = ({
726726
const nodeText = target.dataset.clipboardNodeText;
727727
if (!nodeUid || !nodeText) return;
728728

729-
const nodeType = findDiscourseNode(nodeUid);
729+
const nodeType = findDiscourseNode({ uid: nodeUid });
730730
if (!nodeType) return;
731731
const { backgroundColor, textColor } = getDiscourseNodeColors({
732732
nodeType: nodeType.type,

apps/roam/src/components/canvas/Tldraw.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,10 @@ const TldrawCanvas = ({ title }: { title: string }) => {
478478
const position = lastTime
479479
? { x: lastTime.x + w * 0.025, y: lastTime.y + h * 0.05 }
480480
: { x: x - DEFAULT_WIDTH / 2, y: y - DEFAULT_HEIGHT / 2 };
481-
const nodeType = findDiscourseNode(e.detail.uid, allNodes);
481+
const nodeType = findDiscourseNode({
482+
uid: e.detail.uid,
483+
nodes: allNodes,
484+
});
482485
if (nodeType) {
483486
app.createShapes([
484487
{

apps/roam/src/utils/deriveDiscourseNodeAttribute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const deriveNodeAttribute = async ({
4949
}): Promise<string | number> => {
5050
const relations = getDiscourseRelations();
5151
const nodes = getDiscourseNodes(relations);
52-
const discourseNode = findDiscourseNode(uid, nodes);
52+
const discourseNode = findDiscourseNode({ uid, nodes });
5353
if (!discourseNode) return 0;
5454
const nodeType = discourseNode.type;
5555
const attributeNode = getSubTree({

apps/roam/src/utils/findDiscourseNode.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,27 @@ import matchDiscourseNode from "./matchDiscourseNode";
33

44
const discourseNodeTypeCache: Record<string, DiscourseNode | false> = {};
55

6-
const findDiscourseNode = (
7-
uid = "",
8-
nodes = getDiscourseNodes(),
9-
): DiscourseNode | false => {
10-
if (typeof discourseNodeTypeCache[uid] !== "undefined") {
11-
return discourseNodeTypeCache[uid];
12-
}
13-
14-
const matchingNode = nodes.find((node) =>
15-
matchDiscourseNode({ ...node, uid }),
16-
);
17-
18-
discourseNodeTypeCache[uid] = matchingNode || false;
19-
return discourseNodeTypeCache[uid];
20-
};
21-
export default findDiscourseNode;
22-
23-
export const findDiscourseNodeByTitleAndUid = ({
6+
const findDiscourseNode = ({
247
uid,
258
title,
26-
nodes,
9+
nodes = getDiscourseNodes(),
2710
}: {
28-
uid: string;
29-
title: string;
11+
uid?: string;
12+
title?: string;
3013
nodes?: DiscourseNode[];
3114
}): DiscourseNode | false => {
32-
nodes = nodes || getDiscourseNodes();
15+
if (uid === undefined) return false;
3316
if (typeof discourseNodeTypeCache[uid] !== "undefined") {
3417
return discourseNodeTypeCache[uid];
3518
}
3619

37-
const matchingNode = nodes.find((node) =>
38-
matchDiscourseNode({ ...node, title }),
39-
);
40-
41-
discourseNodeTypeCache[uid] = matchingNode || false;
42-
return discourseNodeTypeCache[uid];
20+
const matchingNode =
21+
nodes.find((node) =>
22+
title === undefined
23+
? matchDiscourseNode({ ...node, uid: uid })
24+
: matchDiscourseNode({ ...node, title }),
25+
) || false;
26+
discourseNodeTypeCache[uid] = matchingNode;
27+
return matchingNode;
4328
};
29+
export default findDiscourseNode;

apps/roam/src/utils/formatUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export const getReferencedNodeInFormat = ({
136136
}) => {
137137
let format = providedFormat;
138138
if (!format) {
139-
const discourseNode = findDiscourseNode(uid);
139+
const discourseNode = findDiscourseNode({ uid });
140140
if (discourseNode) format = discourseNode.format;
141141
}
142142
if (!format) return null;

apps/roam/src/utils/getDiscourseContextResults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ const getDiscourseContextResults = async ({
182182
}) => {
183183
const args = { ignoreCache };
184184

185-
const discourseNode = findDiscourseNode(targetUid);
185+
const discourseNode = findDiscourseNode({ uid: targetUid });
186186
if (!discourseNode) return [];
187187
const nodeType = discourseNode?.type;
188188
const nodeTextByType = Object.fromEntries(

0 commit comments

Comments
 (0)