-
Notifications
You must be signed in to change notification settings - Fork 4
ENG-1074 Prod duplicate node alert on page #564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sid597
wants to merge
7
commits into
main
Choose a base branch
from
eng-1074-prod-duplicate-node-alert-using-vector-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9cfe5bc
duplicate node alert on page
sid597 efdc429
add to node create dialog, only show small list, fix lint errors, fix…
sid597 d11bab2
remove duplication vector match shape
sid597 74f63a3
address review
sid597 81ad95b
optimise discourse node page observer, run isDiscourseNode only once
sid597 bcff59d
remove overlay from node dialog, remove usenoecontext, pass node from…
sid597 612a9fa
use handleTitleAddition .. only show when suggestive mode is enabled …
sid597 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import React, { useEffect, useState, useMemo } from "react"; | ||
| import { Collapse, Spinner, Icon } from "@blueprintjs/core"; | ||
| import { findSimilarNodesVectorOnly, type VectorMatch } from "~/utils/hyde"; | ||
| import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle"; | ||
| import { DiscourseNode } from "~/utils/getDiscourseNodes"; | ||
| import extractContentFromTitle from "~/utils/extractContentFromTitle"; | ||
| import { handleTitleAdditions } from "~/utils/handleTitleAdditions"; | ||
|
|
||
| type VectorSearchParams = { | ||
| text: string; | ||
| threshold?: number; | ||
| limit?: number; | ||
| }; | ||
|
|
||
| const vectorSearch = (params: VectorSearchParams) => | ||
| findSimilarNodesVectorOnly(params); | ||
|
|
||
| export const VectorDuplicateMatches = ({ | ||
| pageTitle, | ||
| text, | ||
| limit = 15, | ||
| node, | ||
| }: { | ||
| pageTitle?: string; | ||
| text?: string; | ||
| limit?: number; | ||
| node: DiscourseNode; | ||
| }) => { | ||
| const [debouncedText, setDebouncedText] = useState(text); | ||
| useEffect(() => { | ||
| const handler = setTimeout(() => { | ||
| setDebouncedText(text); | ||
| }, 500); | ||
| return () => { | ||
| clearTimeout(handler); | ||
| }; | ||
| }, [text]); | ||
|
|
||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [suggestionsLoading, setSuggestionsLoading] = useState(false); | ||
| const [hasSearched, setHasSearched] = useState(false); | ||
| const [suggestions, setSuggestions] = useState<VectorMatch[]>([]); | ||
|
|
||
| const searchText = extractContentFromTitle(pageTitle || "", node); | ||
| const pageUid = getPageUidByPageTitle(searchText); | ||
| const activeContext = useMemo( | ||
| () => | ||
| text !== undefined | ||
| ? { searchText: debouncedText || "", pageUid: null } | ||
| : { searchText, pageUid }, | ||
| [text, debouncedText, searchText, pageUid], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| setHasSearched(false); | ||
| }, [activeContext?.searchText]); | ||
|
|
||
| useEffect(() => { | ||
| let isCancelled = false; | ||
| const fetchSuggestions = async () => { | ||
| if (!isOpen || hasSearched) return; | ||
| if (!activeContext || !activeContext.searchText.trim()) return; | ||
|
|
||
| const { searchText, pageUid } = activeContext; | ||
|
|
||
| setSuggestionsLoading(true); | ||
| try { | ||
| const raw = await vectorSearch({ | ||
| text: searchText, | ||
| threshold: 0.3, | ||
| limit, | ||
| }); | ||
| const results: VectorMatch[] = raw.filter((candidate) => { | ||
| const sameUid = !!pageUid && candidate.node.uid === pageUid; | ||
| return !sameUid; | ||
| }); | ||
| if (!isCancelled) { | ||
| setSuggestions(results); | ||
| setSuggestionsLoading(false); | ||
| setHasSearched(true); | ||
| } | ||
| } catch (error: unknown) { | ||
| console.error("Error fetching vector duplicates:", error); | ||
| if (!isCancelled) { | ||
| setSuggestionsLoading(false); | ||
| } | ||
| } | ||
| }; | ||
| void fetchSuggestions(); | ||
| return () => { | ||
| isCancelled = true; | ||
| }; | ||
| }, [isOpen, hasSearched, activeContext, pageTitle, limit]); | ||
|
|
||
| const handleSuggestionClick = async (node: VectorMatch["node"]) => { | ||
| await window.roamAlphaAPI.ui.rightSidebar.addWindow({ | ||
| window: { | ||
| type: "outline", | ||
| // @ts-expect-error - type definition mismatch | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| "block-uid": node.uid, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| if (!activeContext) { | ||
| return null; | ||
| } | ||
|
|
||
| const hasSuggestions = suggestions.length > 0; | ||
|
|
||
| return ( | ||
| <div className="my-2 rounded border border-gray-200"> | ||
| <div | ||
| className="flex cursor-pointer items-center justify-between p-2" | ||
| onClick={() => { | ||
| setIsOpen(!isOpen); | ||
| }} | ||
| > | ||
| <div className="flex items-center gap-2"> | ||
| <Icon icon={isOpen ? "chevron-down" : "chevron-right"} /> | ||
| <h5 className="m-0 font-semibold">Possible Duplicates</h5> | ||
| </div> | ||
| {hasSearched && !suggestionsLoading && hasSuggestions && ( | ||
| <span className="rounded-full bg-orange-500 px-2 py-0.5 text-xs text-white"> | ||
| {suggestions.length} | ||
| </span> | ||
| )} | ||
| </div> | ||
|
|
||
| <Collapse isOpen={isOpen}> | ||
| <div className="border-t border-gray-200 p-2"> | ||
| {suggestionsLoading && ( | ||
| <div className="ml-2 flex items-center gap-2 py-4"> | ||
| <Spinner size={20} /> | ||
| <span className="text-sm text-gray-600"> | ||
| Searching for duplicates... | ||
| </span> | ||
| </div> | ||
| )} | ||
|
|
||
| {!suggestionsLoading && hasSearched && !hasSuggestions && ( | ||
| <p className="py-2 text-sm text-gray-600">No matches found.</p> | ||
| )} | ||
|
|
||
| {!suggestionsLoading && hasSearched && hasSuggestions && ( | ||
| <ul className="flex flex-col gap-1"> | ||
| {suggestions.map((match) => ( | ||
| <li key={match.node.uid} className="flex items-start gap-2"> | ||
| <a | ||
| onClick={() => { | ||
| void handleSuggestionClick(match.node); | ||
| }} | ||
| className="min-w-0 flex-1 cursor-pointer break-words text-blue-600 opacity-70 hover:underline" | ||
| > | ||
| {match.node.text} | ||
| </a> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| )} | ||
| </div> | ||
| </Collapse> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const renderPossibleDuplicates = ( | ||
| h1: HTMLHeadingElement, | ||
| title: string, | ||
| node: DiscourseNode, | ||
| ) => { | ||
| handleTitleAdditions( | ||
| h1, | ||
| <VectorDuplicateMatches pageTitle={title} node={node} />, | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import getDiscourseNodeFormatExpression from "./getDiscourseNodeFormatExpression"; | ||
|
|
||
| const extractContentFromTitle = ( | ||
| title: string, | ||
| node: { format: string }, | ||
| ): string => { | ||
| if (!node.format) return title; | ||
| const placeholderRegex = /{([\w\d-]+)}/g; | ||
| const placeholders: string[] = []; | ||
| let placeholderMatch: RegExpExecArray | null = null; | ||
| while ((placeholderMatch = placeholderRegex.exec(node.format))) { | ||
| placeholders.push(placeholderMatch[1]); | ||
| } | ||
| const expression = getDiscourseNodeFormatExpression(node.format); | ||
| const expressionMatch = expression.exec(title); | ||
| if (!expressionMatch || expressionMatch.length <= 1) { | ||
| return title; | ||
| } | ||
| const contentIndex = placeholders.findIndex( | ||
| (name) => name.toLowerCase() === "content", | ||
| ); | ||
| if (contentIndex >= 0) { | ||
| return expressionMatch[contentIndex + 1]?.trim() || title; | ||
| } | ||
| return expressionMatch[1]?.trim() || title; | ||
| }; | ||
|
|
||
| export default extractContentFromTitle; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.