Carlos Eduardo Gatti Ferreira

Architecture Overview

This document describes the high-level architecture of the frontend monorepo, how applications interact, and the design patterns used throughout the codebase.

System Overview

This is a monorepo containing multiple Next.js applications that share code, components, and infrastructure while maintaining clear boundaries for independent operation.

Core Principles

  1. Code Reuse — Share logic, components, and utilities across products
  2. Clear Boundaries — Each app is independent but can leverage shared infrastructure
  3. Consistent Patterns — Same patterns and conventions across all applications
  4. Progressive Enhancement — New apps benefit from shared improvements

Application Architecture

High-Level Diagram

┌─────────────────────────────────────────────────────────┐
│                   Frontend Monorepo                      │
├─────────────────────────────────────────────────────────┤
│                                                           │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  │
│  │ Personal     │  │ Discart-me   │  │ QRACK        │  │
│  │ Website      │  │ (Marketplace)│  │ (Inventory)  │  │
│  └──────┬───────┘  └──────┬───────┘  └──────┬───────┘  │
│         │                  │                  │          │
│         └──────────────────┼──────────────────┘          │
│                            │                             │
│         ┌──────────────────▼──────────────────┐          │
│         │      Shared Infrastructure           │          │
│         │  • Components (UI, Features)        │          │
│         │  • Utilities (API clients, hooks)   │          │
│         │  • Types & Interfaces               │          │
│         │  • Design System (Tailwind)         │          │
│         └──────────────────┬──────────────────┘          │
│                            │                             │
│         ┌──────────────────▼──────────────────┐          │
│         │      Backend APIs                    │          │
│         │  • REST (Discart-me)                 │          │
│         │  • GraphQL (QRACK, BoxHub)           │          │
│         └──────────────────────────────────────┘          │
└─────────────────────────────────────────────────────────┘

Application Boundaries

Personal Website

Router: Pages Router (/pages)
State Management: Local component state, React Context
Data Fetching: Static generation, API routes
Purpose: Public-facing portfolio and blog

Key Characteristics:

Discart-me

Router: App Router (/app/discart-me)
State Management: Context API (DiscartAuthContext)
Data Fetching: REST API with fetch
Purpose: Community marketplace

Key Characteristics:

QRACK

Router: App Router (/app/qrack)
State Management: Generic Auth Provider (QrackAuthProvider)
Data Fetching: GraphQL API
Purpose: Container and inventory management

Key Characteristics:


Shared Infrastructure

Components

Location: /src/components and /components

Organization:

Guidelines:

Features

Location: /src/features

Feature modules encapsulate related functionality:

Pattern:

features/
  {domain}/
    components/     # Feature-specific UI
    hooks/          # Feature-specific logic
    types.ts        # Feature-specific types

Libraries & Utilities

Location: /src/lib

Core Libraries:

Utilities:


Data Flow

Authentication Flow

User Login
    │
    ├─► Domain-specific auth provider (e.g., QrackAuthProvider)
    │   │
    │   ├─► Calls generic AuthProvider with domain config
    │   │
    │   ├─► API call (REST or GraphQL)
    │   │
    │   ├─► Token stored in localStorage
    │   │
    │   └─► User data stored in context
    │
    └─► Protected routes check auth status

Data Fetching Flow

Discart-me (REST):

Component
    │
    ├─► Calls function in discartMeApi.ts
    │   │
    │   ├─► fetch() with Authorization header
    │   │
    │   ├─► Parse JSON response
    │   │
    │   └─► Return typed data

QRACK (GraphQL):

Component
    │
    ├─► Calls function in qrackApi.ts
    │   │
    │   ├─► Uses shared graphqlClient.ts
    │   │   │
    │   │   ├─► Constructs GraphQL query
    │   │   ├─► Adds Authorization header
    │   │   ├─► Handles errors
    │   │   └─► Returns typed response

Routing Strategy

App Router (Next.js 13+)

Used by: Discart-me, QRACK

Structure:

app/
  {domain}/
    layout.tsx        # Domain-specific layout
    page.tsx          # Home page
    {route}/
      page.tsx        # Route page
      layout.tsx      # Nested layout (optional)

Benefits:

Pages Router (Legacy)

Used by: Personal website

Structure:

pages/
  index.tsx           # Home page
  {route}.tsx         # Route page
  api/
    {endpoint}.tsx    # API route

Migration Plan: Gradually migrate to App Router (future work)


State Management

Local Component State

Most components use useState for local, component-scoped state.

Context API

Used for:

Pattern:

// Create context
const MyContext = createContext<MyContextValue | null>(null);

// Provider component
export function MyProvider({ children }: { children: ReactNode }) {
  const [state, setState] = useState<State>(initialState);
  const value = useMemo(() => ({ state, setState }), [state]);
  return <MyContext.Provider value={value}>{children}</MyContext.Provider>;
}

// Custom hook
export function useMyContext() {
  const context = useContext(MyContext);
  if (!context) throw new Error('Must be used within MyProvider');
  return context;
}

React Query (Future)

Some features use @tanstack/react-query for server state management (e.g., comments, ratings). This will be expanded for consistent data fetching patterns.


Error Handling

API Errors

GraphQL:

REST:

UI Error Handling


Performance Considerations

Code Splitting

Image Optimization

Caching


Security

Authentication

API Security


Testing Strategy

Current State: Manual testing, no automated tests yet

Future Plans:


Future Architecture Considerations

Planned Improvements

  1. Consolidate Authentication — Migrate all apps to shared auth system
  2. Expand React Query — Consistent server state management
  3. Micro-frontends — Evaluate for larger scale (if needed)
  4. Design Tokens — Formalize design system tokens
  5. Storybook — Component library documentation and testing

Scalability

The current architecture supports: