Zero icon indicating copy to clipboard operation
Zero copied to clipboard

feat: server-action/SWR to trpc/tanstack-query

Open BlankParticle opened this issue 7 months ago • 4 comments

Current Status: Unstable, needs testing

Known Issues

  • [x] ~~File uploads in send emails dont work yet~~

Tested basic stuff locally, but need a lot more testing, not suitable for merging to main until everything has been to tested for feature parity.

App might feel slow because the indexed db cache used in SWR is not ported to this version yet. We might need to think of a better caching mechanism as caching every request is too aggressive and cause way too many issues while using multiple connections due to the cache not being keyd properly.

These changes might be hard for everyone to adopt to as a contributor, but the experience using server actions and SWR without proper typesafety became frustrating to me and others, motivations and wins due to these changes has been already discussed on discord.

Due to these changes the backend is now decoupled from NextJS specific features like headers() and in future the backend can be seperated out if needed.

If you have any questions, please feel free to ask on discord.


Type of Change

Please delete options that are not relevant.

  • [x] ✨ New feature (non-breaking change which adds functionality)
  • [x] 💥 Breaking change (fix or feature with breaking changes)

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

Summary by CodeRabbit

  • Refactor
    • Replaced legacy data fetching and mutation logic throughout the app with a new architecture based on React Query and tRPC, improving consistency and performance.
    • Updated components and hooks to use the new query and mutation patterns, removing older REST API endpoints and SWR-based data flows.
  • New Features
    • Introduced advanced AI-powered email generation, reply, and search features accessible via the new tRPC API.
    • Added robust email template and invitation handling, including a new golden ticket email template.
  • Chores
    • Removed unused files, legacy API routes, and deprecated hooks.
    • Updated dependencies, adding React Query, tRPC, and related libraries, while removing obsolete packages.
  • Bug Fixes
    • Improved error handling and user feedback for mutations and queries.
  • Style
    • Standardized code formatting, import order, and string literal usage across the codebase.

BlankParticle avatar Apr 27 '25 05:04 BlankParticle

@BlankParticle is attempting to deploy a commit to the Zero Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Apr 27 '25 05:04 vercel[bot]

[!IMPORTANT]

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

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

This change migrates core backend and frontend data-fetching, mutation, and state management logic in the mail application from legacy REST API endpoints and SWR-based hooks to a unified tRPC and React Query architecture. Numerous server-side action files and API route handlers are deleted, with their functionality refactored into tRPC routers and procedures. Corresponding frontend hooks and components are updated to use React Query and tRPC hooks for data retrieval and mutations. Supporting modules are added or modified to provide new query providers, email templates, and utility functions. The update also removes obsolete or redundant code, such as SWR cache providers and direct REST fetchers, consolidating all data operations under the new architecture.

Changes

File(s) / Path(s) Change Summary
apps/mail/actions/ai-reply.ts, apps/mail/actions/ai.ts, apps/mail/actions/brain.ts, ... Deleted legacy server-side action files for AI, brain, connections, cookies, drafts, extractText, getSummary, mail, notes, send, settings, and user management. Their logic is refactored into tRPC routers and procedures.
apps/mail/app/api/[...all]/route.ts Introduced new Hono-based API route handler integrating authentication, tRPC, and custom endpoints, replacing many individual REST API routes.
apps/mail/app/api/ai-search/route.ts, apps/mail/app/api/auth/[...all]/route.ts, ... Deleted numerous REST API route handlers for AI search, authentication, early access, chat, driver, golden ticket, mailto-handler, notes, v1 endpoints, and others, as their logic is now handled by tRPC.
apps/mail/app/(full-width)/contributors/page.tsx, ... Refactored frontend data fetching from SWR to React Query with tRPC hooks in contributors, settings, connections, labels, and other pages.
apps/mail/components/context/thread-context.tsx, ... Updated components to use React Query and tRPC mutations for mail, note, and settings actions, replacing direct imports of server actions and SWR mutate calls.
apps/mail/hooks/use-ai-search.ts, apps/mail/hooks/use-contacts.ts, ... Removed or refactored hooks to use React Query and tRPC queries/mutations instead of SWR and REST fetchers.
apps/mail/lib/brain.ts, apps/mail/lib/email-templates.ts, apps/mail/lib/resend.ts Added new modules for brain feature enablement, email templates (e.g., golden ticket), and email sending client abstraction.
apps/mail/providers/query-provider.tsx Added a new provider integrating React Query and tRPC, with custom error handling, query key hashing, and singleton management for client/server environments.
apps/mail/trpc/index.ts, apps/mail/trpc/routes/ai/, ... Introduced new tRPC routers and procedures for AI, brain, connections, cookies, drafts, early access, labels, mail, notes, settings, and user management, centralizing backend logic.
apps/mail/package.json Updated dependencies: removed axios and swr, added @tanstack/react-query, @trpc/client, @trpc/server, @trpc/tanstack-react-query, dedent, hono, and updated zod.
apps/mail/lib/idb.ts Commented out and removed the IndexedDB SWR cache provider code, with a note to reintroduce it later with more granular control.
Many files Refactored imports, replaced SWR with React Query, removed direct REST fetchers, updated data access patterns, and improved code style and consistency.

Sequence Diagram(s)

sequenceDiagram
    participant UI
    participant QueryProvider
    participant tRPCClient
    participant tRPCServer
    participant DB

    UI->>QueryProvider: Render component
    UI->>tRPCClient: useQuery/useMutation (e.g., fetch threads, send email)
    tRPCClient->>tRPCServer: tRPC procedure call (e.g., mail.get, mail.send)
    tRPCServer->>DB: Query/update database (if needed)
    tRPCServer-->>tRPCClient: Return result/error
    tRPCClient-->>UI: Update UI state via React Query cache
sequenceDiagram
    participant UI
    participant tRPCClient
    participant AIService

    UI->>tRPCClient: useMutation (e.g., generate AI email reply)
    tRPCClient->>AIService: Call AI completion API with prompt and context
    AIService-->>tRPCClient: Return AI-generated reply
    tRPCClient-->>UI: Display generated reply in UI

Possibly related PRs

  • Mail-0/Zero#771: Refactors and relocates the AI reply generation function to a tRPC route, directly related to the migration in this PR.
  • Mail-0/Zero#702: Modifies the same AI reply action file, focusing on error handling and formatting, which is now removed in this PR.
  • Mail-0/Zero#534: Improves the same AI email reply function that is deleted in this PR, indicating direct overlap in functionality.

Suggested labels

enhancement

Suggested reviewers

  • MrgSub
  • ahmetskilinc

Poem

Hoppity hop, the code did grow, From REST and SWR, we let them go! tRPC and Query, now lead the way, With hooks and routers, hooray hooray! Old actions retired, the future is clear, A faster, smarter mailbox—let’s all cheer! 🐇✨


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

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.

coderabbitai[bot] avatar Apr 27 '25 05:04 coderabbitai[bot]

@coderabbitai review

BlankParticle avatar Apr 27 '25 05:04 BlankParticle

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

coderabbitai[bot] avatar Apr 27 '25 05:04 coderabbitai[bot]