| name | toucan-codebase-architecture |
| description | React SPA codebase architecture guide. Use when scaffolding a new feature, placing files, wiring up an API hook, adding a Zustand store, protecting a route, handling errors, or optimising performance. Covers the complete vertical slice from API to UI using a feature-driven architecture. Triggers on any task involving project structure, API design, state management, security, components, error handling, performance, deployment, or testing in a React + Vite + React Query codebase.
|
| license | MIT |
| metadata | {"author":"thinkthroo","version":"1.0.0","series":"amazon-rainforest"} |
Toucan โ Codebase Architecture
A comprehensive architecture guide for production-grade React SPAs, demonstrated
through a feature-driven architecture with React, Vite, React Query, Zustand,
and React Hook Form.
All code examples use generic Resource, createResource, useResources, etc.
so the patterns transfer cleanly to any domain โ swap the entity name for your own.
When to Apply
Reference these guidelines when:
- Scaffolding a new feature end-to-end (API hook โ store โ UI)
- Deciding where a new file belongs
- Writing or reviewing React Query hooks and API clients
- Creating or refactoring Zustand stores
- Protecting routes or implementing authorization
- Adding error handling and observability
- Optimising re-renders or bundle size
- Setting up tests or CI/CD pipelines
Rule Categories by Priority
| Priority | Category | Impact | Prefix | Rule file |
|---|
| 1 | Project Structure | CRITICAL | struct- | rules/project-structure.md |
| 2 | API Layer | CRITICAL | api- | rules/api-layer.md |
| 3 | Security | HIGH | sec- | rules/security.md |
| 4 | State Management | MEDIUM-HIGH | state- | rules/state-management.md |
| 5 | Component Structure | MEDIUM | comp- | rules/component-structure.md |
| 6 | Error Handling | MEDIUM | err- | rules/error-handling.md |
| 7 | Performance | LOW-MEDIUM | perf- | rules/performance.md |
| 8 | Project Standards | LOW-MEDIUM | standards- | rules/project-standards.md |
| 9 | Deployment | LOW | deploy- | rules/deployment.md |
| 10 | Testing | LOW | test- | rules/testing.md |
Quick Reference
1. Project Structure (CRITICAL)
struct-directory-layout โ Feature-driven layout with src/features/ as the core
struct-data-flow โ Unidirectional: shared โ features โ app
struct-feature-modules โ One self-contained module per feature
struct-no-cross-feature โ Features never import from other features
struct-file-placement โ Decision table for where every type of file belongs
2. API Layer (CRITICAL)
api-single-client โ One pre-configured Axios instance in src/lib/api-client.ts
api-interceptors โ Auth headers, response unwrapping, and error notifications globally
api-query-pattern โ Fetcher function + queryOptions factory + useQuery hook
api-mutation-pattern โ Zod schema + mutation function + cache invalidation hook
api-prefetching โ Prefetch query data on hover for snappy navigation
3. Security (HIGH)
sec-token-storage โ Tokens stored in HttpOnly cookies; never localStorage
sec-auth-config โ react-query-auth wraps all auth mutations and state
sec-protected-routes โ ProtectedRoute guard wraps all authenticated routes
sec-rbac โ Role-based access with useAuthorization hook and Authorization component
sec-pbac โ Policy-based access for resource-level permission checks
sec-xss-prevention โ Always sanitize user-generated HTML with DOMPurify
4. State Management (MEDIUM-HIGH)
state-categories โ Five distinct categories: component, application, server cache, form, URL
state-component-state โ Start with useState/useReducer; elevate only when needed
state-global-ui โ Zustand for global UI state (notifications, modals, theme)
state-server-cache โ React Query exclusively for all server data; never Zustand for API data
state-form-state โ React Hook Form + Zod for all form state and validation
state-url-state โ React Router params and search params for bookmarkable state
state-decision-tree โ Decision tree for choosing the right state tool
5. Component Structure (MEDIUM)
comp-shared-vs-feature โ Shared in src/components/, feature-scoped in src/features/<f>/components/
comp-no-nested-render โ Never write nested render functions; extract into components
comp-composition-pattern โ Use children/slots instead of excessive props
comp-tailwind-cn โ Tailwind CSS + cn() helper for all styling
comp-shadcn-pattern โ ShadCN UI for component library; components are owned code
comp-form-pattern โ Abstracted Form component built on React Hook Form
6. Error Handling (MEDIUM)
err-central-interceptor โ All API errors caught in Axios response interceptor
err-error-boundaries โ App-level, route-level, and component-level error boundaries
err-query-errors โ React Query isError state for inline error display
err-sentry โ Sentry for production error tracking with source maps
7. Performance (LOW-MEDIUM)
perf-code-splitting โ Route-level lazy() imports for bundle splitting
perf-state-optimizations โ Split state, keep state local, lazy initializers
perf-children-optimization โ Pass stable children to avoid re-renders
perf-image-optimization โ Lazy loading, WEBP format, responsive srcset
perf-prefetching โ Prefetch on hover; route loaders for data-before-render
8. Project Standards (LOW-MEDIUM)
standards-eslint โ ESLint with import restriction rules and naming conventions
standards-prettier โ Prettier with format-on-save
standards-typescript โ TypeScript strict mode; no any
standards-husky โ Husky + lint-staged pre-commit hooks
standards-absolute-imports โ @/ prefix for all src/ imports
9. Deployment (LOW)
deploy-platform โ Recommended platforms (Vercel, Netlify, AWS CloudFront)
deploy-env-vars โ Validated env module; never import.meta.env directly
deploy-ci-cd โ CI/CD pipeline: lint โ typecheck โ test โ build โ E2E โ deploy
deploy-caching โ Long-lived static assets; no-cache HTML
deploy-preview โ Preview deployments for every pull request
10. Testing (LOW)
test-strategy โ Integration-first: unit < integration < E2E
test-unit โ Pure functions, utilities, and complex isolated components
test-integration โ Every feature route tested with MSW-mocked API
test-e2e โ Critical user journeys with Playwright
test-msw โ MSW for HTTP-level mocking; never mock modules directly
How to Use
Read a rule file for the detailed explanation and code examples:
rules/project-structure.md โ struct-* rules
rules/api-layer.md โ api-* rules
rules/security.md โ sec-* rules
rules/state-management.md โ state-* rules
rules/component-structure.md โ comp-* rules
rules/error-handling.md โ err-* rules
rules/performance.md โ perf-* rules
rules/project-standards.md โ standards-* rules
rules/deployment.md โ deploy-* rules
rules/testing.md โ test-* rules
Each rule contains:
- Why it matters โ the problem it prevents
- Bad example โ what not to do, with explanation
- Good example โ the correct pattern, with explanation
Full Compiled Document
For all rules expanded in one flat document: AGENTS.md