# ayazsa/zerocode-fe-assignment architecture

> Architecture diagram of the GitHub repository ayazsa/zerocode-fe-assignment, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ayazsa/zerocode-fe-assignment
- Repository: https://github.com/ayazsa/zerocode-fe-assignment

## Overview

##### Project Type and Purpose
- This is a full-stack, serverless web application (“AI Chatbot Interface”) built with Next.js (React + TypeScript) on the front end, Next.js API Routes on the back end, and Firebase Authentication as an external identity provider. It provides user authentication, an interactive chat UI, persistent chat history (via localStorage), and simple bot-reply logic in a serverless function.

##### Main Architectural Components
a. Client Browser
- Next.js pages and React components (ChatContainer, ChatInput, Header, Message, ConfirmModal)
- Global providers and hooks (AuthContext for user state, useTheme for UI theme)
- LocalStorage for storing/fetching chat history
b. Next.js Server (Serverless Runtime)
- API Routes under pages/api:
- chat.ts – receives user message, applies business logic (typing delay, conditional replies), returns bot response
- hello.ts – simple “hello” endpoint (often used for health checks or sample data)
- _app.tsx & _document.tsx – custom app wrapper, global CSS and meta tags
c. Firebase Authentication (External Service)
- Handled in lib/firebase.ts and lib/firebaseAuth.ts
- Provides sign-up, login, logout, current user verification
- Integrated into AuthContext for React-wide access
d. Styling and Static Assets
- Tailwind CSS config (tailwind.config.js, postcss.config.js) for design system
- Global and component-level CSS under styles/

##### Component Interactions & Data Flow
- On page load:
- Browser requests a Next.js page (index/login/register/dashboard) → Next.js server returns SSR/CSR HTML + JS.
- AuthContext initializes by checking Firebase Auth SDK for current user token.
- User logs in/registers:
- React form → Firebase Auth SDK (lib/firebaseAuth.ts) → Firebase backend → returns user tokens → stored in AuthContext.
- Chat interaction (dashboard):
- User types message in ChatInput → ChatContainer captures it → updates localStorage and UI.
- Front-end sends POST /api/chat with message and user token.
- Next.js API Route (chat.ts) verifies token via Firebase Admin (optional) → applies reply logic → returns JSON reply.
- Front-end appends bot reply to UI and localStorage.
- Auxiliary flows:
- Clear Chat → ConfirmModal triggers localStorage.clear() and UI reset.
- Export Chat → front-end serializes chat array → triggers browser download of .txt file.
- Theme Toggle → useTheme hook updates CSS variable or Tailwind class on <html>.

##### Architectural Patterns & Design Principles
- Separation of Concerns: UI (React components) vs. Business Logic (API routes) vs. Auth (Firebase SDK).
- Serverless Functions: stateless APIs under pages/api for chat logic, enabling horizontal scaling.
- Context & Hooks: centralized AuthContext and custom useTheme for cross-component state.
- Client-side Persistence: localStorage for chat history avoids a full database.

##### Final Advice
- Be thorough but focused: include all components listed above.
- Serverless nature of the API layer.
- Both UI state flow (chat history, theme) and backend flows (authentication, chat logic).

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Client Layer (Browser)"
        direction TB
        Header["Header"]:::ui
        ChatContainer["ChatContainer"]:::ui
        ChatInput["ChatInput"]:::ui
        Message["Message"]:::ui
        ConfirmModal["ConfirmModal"]:::ui
        AuthContext["AuthContext"]:::ui
        useTheme["useTheme Hook"]:::ui
        LocalStorage[("LocalStorage")]:::storage
        libFirebase["lib/firebase.ts"]:::ui
        libFirebaseAuth["lib/firebaseAuth.ts"]:::ui
    end

    subgraph "Next.js Serverless Layer"
        direction TB
        AppWrapper["_app.tsx"]:::api
        Document["_document.tsx"]:::api
        HomePage["index.tsx"]:::api
        LoginPage["login.tsx"]:::api
        RegisterPage["register.tsx"]:::api
        DashboardPage["dashboard.tsx"]:::api
        ChatAPI["/api/chat"]:::api
        HelloAPI["/api/hello"]:::api
    end

    subgraph "External Services"
        FirebaseAuth[(Firebase Auth)]:::external
    end

    %% Data Flows
    HomePage -->|"SSR/CSR HTML+JS"| Header
    HomePage --> Header
    LoginPage -->|"form submit"| libFirebaseAuth
    RegisterPage -->|"form submit"| libFirebaseAuth
    libFirebaseAuth -->|"signUp/login/logout"| FirebaseAuth
    FirebaseAuth -->|"returns tokens"| AuthContext
    AuthContext -->|"provides user state"| Header
    DashboardPage --> ChatContainer
    ChatContainer --> ChatInput
    ChatContainer --> Message
    ChatInput -->|"POST message| /api/chat"| ChatAPI
    ChatAPI -->|"verify token"| FirebaseAuth
    ChatAPI -->|"returns reply"| ChatContainer
    ChatContainer --> LocalStorage
    ConfirmModal -->|"clear history"| LocalStorage
    useTheme -->|"update CSS vars"| Header

    %% Click Events
    click ChatContainer "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/components/ChatContainer.tsx"
    click ChatInput "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/components/ChatInput.tsx"
    click Header "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/components/Header.tsx"
    click Message "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/components/Message.tsx"
    click ConfirmModal "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/components/ConfirmModal.tsx"
    click AuthContext "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/context/AuthContext.tsx"
    click useTheme "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/hooks/useTheme.ts"
    click libFirebase "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/lib/firebase.ts"
    click libFirebaseAuth "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/lib/firebaseAuth.ts"
    click ChatAPI "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/pages/api/chat.ts"
    click HelloAPI "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/pages/api/hello.ts"
    click AppWrapper "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/pages/_app.tsx"
    click Document "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/pages/_document.tsx"
    click HomePage "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/pages/index.tsx"
    click LoginPage "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/pages/login.tsx"
    click RegisterPage "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/pages/register.tsx"
    click DashboardPage "https://github.com/ayazsa/zerocode-fe-assignment/blob/main/pages/dashboard.tsx"

    classDef ui fill:#D0E8FF,stroke:#0366d6,color:#0366d6
    classDef api fill:#DFFFE0,stroke:#22863a,color:#22863a
    classDef external fill:#FFEDD5,stroke:#D15704,color:#D15704
    classDef storage fill:#F0F0F0,stroke:#666666,color:#333333
```
