Skip to content

[WEB-2047] dev: pages side menu refactor#5371

Merged
SatishGandham merged 2 commits intopreviewfrom
dev/pages-ai-menu
Aug 16, 2024
Merged

[WEB-2047] dev: pages side menu refactor#5371
SatishGandham merged 2 commits intopreviewfrom
dev/pages-ai-menu

Conversation

@aaryan610
Copy link
Member

@aaryan610 aaryan610 commented Aug 16, 2024

Improvements:

Refactored the editor side menu and added the support for AI handle.

Summary by CodeRabbit

  • New Features

    • Introduced an AI features menu for enhanced user interaction within the text editor.
    • Added optional aiHandler properties to DocumentEditor and PageRenderer, enabling AI functionalities.
    • New components for interacting with AI responses have been created, including AskPiMenu and EditorAIMenu.
    • Enhanced text manipulation capabilities with new methods in the editor.
  • Bug Fixes

    • Adjusted CSS rules for drag-and-drop elements to improve visual consistency.
  • Documentation

    • Updated type declarations and export statements to include new AI-related functionalities.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 16, 2024

Walkthrough

The recent updates enhance the text editor by integrating AI features throughout various components. Key changes include the addition of new interfaces and components for AI interactions, modifications to existing editor functionalities, and improved export structures. These enhancements aim to provide a more seamless and interactive experience for users, allowing for richer text editing capabilities.

Changes

File Path Change Summary
packages/editor/src/ce/extensions/index.ts Removed export from ./ai-features, altering the public API.
packages/editor/src/core/components/editors/document/... Added aiHandler property to IDocumentEditor and IPageRenderer interfaces, enhancing AI functionality.
packages/editor/src/core/components/menus/ai-menu.tsx Introduced AIFeaturesMenu for AI interactions.
packages/editor/src/core/components/menus/index.ts Added export for ai-menu, enhancing module functionality.
packages/editor/src/core/hooks/use-editor.ts Added getSelectedText and insertText methods to useEditor hook for improved text manipulation.
packages/editor/src/core/types/ai.ts Introduced TMenuProps and TAIHandler types for AI functionality.
packages/editor/src/core/types/editor.ts Added getSelectedText and insertText methods to EditorRefApi interface.
web/ce/components/pages/editor/ai/ask-pi-menu.tsx Introduced AskPiMenu component for AI response interaction.
web/ce/components/pages/editor/ai/menu.tsx Implemented EditorAIMenu to enhance text editor with AI capabilities.
web/ce/constants/ai.ts Introduced AI_EDITOR_TASKS enum for defining tasks related to AI.
web/core/services/ai.service.ts Updated AIService class with new TTaskPayload type and method for performing editor tasks.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Editor
    participant AIFeaturesMenu
    participant PageRenderer

    User->>Editor: Interacts with DocumentEditor
    Editor->>PageRenderer: Passes aiHandler
    PageRenderer->>AIFeaturesMenu: Conditionally renders based on aiHandler
    AIFeaturesMenu->>User: Displays AI options
Loading

🐇 "In the garden of code, we hop with glee,
New features bloom like flowers, you see!
With AI at play, our tasks are bright,
Editing joyfully, from morning to night!"


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

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

Outside diff range, codebase verification and nitpick comments (3)
web/core/services/ai.service.ts (1)

32-43: Consider adding logging for error handling in performEditorTask.

While the method correctly handles errors by throwing them, adding logging could help in debugging and monitoring.

.catch((error) => {
+  console.error('Error performing editor task:', error?.response?.data);
  throw error?.response?.data;
});
packages/editor/src/core/hooks/use-editor.ts (1)

217-237: Consider removing console log in production.

The console.log(selection); statement might be useful for debugging but should be removed or replaced with a more appropriate logging mechanism for production.

-        console.log(selection);
web/ce/components/pages/editor/ai/menu.tsx (1)

69-72: Add error handling for async operation.

Consider adding error handling to the handleGenerateResponse function to manage potential errors from aiService.performEditorTask.

await aiService.performEditorTask(workspaceSlug.toString(), payload)
  .then((res) => setResponse(res.response))
  .catch((error) => console.error("Error generating response:", error));
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1757b36 and 24412c0.

Files selected for processing (20)
  • packages/editor/src/ce/extensions/index.ts (1 hunks)
  • packages/editor/src/core/components/editors/document/editor.tsx (3 hunks)
  • packages/editor/src/core/components/editors/document/page-renderer.tsx (3 hunks)
  • packages/editor/src/core/components/menus/ai-menu.tsx (1 hunks)
  • packages/editor/src/core/components/menus/index.ts (1 hunks)
  • packages/editor/src/core/extensions/side-menu.tsx (3 hunks)
  • packages/editor/src/core/hooks/use-editor.ts (2 hunks)
  • packages/editor/src/core/plugins/ai-handle.ts (1 hunks)
  • packages/editor/src/core/types/ai.ts (1 hunks)
  • packages/editor/src/core/types/editor.ts (1 hunks)
  • packages/editor/src/core/types/index.ts (1 hunks)
  • packages/editor/src/styles/drag-drop.css (3 hunks)
  • web/ce/components/pages/editor/ai/ask-pi-menu.tsx (1 hunks)
  • web/ce/components/pages/editor/ai/index.ts (1 hunks)
  • web/ce/components/pages/editor/ai/menu.tsx (1 hunks)
  • web/ce/components/pages/editor/index.ts (1 hunks)
  • web/ce/constants/ai.ts (1 hunks)
  • web/ce/hooks/use-editor-flagging.ts (1 hunks)
  • web/core/components/pages/editor/editor-body.tsx (2 hunks)
  • web/core/services/ai.service.ts (2 hunks)
Files skipped from review due to trivial changes (2)
  • packages/editor/src/core/components/menus/index.ts
  • web/ce/components/pages/editor/ai/index.ts
Additional comments not posted (32)
packages/editor/src/ce/extensions/index.ts (1)

1-1: Verify the impact of the removed export for ./ai-features.

The export for ./ai-features has been removed. Ensure that this change does not break any dependencies in the codebase that rely on these exports.

Verification successful

Let's correct the regex pattern and search again for any references to ./ai-features to ensure that its removal does not break dependencies.


Let's adjust the search command to look for references to ./ai-features without specifying a file type.


No dependencies on ./ai-features found. The removal of the export for ./ai-features does not affect any dependencies in the codebase, as there are no remaining references to it in the .ts files.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of `./ai-features` to ensure removal does not break dependencies.

# Test: Search for any remaining references to `./ai-features`. Expect: No references.
rg --type typescript 'from\s+"\.\/ai-features"'

Length of output: 150


Script:

#!/bin/bash
# Description: Verify usage of `./ai-features` to ensure removal does not break dependencies.

# Corrected Test: Search for any remaining references to `./ai-features`. Expect: No references.
rg --type typescript 'from\s+"\./ai-features"'

Length of output: 83


Script:

#!/bin/bash
# Description: Verify usage of `./ai-features` to ensure removal does not break dependencies.

# Adjusted Test: Search for any remaining references to `./ai-features` in .ts files. Expect: No references.
rg 'from\s+"\./ai-features"' --glob '*.ts'

Length of output: 44

web/ce/components/pages/editor/index.ts (1)

1-1: LGTM! Verify the integration completeness of AI features.

The addition of export * from "./ai"; appears to be part of an effort to centralize AI-related functionality. Ensure that all necessary components and dependencies are correctly integrated.

Would you like to verify the integration of AI features in the project?

packages/editor/src/core/types/ai.ts (1)

1-7: Type definitions look good! Verify their usage in the codebase.

The new type definitions for TMenuProps and TAIHandler are clear and well-structured. Ensure that these types are correctly utilized throughout the codebase to support the AI feature integration.

Would you like to verify the usage of these types in the project?

web/ce/constants/ai.ts (1)

1-3: Enum definition is clear and correct.

The AI_EDITOR_TASKS enum is well-defined, making it easy to extend with additional tasks in the future.

packages/editor/src/core/types/index.ts (1)

1-1: New export statement is appropriate.

The addition of export * from "./ai"; is correct and expands the module's interface to include AI-related types, which aligns with the PR's objectives.

web/ce/hooks/use-editor-flagging.ts (1)

11-12: Verify alignment with intended functionality.

The change to include "ai" in both documentEditor and richTextEditor suggests AI features are enabled. Ensure this aligns with the overall design and does not interfere with existing functionalities.

web/core/services/ai.service.ts (1)

12-17: Type definition TTaskPayload is well-structured.

The definition of TTaskPayload is clear and effectively structures the data payload for AI-related tasks.

packages/editor/src/core/types/editor.ts (2)

23-23: Method getSelectedText added to EditorRefApi.

The addition of getSelectedText is appropriate for retrieving selected text. Ensure its implementation correctly handles cases where no text is selected.


24-24: Method insertText added to EditorRefApi.

The addition of insertText is suitable for inserting HTML content. Ensure its implementation correctly handles the insertOnNextLine parameter.

packages/editor/src/styles/drag-drop.css (2)

6-6: Opacity setting for #editor-side-menu is appropriate.

The opacity value is set to 1, ensuring the element is fully visible.


32-39: New CSS rule for #ai-handle is well-implemented.

The rule ensures the AI handle is visible and interactive when needed, and hidden and non-interactive otherwise.

packages/editor/src/core/components/editors/document/editor.tsx (2)

24-24: Optional aiHandler addition is well-implemented.

The addition of aiHandler as an optional property in the IDocumentEditor interface maintains backward compatibility and enhances the component's functionality.


46-46: Integration of aiHandler is seamless.

The aiHandler is correctly destructured from props and passed to PageRenderer, ensuring optional usage without breaking existing functionality.

Also applies to: 90-90

packages/editor/src/core/components/menus/ai-menu.tsx (2)

20-44: Ensure proper setup and teardown of tippy.js.

The useEffect correctly initializes tippy.js and cleans up on unmount. Ensure that the getReferenceClientRect is correctly assigned when popup.current is set.


51-80: Efficient event handling in AIFeaturesMenu.

The component efficiently manages event listeners for clicks and keydown events. Ensure that the handleClickAIHandle function correctly identifies the target elements.

web/ce/components/pages/editor/ai/ask-pi-menu.tsx (1)

17-102: Well-structured AskPiMenu component.

The component effectively manages user input and AI responses, with clear UI interactions and state management. Ensure that the handleInsertText and handleRegenerate functions are correctly implemented and integrated.

packages/editor/src/core/components/editors/document/page-renderer.tsx (4)

Line range hint 23-29:
Review the addition of aiHandler to IPageRenderer.

The aiHandler property is optional and allows integration of AI features. Ensure that any usage of this property accounts for its optional nature.


32-32: Destructure aiHandler safely.

The destructuring of aiHandler from props is correct. Ensure that its usage in the component considers that it might be undefined.


142-147: Conditional rendering of AIFeaturesMenu.

The AIFeaturesMenu is conditionally rendered based on editor.isEditable and the presence of aiHandler. This logic is sound; however, ensure that AIFeaturesMenu handles cases where aiHandler is undefined.


18-20: Ensure all necessary imports are included.

The imports for AIFeaturesMenu and TAIHandler are relevant to the new AI functionality. Ensure that these components and types are correctly implemented and available.

Verification successful

Imports are correctly implemented and available.

The AIFeaturesMenu component and TAIHandler type are properly defined and imported in the codebase. No issues found with these imports.

  • AIFeaturesMenu is defined in packages/editor/src/core/components/menus/ai-menu.tsx.
  • TAIHandler is defined in packages/editor/src/core/types/ai.ts.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence of the `AIFeaturesMenu` component and `TAIHandler` type.

# Test: Search for the `AIFeaturesMenu` component definition.
ast-grep --lang typescript --pattern 'export const AIFeaturesMenu = $_' 

# Test: Search for the `TAIHandler` type definition.
ast-grep --lang typescript --pattern 'export type TAIHandler = $_'

Length of output: 338


Script:

#!/bin/bash
# Search for any definition or export of `AIFeaturesMenu` in the codebase.
rg 'AIFeaturesMenu' --type=typescript --context 5

Length of output: 84


Script:

#!/bin/bash
# Correcting the file type and searching for any definition or export of `AIFeaturesMenu` in the codebase.
rg 'AIFeaturesMenu' --type ts --context 5

Length of output: 3260

packages/editor/src/core/extensions/side-menu.tsx (4)

108-108: Destructure domEvents from AIHandlePlugin.

The destructuring of domEvents allows handling additional events. Ensure that these events are correctly implemented and used.


119-121: Conditional invocation of aiHandleView and dragHandleView.

The conditional logic ensures that plugins are only invoked if their respective configurations are enabled. This enhances modularity and is correctly implemented.


178-183: Handle mouse events based on configuration.

The handling of mousemove events is correctly tied to the configuration. Ensure that the event handlers are implemented and perform as expected.


4-5: Ensure correct import paths for plugins.

The import path for AIHandlePlugin has been updated. Verify that the path is correct and aligns with the project's structure.

Verification successful

Import Path for AIHandlePlugin is Correct

The import path for AIHandlePlugin aligns with the project's alias configuration in tsconfig.json. The alias @ correctly maps to the src/core directory, ensuring the import path "@/plugins/ai-handle" is valid. No further action is required.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the import path for `AIHandlePlugin`.

# Test: Search for the `AIHandlePlugin` definition.
ast-grep --lang typescript --pattern 'export const AIHandlePlugin = $_' 

Length of output: 6553


Script:

#!/bin/bash
# Check if the alias `@` is defined in tsconfig.json or webpack.config.js

# Search for alias configuration in tsconfig.json
rg '"paths"' tsconfig.json -A 5

# Search for alias configuration in webpack.config.js
rg 'alias' webpack.config.js -A 5

Length of output: 179


Script:

#!/bin/bash
# Use fd to find potential configuration files that might define path aliases
fd -e json -e js -e ts -e mjs -e cjs -e rc

# Search for alias configurations in the found configuration files
for file in $(fd -e json -e js -e ts -e mjs -e cjs -e rc); do
  echo "Searching in $file"
  rg 'alias|paths' "$file" -A 5
done

Length of output: 261355

packages/editor/src/core/plugins/ai-handle.ts (4)

6-7: Review SVG icon usage.

The sparklesIcon SVG is defined as a string. Ensure that this icon is used appropriately and does not introduce any performance issues.


9-42: Check nodeDOMAtCoords function for accuracy.

The nodeDOMAtCoords function retrieves DOM elements based on coordinates. Ensure that the logic correctly identifies the intended elements and handles edge cases.


78-153: Review AIHandlePlugin implementation.

The AIHandlePlugin is implemented to add AI-related functionality. Ensure that event handling and DOM manipulations are correctly implemented and consider potential edge cases.


1-4: Ensure correct imports and usage.

The imports are relevant to the functionality provided by the AIHandlePlugin. Verify that these modules are correctly implemented and available.

web/core/components/pages/editor/editor-body.tsx (1)

160-162: Integration of AI handler looks good.

The aiHandler prop is correctly integrated into DocumentEditorWithRef, enhancing the editor with AI-driven interactions.

packages/editor/src/core/hooks/use-editor.ts (1)

239-251: Text insertion logic is well-implemented.

The insertText function correctly handles both replacing selected text and appending new text on a new line.

web/ce/components/pages/editor/ai/menu.tsx (2)

74-85: Task selection logic is well-implemented.

The handleClick function correctly manages task selection and initiates response generation.


123-127: AI text insertion logic is correct.

The handleInsertText function correctly inserts AI-generated text into the editor.

Comment on lines +5 to +9
export const LOADING_TEXTS: {
[key in AI_EDITOR_TASKS]: string;
} = {
[AI_EDITOR_TASKS.ASK_ANYTHING]: "Pi is generating response",
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider extensibility for LOADING_TEXTS.

While the current mapping is correct, ensure that the logic handling these texts can accommodate future additions to AI_EDITOR_TASKS without requiring changes in multiple places.

// Suggestion: Consider using a function to retrieve loading texts, allowing for defaults.
export const getLoadingText = (task: AI_EDITOR_TASKS): string => {
  const texts = {
    [AI_EDITOR_TASKS.ASK_ANYTHING]: "Pi is generating response",
    // Add more tasks here
  };
  return texts[task] || "Loading...";
};

@SatishGandham SatishGandham merged commit a36adae into preview Aug 16, 2024
@SatishGandham SatishGandham deleted the dev/pages-ai-menu branch August 16, 2024 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants