Skip to content

Comments

feat: drag-and-drop functionality for category reordering in settings#1398

Merged
MrgSub merged 3 commits intoMail-0:stagingfrom
retrogtx:draggable-category
Jun 27, 2025
Merged

feat: drag-and-drop functionality for category reordering in settings#1398
MrgSub merged 3 commits intoMail-0:stagingfrom
retrogtx:draggable-category

Conversation

@retrogtx
Copy link
Contributor

@retrogtx retrogtx commented Jun 20, 2025

Before:
image

After:

image

Summary by CodeRabbit

  • New Features

    • Introduced drag-and-drop functionality for reordering mail categories in settings.
    • Added interactive drag handles for easier category management.
  • Improvements

    • Updated UI to reflect drag-and-drop reordering and removed manual order input.
    • Enhanced default category selection to ensure only one default can be set at a time.
    • Improved category item layout and usability.

@retrogtx retrogtx marked this pull request as draft June 20, 2025 07:37
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 20, 2025

Walkthrough

The mail categories settings page is refactored to support drag-and-drop reordering using the @dnd-kit library. A memoized SortableCategoryItem component is introduced for rendering each category, and the page's state management is updated to handle reordering and default toggling through new callbacks and UI changes.

Changes

File(s) Change Summary
apps/mail/app/(routes)/settings/categories/page.tsx Refactored to use drag-and-drop for category reordering, added SortableCategoryItem component, removed manual order input, updated state and save logic.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CategoriesSettingsPage
    participant SortableCategoryItem

    User->>CategoriesSettingsPage: Drag category item
    CategoriesSettingsPage->>CategoriesSettingsPage: handleDragEnd (update order)
    User->>SortableCategoryItem: Toggle default switch
    SortableCategoryItem->>CategoriesSettingsPage: toggleDefault (update default)
    User->>CategoriesSettingsPage: Click Save
    CategoriesSettingsPage->>CategoriesSettingsPage: Validate and save categories
Loading

Possibly related PRs

  • feat: ability to customize categories #1328: The main PR builds directly on the earlier PR by replacing the manual category order input and static ordering logic introduced in the retrieved PR with a drag-and-drop reordering UI and updated state management for categories in the same CategoriesSettingsPage component file.

Suggested reviewers

  • MrgSub

Poem

🐰
In the garden of mail, categories play,
Now they can dance and hop, not just stay.
With drag-and-drop, the order is new,
A bunny's delight—so easy to do!
Default toggles shine,
The settings refined,
Hooray for a UI that’s clever and kind!

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

@retrogtx retrogtx marked this pull request as ready for review June 25, 2025 07:16
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)
apps/mail/app/(routes)/settings/categories/page.tsx (1)

291-294: Remove redundant order reassignment.

The handleDragEnd function already updates the order property when categories are reordered. This reassignment is redundant.

Apply this diff to simplify the code:

-    const sortedCategories = categories.map((cat, index) => ({
-      ...cat,
-      order: index,
-    }));

    try {
-      await saveUserSettings({ categories: sortedCategories });
+      await saveUserSettings({ categories });
      queryClient.setQueryData(trpc.settings.get.queryKey(), (updater: any) => {
        if (!updater) return;
        return {
-          settings: { ...updater.settings, categories: sortedCategories },
+          settings: { ...updater.settings, categories },
        };
      });
-      setCategories(sortedCategories);
      toast.success('Categories saved');
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b56b6f8 and fdf97cd.

📒 Files selected for processing (1)
  • apps/mail/app/(routes)/settings/categories/page.tsx (5 hunks)
🔇 Additional comments (1)
apps/mail/app/(routes)/settings/categories/page.tsx (1)

228-235: Well-configured drag sensors!

Good implementation of both pointer and keyboard sensors. The 8-pixel activation constraint helps prevent accidental drags when interacting with form elements.

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

♻️ Duplicate comments (1)
apps/mail/app/(routes)/settings/categories/page.tsx (1)

206-214: Fix incomplete memo equality check to prevent stale closures.

The custom equality check only compares a subset of props, omitting handler functions. This could lead to stale closures if these functions change.

Apply this diff to include all props in the equality check:

}, (prev, next) => {
  // Custom equality check to avoid unnecessary re-renders
  return (
    prev.cat === next.cat &&
    prev.isActiveAi === next.isActiveAi &&
    prev.promptValue === next.promptValue &&
-    prev.isGeneratingQuery === next.isGeneratingQuery
+    prev.isGeneratingQuery === next.isGeneratingQuery &&
+    prev.setPromptValue === next.setPromptValue &&
+    prev.setActiveAiCat === next.setActiveAiCat &&
+    prev.generateSearchQuery === next.generateSearchQuery &&
+    prev.handleFieldChange === next.handleFieldChange &&
+    prev.toggleDefault === next.toggleDefault
  );
});

Alternatively, consider removing the custom equality function entirely to use React's default shallow comparison.

🧹 Nitpick comments (1)
apps/mail/app/(routes)/settings/categories/page.tsx (1)

353-355: Optimize callback creation to prevent unnecessary re-renders.

The setPromptValue callback is recreated on every render, which may cause unnecessary re-renders of the memoized SortableCategoryItem components.

Consider memoizing this callback or moving it to a separate function:

+  const handlePromptValueChange = useCallback((catId: string, val: string) => {
+    setPromptValues((prev) => ({ ...prev, [catId]: val }));
+  }, []);

  // In the render section:
-                  setPromptValue={(val) =>
-                    setPromptValues((prev) => ({ ...prev, [cat.id]: val }))
-                  }
+                  setPromptValue={(val) => handlePromptValueChange(cat.id, val)}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between fdf97cd and 2bcb20d.

📒 Files selected for processing (1)
  • apps/mail/app/(routes)/settings/categories/page.tsx (5 hunks)
🔇 Additional comments (7)
apps/mail/app/(routes)/settings/categories/page.tsx (7)

8-53: LGTM: Clean imports and well-defined interface.

The drag-and-drop imports are properly organized and the SortableCategoryItemProps interface is comprehensive with appropriate TypeScript typing.


236-243: LGTM: Well-configured drag-and-drop sensors with accessibility support.

The sensor configuration follows best practices with proper activation constraints and keyboard accessibility support.


245-252: LGTM: Well-implemented toggle function with proper memoization.

The toggleDefault function correctly ensures only one category can be default at a time and is properly memoized.


273-291: LGTM: Solid drag-and-drop reordering implementation.

The handleDragEnd function properly handles reordering with arrayMove and updates order indices correctly. The early return for invalid drop targets is good defensive programming.


293-318: LGTM: Simplified save logic appropriate for drag-and-drop interface.

The removal of manual order validation is correct since order is now managed through drag-and-drop. The function properly ensures exactly one default category exists.


328-328: LGTM: Updated description accurately reflects new functionality.

The description change properly informs users about the drag-and-drop reordering capability.


338-364: LGTM: Proper drag-and-drop context implementation.

The DndContext and SortableContext are correctly configured with appropriate sensors, collision detection, and sorting strategy.

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 (1)
apps/mail/app/(routes)/settings/categories/page.tsx (1)

55-206: Solid component implementation with good drag-and-drop integration.

The SortableCategoryItem component properly implements the drag-and-drop functionality and consolidates the previously inline UI logic. The use of React.memo without a custom equality function correctly addresses the previous review concern about stale closures.

Consider breaking this component down into smaller sub-components (e.g., CategoryHeader, CategoryFields, AIQueryPopover) for better maintainability and testing, but the current implementation is functional and well-structured.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2bcb20d and a66d703.

📒 Files selected for processing (1)
  • apps/mail/app/(routes)/settings/categories/page.tsx (5 hunks)
🔇 Additional comments (7)
apps/mail/app/(routes)/settings/categories/page.tsx (7)

8-8: LGTM! Comprehensive imports for drag-and-drop functionality.

The imports are well-organized and include all necessary components from the @dnd-kit library for implementing drag-and-drop reordering.

Also applies to: 15-15, 24-42


43-53: Well-defined interface with comprehensive prop types.

The interface properly types all props needed for the SortableCategoryItem component, including state management and callback functions.


228-235: Excellent sensors configuration for accessibility and UX.

The sensors setup properly prevents accidental drags with the distance constraint and includes keyboard navigation support for accessibility compliance.


237-244: Proper implementation of default category toggling.

The function is correctly memoized and enforces the business rule that only one category can be default at a time.


265-283: Robust drag-and-drop reordering implementation.

The function properly handles drag events, uses the arrayMove utility correctly, and maintains data consistency by reassigning sequential order indices after reordering.


285-310: Appropriate simplification of save logic for drag-and-drop.

The function correctly removes manual order validation since drag-and-drop now handles ordering, while maintaining the important business rule validation for default categories.


320-320: Clean integration of drag-and-drop context and rendering.

The DndContext is properly configured with appropriate sensors, collision detection, and sorting strategy. The rendering refactor using SortableCategoryItem components improves code organization, and the description update accurately reflects the new drag-and-drop functionality.

Also applies to: 330-356

@MrgSub MrgSub merged commit 4e2ae18 into Mail-0:staging Jun 27, 2025
3 checks passed
This was referenced Jun 27, 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.

2 participants