Files
Mohammed Abad d28b4caa7e docs(react_frontend): add comprehensive documentation & AGENTS.md
- docs/README.md: overview and table of contents
- docs/tech_stack.md: full dependency inventory and design decisions
- docs/project_structure.md: directory tree with conventions
- docs/architecture.md: data flow, layer responsibilities, key patterns
- docs/routing.md: route table, code splitting, discover categories
- docs/state_management.md: Zustand stores and React Query conventions
- docs/api_layer.md: all API modules with endpoint mappings
- docs/components.md: layout, entries, collections, charts, dialogs, UI primitives
- docs/pages.md: all 16 pages with features and routes
- docs/styling.md: theming, CSS tokens, responsive breakpoints, animations
- AGENTS.md: architecture summary, code conventions, data flow, key files
2026-04-10 02:44:06 +01:00

4.7 KiB

Architecture

Data Flow

┌──────────────┐     ┌───────────────┐     ┌──────────────────┐     ┌──────────────┐
│   Pages /    │────▶│  React Query  │────▶│   API modules    │────▶│   Backend    │
│  Components  │     │   Hooks       │     │  (src/api/*.ts)  │     │  REST API    │
│              │◀────│  (src/hooks/) │◀────│                  │◀────│              │
└──────┬───────┘     └───────────────┘     └──────────────────┘     └──────────────┘
       │                                           │
       │  reads / writes                           │ normalizeEntry()
       ▼                                           │ normalizeCollection()
┌──────────────┐                                   │
│   Zustand    │◀──────────────────────────────────┘
│   Stores     │   (library store updated on fetch & mutation)
└──────────────┘
  1. Pages call React Query hooks (e.g. useLibrary(), useEntry(id)).
  2. Hooks invoke API module functions which make Axios HTTP requests to the backend.
  3. Responses are run through normaliser functions (normalizeEntry, normalizeCollection) to transform snake_case backend data and alias inconsistencies into a consistent frontend shape.
  4. On library-level fetches and mutations, the Zustand library store is also updated so that all components reflect changes instantly without waiting for cache invalidation.

Layer Responsibilities

Layer Directory Responsibility
Pages pages/ Route entry-points. Compose components, call hooks, manage local UI state (search, filters, dialogs)
Components components/ Reusable UI building blocks. Receive data via props. Emit events via callbacks
Hooks hooks/ Encapsulate React Query queries & mutations. Define cache keys, stale times, invalidation
API api/ Thin Axios wrappers. One function per endpoint. Run normalisation on responses
Stores stores/ Zustand global state. Library entry cache (Map), theme preference
Lib lib/ Pure utility functions — class merging, media URL building, data normalisation
Types types/ All TypeScript interfaces and type aliases
UI components/ui/ shadcn/ui primitives — buttons, dialogs, inputs, etc.

Key Patterns

Normalisation

Backend responses use a mix of casing conventions and singular/plural aliases. The normaliser layer (lib/normalizers.ts) transforms these into a consistent frontend shape:

  • entryRating → entryRatings
  • entryId → entryIds
  • entrySeen → entrySeens
  • Snake-case object keys preserved for domain fields; camelCase for discriminators

Lazy Loading

All pages are loaded with React.lazy() and wrapped in <Suspense> with route-specific skeleton fallbacks. This code-splits each page into a separate chunk.

Infinite Queries

TMDB browse endpoints (trending, popular, top-rated, search) use React Query's useInfiniteQuery with:

  • initialPageParam: 1
  • getNextPageParam: (lastPage) => lastPage.page < lastPage.total_pages ? lastPage.page + 1 : undefined
  • Pages are flattened via .data?.pages.flatMap(p => p.results)

Library Merge

When displaying TMDB entries alongside library entries (e.g. on the Home or Discover pages), the EntryCard component checks the Zustand library store by TMDB ID. If an entry is already in the library, the richer local version (with ratings, favourite status, etc.) is displayed instead of the raw TMDB data.

Mutation Optimism

Mutations (add/remove/update) update the Zustand store immediately on success, then invalidate related React Query caches for eventual consistency.