Skip to content

Conversation

@trangdoan982
Copy link
Collaborator

@trangdoan982 trangdoan982 commented Mar 21, 2025

Screenshot 2025-03-21 at 11 22 56 Screenshot 2025-03-21 at 11 23 05

Summary by CodeRabbit

  • New Features
    • Introduced a context view that dynamically displays discourse details based on the active file.
    • Enhanced the node creation workflow to support the dynamic setup of new discourse nodes.
    • Added a toggle mechanism for the context view, accessible via a ribbon icon and a new keyboard shortcut (Mod + Shift + I).

@vercel
Copy link

vercel bot commented Mar 21, 2025

@trangdoan982 is attempting to deploy a commit to the Discourse Graphs Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 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

The pull request introduces a new discourse context feature within the Obsidian plugin. A React component and its associated view class manage the display of file metadata and discourse node information. Enhancements allow for asynchronous creation of discourse nodes via a dedicated modal. The plugin now registers the discourse context view, provides a toggle functionality through both a ribbon icon and a hotkey, and updates its lifecycle management accordingly.

Changes

File(s) Change Summary
apps/obsidian/.../DiscourseContextView.tsx, apps/obsidian/.../index.ts, apps/obsidian/.../registerCommands.ts Introduces a new discourse context view with a dedicated React component and view class. Registers the view in the plugin, adds lifecycle methods, a toggle mechanism via a ribbon icon, and a hotkey command (Mod+Shift+i).
apps/obsidian/.../NodeTypeModal.tsx Adds an asynchronous createDiscourseNode method to create new discourse node files, and updates onChooseSuggestion to await file creation before updating the editor.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant P as DiscourseGraphPlugin
    participant W as WorkspaceLeaf
    participant V as DiscourseContextView
    U->>P: Toggle view command (ribbon/hotkey)
    P->>W: Check for existing discourse context leaf
    alt Leaf exists
       P->>W: Detach leaf
    else Leaf missing
       P->>W: Create new leaf on right
       W->>V: Set view state to discourse context view
       V-->>P: Render DiscourseContext component
       Note over P,V: Active file updated after timeout
    end
Loading
sequenceDiagram
    participant U as User
    participant M as NodeTypeModal
    participant V as Vault
    participant E as Editor
    U->>M: Choose discourse node suggestion
    M->>M: Call async createDiscourseNode()
    M->>V: Create file with generated node frontmatter
    V-->>M: Return new TFile
    M->>E: Replace selected text with node name
Loading

Suggested reviewers

  • mdroidian

Poem

Oh, what a hop this change does bring,
New views bloom like fresh spring,
Code and nodes in joyful dance,
A rabbit’s cheer, a tech romance 🐇,
With every toggle, we leap ahead!


🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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
Copy link
Collaborator Author

trangdoan982 commented Mar 21, 2025

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 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.

1 similar comment
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 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.

@trangdoan982 trangdoan982 marked this pull request as ready for review March 21, 2025 15:24
@trangdoan982
Copy link
Collaborator Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 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: 0

🧹 Nitpick comments (4)
apps/obsidian/src/index.ts (1)

34-59: Consider replacing setTimeout with an event-driven approach
This toggle method is logically sound, detaching the existing leaf if found and creating one if not. The 50ms setTimeout ensures the new leaf is ready before setting its active file. However, if large files or slow operations occur, a simple time delay can cause race conditions. Consider an event-driven approach (e.g., hooking into Obsidian’s internal signals) instead of using a hard-coded delay.

apps/obsidian/src/components/NodeTypeModal.tsx (2)

28-44: Sanitize filenames to avoid vault errors
The logic for creating new node files is correct. However, if titles include forbidden filesystem characters, it could trigger errors or unintended folder creation. Consider sanitizing the filename:

 async createDiscourseNode(
   nodeType: DiscourseNode,
   title: string,
 ): Promise<TFile> {
   const instanceId = `${nodeType.id}-${Date.now()}`;
   const frontmatter = `---
   nodeTypeId: ${nodeType.id}
   nodeInstanceId: ${instanceId}
   ---
   
   `;
-  const filename = `${title}.md`;
+  const sanitizedTitle = title.replace(/[\/\\?%*:|"<>]/g, "-");
+  const filename = `${sanitizedTitle}.md`;

   const file = await this.app.vault.create(filename, frontmatter);
   return file;
 }

46-61: Handle potential errors when creating new nodes
createDiscourseNode is an async operation but the code does not handle rejections. It may be beneficial to wrap this call in a try/catch and notify the user with a Notice if something goes wrong (e.g., insufficient vault permissions or invalid filename).

apps/obsidian/src/components/DiscourseContextView.tsx (1)

50-72: Display node type details
Showing the node type name and extracted content gives a concise snapshot of the discourse node. Inline styling is acceptable, but consider using a unified styling approach (CSS or Obsidian theming) for consistency.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 750974c and 294298a.

📒 Files selected for processing (4)
  • apps/obsidian/src/components/DiscourseContextView.tsx (1 hunks)
  • apps/obsidian/src/components/NodeTypeModal.tsx (3 hunks)
  • apps/obsidian/src/index.ts (2 hunks)
  • apps/obsidian/src/utils/registerCommands.ts (1 hunks)
🧰 Additional context used
🧬 Code Definitions (2)
apps/obsidian/src/index.ts (1)
apps/obsidian/src/components/DiscourseContextView.tsx (2) (2)
  • VIEW_TYPE_DISCOURSE_CONTEXT (6-6)
  • DiscourseContextView (83-145)
apps/obsidian/src/components/NodeTypeModal.tsx (1)
apps/obsidian/src/types.ts (1) (1)
  • DiscourseNode (1-7)
🔇 Additional comments (13)
apps/obsidian/src/utils/registerCommands.ts (1)

55-63: Add command for toggling discourse context successfully
This command properly registers a hotkey and references the toggleDiscourseContextView method. It appears well-integrated into the overall plugin flow, allowing users to quickly reveal or hide the discourse context view.

apps/obsidian/src/index.ts (3)

1-2: New imports for enhanced features
Importing Notice and WorkspaceLeaf looks consistent for future usage of Obsidian's APIs.


5-8: Import DiscourseContextView modules
Bringing in DiscourseContextView and its associated view type is essential for registering the custom view.


23-32: Register new view and ribbon icon
Registering the view via this.registerView and adding a ribbon icon to toggle it significantly enhances user discoverability. The approach is appropriate and follows Obsidian’s plugin conventions.

apps/obsidian/src/components/NodeTypeModal.tsx (1)

1-1: Import TFile for promise-based file creation
Including TFile correctly declares the return type from createDiscourseNode.

apps/obsidian/src/components/DiscourseContextView.tsx (8)

1-2: Core React and Obsidian imports established
These imports correctly set up the ItemView subclass along with React’s createRoot.


3-4: Index and utility imports
Pulling in DiscourseGraphPlugin and getDiscourseNodeFormatExpression is crucial for linking context logic and node formatting.


6-6: Unique view type definition
Defining VIEW_TYPE_DISCOURSE_CONTEXT helps keep code consistent, ensuring the plugin registers and references the correct view.


8-11: Interface for context props
Clear typing: the DiscourseContextProps interface ensures that the DiscourseContext component receives everything it needs.


13-22: Extract content from file title
The helper function extractContentFromTitle is a neat way to parse the active file’s name based on the node type’s format. Returning the full title if no match is found is a safe fallback.


24-47: Context-based rendering checks
Each condition (no file, no metadata, missing frontmatter, unknown node type) is handled gracefully with user-facing messages. This approach clearly communicates the status of the discourse node setup.


75-81: High-level “Discourse Context” heading
Wrapping the rendered content is logical, keeping the entire context UI in a clear container with an identifiable heading.


83-145: Full-featured ItemView for discourse context
The class-based view links seamlessly with Obsidian. Using onOpen and onClose to mount/unmount the React component avoids memory leaks. Implementing file-open events to dynamically update activeFile ensures behavior is automatically in sync with the user’s context.

@trangdoan982 trangdoan982 force-pushed the eng-40/identify-node-obsidian branch from 294298a to 037d577 Compare March 21, 2025 15:30
@trangdoan982 trangdoan982 requested a review from mdroidian March 21, 2025 15:46
@trangdoan982 trangdoan982 changed the title [Eng 40] Identify node obsidian [ENG-40] Identify node obsidian Mar 21, 2025
Copy link
Contributor

@mdroidian mdroidian left a comment

Choose a reason for hiding this comment

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

A few small changes.

I think this will be a good time to step back and think more about the schema and architecture: https://linear.app/discourse-graphs/issue/ENG-142/v0-schema-and-architecture-planning
We can discuss this more on Monday.

But for now, I think we can keep the implementation presented here.

I also noticed possible improvements on the getDiscourseNodeFormatExpression() / regex implementations (cross plugin and normalize if possible). But we can tackle that later. I've created one of the tasks: https://linear.app/discourse-graphs/issue/ENG-141/updates-to-getdiscoursenodeformatexpression

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.

2 participants