Skip to content

Commit b0eedde

Browse files
committed
make sure there's initial source node
1 parent 9b70982 commit b0eedde

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

apps/roam/src/components/FuzzySelectInput.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type FuzzySelectInputProps<T extends Result = Result> = {
2121
placeholder?: string;
2222
autoFocus?: boolean;
2323
disabled?: boolean;
24+
initialIsLocked?: boolean;
2425
};
2526

2627
const FuzzySelectInput = <T extends Result = Result>({
@@ -33,9 +34,9 @@ const FuzzySelectInput = <T extends Result = Result>({
3334
placeholder = "Enter value",
3435
autoFocus,
3536
disabled,
37+
initialIsLocked,
3638
}: FuzzySelectInputProps<T>) => {
37-
const [isLocked, setIsLocked] = useState(false);
38-
const [lockedValue, setLockedValue] = useState<T | undefined>(undefined);
39+
const [isLocked, setIsLocked] = useState(initialIsLocked || false);
3940
const [query, setQuery] = useState<string>(() => value?.text || "");
4041
const [isOpen, setIsOpen] = useState(false);
4142
const [activeIndex, setActiveIndex] = useState(0);
@@ -57,7 +58,6 @@ const FuzzySelectInput = <T extends Result = Result>({
5758
(item: T) => {
5859
if (mode === "create" && item.uid && item.uid !== initialUid) {
5960
// Lock the value
60-
setLockedValue(item);
6161
setIsLocked(true);
6262
setQuery(item.text);
6363
setValue(item);
@@ -76,7 +76,6 @@ const FuzzySelectInput = <T extends Result = Result>({
7676
// Handle clear locked value
7777
const handleClear = useCallback(() => {
7878
setIsLocked(false);
79-
setLockedValue(undefined);
8079
setQuery("");
8180
setValue({ text: "", uid: "" } as T);
8281
onLockedChange?.(false);
@@ -162,12 +161,12 @@ const FuzzySelectInput = <T extends Result = Result>({
162161
}
163162

164163
// Create mode: locked value display
165-
if (isLocked && lockedValue) {
164+
if (isLocked || initialIsLocked) {
166165
return (
167166
<div className="flex w-full items-center gap-2">
168167
<div className="flex flex-1 items-center gap-2 rounded border border-gray-300 bg-gray-100 px-3 py-2 dark:border-gray-600 dark:bg-gray-800">
169168
<span className="flex-1 text-gray-900 dark:text-gray-100">
170-
{lockedValue.text}
169+
{value?.text}
171170
</span>
172171
<Button
173172
icon="cross"

apps/roam/src/components/ModifyNodeDialog.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ const ModifyNodeDialog = ({
7272
initialReferencedNode?.uid || "",
7373
);
7474
const [isContentLocked, setIsContentLocked] = useState(false);
75-
const [isReferencedNodeLocked, setIsReferencedNodeLocked] = useState(false);
75+
const [isReferencedNodeLocked, setIsReferencedNodeLocked] = useState(
76+
Boolean(initialReferencedNode?.uid),
77+
);
7678
const [contentOptions, setContentOptions] = useState<Result[]>([]);
7779
const [referencedNodeOptions, setReferencedNodeOptions] = useState<Result[]>(
7880
[],
@@ -482,6 +484,7 @@ const ModifyNodeDialog = ({
482484
onLockedChange={setIsReferencedNodeLocked}
483485
mode={"create"}
484486
initialUid={referencedNodeUid}
487+
initialIsLocked={isReferencedNodeLocked}
485488
/>
486489
</div>
487490
)}

apps/roam/src/utils/renderNodeTagPopup.tsx

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
33
import { Button, Popover, Position } from "@blueprintjs/core";
4-
import { OnloadArgs } from "roamjs-components/types";
4+
import { OnloadArgs, PullBlock } from "roamjs-components/types";
55
import getUids from "roamjs-components/dom/getUids";
66
import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid";
77
import { type DiscourseNode } from "./getDiscourseNodes";
88
import { renderModifyNodeDialog } from "~/components/ModifyNodeDialog";
9+
import { getReferencedNodeInFormat } from "./formatUtils";
10+
import discourseNodeFormatToDatalog from "./discourseNodeFormatToDatalog";
11+
import compileDatalog from "./compileDatalog";
12+
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
913

1014
export const renderNodeTagPopupButton = (
1115
parent: HTMLSpanElement,
@@ -43,7 +47,38 @@ export const renderNodeTagPopupButton = (
4347
const cleanedBlockText = rawBlockText.replace(textContent, "").trim();
4448

4549
const getInitialReferencedNode = () => {
46-
// TODO: Implement this in a follow-up PR
50+
if (!blockUid) return { text: "", uid: "" };
51+
52+
const referencedNodeType = getReferencedNodeInFormat({
53+
format: matchedNode.format,
54+
});
55+
56+
if (!referencedNodeType) return { text: "", uid: "" };
57+
58+
try {
59+
const referenced = window.roamAlphaAPI.data.fast.q(
60+
`[:find (pull ?r [:node/title :block/string]) :where [?b :block/uid "${blockUid}"] (or-join [?b ?r] (and [?b :block/parents ?p] [?p :block/refs ?r]) (and [?b :block/page ?r])) ${discourseNodeFormatToDatalog(
61+
{
62+
freeVar: "r",
63+
...referencedNodeType,
64+
},
65+
)
66+
.map((c) => compileDatalog(c, 0))
67+
.join(" ")}]`,
68+
)?.[0]?.[0] as PullBlock;
69+
70+
if (referenced) {
71+
const title =
72+
referenced[":node/title"] || referenced[":block/string"] || "";
73+
if (title) {
74+
const uid = getPageUidByPageTitle(title);
75+
return { text: title, uid: uid };
76+
}
77+
}
78+
} catch (error) {
79+
console.error("Error getting initial referenced node:", error);
80+
}
81+
4782
return { text: "", uid: "" };
4883
};
4984

0 commit comments

Comments
 (0)