Carlos Eduardo Gatti Ferreira

ADR-001: Frontend Foundation Phase

Status: Implemented
Date: 2024-01-XX
Authors: Foundation Phase Implementation

Context

The frontend monorepo supports multiple domains:

The codebase had significant duplication in:

  1. GraphQL request handling (duplicated across qrackApi.ts, discartMeApi.ts, boxhubApi.ts)
  2. Authentication logic (separate contexts for each domain with nearly identical logic)

This created maintenance burden and risk of inconsistencies between domains.

Decision

Implement Phase 1: Foundation — introduce shared infrastructure to reduce duplication without changing user-facing behavior or touching production systems (Discart-me).

What Was Created

1. Shared GraphQL Client

File: src/lib/api/core/graphqlClient.ts

Benefits:

2. QRACK API Refactored

File: src/lib/qrackApi.ts

Preserved:

3. Generic Authentication Feature

Files:

Features:

Configuration:

{
  domain: 'qrack' | 'discart-me',
  appCode: 'QRACK' | 'DISCARD_ME',
  storageKeyPrefix: 'qrack' | 'discart',
  loginFn: (email, password) => Promise<AuthResponse>,
  registerFn: (payload) => Promise<AuthResponse>,
  getMeFn: (token) => Promise<User>,
}

4. QRACK Migration

Changes:

Result:

What Was Intentionally NOT Refactored

Discart-me

Other APIs

UI Components

Old Contexts

Architecture Decisions

1. Compatibility Layer

Decision: Created useQrackAuth compatibility hook instead of forcing pages to use useAuth('qrack') directly.

Rationale:

2. Domain Configuration

Decision: Used configuration object pattern for AuthProvider instead of separate providers per domain.

Rationale:

3. Framework-Agnostic GraphQL Client

Decision: Created pure TypeScript client with no React dependencies.

Rationale:

Known Follow-ups for Phase 2

  1. Migrate Discart-me to use shared GraphQL client
    • Refactor discartMeApi.ts to use graphqlClient
    • Preserve all error handling and CORS logic
  2. Migrate Discart-me to use generic auth (optional)
    • Only if beneficial (may have domain-specific requirements)
    • Must be done carefully to avoid production issues
  3. Migrate BoxHub (if still active)
    • Similar pattern to QRACK migration
  4. Remove old contexts
    • Delete src/context/QrackAuthContext.tsx
    • Document migration path for Discart-me if applicable
  5. Remove compatibility layer
    • Update QRACK pages to use useAuth('qrack') directly
    • Remove useQrackAuth hook
  6. Component extraction (if needed)
    • Extract shared UI components
    • Standardize form patterns

Success Criteria

✅ QRACK works exactly as before
✅ Discart-me works exactly as before
✅ Code duplication reduced
✅ Foundation ready for Phase 2
✅ No visual or routing changes
✅ No breaking changes

Testing Recommendations

  1. QRACK:
    • Login works
    • Token persists across page reloads
    • Protected routes redirect correctly
    • Registration works
    • Logout clears state
  2. Discart-me:
    • Login works
    • Can browse items
    • Can create items
    • No console errors

Risks Mitigated

  1. Production Impact: Discart-me completely untouched
  2. Breaking Changes: Compatibility layer maintains interface
  3. Visual Changes: No UI modifications
  4. Route Changes: No routing modifications

Notes