Skip to content

Conversation

trangdoan982
Copy link
Collaborator

@trangdoan982 trangdoan982 commented Oct 16, 2025

https://www.loom.com/share/8706a4b84aa94e69b0cce35bffc76654

Summary by CodeRabbit

  • New Features
    • Search results now use fuzzy-based local filtering for improved relevance.
    • Added per-type result caching in the Discourse Node Search Menu.

Copy link

linear bot commented Oct 16, 2025

Copy link

supabase bot commented Oct 16, 2025

This pull request has been ignored for the connected project zytfjzqyijgagqxrzbmz because there are no changes detected in packages/database/supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@trangdoan982
Copy link
Collaborator Author

@coderabbitai full review

Copy link
Contributor

coderabbitai bot commented Oct 16, 2025

✅ Actions performed

Full review triggered.

Copy link
Contributor

coderabbitai bot commented Oct 16, 2025

📝 Walkthrough

Walkthrough

This PR refactors DiscourseNodeSearchMenu to introduce local fuzzy filtering with per-type node result caching. It modifies API signatures (waitForBlock to accept object parameters), precomputes and caches node results on initial load, and implements cached fuzzy filtering for subsequent searches.

Changes

Cohort / File(s) Summary
Fuzzy local filtering and per-type caching
apps/roam/src/components/DiscourseNodeSearchMenu.tsx
Adds allNodes state to store pre-fetched per-type node results; introduces filterNodesLocally helper for fuzzy filtering on cached results; precomputes all node types on initial load; adjusts useEffect dependencies to rely on caching and filtering logic; reworks data access patterns via getUids(el).
API signature refactoring
apps/roam/src/components/DiscourseNodeSearchMenu.tsx
Reworks waitForBlock to accept a single object parameter instead of separate uid/text arguments; replaces explicit function calls with equivalent object-based calls; wraps promise operations in void for consistent handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Menu as DiscourseNodeSearchMenu
    participant Cache as allNodes State
    participant Filter as filterNodesLocally
    participant API as Roam API

    Note over Menu: Component Mount
    Menu->>API: Fetch all node types (pre-compute)
    API-->>Cache: Store results per type
    
    Note over Menu: User types query
    User->>Menu: Search input
    Menu->>Cache: Retrieve cached nodes
    Menu->>Filter: Apply fuzzy filter
    Filter-->>Menu: Filtered results
    Menu->>User: Display filtered results
    
    Note over Menu: User selects node
    User->>Menu: Click/Enter selection
    Menu->>API: waitForBlock({uid, text, ...})
    API-->>Menu: Block focused/updated
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

This refactoring involves multiple interconnected logic changes (caching mechanism, fuzzy filtering, data access patterns) within a single file, requiring verification of state management correctness, caching integrity, and compatibility of the API signature changes throughout the component.

Possibly related PRs

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly identifies the primary change—modifying how the discourse node search query operates—and directly corresponds to the key updates in filtering and caching logic described in the PR, making it both specific and concise.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
apps/roam/src/components/DiscourseNodeSearchMenu.tsx (2)

153-173: Synchronous prefetch of all types can block UI on large graphs.

This loops q() over all types in one tick, freezing the UI. Make it async and yield between queries.

Apply this diff:

-    const fetchNodeTypes = () => {
+    const fetchNodeTypes = async () => {
       setIsLoading(true);
@@
-      const allNodesCache: Record<string, Result[]> = {};
-      allNodeTypes.forEach((type) => {
-        allNodesCache[type.type] = searchNodesForType(type);
-      });
-      setAllNodes(allNodesCache);
+      const allNodesCache: Record<string, Result[]> = {};
+      for (const type of allNodeTypes) {
+        allNodesCache[type.type] = searchNodesForType(type);
+        // Yield to event loop to keep UI responsive
+        await new Promise((r) => setTimeout(r, 0));
+      }
+      setAllNodes(allNodesCache);

611-614: Trigger is double-escaped; store raw, escape only at match time.

Escaping on save plus escaping on use will break triggers like “+” and “\”. Save raw; build a safe regex later (already done in handleTextAreaInput).

Apply this diff:

-    const trigger = value
-      .replace(/"/g, "")
-      .replace(/\\/g, "\\\\")
-      .replace(/\+/g, "\\+")
-      .trim();
+    const trigger = value;
🧹 Nitpick comments (5)
apps/roam/src/components/DiscourseNodeSearchMenu.tsx (5)

37-47: waitForBlock API change looks good; consider tunable delay/backoff.

Signature refactor is clean. Optionally add a delayMs/backoff to reduce tight polling and make timeouts clearer.

Also applies to: 54-62


106-136: Harden regex string embedding in Datalog.

If regex.source contains newlines, the query string can break. Escape newlines in addition to quotes and backslashes.

Apply this diff:

-      const regexPattern = regex.source
-        .replace(/\\/g, "\\\\")
-        .replace(/"/g, '\\"');
+      const regexPattern = regex.source
+        .replace(/\\/g, "\\\\")
+        .replace(/"/g, '\\"')
+        .replace(/\n/g, "\\n");

138-150: Local fuzzy filter OK; consider capping results for UX/perf.

For large node sets, cap per-type results (e.g., slice to top N) to avoid huge menus and layout thrash.


186-197: Filtering every type each keystroke: add cheap guard or cap.

Minor perf nit: if you decide not to show “all results” by default, gate filtering until searchTerm length ≥ 2, or cap per-type results to reduce work.


581-581: React 18: replace deprecated ReactDOM.render/unmount.

Use createRoot to avoid deprecated APIs.

Apply this diff:

-  // eslint-disable-next-line react/no-deprecated
-  ReactDOM.render(
+  const root = (ReactDOM as any).createRoot
+    ? (require("react-dom/client").createRoot as (e: Element) => any)(parent)
+    : null;
+  const renderFn = root ? root.render.bind(root) : ReactDOM.render;
+  renderFn(
@@
-        // eslint-disable-next-line react/no-deprecated
-        ReactDOM.unmountComponentAtNode(parent);
+        if (root && root.unmount) root.unmount();
+        else ReactDOM.unmountComponentAtNode(parent);
         parent.remove();

Additionally, add (outside this hunk) at top-level imports:

// If you can modify imports:
import { createRoot } from "react-dom/client";

Also applies to: 587-588

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5ef439f and 2ef4605.

📒 Files selected for processing (1)
  • apps/roam/src/components/DiscourseNodeSearchMenu.tsx (11 hunks)
🔇 Additional comments (1)
apps/roam/src/components/DiscourseNodeSearchMenu.tsx (1)

28-28: Import fuzzy via namespace for CommonJS
CommonJS-only fuzzy lacks a default export in ESM; switch to:

-import fuzzy from "fuzzy";
+import * as fuzzy from "fuzzy";

or enable esModuleInterop/allowSyntheticDefaultImports: true in your TypeScript config.

@trangdoan982 trangdoan982 force-pushed the eng-748-node-summoning-menu-not-showing-all-results branch from ebc4520 to 36a2d9c Compare October 17, 2025 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant