Skip to content

Commit a9c5c30

Browse files
authored
Refactor getReferencedNodeInFormat() (#245)
* move to formatUtils, return DiscourseNode * 1.28.1 * pass discourseNodes to reduce queries, rm excess
1 parent d4acc6e commit a9c5c30

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "query-builder",
3-
"version": "1.28.0",
3+
"version": "1.28.1",
44
"description": "Introduces new user interfaces for building queries in Roam",
55
"main": "./build/main.js",
66
"author": {

src/components/TldrawCanvasLabelDialog.tsx

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { getPlainTitleFromSpecification } from "../discourseGraphsMode";
2424
import isLiveBlock from "roamjs-components/queries/isLiveBlock";
2525
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
2626
import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid";
27+
import { getReferencedNodeInFormat } from "../utils/formatUtils";
28+
import { DiscourseNode } from "../utils/getDiscourseNodes";
2729

2830
const LabelDialogAutocomplete = ({
2931
setLabel,
@@ -45,7 +47,7 @@ const LabelDialogAutocomplete = ({
4547
initialValue: { text: string; uid: string };
4648
onSubmit: () => void;
4749
isCreateCanvasNode: boolean;
48-
referencedNode: { name: string; nodeType: string } | null;
50+
referencedNode: DiscourseNode | null;
4951
action: string;
5052
format: string;
5153
label: string;
@@ -90,7 +92,7 @@ const LabelDialogAutocomplete = ({
9092
{
9193
source: "node",
9294
relation: "is a",
93-
target: referencedNode.nodeType,
95+
target: referencedNode.type,
9496
uid: conditionUid,
9597
type: "clause",
9698
},
@@ -103,12 +105,7 @@ const LabelDialogAutocomplete = ({
103105
setIsLoading(false);
104106
}
105107
}, 100);
106-
}, [
107-
nodeType,
108-
referencedNode?.nodeType,
109-
setOptions,
110-
setReferencedNodeOptions,
111-
]);
108+
}, [nodeType, referencedNode?.type, setOptions, setReferencedNodeOptions]);
112109
const inputDivRef = useRef<HTMLDivElement>(null);
113110
useEffect(() => {
114111
if (isAddReferencedNode && inputDivRef.current) {
@@ -126,7 +123,7 @@ const LabelDialogAutocomplete = ({
126123
if (/content/i.test(val)) return r.text;
127124
if (
128125
referencedNode &&
129-
new RegExp(referencedNode.name, "i").test(val) &&
126+
new RegExp(referencedNode.text, "i").test(val) &&
130127
isAddReferencedNode
131128
)
132129
return referencedNodeValue;
@@ -157,7 +154,7 @@ const LabelDialogAutocomplete = ({
157154
} else {
158155
const pageName = format.replace(/{([\w\d-]*)}/g, (_, val) => {
159156
if (/content/i.test(val)) return content;
160-
if (new RegExp(referencedNode.name, "i").test(val))
157+
if (new RegExp(referencedNode.text, "i").test(val))
161158
return `[[${r.text}]]`;
162159
return "";
163160
});
@@ -229,7 +226,7 @@ const LabelDialogAutocomplete = ({
229226
)}
230227
{referencedNode && (
231228
<Checkbox
232-
label={`Set ${referencedNode?.name}`}
229+
label={`Set ${referencedNode?.text}`}
233230
checked={isAddReferencedNode}
234231
onChange={(e) => {
235232
const checked = e.target as HTMLInputElement;
@@ -262,7 +259,7 @@ const LabelDialogAutocomplete = ({
262259
{isAddReferencedNode &&
263260
(action === "creating" || action === "editing") && (
264261
<div className="referenced-node-autocomplete" ref={inputDivRef}>
265-
<Label>{referencedNode?.name}</Label>
262+
<Label>{referencedNode?.text}</Label>
266263
<AutocompleteInput
267264
value={
268265
referencedNodeValue
@@ -276,7 +273,7 @@ const LabelDialogAutocomplete = ({
276273
itemToQuery={itemToQuery}
277274
filterOptions={filterOptions}
278275
placeholder={
279-
isLoading ? "..." : `Enter a ${referencedNode?.name} ...`
276+
isLoading ? "..." : `Enter a ${referencedNode?.text} ...`
280277
}
281278
maxItemsDisplayed={100}
282279
/>
@@ -331,29 +328,10 @@ const LabelDialog = ({
331328
const [loading, setLoading] = useState(false);
332329
const isCreateCanvasNode = !isLiveBlock(initialUid);
333330
const { format } = discourseContext.nodes[nodeType];
334-
const getReferencedNodeInFormat = () => {
335-
const regex = /{([\w\d-]*)}/g;
336-
const matches = [...format.matchAll(regex)];
337-
338-
for (const match of matches) {
339-
const val = match[1];
340-
if (val.toLowerCase() === "context") continue;
341-
342-
const referencedNode = Object.values(discourseContext.nodes).find(
343-
({ text }) => new RegExp(text, "i").test(val)
344-
);
345-
346-
if (referencedNode) {
347-
return {
348-
name: referencedNode.text,
349-
nodeType: referencedNode.type,
350-
};
351-
}
352-
}
353-
354-
return null;
355-
};
356-
const referencedNode = getReferencedNodeInFormat();
331+
const referencedNode = getReferencedNodeInFormat({
332+
format,
333+
discourseNodes: Object.values(discourseContext.nodes),
334+
});
357335

358336
const renderCalloutText = () => {
359337
let title = "Please provide a label";

src/utils/formatUtils.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// https://github.com/RoamJS/query-builder/issues/189
33

44
import { PullBlock } from "roamjs-components/types";
5-
import getDiscourseNodes from "../utils/getDiscourseNodes";
5+
import getDiscourseNodes, { DiscourseNode } from "../utils/getDiscourseNodes";
66
import compileDatalog from "./compileDatalog";
77
import discourseNodeFormatToDatalog from "./discourseNodeFormatToDatalog";
88
import createOverlayRender from "roamjs-components/util/createOverlayRender";
@@ -85,3 +85,27 @@ export const getNewDiscourseNodeText = async ({
8585
});
8686
return formattedText;
8787
};
88+
89+
export const getReferencedNodeInFormat = ({
90+
format,
91+
discourseNodes = getDiscourseNodes(),
92+
}: {
93+
format: string;
94+
discourseNodes?: DiscourseNode[];
95+
}) => {
96+
const regex = /{([\w\d-]*)}/g;
97+
const matches = [...format.matchAll(regex)];
98+
99+
for (const match of matches) {
100+
const val = match[1];
101+
if (val.toLowerCase() === "context") continue;
102+
103+
const referencedNode = Object.values(discourseNodes).find(({ text }) =>
104+
new RegExp(text, "i").test(val)
105+
);
106+
107+
if (referencedNode) return referencedNode;
108+
}
109+
110+
return null;
111+
};

0 commit comments

Comments
 (0)