Skip to content

Arjun9234/ComponentAi

Repository files navigation

ComponentAI - AI-Driven Component Generator Platform

A full-stack Next.js application that allows users to generate React components through natural language conversation with AI. Built with MongoDB, JWT authentication, and the AI SDK.

🚀 Features

Core Features

  • Authentication & Persistence: Secure JWT-based auth with MongoDB
  • Conversational UI: Chat interface for component generation
  • Live Preview: Real-time component rendering in iframe sandbox
  • Code Inspection: Syntax-highlighted JSX/CSS code tabs
  • Export Functionality: Copy and download generated components
  • Session Management: Persistent chat history and auto-save

Technical Highlights

  • Frontend: Next.js 14 with React 18, Tailwind CSS, shadcn/ui
  • Backend: Next.js API routes with MongoDB integration
  • AI Integration: AI SDK with OpenAI GPT-4o-mini
  • Authentication: JWT tokens with bcrypt password hashing
  • State Management: React hooks with localStorage persistence
  • Code Rendering: Secure iframe sandbox with Babel transpilation

🛠️ Tech Stack

  • Frontend: React, Next.js, TypeScript, Tailwind CSS
  • Backend: Node.js, Express (via Next.js API routes)
  • Database: MongoDB with Mongoose ODM
  • Authentication: JWT tokens, bcryptjs
  • AI: AI SDK with OpenAI integration
  • UI Components: shadcn/ui, Radix UI primitives
  • Styling: Tailwind CSS with responsive design
  • Code Highlighting: react-syntax-highlighter with Prism

📋 Prerequisites

  • Node.js 18+ and npm
  • MongoDB database (local or cloud)
  • OpenAI API key

🚀 Quick Start

  1. Clone the repository ```bash git clone cd ai-component-generator ```

  2. Install dependencies ```bash npm install ```

  3. Set up environment variables ```bash cp .env.example .env.local ```

    Update .env.local with your values: ```env MONGODB_URI=your mongodb url JWT_SECRET=your-super-secret-jwt-key-here OPENAI_API_KEY=your-openai-api-key-here ```

  4. Start MongoDB ```bash

    If using local MongoDB

    mongod ```

  5. Run the development server ```bash npm run dev ```

  6. Open your browser Navigate to http://localhost:3000

🏗️ Architecture

Frontend Architecture

``` app/ ├── page.tsx # Landing page ├── auth/ │ ├── login/page.tsx # Login page │ └── signup/page.tsx # Signup page ├── dashboard/page.tsx # User dashboard └── playground/[sessionId]/ # Component generator └── page.tsx ```

Backend API Routes

``` app/api/ ├── auth/ │ ├── login/route.ts # User authentication │ └── signup/route.ts # User registration └── sessions/ ├── route.ts # Session CRUD operations ├── [sessionId]/route.ts # Individual session └── [sessionId]/messages/route.ts # Chat messages ```

Database Schema

```javascript // User Model { name: String, email: String (unique), password: String (hashed), timestamps: true }

// Session Model { userId: ObjectId, title: String, messages: [{ role: 'user' | 'assistant', content: String, timestamp: Date }], generatedCode: { jsx: String, css: String }, timestamps: true } ```

🔧 Key Implementation Details

Authentication Flow

  1. User registers/logs in with email/password
  2. Server validates credentials and returns JWT token
  3. Client stores token in localStorage
  4. All API requests include Authorization header
  5. Server validates JWT on protected routes

AI Component Generation

  1. User sends natural language prompt
  2. System builds conversation context
  3. AI SDK generates React component code
  4. Code is extracted and stored in session
  5. Live preview updates via iframe sandbox

State Management Strategy

  • Client State: React hooks + localStorage for auth
  • Server State: MongoDB for persistent data
  • Auto-save: Triggered after each chat interaction
  • Session Resume: Full state restoration on login

Security Considerations

  • Password hashing with bcrypt (12 rounds)
  • JWT tokens with 7-day expiration
  • Input validation and sanitization
  • Iframe sandbox for code execution
  • CORS protection on API routes

🎯 Usage Guide

Creating Components

  1. Sign up/Login to access the platform
  2. Create New Session from dashboard
  3. Describe your component in natural language:
    • "Create a responsive login form with email and password fields"
    • "Build a product card with image, title, price, and buy button"
    • "Make a navigation bar with logo and menu items"

Iterating on Components

  • Refine existing components: "Make the button larger and blue"
  • Add functionality: "Add form validation with error messages"
  • Modify styling: "Use a dark theme with rounded corners"

Exporting Code

  • Copy: Click copy button to get JSX code
  • Download: Get complete component files as ZIP
  • Preview: See live component rendering

🚀 Deployment

Vercel Deployment (Recommended)

  1. Push to GitHub ```bash git add . git commit -m "Initial commit" git push origin main ```

  2. Deploy to Vercel

    • Connect GitHub repository
    • Add environment variables
    • Deploy automatically
  3. Set up MongoDB Atlas

    • Create cluster on MongoDB Atlas
    • Update MONGODB_URI in Vercel environment

Environment Variables for Production

```env MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/componentai JWT_SECRET=production-secret-key OPENAI_API_KEY=your-openai-api-key NEXTAUTH_URL=https://your-domain.vercel.app ```

📊 Evaluation Checklist

Core Requirements (95 points)

  • Auth & Backend (10 pts): JWT sessions, password hashing, MongoDB
  • State Management (15 pts): Session persistence, auto-save, reload
  • AI Integration (20 pts): OpenAI integration, prompt handling
  • Micro-Frontend (10 pts): Iframe sandbox, live preview
  • Code Editor (10 pts): Syntax highlighting, export functionality
  • Iterative Workflow (10 pts): Chat interface, component refinement
  • Persistence (10 pts): Auto-save, session resume
  • Polish & Accessibility (10 pts): Responsive design, loading states

Bonus Features (45 points)

  • 🔄 Interactive Property Editor: Click-to-edit component properties
  • 🔄 Chat-Driven Overrides: Target specific elements via chat

🤝 Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open Pull Request

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🆘 Support

For issues and questions:

  • Create GitHub issue
  • Check documentation
  • Review code comments

Live Demo: Deploy your own instance GitHub: Repository Link

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published