Skip to content
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

refactor(frontend): update sidebar components to use useRouter for na… #184

Merged
merged 2 commits into from
Mar 16, 2025

Conversation

Sma1lboy
Copy link
Collaborator

@Sma1lboy Sma1lboy commented Mar 15, 2025

https://jam.dev/c/c3718687-e412-4189-a082-3f3a6687c6fb

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Streamlined sidebar navigation: deleting a chat now smoothly redirects you to the home view.
    • Enhanced chat selection behavior for more intuitive and responsive transitions.
    • Added functionality to navigate to a new URL after successfully creating a project.
  • Refactor

    • Refined sidebar interactions to ensure consistent and seamless updates during chat management.
    • Improved type safety and internal logic within the ChatRow component.

Copy link
Contributor

coderabbitai bot commented Mar 15, 2025

Walkthrough

The changes streamline navigation and type safety across sidebar components. In sidebar-item.tsx, the direct manipulation of browser history is replaced with Next.js’s router.push for routing after a chat deletion. In sidebar.tsx, type definitions are improved by changing error to unknown, adding a new onRefetch function, and introducing ChatRowData and ChatRowProps interfaces. Internal logic for chat selection and new chat handling has been refactored to invoke navigation and event dispatching directly. Additionally, modifications in page.tsx and project-context.tsx enhance project creation flow and return values.

Changes

File(s) Changed Summary
frontend/src/components/sidebar-item.tsx - Introduced the Next.js useRouter hook.
- Replaced window.history.replaceState with router.push('/') in the onCompleted callback for chat deletion.
- Removed legacy history update and event dispatch logic from the handleChatClick function.
frontend/src/components/sidebar.tsx - Updated SidebarProps: changed error: any to error: unknown and added onRefetch: () => void.
- Added ChatRowData and ChatRowProps interfaces.
- Refactored ChatRow to use the new interfaces and streamlined selection by directly invoking onSelect.
- Modified handleNewChat to use router.push('/') and enhanced chat selection logic with additional event dispatch and memoization updates.
frontend/src/app/(main)/page.tsx - Updated handleSubmit in HomePage to store chatId from createProjectFromPrompt and navigate to a new URL with this ID using router.push.
frontend/src/components/chat/code-engine/project-context.tsx - Updated projectLoading comment in ProjectContextType interface.
- Changed createProjectFromPrompt to return the project ID instead of a boolean, with a console log for the result.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant SidebarItem
  participant DeleteChatMutation
  participant Router

  User->>SidebarItem: Initiate chat deletion
  SidebarItem->>DeleteChatMutation: Execute deleteChat mutation
  DeleteChatMutation-->>SidebarItem: onCompleted callback received
  SidebarItem->>Router: router.push('/')
  Router-->>User: Navigate to home page
Loading
sequenceDiagram
  participant User
  participant ChatSideBar
  participant Router

  User->>ChatSideBar: Click chat row or trigger new chat
  ChatSideBar->>ChatSideBar: Process selection (invoke onSelect/handleNewChat)
  ChatSideBar->>Router: router.push('/')
  Router-->>User: Update UI for chat selection
Loading

Possibly related PRs

  • Refactor fd structure #135: The changes in the main PR are related to the modifications in the sidebar.tsx file, specifically the use of the router.push method for navigation, which aligns with similar navigation logic updates in the `ChatSideBarComponent.
  • feat(frontend): css optimization in frontend #156: The changes in the main PR and the retrieved PR are related through the use of the router.push method for navigation within the handleNewChat function in the sidebar, which aligns with the navigation logic updates in the handleSubmit function of the HomePage component.
  • feat(frontend): fix sidebar need reload when first time load #150: The changes in the main PR and the retrieved PR are related through the use of the useRouter hook from Next.js, as both PRs modify navigation logic within components that utilize this hook.

Suggested reviewers

  • NarwhalChen
  • ZHallen122

Poem

I'm a rabbit with a code-filled heart,
Hopping through changes, a brand new start.
With router.push, I sprint and play,
Leaving old history far away.
Sidebars now swift, we bound and dart,
Celebrating changes with a joyful art!
🥕🐇

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

 ERR_PNPM_OPTIONAL_DEPS_REQUIRE_PROD_DEPS  Optional dependencies cannot be installed without production dependencies

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ Finishing Touches
  • 📝 Generate Docstrings

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

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

🧹 Nitpick comments (1)
frontend/src/components/chat/code-engine/project-context.tsx (1)

717-717: Remove console.log statement

This console.log statement contains a typo ("creatae" instead of "create") and appears to be debugging code that should be removed before merging to production.

-        console.log('creatae a project result:', result);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e7f227d and 6a1af86.

📒 Files selected for processing (2)
  • frontend/src/app/(main)/page.tsx (1 hunks)
  • frontend/src/components/chat/code-engine/project-context.tsx (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Install and Build Frontend
🔇 Additional comments (2)
frontend/src/app/(main)/page.tsx (1)

33-35: Good improvement to navigation flow

The modification correctly captures the chat ID returned from createProjectFromPrompt and uses it to navigate to the chat page. This creates a more seamless user experience by automatically directing users to their newly created project.

frontend/src/components/chat/code-engine/project-context.tsx (1)

31-31: LGTM - Comment removal

The removal of a comment from this property definition is appropriate as it maintains clean code.


return !!result.data?.createProject;
return result.data.id;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update function return type in interface definition

The function now returns result.data.id (a string) instead of a boolean, but the interface definition on line 39 still declares the return type as Promise<boolean>. This mismatch should be fixed to maintain type safety.

Apply this change to the interface definition:

  createProjectFromPrompt: (
    prompt: string,
    isPublic: boolean,
    model?: string
-  ) => Promise<boolean>;
+  ) => Promise<string>;

Additionally, the error case on line 725 should be updated to either:

- return false;
+ throw new Error('Failed to create project from prompt');

or

- return false;
+ return '';  // Return empty string for error case
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return result.data.id;
// ----- Interface Definition (around line 39) -----
interface IProjectContext {
createProjectFromPrompt: (
prompt: string,
isPublic: boolean,
model?: string
) => Promise<string>;
// ... other methods
}
// ----- Function Implementation (relevant portions) -----
async function createProjectFromPrompt(prompt: string, isPublic: boolean, model?: string): Promise<string> {
// ... function logic before error handling
// When an error is detected (previously returning a boolean false)
if (/* error condition */) {
- return false;
+ throw new Error('Failed to create project from prompt');
}
// Function now correctly returns a string ID
return result.data.id;
}

@Sma1lboy Sma1lboy merged commit 5a5bb35 into main Mar 16, 2025
3 of 4 checks passed
PengyuChen01 pushed a commit that referenced this pull request Mar 16, 2025
#184)

https://jam.dev/c/c3718687-e412-4189-a082-3f3a6687c6fb
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary by CodeRabbit

- **New Features**
- Streamlined sidebar navigation: deleting a chat now smoothly redirects
you to the home view.
- Enhanced chat selection behavior for more intuitive and responsive
transitions.
- Added functionality to navigate to a new URL after successfully
creating a project.

- **Refactor**
- Refined sidebar interactions to ensure consistent and seamless updates
during chat management.
- Improved type safety and internal logic within the ChatRow component.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@coderabbitai coderabbitai bot mentioned this pull request Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant