Skip to content

Comments

refactor: perf improvements to opening emails, tanstack query caching…#1694

Merged
MrgSub merged 2 commits intoMail-0:stagingfrom
izadoesdev:perf/open-email
Jul 9, 2025
Merged

refactor: perf improvements to opening emails, tanstack query caching…#1694
MrgSub merged 2 commits intoMail-0:stagingfrom
izadoesdev:perf/open-email

Conversation

@izadoesdev
Copy link
Contributor

@izadoesdev izadoesdev commented Jul 9, 2025

… improvements, callbacks where needed in optimistic actions hook, some formatting

Description

Please provide a clear description of your changes.


Type of Change

Please delete options that are not relevant.

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature with breaking changes)
  • 📝 Documentation update
  • 🎨 UI/UX improvement
  • 🔒 Security enhancement
  • ⚡ Performance improvement

Areas Affected

Please check all that apply:

  • Email Integration (Gmail, IMAP, etc.)
  • User Interface/Experience
  • Authentication/Authorization
  • Data Storage/Management
  • API Endpoints
  • Documentation
  • Testing Infrastructure
  • Development Workflow
  • Deployment/Infrastructure

Testing Done

Describe the tests you've done:

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing performed
  • Cross-browser testing (if UI changes)
  • Mobile responsiveness verified (if UI changes)

Security Considerations

For changes involving data or authentication:

  • No sensitive data is exposed
  • Authentication checks are in place
  • Input validation is implemented
  • Rate limiting is considered (if applicable)

Checklist

  • I have read the CONTRIBUTING document
  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in complex areas
  • I have updated the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix/feature works
  • All tests pass locally
  • Any dependent changes are merged and published

Additional Notes

Add any other context about the pull request here.

Screenshots/Recordings

Add screenshots or recordings here if applicable.


By submitting this pull request, I confirm that my contribution is made under the terms of the project's license.

Summary by CodeRabbit

  • Bug Fixes

    • Improved performance and consistency by disabling automatic refetching of thread notes and summary data when mounting, refocusing, or reconnecting.
    • Adjusted unsubscribe menu display to show only when specific unsubscribe data is available.
  • Refactor

    • Optimized state updates and improved reliability in mail actions such as marking as read, starring, and marking as important.
    • Enhanced code readability and maintainability through formatting and minor whitespace adjustments.
    • Simplified mail thread handling and mail list interactions for smoother user experience.

… improvements, callbacks where needed in optimistic actions hook, some formatting
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 9, 2025

Walkthrough

This update refactors several mail-related components and hooks. It removes unnecessary await keywords, simplifies template string formatting, narrows a conditional check for unsubscribe rendering, and converts optimistic action functions into useCallback hooks with proper dependencies. Additionally, it adjusts query options in hooks to reduce automatic refetching and extend cache duration.

Changes

File(s) Change Summary
apps/mail/components/mail/mail-list.tsx Removed unnecessary await keywords, adjusted whitespace, and reformatted destructuring and className concatenation.
apps/mail/components/mail/thread-display.tsx Simplified template string formatting in printThread and narrowed unsubscribe menu condition.
apps/mail/hooks/use-notes.tsx
apps/mail/hooks/use-summary.ts
Disabled automatic refetching (on mount, focus, reconnect) and set longer cache duration in query options.
apps/mail/hooks/use-optimistic-actions.ts Refactored optimistic action functions into useCallback hooks with dependency arrays and functional state updates.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MailList
    participant State
    participant OptimisticActions

    User->>MailList: Clicks on mail thread
    MailList->>State: setThreadId / setDraftId (sync, no await)
    MailList->>OptimisticActions: (if needed) optimisticMarkAsRead (via useCallback)
    OptimisticActions->>State: setMail (functional update)
Loading

Possibly related PRs

  • Fix/email printing #1529: Also modifies printThread in thread-display.tsx, but focuses on HTML sanitization rather than formatting and conditional rendering.
  • hotfix #1207: Refactors optimistic action functions into useCallback hooks and updates state patterns, related to this PR’s similar refactor in use-optimistic-actions.ts.
  • Optimistic Fixes #1231: Refactors and changes handleMailClick and optimistic actions in mail-list.tsx, overlapping with this PR’s removal of unnecessary awaits and callback adjustments.

Suggested reviewers

  • MrgSub

Poem

In the garden of mail, where threads entwine,
We pruned the awaits and let the hooks shine.
With callbacks that sparkle and queries that rest,
Our code hops forward, feeling its best.
🐇✨
Refreshed and tidy, the mailbox anew—
A rabbit’s salute to the work we do!


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 33ed2da and c654f60.

📒 Files selected for processing (1)
  • apps/mail/components/mail/mail-list.tsx (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • apps/mail/components/mail/mail-list.tsx
✨ 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.

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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 16c3f95 and 33ed2da.

📒 Files selected for processing (5)
  • apps/mail/components/mail/mail-list.tsx (3 hunks)
  • apps/mail/components/mail/thread-display.tsx (4 hunks)
  • apps/mail/hooks/use-notes.tsx (1 hunks)
  • apps/mail/hooks/use-optimistic-actions.ts (3 hunks)
  • apps/mail/hooks/use-summary.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: retrogtx
PR: Mail-0/Zero#1328
File: apps/mail/lib/hotkeys/mail-list-hotkeys.tsx:202-209
Timestamp: 2025-06-18T17:26:50.918Z
Learning: In apps/mail/lib/hotkeys/mail-list-hotkeys.tsx, the switchCategoryByIndex function using hardcoded indices for category hotkeys does not break when users reorder categories, contrary to the theoretical index-shifting issue. The actual implementation has constraints or mechanisms that prevent hotkey targeting issues.
apps/mail/components/mail/mail-list.tsx (4)
Learnt from: retrogtx
PR: Mail-0/Zero#1328
File: apps/mail/lib/hotkeys/mail-list-hotkeys.tsx:202-209
Timestamp: 2025-06-18T17:26:50.918Z
Learning: In apps/mail/lib/hotkeys/mail-list-hotkeys.tsx, the switchCategoryByIndex function using hardcoded indices for category hotkeys does not break when users reorder categories, contrary to the theoretical index-shifting issue. The actual implementation has constraints or mechanisms that prevent hotkey targeting issues.
Learnt from: retrogtx
PR: Mail-0/Zero#1468
File: apps/server/src/trpc/routes/mail.ts:331-331
Timestamp: 2025-06-28T03:56:09.376Z
Learning: In apps/server/src/trpc/routes/mail.ts, the user indicated they are not using ISO format for the scheduleAt parameter, despite the frontend code showing toISOString() usage in the ScheduleSendPicker component.
Learnt from: retrogtx
PR: Mail-0/Zero#1468
File: apps/server/src/trpc/routes/mail.ts:386-391
Timestamp: 2025-06-27T04:59:29.731Z
Learning: In apps/server/src/trpc/routes/mail.ts, the attachment processing logic conditionally handles mixed attachment types - it preserves existing File-like objects with arrayBuffer methods while only converting serialized attachments that need processing through toAttachmentFiles.
Learnt from: danteissaias
PR: Mail-0/Zero#618
File: apps/mail/components/mail/mail-iframe.tsx:12-12
Timestamp: 2025-04-07T20:46:11.697Z
Learning: In the Mail-0/Zero application, sender emails are guaranteed to be non-empty when passed to components that handle them, making additional empty string validation unnecessary.
apps/mail/components/mail/thread-display.tsx (6)
Learnt from: retrogtx
PR: Mail-0/Zero#1328
File: apps/mail/lib/hotkeys/mail-list-hotkeys.tsx:202-209
Timestamp: 2025-06-18T17:26:50.918Z
Learning: In apps/mail/lib/hotkeys/mail-list-hotkeys.tsx, the switchCategoryByIndex function using hardcoded indices for category hotkeys does not break when users reorder categories, contrary to the theoretical index-shifting issue. The actual implementation has constraints or mechanisms that prevent hotkey targeting issues.
Learnt from: retrogtx
PR: Mail-0/Zero#1468
File: apps/server/src/trpc/routes/mail.ts:386-391
Timestamp: 2025-06-27T04:59:29.731Z
Learning: In apps/server/src/trpc/routes/mail.ts, the attachment processing logic conditionally handles mixed attachment types - it preserves existing File-like objects with arrayBuffer methods while only converting serialized attachments that need processing through toAttachmentFiles.
Learnt from: snehendu098
PR: Mail-0/Zero#1323
File: apps/mail/lib/themes/theme-utils.ts:318-318
Timestamp: 2025-06-24T06:22:58.753Z
Learning: In the Mail-0/Zero theme system (apps/mail/lib/themes/theme-utils.ts), when color themes are being applied, all color values come in HSL format, so there's no need for additional format validation when converting colors with hslToHex().
Learnt from: danteissaias
PR: Mail-0/Zero#618
File: apps/mail/components/mail/mail-iframe.tsx:12-12
Timestamp: 2025-04-07T20:46:11.697Z
Learning: In the Mail-0/Zero application, sender emails are guaranteed to be non-empty when passed to components that handle them, making additional empty string validation unnecessary.
Learnt from: retrogtx
PR: Mail-0/Zero#1468
File: apps/server/src/trpc/routes/mail.ts:331-331
Timestamp: 2025-06-28T03:56:09.376Z
Learning: In apps/server/src/trpc/routes/mail.ts, the user indicated they are not using ISO format for the scheduleAt parameter, despite the frontend code showing toISOString() usage in the ScheduleSendPicker component.
Learnt from: danteissaias
PR: Mail-0/Zero#458
File: apps/mail/lib/email-utils.ts:91-134
Timestamp: 2025-03-16T23:15:24.659Z
Learning: According to RFC 2369, when processing list-unsubscribe headers with multiple URLs, the client should use the leftmost protocol that it supports (process URLs from left to right).
apps/mail/hooks/use-optimistic-actions.ts (1)
Learnt from: retrogtx
PR: Mail-0/Zero#1328
File: apps/mail/lib/hotkeys/mail-list-hotkeys.tsx:202-209
Timestamp: 2025-06-18T17:26:50.918Z
Learning: In apps/mail/lib/hotkeys/mail-list-hotkeys.tsx, the switchCategoryByIndex function using hardcoded indices for category hotkeys does not break when users reorder categories, contrary to the theoretical index-shifting issue. The actual implementation has constraints or mechanisms that prevent hotkey targeting issues.
🧬 Code Graph Analysis (3)
apps/mail/components/mail/mail-list.tsx (1)
apps/mail/hooks/use-threads.ts (1)
  • useThreads (13-61)
apps/mail/components/mail/thread-display.tsx (1)
apps/mail/lib/utils.ts (1)
  • formatFileSize (224-230)
apps/mail/hooks/use-optimistic-actions.ts (2)
apps/server/src/routes/chat.ts (2)
  • markAsRead (242-246)
  • markAsRead (786-791)
apps/server/src/lib/driver/google.ts (1)
  • markAsRead (115-134)
🪛 Biome (1.9.4)
apps/mail/components/mail/mail-list.tsx

[error] 73-73: Unexpected empty object pattern.

(lint/correctness/noEmptyPattern)

🔇 Additional comments (9)
apps/mail/hooks/use-summary.ts (1)

20-27: Excellent caching optimization!

The addition of a 1-hour stale time and disabling automatic refetching behaviors will significantly reduce unnecessary network requests and improve performance. This aligns perfectly with the PR's goal of TanStack Query caching improvements.

apps/mail/hooks/use-notes.tsx (1)

19-21: Consistent caching improvements applied.

The disabled refetching behaviors are consistent with the broader caching optimization effort across the application. This will prevent unnecessary data fetching and improve user experience.

apps/mail/components/mail/mail-list.tsx (1)

852-853: Correct removal of unnecessary await keywords.

The removal of await keywords from setThreadId and setDraftId calls is correct since these are synchronous operations from useQueryState. This cleanup improves code clarity and performance.

apps/mail/components/mail/thread-display.tsx (2)

517-638: Clean template string formatting.

The simplified formatting of the template strings improves code readability while maintaining the same functionality. The compact .map() calls are easier to follow.


982-983: Ignore outdated unsubscribe condition suggestion

The current code in apps/mail/components/mail/thread-display.tsx still correctly shows the unsubscribe option when either listUnsubscribe or listUnsubscribePost is present:

{emailData.latest?.listUnsubscribe ||
 emailData.latest?.listUnsubscribePost ? (
   <DropdownMenuItem onClick={handleUnsubscribeProcess}></DropdownMenuItem>
) : null}

No narrowing to only listUnsubscribePost has been applied—both headers remain supported per RFC 2369. You can disregard the original review comment.

Likely an incorrect or invalid review comment.

apps/mail/hooks/use-optimistic-actions.ts (4)

185-214: Excellent useCallback optimization for optimisticMarkAsRead.

The conversion to useCallback with proper dependencies and functional state updates will prevent unnecessary re-renders and ensure the latest state is used. The dependency array correctly includes all external values used within the callback.


244-271: Proper useCallback implementation for optimisticToggleStar.

The memoization and dependency management follow React best practices. The function will only be recreated when its dependencies change, improving performance.


382-411: Consistent useCallback pattern for optimisticToggleImportant.

The implementation maintains consistency with the other optimistic action functions while properly handling dependencies and state updates.


204-204: Correct functional state updates.

Using setMail((prev) => ({ ...prev, bulkSelected: [] })) instead of setMail({ ...mail, bulkSelected: [] }) ensures the latest state is used, preventing potential race conditions in concurrent updates.

Also applies to: 401-401

@MrgSub MrgSub merged commit 7cc9838 into Mail-0:staging Jul 9, 2025
4 checks passed
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