Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions apps/roam/src/components/FuzzySelectInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useState, useCallback, useMemo, useRef, useEffect } from "react";
import React, {
useState,
useCallback,
useMemo,
useRef,
useEffect,
} from "react";
import {
Button,
TextArea,
Expand All @@ -14,27 +20,22 @@ import { Result } from "~/utils/types";
type FuzzySelectInputProps<T extends Result = Result> = {
value?: T;
setValue: (q: T) => void;
onLockedChange?: (isLocked: boolean) => void;
mode: "create" | "edit";
initialUid: string;
options?: T[];
placeholder?: string;
autoFocus?: boolean;
initialIsLocked?: boolean;
isLocked: boolean;
};

const FuzzySelectInput = <T extends Result = Result>({
value,
setValue,
onLockedChange,
mode,
initialUid,
options = [],
placeholder = "Enter value",
autoFocus,
initialIsLocked,
isLocked,
}: FuzzySelectInputProps<T>) => {
const [isLocked, setIsLocked] = useState(initialIsLocked || false);
const [query, setQuery] = useState<string>(() => value?.text || "");
const [isOpen, setIsOpen] = useState(false);
const [activeIndex, setActiveIndex] = useState(0);
Expand All @@ -52,28 +53,31 @@ const FuzzySelectInput = <T extends Result = Result>({

const handleSelect = useCallback(
(item: T) => {
if (mode === "create" && item.uid && item.uid !== initialUid) {
Copy link
Collaborator

@trangdoan982 trangdoan982 Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think we need another variable to detect if we're creating nodes in canvas.
Removing this and asserting const isLocked = mode === "create" && Boolean(value?.uid); makes it so that every new node creation in Canvas will have isLocked=true by default (because block uid is created first before the flow is finished)

Image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Re-added isLocked and made it required.
https://www.loom.com/share/0403ecba18dd478995bfb9310a0a6aea

setIsLocked(true);
setQuery(item.text);
setValue(item);
setIsOpen(false);
onLockedChange?.(true);
} else {
setQuery(item.text);
setValue(item);
setIsOpen(false);
setQuery(item.text);
setValue(item);
setIsOpen(false);

if (!(mode === "create" && item.uid)) {
requestAnimationFrame(() => inputRef.current?.focus());
}
},
[mode, initialUid, setValue, onLockedChange],
[mode, setValue],
);

const handleClear = useCallback(() => {
setIsLocked(false);
setQuery("");
setValue({ ...value, text: "", uid: "" } as T);
onLockedChange?.(false);
}, [value, setValue, onLockedChange]);
}, [value, setValue]);

const handleInputChange = useCallback(
(text: string) => {
setQuery(text);
if (mode === "create" && !isLocked) {
setValue({ text, uid: "" } as T);
}
},
[mode, isLocked, setValue],
);

const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLInputElement>) => {
Expand All @@ -100,10 +104,9 @@ const FuzzySelectInput = <T extends Result = Result>({
);

useEffect(() => {
if (mode === "create" && !isLocked) {
setValue({ text: query, uid: "" } as T);
}
}, [query, mode, isLocked, setValue]);
if (isLocked) return;
setQuery(value?.text ?? "");
}, [isLocked, value?.text]);

useEffect(() => {
if (isFocused && filteredItems.length > 0 && query) {
Expand Down Expand Up @@ -206,7 +209,7 @@ const FuzzySelectInput = <T extends Result = Result>({
inputRef={inputRef}
className="w-full"
value={query}
onChange={(e) => setQuery(e.target.value)}
onChange={(e) => handleInputChange(e.target.value)}
onKeyDown={handleKeyDown}
autoFocus={autoFocus}
placeholder={placeholder}
Expand All @@ -223,4 +226,4 @@ const FuzzySelectInput = <T extends Result = Result>({
);
};

export default FuzzySelectInput;
export default FuzzySelectInput;
13 changes: 4 additions & 9 deletions apps/roam/src/components/ModifyNodeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ const ModifyNodeDialog = ({
[content.uid, initialValue.uid, mode],
);
const isReferencedNodeLocked = useMemo(
() =>
Boolean(
referencedNodeValue.uid &&
referencedNodeValue.uid !== initialReferencedNode?.uid,
),
[referencedNodeValue.uid, initialReferencedNode?.uid],
() => Boolean(referencedNodeValue.uid),
[referencedNodeValue.uid],
);

const [options, setOptions] = useState<{
Expand Down Expand Up @@ -537,7 +533,7 @@ const ModifyNodeDialog = ({
: `Enter a ${selectedNodeType.text.toLowerCase()} ...`
}
mode={mode}
initialUid={content.uid}
isLocked={isContentLocked}
autoFocus
/>
</div>
Expand All @@ -552,8 +548,7 @@ const ModifyNodeDialog = ({
options={options.referencedNode}
placeholder={loading ? "..." : "Select a referenced node"}
mode={"create"}
initialUid={referencedNodeValue.uid}
initialIsLocked={isReferencedNodeLocked}
isLocked={isReferencedNodeLocked}
autoFocus={false}
/>
</div>
Expand Down