# techwithemmayt/advanced-ai-formy-builder-drag-n-drop architecture

> Architecture diagram of the GitHub repository techwithemmayt/advanced-ai-formy-builder-drag-n-drop, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop
- Repository: https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop

## Overview

##### Project Type and Purpose
- This is a full-stack, AI-powered form-builder web application built with Next.js 14.
- Users log in (Kinde), drag-and-drop to build forms, leverage AI to generate fields, save/publish forms, collect responses, and view analytics.

##### Main System Components
A. Client (Web Browser)
- Next.js App Router pages under `app/(routes)` (Landing, Dashboard, Public form‐submit).
- React/TSX UI components in `components/` and Shadcn UI.
- TailwindCSS styling.

B. Next.js Server (Node.js runtime on Vercel)
1. Routing & Rendering
- Server‐rendered pages + Client-side navigation.
- Nested routes for `/dashboard`, `/public/submit-form`, etc.
2. Server Actions / API Routes (`app/api/*` & `actions/form.action.ts`)
- Form CRUD, fetchFormById, auth callbacks.
3. Business Logic
- `lib/` (helper functions, Google-AI integration, prompts).
- `context/` (BuilderProvider for drag/drop state).
- `hooks/` (use-toast, use-mobile).
4. Authentication
- Kinde OAuth endpoints in `app/api/auth/[kindeAuth]/route.ts`.
5. Data Layer
- Prisma ORM (`lib/prismadb.ts`) + `prisma/schema.prisma` connecting to Neon PostgreSQL.

C. Database
- Neon PostgreSQL (Primary & Pooler connections).
- Prisma migrations & seeding (`prisma/seed.ts`).

D. External Services
- Kinde for authentication.
- Google/Gemini API for AI-powered form generation.

##### Component Interactions and Dataflows
- Client → Next.js Page: user visits `/dashboard`.
- Page invokes Server Action (saveForm, publishForm) → Business Logic in `actions/` → Prisma → Neon DB.
- For AI generation: Server Action calls `lib/google-ai.ts` → Google API → returns generated blocks → persisted.
- Auth: Client redirects to Kinde → callback to Next.js API → session established.
- Public form submit: Client POST to `app/api/fetchFormById` → DB reads and server returns form schema.
- Analytics: Dashboard page fetches response counts via server action/Prisma.

##### Architectural Patterns & Principles
- App Router & Nested Routing → clear page segmentation.
- Server Actions for co-located UI & server code (no separate Express).
- Context Provider pattern for in-page state (form builder).
- MVC-like layering: UI → Actions (Controller) → lib/Business Logic → ORM (Model) → DB.
- Type-safe contracts (`@types`, TS throughout).

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% Browser / UI Zone
  subgraph "Browser / UI"
    style BrowserUI fill:#D0E6FF,stroke:#000
    subgraph "Landing Page"  
      LPPage["page.tsx"]:::ui
      LPLayout["layout.tsx"]:::ui
      LPNavbar["_components/Navbar.tsx"]:::ui
    end
    subgraph "Dashboard Pages"
      DBPage["page.tsx"]:::ui
      DBLayout["layout.tsx"]:::ui
    end
    subgraph "Form Builder UI Components"
      BuilderComp["Builder.tsx"]:::ui
      FormBuilderComp["FormBuilder.tsx"]:::ui
      FBPage["[formId]/page.tsx"]:::ui
    end
    subgraph "Responds & Analytics"
      RESPPage["[formId]/page.tsx"]:::ui
      AllResponds["_components/AllReponds.tsx"]:::ui
      StatsCards["_components/StatsCards.tsx"]:::ui
    end
    subgraph "Public Form Submission"
      PublicPage["[formId]/page.tsx"]:::ui
      FormSubmitComp["_components/FormSubmitComponent.tsx"]:::ui
    end
    SharedUI["components/ui/*"]:::ui
    BlocksUI["components/blocks/*"]:::ui
  end

  %% Next.js Server Zone
  subgraph "Next.js Server"
    style Server fill:#DFFFD0,stroke:#000
    subgraph "App Router & Rendering"
      ARoutes["app/(routes)"]:::server
    end
    subgraph "Server Actions & API Routes"
      FormAction["actions/form.action.ts"]:::server
      FetchFormAPI["app/api/fetchFormById/route.ts"]:::server
      AuthAPI["app/api/auth/[kindeAuth]/route.ts"]:::server
    end
    subgraph "Business Logic (lib)"
      GAIT["lib/google-ai.ts"]:::server
      Prompts["lib/prompts.ts"]:::server
      BlocksLogic["lib/form-blocks.ts"]:::server
      Helper["lib/helper.ts"]:::server
      Utils["lib/utils.ts"]:::server
    end
    subgraph "Context & Hooks"
      BuilderProvider["context/builder-provider.tsx"]:::server
      ToastHook["hooks/use-toast.ts"]:::server
      MobileHook["hooks/use-mobile.tsx"]:::server
    end
    subgraph "Data Layer"
      PrismaClient["lib/prismadb.ts"]:::server
      Schema["prisma/schema.prisma"]:::server
      Seed["prisma/seed.ts"]:::server
    end
  end

  %% Database Zone
  DB[(Neon PostgreSQL)]:::database

  %% External Services Zone
  KindeCloud[(Kinde Auth)]:::external
  GoogleAICloud[(Google/Gemini API)]:::external

  %% Connections
  LPPage -->|"GET /landing"| ARoutes
  DBPage -->|"GET /dashboard"| ARoutes
  FBPage -->|"navigate to Builder"| ARoutes
  PublicPage -->|"GET /public/submit-form"| ARoutes

  ARoutes -->|"invoke Server Action"| FormAction
  ARoutes -->|"invoke API"| FetchFormAPI
  ARoutes -->|"invoke Auth API"| AuthAPI

  FormAction -->|"generateBlocks(formSchema)"| GAIT
  GAIT -->|"API call"| GoogleAICloud
  FormAction -->|"SQL queries"| PrismaClient
  FetchFormAPI -->|"SQL queries"| PrismaClient
  AuthAPI -->|"OAuth callback"| KindeCloud

  PrismaClient -->|"connect"| DB

  RESPPage -->|"fetch responses"| FormAction
  RESPPage -->|"fetch analytics"| FormAction

  %% Click Events
  click LPPage "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/(landingPage)/page.tsx"
  click LPLayout "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/(landingPage)/layout.tsx"
  click LPNavbar "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/(landingPage)/_components/Navbar.tsx"
  click DBPage "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/dashboard/page.tsx"
  click DBLayout "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/dashboard/layout.tsx"
  click BuilderComp "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/dashboard/_components/Builder.tsx"
  click FormBuilderComp "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/dashboard/_components/FormBuilder.tsx"
  click FBPage "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/dashboard/form/builder/[formId]/page.tsx"
  click RESPPage "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/dashboard/form/responds/[formId]/page.tsx"
  click AllResponds "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/dashboard/form/responds/_components/AllReponds.tsx"
  click StatsCards "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/dashboard/_components/StatsCards.tsx"
  click PublicPage "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/public/submit-form/[formId]/page.tsx"
  click FormSubmitComp "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/(routes)/public/_components/FormSubmitComponent.tsx"
  click SharedUI "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/tree/main/components/ui/"
  click BlocksUI "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/tree/main/components/blocks/"
  click FormAction "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/actions/form.action.ts"
  click FetchFormAPI "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/api/fetchFormById/route.ts"
  click AuthAPI "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/app/api/auth/[kindeAuth]/route.ts"
  click GAIT "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/lib/google-ai.ts"
  click Prompts "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/lib/prompts.ts"
  click BlocksLogic "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/lib/form-blocks.ts"
  click Helper "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/lib/helper.ts"
  click Utils "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/lib/utils.ts"
  click BuilderProvider "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/context/builder-provider.tsx"
  click ToastHook "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/hooks/use-toast.ts"
  click MobileHook "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/hooks/use-mobile.tsx"
  click PrismaClient "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/lib/prismadb.ts"
  click Schema "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/prisma/schema.prisma"
  click Seed "https://github.com/techwithemmayt/advanced-ai-formy-builder-drag-n-drop/blob/main/prisma/seed.ts"

  %% Styles
  classDef ui fill:#CDE8FF,stroke:#0366D6
  classDef server fill:#D0FFD6,stroke:#028A0F
  classDef database fill:#FFE4B2,stroke:#BF7000
  classDef external fill:#E0E0E0,stroke:#888888
```
