Skip to content

Commit 74f63a3

Browse files
committed
address review
1 parent d11bab2 commit 74f63a3

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

apps/roam/src/components/CreateNodeDialog.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,11 @@ const CreateNodeDialog = ({
3535
discourseNodes[0];
3636

3737
const [title, setTitle] = useState(initialTitle);
38-
const [debouncedTitle, setDebouncedTitle] = useState(initialTitle);
3938
const [selectedType, setSelectedType] =
4039
useState<DiscourseNode>(defaultNodeType);
4140
const [loading, setLoading] = useState(false);
4241
const inputRef = useRef<HTMLInputElement>(null);
4342

44-
useEffect(() => {
45-
const handler = setTimeout(() => {
46-
setDebouncedTitle(title);
47-
}, 500);
48-
return () => {
49-
clearTimeout(handler);
50-
};
51-
}, [title]);
52-
5343
useEffect(() => {
5444
if (inputRef.current) {
5545
inputRef.current.focus();
@@ -147,11 +137,7 @@ const CreateNodeDialog = ({
147137
onChange={(e) => setTitle(e.currentTarget.value)}
148138
inputRef={inputRef}
149139
/>
150-
<VectorDuplicateMatches
151-
pageTitle={debouncedTitle}
152-
text={debouncedTitle}
153-
limit={5}
154-
/>
140+
<VectorDuplicateMatches text={title} limit={5} />
155141
</div>
156142

157143
<Label>

apps/roam/src/components/VectorDuplicateMatches.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState, useMemo } from "react";
22
import { Collapse, Spinner, Icon } from "@blueprintjs/core";
3-
import { findSimilarNodesVectorOnly, type VectorMatch } from "../utils/hyde";
4-
import { useNodeContext, type NodeContext } from "../utils/useNodeContext";
3+
import { findSimilarNodesVectorOnly, type VectorMatch } from "~/utils/hyde";
4+
import { useNodeContext, type NodeContext } from "~/utils/useNodeContext";
55
import ReactDOM from "react-dom";
66

77
type VectorSearchParams = {
@@ -18,19 +18,32 @@ export const VectorDuplicateMatches = ({
1818
text,
1919
limit = 15,
2020
}: {
21-
pageTitle: string;
21+
pageTitle?: string;
2222
text?: string;
2323
limit?: number;
2424
}) => {
25+
const [debouncedText, setDebouncedText] = useState(text);
26+
useEffect(() => {
27+
const handler = setTimeout(() => {
28+
setDebouncedText(text);
29+
}, 500);
30+
return () => {
31+
clearTimeout(handler);
32+
};
33+
}, [text]);
34+
2535
const [isOpen, setIsOpen] = useState(false);
2636
const [suggestionsLoading, setSuggestionsLoading] = useState(false);
2737
const [hasSearched, setHasSearched] = useState(false);
2838
const [suggestions, setSuggestions] = useState<VectorMatch[]>([]);
2939

30-
const nodeContext: NodeContext | null = useNodeContext(pageTitle);
40+
const nodeContext: NodeContext | null = useNodeContext(pageTitle || "");
3141
const activeContext = useMemo(
32-
() => (text ? { searchText: text, pageUid: null } : nodeContext),
33-
[text, nodeContext],
42+
() =>
43+
text !== undefined
44+
? { searchText: debouncedText || "", pageUid: null }
45+
: nodeContext,
46+
[text, debouncedText, nodeContext],
3447
);
3548

3649
useEffect(() => {
@@ -101,7 +114,7 @@ export const VectorDuplicateMatches = ({
101114
>
102115
<div className="flex items-center gap-2">
103116
<Icon icon={isOpen ? "chevron-down" : "chevron-right"} />
104-
<h5 className="m-0 font-semibold">Plain Vector Search Matches</h5>
117+
<h5 className="m-0 font-semibold">Possible Duplicates</h5>
105118
</div>
106119
{hasSearched && !suggestionsLoading && hasSuggestions && (
107120
<span className="rounded-full bg-orange-500 px-2 py-0.5 text-xs text-white">
@@ -137,9 +150,6 @@ export const VectorDuplicateMatches = ({
137150
>
138151
{match.node.text}
139152
</a>
140-
<span className="ml-2 shrink-0 text-xs tabular-nums text-gray-500">
141-
{match.score.toFixed(3)}
142-
</span>
143153
</li>
144154
))}
145155
</ul>

apps/roam/src/utils/hyde.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getLoggedInClient, getSupabaseContext } from "./supabaseContext";
22
import { Result } from "./types";
33
import normalizePageTitle from "roamjs-components/queries/normalizePageTitle";
4+
import { render as renderToast } from "roamjs-components/components/Toast";
45
import findDiscourseNode from "./findDiscourseNode";
56
import { nextApiRoot } from "@repo/utils/execContext";
67
import { DiscourseNode } from "./getDiscourseNodes";
@@ -58,7 +59,7 @@ type EmbeddingFunc = (text: string) => Promise<EmbeddingVectorType>;
5859

5960
type SearchFunc = (params: {
6061
queryEmbedding: EmbeddingVectorType;
61-
indexData: { uid: string; text: string }[];
62+
indexData: Result[];
6263
}) => Promise<NodeSearchResult[]>;
6364

6465
const API_CONFIG = {
@@ -173,7 +174,7 @@ const createEmbedding: EmbeddingFunc = async (
173174
}
174175
};
175176

176-
export const searchEmbeddings: SearchFunc = async ({
177+
const searchEmbeddings: SearchFunc = async ({
177178
queryEmbedding,
178179
indexData,
179180
}): Promise<NodeSearchResult[]> => {
@@ -568,23 +569,24 @@ export const findSimilarNodesVectorOnly = async ({
568569

569570
if (!data || !Array.isArray(data)) return [];
570571

571-
const results: VectorMatch[] = data.map(
572-
(item: {
573-
roam_uid: string;
574-
text_content: string;
575-
similarity: number;
576-
}) => ({
577-
node: {
578-
uid: item.roam_uid,
579-
text: item.text_content,
580-
},
581-
score: item.similarity,
582-
}),
583-
);
572+
const results: VectorMatch[] = data.map((item) => ({
573+
node: {
574+
uid: item.roam_uid,
575+
text: item.text_content,
576+
},
577+
score: item.similarity,
578+
}));
584579

585580
return results;
586581
} catch (error) {
587582
console.error("Error in vector-only similar nodes search:", error);
583+
renderToast({
584+
content: `Error in vector-only similar nodes search: ${
585+
error instanceof Error ? error.message : String(error)
586+
}`,
587+
intent: "danger",
588+
id: "vector-search-error",
589+
});
588590
return [];
589591
}
590592
};

0 commit comments

Comments
 (0)