Skip to content

Conversation

@trangdoan982
Copy link
Collaborator

@trangdoan982 trangdoan982 commented May 21, 2025

Summary by CodeRabbit

  • New Features
    • Introduced a searchable dropdown menu for inserting discourse nodes directly into Roam blocks, accessible via a customizable trigger string.
    • Added a settings option to configure the trigger character for the Node Search Menu in personal settings.
    • Enabled filtering and grouping of search results by node type, with keyboard and mouse navigation support.
  • Improvements
    • Enhanced event handling to support the new Node Search Menu trigger.
    • Added a utility for consistent string formatting.

* create event listener for @ key

* fix ui

* address PR comments

* cur progress

* current progress

* tested yayyy

* add the checkbox UI

* fix focus issue

* rm logs

* address PR comments

* address PR comments
* cur progress

* current progress

* rm logs

* address PR comments

* address PR comments

* query works

* address PR comments

* address PR comments

* sm change
* setting menu done

* trigger works correctly

* clean up

* address PR comment

* optimize getFocusedBlock()

* address nit comments
@linear
Copy link

linear bot commented May 21, 2025

@vercel
Copy link

vercel bot commented May 21, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
discourse-graph ⬜️ Skipped (Inspect) May 22, 2025 6:12pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 21, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

This change introduces a configurable discourse node search menu in Roam Research. It adds a new React component for the search menu, integrates a trigger listener for activating the menu based on user-defined text, updates event listener management, and provides a settings UI for configuring the trigger string. Utility and observer logic are also extended to support these features.

Changes

File(s) Change Summary
apps/roam/src/components/DiscourseNodeSearchMenu.tsx Adds NodeSearchMenu React component for a searchable dropdown menu to insert discourse nodes; includes keyboard/mouse navigation, filtering, settings component, and integration with Roam's API.
apps/roam/src/components/settings/HomePersonalSettings.tsx Adds a UI section for configuring the node search menu trigger, rendering the NodeSearchMenuTriggerSetting component in personal settings.
apps/roam/src/index.ts Updates event listener registration and cleanup to include the new discourseNodeSearchTriggerListener for input events.
apps/roam/src/utils/formatUtils.ts Adds the utility function escapeCljString for escaping and lowercasing strings.
apps/roam/src/utils/initializeObserversAndListeners.ts Adds discourseNodeSearchTriggerListener to observer initialization, updating the return type and logic to support the new discourse node search menu trigger.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Textarea
    participant DiscourseNodeSearchTriggerListener
    participant NodeSearchMenu
    participant RoamAPI

    User->>Textarea: Types trigger string
    Textarea-->>DiscourseNodeSearchTriggerListener: "input" event
    DiscourseNodeSearchTriggerListener->>DiscourseNodeSearchMenu: Render menu near cursor
    NodeSearchMenu->>RoamAPI: Fetch discourse node types and search results
    RoamAPI-->>NodeSearchMenu: Return filtered node results
    User->>NodeSearchMenu: Navigate/select node (keyboard/mouse)
    NodeSearchMenu->>RoamAPI: Insert page reference at trigger position
    NodeSearchMenu->>Textarea: Update block text and restore focus
Loading

Possibly related PRs

Suggested reviewers

  • mdroidian

Poem

In Roam’s bright fields where discourse nodes grow,
A search menu springs up where triggers now show.
With filters and settings, the rabbits delight,
Escaping their strings and hopping through night.
The cursor restores, the blocks are anew—
Hooray for the menu! 🐇✨


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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@trangdoan982 trangdoan982 requested review from mdroidian and removed request for mdroidian May 21, 2025 20:33
@trangdoan982
Copy link
Collaborator Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 21, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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: 2

🧹 Nitpick comments (5)
apps/roam/src/utils/formatUtils.ts (1)

199-201: Looks good, but consider adding a JSDoc comment.

The escapeCljString function correctly handles escaping backslashes and double quotes while converting to lowercase. This utility function will be useful for the DG summoning menu.

Consider adding a JSDoc comment explaining the purpose and usage of this function:

+/**
+ * Escapes a string for use in Clojure/ClojureScript by escaping backslashes and double quotes,
+ * and converting to lowercase.
+ * @param str - The string to escape
+ * @returns The escaped string in lowercase
+ */
export const escapeCljString = (str: string) => {
  return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').toLowerCase();
};
apps/roam/src/index.ts (1)

101-106: Good refactoring to named properties for easier access to listeners.

Changing from array-based to named property access improves code readability and maintainability. The inclusion of the new discourseNodeSearchTriggerListener is well-structured.

You might want to add a TypeScript interface for the listeners object structure to further improve code clarity.

apps/roam/src/utils/initializeObserversAndListeners.ts (1)

193-241: Well-implemented event listener with proper validation.

The implementation follows good practices with multiple validation steps:

  1. Checks for existing menu to prevent duplicates
  2. Validates the target is a Roam block textarea
  3. Ensures the trigger is valid and positioned correctly
  4. Confirms cursor position is right after the trigger
  5. Double-checks Roam's API state before rendering

Consider improving these aspects:

  1. The empty onClose callback could be a potential issue:
renderDiscourseNodeSearchMenu({
-  onClose: () => {},
+  onClose: () => {
+    // Perform cleanup if needed when the menu is closed
+  },
  textarea: textarea,
  triggerPosition: lastTriggerPos,
  triggerText: customTrigger,
});
  1. Add error handling for the Roam API call:
- const isEditingBlock = !!window.roamAlphaAPI.ui.getFocusedBlock();
+ let isEditingBlock = false;
+ try {
+   isEditingBlock = !!window.roamAlphaAPI.ui.getFocusedBlock();
+ } catch (error) {
+   console.error("Failed to check focused block:", error);
+ }
  1. Consider adding a debug log for when the trigger is detected but conditions aren't met, which could help with troubleshooting.
apps/roam/src/components/DiscourseNodeSearchMenu.tsx (2)

355-357: Global mutation during render risks mismatched active indices

currentGlobalIndex is a mutable outer variable incremented inside the JSX map.
Because it relies on side-effects during render it can:

• Desynchronise if the render tree changes
• Break when React switches to concurrent rendering

A safer, side-effect-free pattern:

-const isActive = currentGlobalIndex === activeIndex;
+const globalIndex = allItems.findIndex(
+  (i) => i.item.uid === item.uid && i.typeIndex === typeIndex && i.itemIndex === itemIndex,
+);
+const isActive = globalIndex === activeIndex;

Or compute isActive by comparing object identities without any external counter.

Also applies to: 474-476


550-555: Trigger length capped at 5 characters may be too restrictive

maxLength={5} prevents users from choosing longer, mnemonic triggers (e.g. “/dgnode”).
Consider increasing or removing this limit to improve configurability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e40dc6c and de46825.

📒 Files selected for processing (5)
  • apps/roam/src/components/DiscourseNodeSearchMenu.tsx (1 hunks)
  • apps/roam/src/components/settings/HomePersonalSettings.tsx (2 hunks)
  • apps/roam/src/index.ts (2 hunks)
  • apps/roam/src/utils/formatUtils.ts (1 hunks)
  • apps/roam/src/utils/initializeObserversAndListeners.ts (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
apps/roam/src/components/settings/HomePersonalSettings.tsx (1)
apps/roam/src/components/DiscourseNodeSearchMenu.tsx (1)
  • NodeSearchMenuTriggerSetting (526-557)
apps/roam/src/utils/initializeObserversAndListeners.ts (1)
apps/roam/src/components/DiscourseNodeSearchMenu.tsx (1)
  • renderDiscourseNodeSearchMenu (505-524)
🪛 Biome (1.9.4)
apps/roam/src/components/DiscourseNodeSearchMenu.tsx

[error] 134-134: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)

🔇 Additional comments (8)
apps/roam/src/components/settings/HomePersonalSettings.tsx (2)

15-15: LGTM - Import for the new NodeSearchMenuTriggerSetting component.

The import statement correctly brings in the NodeSearchMenuTriggerSetting component from the DiscourseNodeSearchMenu module.


32-40: Good UI addition for the new Node Search Menu Trigger.

The new labeled section follows the same pattern as other settings in this component, with a clear label, helpful description (noting the refresh requirement), and the appropriate component.

The addition integrates well with existing settings and provides users with a clear way to configure the new DG summoning menu trigger.

apps/roam/src/index.ts (2)

110-110: LGTM - Registration of the new input event listener.

Properly adds the event listener for the new discourse node search trigger functionality.


141-141: LGTM - Proper cleanup of the event listener on unload.

Ensures the input event listener is correctly removed during extension unload, preventing memory leaks.

apps/roam/src/utils/initializeObserversAndListeners.ts (4)

37-37: LGTM - Import for the render function.

The import statement correctly brings in the renderDiscourseNodeSearchMenu function from the DiscourseNodeSearchMenu component.


44-51: Good update to the return type for more structured listeners.

Changing from an array of EventListener to a named object with specific listener properties improves type safety and code clarity.


189-192: LGTM - Retrieving the custom trigger setting.

Correctly retrieves the user-configured trigger string from the extension settings.


251-256: LGTM - Updated listener object structure.

The new structure with named properties is more maintainable than the previous array approach.

@trangdoan982 trangdoan982 requested a review from mdroidian May 22, 2025 18:13
@mdroidian mdroidian merged commit b44f888 into main May 27, 2025
4 checks passed
@github-project-automation github-project-automation bot moved this to Done in General May 27, 2025
@mdroidian mdroidian deleted the eng-235-create-dg-summoning-menu branch May 27, 2025 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants