# mohammed-iqramul/ai-flashcard-generator architecture

> Architecture diagram of the GitHub repository mohammed-iqramul/ai-flashcard-generator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mohammed-iqramul/ai-flashcard-generator
- Repository: https://github.com/mohammed-iqramul/ai-flashcard-generator

## Overview

##### Project Type and Purpose
- FlashLearn is a full-stack, server-rendered web application built with Next.js 13 (“app” directory).
- It provides AI-powered flashcard generation, user authentication, storage and retrieval of collections, and a demo payment flow.
- Core external integrations: Clerk (authentication), Firebase (database), OpenAI (AI generation), Stripe (payments), Google Analytics (metrics).

##### File Structure Analysis
- **app/**: Next.js 13 “app router” pages and serverless API routes
- /api/generate/route.js – AI flashcard generation endpoint
- /api/checkout_sessions/route.js – Stripe session creation
- /generate, /flashcards, /flashcard, /order-details, etc. – UI pages (React server/client components)
- **components/**: Shared React components (e.g. GoogleAnalytics)
- **firebase.js**: Firebase client/server initialization
- **utils/get-stripe.js**: Helper to instantiate Stripe SDK
- **middleware.ts**: Next.js middleware for auth guards (Clerk)
- **configuration**: next.config.mjs, tsconfig.json, jsconfig.json, tailwind.config.js, postcss.config.mjs
- **public/**: Static assets

##### Key Architectural Elements
a. Frontend (Next.js pages)
b. Serverless API layer (Next.js API routes)
c. Authentication (Clerk)
d. Database (Firebase Firestore)
e. AI Service (OpenAI)
f. Payment Service (Stripe)
g. Analytics (Google Analytics)
h. UI Frameworks (Material UI, Tailwind CSS)

##### Relationships and Data Flows
- User ↔ Next.js pages (UI rendering, navigation)
- Next.js pages ↔ Clerk SDK (sign-in / sign-up / session management) via middleware.ts
- UI ↔ /api/generate → Next.js serverless → OpenAI API → returns flashcards → firebase.js writes to Firestore → UI displays results
- UI ↔ Firestore (read collections and individual flashcards) via firebase.js in server components or client hooks
- UI ↔ /api/checkout_sessions → Next.js serverless → Stripe SDK (via get-stripe.js) → returns session URL → UI redirects to Stripe
- UI includes Google Analytics script for event tracking (GoogleAnalytics component)

##### Architectural Patterns & Principles
- Jamstack-style frontend with serverless functions handling dynamic workloads
- Separation of concerns: UI, API routes, external services
- Server and client components (Next.js 13 app router)
- Middleware for cross-cutting concerns (auth enforcement)
- Utility modules for external SDK initializations

##### Essential Elements
- Versioning notes (Next.js 13 “app” router) and middleware for auth

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Layer
    User["User / Browser"]:::user

    %% Frontend Layer
    subgraph "Next.js Frontend (React, Tailwind, Material UI)" 
        direction TB
        pageHome["app/page.js"]:::frontend
        layout["app/layout.js"]:::frontend
        dash["app/dashboard/page.js"]:::frontend
        flashcardPage["app/flashcard/page.js"]:::frontend
        flashcardsPage["app/flashcards/page.js"]:::frontend
        generatePage["app/generate/page.js"]:::frontend
        orderPage["app/order-details/page.js"]:::frontend
        resultPage["app/result/page.js"]:::frontend
        globalCSS["app/globals.css"]:::frontend
        tailwindConf["tailwind.config.js"]:::frontend
        postcssConf["postcss.config.mjs"]:::frontend
        GA["GoogleAnalytics Component"]:::external
        Static["/public Static Assets"]:::external
    end

    %% Middleware & Auth
    middleware["middleware.ts"]:::core
    Clerk["Clerk Auth Service"]:::external

    %% Serverless API Layer
    subgraph "Serverless Functions (Node.js)" 
        direction TB
        genAPI["app/api/generate/route.js"]:::api
        checkoutAPI["app/api/checkout_sessions/route.js"]:::api
    end

    %% Utilities & Initialization
    FirebaseInit["firebase.js"]:::core
    GetStripe["utils/get-stripe.js"]:::core

    %% External Services
    OpenAI["OpenAI API"]:::external
    Stripe["Stripe API"]:::external
    Firestore["Firebase Firestore"]:::db

    %% Data Flows
    User -->|visits| pageHome
    pageHome -->|uses layout| layout
    pageHome -->|navigates| dash
    pageHome -->|navigates| flashcardPage
    pageHome -->|navigates| flashcardsPage
    pageHome -->|navigates| generatePage
    pageHome -->|navigates| orderPage
    pageHome -->|navigates| resultPage
    pageHome -->|tracks events| GA

    %% Auth Flow
    pageHome -->|HTTP Request| middleware
    dash -->|HTTP Request| middleware
    flashcardPage -->|HTTP Request| middleware
    flashcardsPage -->|HTTP Request| middleware
    generatePage -->|HTTP Request| middleware
    orderPage -->|HTTP Request| middleware
    resultPage -->|HTTP Request| middleware
    middleware -->|validates tokens| Clerk

    %% API Calls
    generatePage -->|POST /api/generate| genAPI
    genAPI -->|calls| OpenAI
    genAPI -->|writes| FirebaseInit
    FirebaseInit -->|connects to| Firestore
    Firestore -->|reads/writes| generatePage

    orderPage -->|POST /api/checkout_sessions| checkoutAPI
    checkoutAPI -->|uses| GetStripe
    GetStripe -->|calls| Stripe
    Stripe -->|returns session| checkoutAPI
    checkoutAPI -->|redirects| orderPage

    %% Static Assets
    pageHome --> Static

    %% Styles & Config
    pageHome --> globalCSS
    pageHome --> tailwindConf
    pageHome --> postcssConf

    %% Legend (implicit through class colors)

    %% Click Events
    click pageHome "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/page.js"
    click layout "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/layout.js"
    click dash "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/dashboard/page.js"
    click flashcardPage "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/flashcard/page.js"
    click flashcardsPage "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/flashcards/page.js"
    click generatePage "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/generate/page.js"
    click orderPage "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/order-details/page.js"
    click resultPage "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/result/page.js"
    click globalCSS "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/globals.css"
    click tailwindConf "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/tailwind.config.js"
    click postcssConf "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/postcss.config.mjs"
    click genAPI "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/api/generate/route.js"
    click checkoutAPI "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/app/api/checkout_sessions/route.js"
    click middleware "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/middleware.ts"
    click FirebaseInit "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/firebase.js"
    click GetStripe "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/utils/get-stripe.js"
    click GA "https://github.com/mohammed-iqramul/ai-flashcard-generator/blob/main/components/GoogleAnalytics.js"

    %% Class Definitions
    classDef user fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef api fill:#98FB98,stroke:#333,stroke-width:1px
    classDef external fill:#FFA500,stroke:#333,stroke-width:1px
    classDef db fill:#DDA0DD,stroke:#333,stroke-width:1px
    classDef core fill:#F0E68C,stroke:#333,stroke-width:1px
```
