# zengroot147/mail_craft architecture

> Architecture diagram of the GitHub repository zengroot147/mail_craft, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zengroot147/mail_craft
- Repository: https://github.com/zengroot147/mail_craft

## Overview

1. **Overall shape**
   - MailCraft is a **single-page React application** with a **Supabase-backed backend**. The front end is built with **React 18 + TypeScript + Vite**, styled with **Tailwind CSS**, and composed largely from **shadcn/ui** primitives.
   - The backend is split between **Supabase Auth/Postgres** and **Supabase Edge Functions** for privileged actions like AI drafting and Gmail integration.

2. **App entry and routing**
   - The app bootstraps from `src/main.tsx` into `src/App.tsx`.
   - Route-level pages are in `src/pages/`: `Index` (main product), `Auth`, `Profile`, `ResetPassword`, and `NotFound`.
   - This suggests a clear boundary between **public/auth flows** and the **signed-in workspace**.

3. **Authentication and user session boundary**
   - `src/contexts/AuthContext.tsx` centralizes the authenticated session state.
   - Supabase Auth is the primary identity layer, and profile-related data is accessed through hooks such as `useProfile`.
   - Authentication is likely the gate that controls access to email tools, account settings, and connected Gmail features.

4. **Client-side data access**
   - `src/integrations/supabase/client.ts` is the main browser-side DB/auth client.
   - `src/integrations/supabase/types.ts` indicates generated typed schema access, so the app is strongly typed end-to-end against Supabase data.
   - Server state is managed with **TanStack React Query** per the README, which likely handles profile, drafts, analytics, and connection status.

5. **Core product workspace**
   - The main product surface is an email composition and enhancement workspace centered on components like `EmailComposer`, `AIChatAssistant`, `RefinementBar`, `DraftComparison`, and `DraftStats`.
   - Around that composer are specialized analysis tools: `ToneAnalyzer`, `GrammarCheck`, `ComplianceCheck`, `ReadabilityScore`, `SensitiveInfoCheck`, and `LinkDetector`.
   - This makes the UI architecture modular: one primary compose flow with many specialized “assist” panels.

6. **AI generation and writing assistance flow**
   - AI draft generation is handled through the Supabase Edge Function `supabase/functions/draft-email`.
   - The presence of `src/lib/aiHeaders.ts` and `src/lib/sse.ts` suggests the client talks to AI endpoints with authenticated headers and may support **streaming responses** via SSE.
   - README says the AI provider is **Google Gemini**, so the AI boundary is intentionally pushed off the client and into backend functions.

7. **Gmail integration flow**
   - Gmail connectivity is implemented as a backend workflow with three edge functions:
     - `gmail-oauth-start`
     - `gmail-oauth-callback`
     - `gmail-send`
   - `src/hooks/useGmailConnection.ts` manages the client-side connection state.
   - This is a distinct integration boundary: the browser initiates auth, Supabase handles the OAuth callback securely, and send operations happen server-side.

8. **Email intelligence and productivity features**
   - The app includes a wide set of analysis and workflow modules beyond drafting:
     - `EmailSummarizer`, `EmailThreadBuilder`, `EmailToTask`, `QuickReplySuggestions`
     - `SubjectLineSuggestions`, `ColdEmailOptimizer`, `LanguageSelector`, `ToneSelector`, `LengthControl`
     - `ScheduleReminder`, `TextToSpeech`, `VoiceInput`
   - These are likely fed from the same underlying draft/content model, making the email text the central domain object.

9. **Analytics and dashboard subsystem**
   - `DashboardInsights`, `EmailAnalytics`, `WorkspaceStats`, `ProductivityStreaks`, `PeakHoursHeatmap`, and `DraftStats` indicate a separate analytics layer.
   - `useEmailEvents` suggests event collection from user actions, which then drives dashboard metrics and streaks.
   - This is the main observational loop in the app: user composing activity becomes analytics and recommendations.

10. **Persistence and schema**
   - Supabase migrations in `supabase/migrations/` define the database schema over time.
   - The presence of `add_gmail_connections.sql` indicates dedicated storage for connected Gmail accounts, separate from basic auth.
   - The overall data model likely includes users/profiles, drafts, email events, templates/snippets, and Gmail connection metadata.

11. **Reusable UI system**
   - `src/components/ui/` is a full shadcn/Radix component layer: dialogs, sheets, command palette, tables, tabs, tooltips, etc.
   - Higher-level components such as `CommandPalette`, `OnboardingTour`, `ThemeToggle`, and `CollapsibleSection` build product behavior on top of those primitives.
   - This creates a clean separation between **design-system primitives** and **feature components**.

12. **Platform/tooling and deployment**
   - The repo uses **Vite** for the frontend build, **Vitest** for tests, and **ESLint** for linting.
   - `vercel.json` indicates the frontend is deployed on **Vercel**, while Supabase hosts the backend services/functions.
   - Public assets like `sitemap.xml`, `robots.txt`, and structured data files show the app also has an SEO-aware landing/public-facing surface.

13. **How the major flows connect**
   - A signed-in user opens the React SPA, authenticates through Supabase, and loads profile/connection state.
   - They compose or paste email text into the workspace, invoke AI assistance, and receive generated or analyzed content from Supabase Edge Functions.
   - If connected, Gmail OAuth enables sending through backend functions rather than directly from the browser.
   - User actions are recorded as events and summarized in analytics dashboards and productivity views.

14. **Architecture boundary summary**
   - The key architectural boundary is between the **interactive client workspace** and the **server-side integration layer**.
   - The client handles composition, UI state, and local interactions; Supabase handles identity, persistence, OAuth, AI proxying, and Gmail send operations.
   - That split is what makes the repository diagram-friendly: one central SPA, one typed backend/data layer, and several narrowly scoped edge-function integrations.

Main entry points: [`src/components/EmailComposer.tsx`](https://github.com/zengroot147/mail_craft/blob/main/src/components/EmailComposer.tsx) (Workspace), [`src/integrations/supabase/client.ts`](https://github.com/zengroot147/mail_craft/blob/main/src/integrations/supabase/client.ts) (Supabase client), [`src/components/AIChatAssistant.tsx`](https://github.com/zengroot147/mail_craft/blob/main/src/components/AIChatAssistant.tsx) (AI assistant), [`src/hooks/useGmailConnection.ts`](https://github.com/zengroot147/mail_craft/blob/main/src/hooks/useGmailConnection.ts) (Gmail hook)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_client["Client app"]
  node_node_main["Main entry<br/>vite bootstrap<br/>[main.tsx]"]
  node_node_app["App shell<br/>router shell<br/>[App.tsx]"]
  node_node_pages["Pages<br/>route views<br/>[Index.tsx]"]
  node_node_auth_ctx(("Auth context<br/>session state<br/>[AuthContext.tsx]"))
  node_node_profile_hook(("Profile hook<br/>data hook<br/>[useProfile.ts]"))
  node_node_gmail_hook(("Gmail hook<br/>integration hook"))
  node_node_email_events(("Event hook<br/>analytics hook<br/>[useEmailEvents.ts]"))
  node_node_workspace["Workspace<br/>compose surface<br/>[EmailComposer.tsx]"]
  node_node_assistant["AI assistant<br/>ai help"]
  node_node_analysis["Analysis tools<br/>email checks<br/>[ToneAnalyzer.tsx]"]
  node_node_productivity["Productivity tools<br/>workflow helpers"]
  node_node_templates["Templates<br/>content library"]
  node_node_dashboard["Dashboard<br/>insights view"]
  node_node_ai_headers["AI headers<br/>client auth<br/>[aiHeaders.ts]"]
  node_node_sse["SSE stream<br/>streaming client<br/>[sse.ts]"]
end

subgraph group_group_ui["UI kit"]
  node_node_design_system["UI primitives<br/>component kit<br/>[button.tsx]"]
end

subgraph group_group_backend["Supabase backend"]
  node_node_supabase_client[("Supabase client<br/>browser db client<br/>[client.ts]")]
  node_node_supabase_types["DB types<br/>typed schema<br/>[types.ts]"]
  node_node_cloud_auth["Cloud auth<br/>integration adapter<br/>[index.ts]"]
  node_node_edge_ai{{"Draft function<br/>edge function<br/>[index.ts]"}}
  node_node_edge_gmail_oauth_start{{"OAuth start<br/>edge function<br/>[index.ts]"}}
  node_node_edge_gmail_oauth_callback{{"OAuth callback<br/>edge function<br/>[index.ts]"}}
  node_node_edge_gmail_send{{"Gmail send<br/>edge function<br/>[index.ts]"}}
  node_node_migrations["Migrations<br/>schema changes"]
end

subgraph group_group_platform["Platform"]
  node_node_public["Public assets<br/>seo assets<br/>[sitemap.xml]"]
  node_node_build["Build config<br/>tooling<br/>[vite.config.ts]"]
end

node_node_main -->|"mounts"| node_node_app
node_node_app -->|"routes to"| node_node_pages
node_node_pages -->|"uses"| node_node_auth_ctx
node_node_pages -->|"opens"| node_node_workspace
node_node_workspace -->|"invokes"| node_node_assistant
node_node_workspace -->|"runs"| node_node_analysis
node_node_workspace -->|"feeds"| node_node_productivity
node_node_workspace -->|"reuses"| node_node_templates
node_node_workspace -->|"emits"| node_node_email_events
node_node_dashboard -->|"consumes"| node_node_email_events
node_node_auth_ctx -->|"authenticates"| node_node_supabase_client
node_node_profile_hook -->|"queries"| node_node_supabase_client
node_node_profile_hook -->|"types"| node_node_supabase_types
node_node_gmail_hook -->|"tracks"| node_node_supabase_client
node_node_ai_headers -->|"secures"| node_node_supabase_client
node_node_sse -->|"streams"| node_node_edge_ai
node_node_assistant -->|"uses"| node_node_ai_headers
node_node_assistant -->|"streams"| node_node_sse
node_node_assistant -->|"calls"| node_node_edge_ai
node_node_gmail_hook -->|"starts"| node_node_edge_gmail_oauth_start
node_node_edge_gmail_oauth_start -->|"returns"| node_node_edge_gmail_oauth_callback
node_node_gmail_hook -->|"sends via"| node_node_edge_gmail_send
node_node_edge_gmail_oauth_callback -->|"persists"| node_node_migrations
node_node_cloud_auth -->|"bridges"| node_node_supabase_client
node_node_build -->|"packages"| node_node_public
node_node_build -->|"supports"| node_node_design_system

click node_node_main "https://github.com/zengroot147/mail_craft/blob/main/src/main.tsx"
click node_node_app "https://github.com/zengroot147/mail_craft/blob/main/src/App.tsx"
click node_node_pages "https://github.com/zengroot147/mail_craft/blob/main/src/pages/Index.tsx"
click node_node_auth_ctx "https://github.com/zengroot147/mail_craft/blob/main/src/contexts/AuthContext.tsx"
click node_node_profile_hook "https://github.com/zengroot147/mail_craft/blob/main/src/hooks/useProfile.ts"
click node_node_gmail_hook "https://github.com/zengroot147/mail_craft/blob/main/src/hooks/useGmailConnection.ts"
click node_node_email_events "https://github.com/zengroot147/mail_craft/blob/main/src/hooks/useEmailEvents.ts"
click node_node_workspace "https://github.com/zengroot147/mail_craft/blob/main/src/components/EmailComposer.tsx"
click node_node_assistant "https://github.com/zengroot147/mail_craft/blob/main/src/components/AIChatAssistant.tsx"
click node_node_analysis "https://github.com/zengroot147/mail_craft/blob/main/src/components/ToneAnalyzer.tsx"
click node_node_productivity "https://github.com/zengroot147/mail_craft/blob/main/src/components/EmailSummarizer.tsx"
click node_node_templates "https://github.com/zengroot147/mail_craft/blob/main/src/components/TemplatesGallery.tsx"
click node_node_dashboard "https://github.com/zengroot147/mail_craft/blob/main/src/components/DashboardInsights.tsx"
click node_node_design_system "https://github.com/zengroot147/mail_craft/blob/main/src/components/ui/button.tsx"
click node_node_ai_headers "https://github.com/zengroot147/mail_craft/blob/main/src/lib/aiHeaders.ts"
click node_node_sse "https://github.com/zengroot147/mail_craft/blob/main/src/lib/sse.ts"
click node_node_supabase_client "https://github.com/zengroot147/mail_craft/blob/main/src/integrations/supabase/client.ts"
click node_node_supabase_types "https://github.com/zengroot147/mail_craft/blob/main/src/integrations/supabase/types.ts"
click node_node_cloud_auth "https://github.com/zengroot147/mail_craft/blob/main/src/integrations/cloud-auth/index.ts"
click node_node_edge_ai "https://github.com/zengroot147/mail_craft/blob/main/supabase/functions/draft-email/index.ts"
click node_node_edge_gmail_oauth_start "https://github.com/zengroot147/mail_craft/blob/main/supabase/functions/gmail-oauth-start/index.ts"
click node_node_edge_gmail_oauth_callback "https://github.com/zengroot147/mail_craft/blob/main/supabase/functions/gmail-oauth-callback/index.ts"
click node_node_edge_gmail_send "https://github.com/zengroot147/mail_craft/blob/main/supabase/functions/gmail-send/index.ts"
click node_node_migrations "https://github.com/zengroot147/mail_craft/blob/main/supabase/migrations/20260407000100_add_gmail_connections.sql"
click node_node_public "https://github.com/zengroot147/mail_craft/blob/main/public/sitemap.xml"
click node_node_build "https://github.com/zengroot147/mail_craft/blob/main/vite.config.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_main,node_node_app,node_node_pages,node_node_auth_ctx,node_node_profile_hook,node_node_gmail_hook,node_node_email_events,node_node_workspace,node_node_assistant,node_node_analysis,node_node_productivity,node_node_templates,node_node_dashboard,node_node_ai_headers,node_node_sse toneBlue
class node_node_design_system toneAmber
class node_node_supabase_client,node_node_supabase_types,node_node_cloud_auth,node_node_edge_ai,node_node_edge_gmail_oauth_start,node_node_edge_gmail_oauth_callback,node_node_edge_gmail_send,node_node_migrations toneMint
class node_node_public,node_node_build toneRose
```

## Components

### Client app

React SPA, pages, workspace features, and client-side state.

- [Main entry](https://github.com/zengroot147/mail_craft/blob/main/src/main.tsx): vite bootstrap · Bootstraps the SPA and mounts the application root. · `src/main.tsx`
- [App shell](https://github.com/zengroot147/mail_craft/blob/main/src/App.tsx): router shell · Top-level application shell and route wiring. · `src/App.tsx`
- [Pages](https://github.com/zengroot147/mail_craft/blob/main/src/pages/Index.tsx): route views · Public and authenticated route-level screens. · `src/pages/Index.tsx`
- [Auth context](https://github.com/zengroot147/mail_craft/blob/main/src/contexts/AuthContext.tsx): session state · Centralizes authenticated session state for the client. · `src/contexts/AuthContext.tsx`
- [Profile hook](https://github.com/zengroot147/mail_craft/blob/main/src/hooks/useProfile.ts): data hook · Loads and exposes profile-related state. · `src/hooks/useProfile.ts`
- [Gmail hook](https://github.com/zengroot147/mail_craft/blob/main/src/hooks/useGmailConnection.ts): integration hook · Tracks Gmail connection state in the client. · `src/hooks/useGmailConnection.ts`
- [Event hook](https://github.com/zengroot147/mail_craft/blob/main/src/hooks/useEmailEvents.ts): analytics hook · Collects email activity events for workspace analytics. · `src/hooks/useEmailEvents.ts`
- [Workspace](https://github.com/zengroot147/mail_craft/blob/main/src/components/EmailComposer.tsx): compose surface · Primary email drafting and refinement workspace. · `src/components/EmailComposer.tsx`
- [AI assistant](https://github.com/zengroot147/mail_craft/blob/main/src/components/AIChatAssistant.tsx): ai help · Generates and refines email content with AI assistance. · `src/components/AIChatAssistant.tsx`
- [Analysis tools](https://github.com/zengroot147/mail_craft/blob/main/src/components/ToneAnalyzer.tsx): email checks · Tone, grammar, compliance, readability, and safety review modules. · `src/components/ToneAnalyzer.tsx`
- [Productivity tools](https://github.com/zengroot147/mail_craft/blob/main/src/components/EmailSummarizer.tsx): workflow helpers · Summaries, replies, tasks, scheduling, voice, and related helpers. · `src/components/EmailSummarizer.tsx`
- [Templates](https://github.com/zengroot147/mail_craft/blob/main/src/components/TemplatesGallery.tsx): content library · Saved drafts, snippets, templates, and signature assets. · `src/components/TemplatesGallery.tsx`
- [Dashboard](https://github.com/zengroot147/mail_craft/blob/main/src/components/DashboardInsights.tsx): insights view · Analytics, streaks, heatmaps, and workspace metrics. · `src/components/DashboardInsights.tsx`
- [AI headers](https://github.com/zengroot147/mail_craft/blob/main/src/lib/aiHeaders.ts): client auth · Prepares authenticated request headers for AI calls. · `src/lib/aiHeaders.ts`
- [SSE stream](https://github.com/zengroot147/mail_craft/blob/main/src/lib/sse.ts): streaming client · Client-side support for streaming AI responses. · `src/lib/sse.ts`

### UI kit

Reusable shadcn/Radix primitives and shared UI helpers.

- [UI primitives](https://github.com/zengroot147/mail_craft/blob/main/src/components/ui/button.tsx): component kit · Shared shadcn/Radix building blocks used across the app. · `src/components/ui/button.tsx`

### Supabase backend

Auth, database types, migrations, and edge functions.

- [Supabase client](https://github.com/zengroot147/mail_craft/blob/main/src/integrations/supabase/client.ts): browser db client · Main browser-side Supabase client for auth and data access. · `src/integrations/supabase/client.ts`
- [DB types](https://github.com/zengroot147/mail_craft/blob/main/src/integrations/supabase/types.ts): typed schema · Generated TypeScript schema types for Supabase data. · `src/integrations/supabase/types.ts`
- [Cloud auth](https://github.com/zengroot147/mail_craft/blob/main/src/integrations/cloud-auth/index.ts): integration adapter · Cloud-auth integration layer used by the client. · `src/integrations/cloud-auth/index.ts`
- [Draft function](https://github.com/zengroot147/mail_craft/blob/main/supabase/functions/draft-email/index.ts): edge function · Server-side AI drafting function backed by Supabase Edge Functions. · `supabase/functions/draft-email/index.ts`
- [OAuth start](https://github.com/zengroot147/mail_craft/blob/main/supabase/functions/gmail-oauth-start/index.ts): edge function · Starts Gmail OAuth in a server-side flow. · `supabase/functions/gmail-oauth-start/index.ts`
- [OAuth callback](https://github.com/zengroot147/mail_craft/blob/main/supabase/functions/gmail-oauth-callback/index.ts): edge function · Completes Gmail OAuth and persists connection state securely. · `supabase/functions/gmail-oauth-callback/index.ts`
- [Gmail send](https://github.com/zengroot147/mail_craft/blob/main/supabase/functions/gmail-send/index.ts): edge function · Server-side email sending through connected Gmail accounts. · `supabase/functions/gmail-send/index.ts`
- [Migrations](https://github.com/zengroot147/mail_craft/blob/main/supabase/migrations/20260407000100_add_gmail_connections.sql): schema changes · Database schema evolution and Gmail connection storage. · `supabase/migrations/20260407000100_add_gmail_connections.sql`

### Platform

Build, deployment, and public assets.

- [Public assets](https://github.com/zengroot147/mail_craft/blob/main/public/sitemap.xml): seo assets · Robots, sitemap, web manifest, and structured data. · `public/sitemap.xml`
- [Build config](https://github.com/zengroot147/mail_craft/blob/main/vite.config.ts): tooling · Frontend build, tests, linting, and deployment configuration. · `vite.config.ts`

## Connections

- Main entry → App shell: mounts · The Vite entry mounts the application shell.
- App shell → Pages: routes to · The app shell selects route-level pages.
- Pages → Auth context: uses · Pages depend on session state for auth gating.
- Pages → Workspace: opens · Signed-in pages expose the composition workspace.
- Workspace → AI assistant: invokes · The composer is paired with AI drafting and refinement.
- Workspace → Analysis tools: runs · Workspace content is checked by specialized analysis modules.
- Workspace → Productivity tools: feeds · Draft text powers summarization and workflow helpers.
- Workspace → Templates: reuses · Drafting pulls from templates, snippets, and signatures.
- Workspace → Event hook: emits · Composition activity is recorded for analytics.
- Dashboard → Event hook: consumes · Dashboard metrics are derived from user email events.
- Auth context → Supabase client: authenticates · Session state is backed by Supabase auth.
- Profile hook → Supabase client: queries · Profile state is loaded through the Supabase client.
- Profile hook → DB types: types · Profile queries use generated database types.
- Gmail hook → Supabase client: tracks · Gmail connection state comes from backend data.
- AI headers → Supabase client: secures · AI requests are prepared with authenticated client context.
- SSE stream → Draft function: streams · The client receives streamed AI output from the edge function.
- AI assistant → AI headers: uses · AI assistance uses authenticated request headers.
- AI assistant → SSE stream: streams · Assistant UI supports incremental AI responses.
- AI assistant → Draft function: calls · Draft generation is handled by the backend function.
- Gmail hook → OAuth start: starts · The client initiates Gmail authorization.
- OAuth start → OAuth callback: returns · OAuth start flows into the callback handler.
- Gmail hook → Gmail send: sends via · Connected Gmail accounts are used for backend sending.
- OAuth callback → Migrations: persists · Connection metadata is stored in the database schema.
- Cloud auth → Supabase client: bridges · Cloud auth sits beside the main Supabase client integration.
- Build config → Public assets: packages · Build and deployment include public SEO assets.
- Build config → UI primitives: supports · Shared UI primitives are used throughout the build.
