File size: 6,862 Bytes
cbf89ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# LLM Guide: NLP-IBM-Debater Workspace

This repository is a React + TypeScript (Create React App) workspace prepared with Tailwind CSS and a modular app layout. Use this guide as the single source of truth when making automated or AI-assisted changes.

## Project map

- `src/app/App.tsx` β€” entry composition (TypeScript).
- `src/app/layouts/MainLayout.tsx` β€” basic layout wrapper (TypeScript).
- `src/app/pages/` β€” page components (use `.tsx` extension).
- `src/app/components/` β€” reusable UI pieces (common/navigation, use `.tsx` extension).
  - `common/ErrorBoundary.tsx` β€” error boundary component (class component, required by React).
  - `common/Loading.tsx` β€” loading spinner component.
- `src/app/services/api-wrapper.ts` β€” centralized API client with fetch wrapper (GET/POST/PUT/PATCH/DELETE).
- `src/app/hooks/` β€” custom React hooks (use `.ts` extension).
  - `useApi.ts` β€” hook for API calls with loading/error states.
- `src/app/types/` β€” TypeScript type definitions (use `.ts` extension).
  - `api.types.ts` β€” API-related types.
  - `index.ts` β€” central export point for all types.
- `src/app/utils/` β€” utility functions (use `.ts` extension, debounce, formatError, etc.).
- `src/app/constants/` β€” app-wide constants (use `.ts` extension, API endpoints, config).
- `src/index.js` β€” CRA entry point mounting `App` with ErrorBoundary.
- `tailwind.config.js` & `postcss.config.js` β€” Tailwind setup.
- `.env.example` β€” environment variables template (all `.env*` files are gitignored).

## Conventions

- **File Extensions**: 
  - Use `.tsx` for React components (components, pages, layouts)
  - Use `.ts` for non-JSX TypeScript files (hooks, utils, types, services)
  - Use `.js` only for config files (index.js, config files)
- **TypeScript**: This project uses TypeScript for type safety. Always type your props, functions, and API responses.
- Prefer feature-first folders under `src/app` (`components`, `pages`, `layouts`, `data`, `hooks`, `services` as needed).
- Styling is Tailwind-first; avoid ad-hoc CSS files unless adding global tokens.
- Keep components presentational and pass data via props; co-locate tests next to features or under `__tests__`.
- Use functional components and hooks; avoid class components (except ErrorBoundary which requires a class).
- Keep imports relative to `src/` (CRA default) and prefer absolute paths only if `NODE_PATH`/aliases are introduced.

## Commands

- Install deps: `npm install`
- Start dev server: `npm start` (loads `.env.development`)
- Build for prod: `npm run build` (loads `.env.production`)
- Run tests: `npm test`

## Environment Variables

- **`.env.example`** β€” Template showing required variables (committed to git)
- **All `.env*` files** β€” Gitignored for security (create from `.env.example`)

**Important**: All environment variables exposed to the browser must start with `REACT_APP_` prefix.

**Setup**:
1. Copy `.env.example` to create your local env files:
   - Development: `cp .env.example .env.development.local`
   - Production: `cp .env.example .env.production.local`
2. Update the values in your local `.env.*.local` files

Current variables:
- `REACT_APP_API_BASE_URL` β€” API base URL (defaults to `http://localhost:4000` if not set)

To add new variables:
1. Add to `.env.example` as documentation
2. Update your local `.env.*.local` files
3. Access via `process.env.REACT_APP_YOUR_VAR`

## Patterns to follow

- Add new UI to `src/app/pages` (use `.tsx` extension) and wire via `App.tsx`.
- Reuse `MainLayout` for consistent shell elements.
- Keep static strings in `src/app/data` for reuse/testing.
- **API calls**: 
  - Use `src/app/services/api-wrapper.ts` for direct API calls: `api.get()`, `api.post()`, etc.
  - Use `useApi` hook from `src/app/hooks/useApi.ts` for components that need loading/error states.
- **Types**: Add new TypeScript types to `src/app/types/` (use `.ts` extension) and export from `index.ts`.
- **Constants**: Add app-wide constants to `src/app/constants/index.ts`.
- **Utils**: Add reusable utility functions to `src/app/utils/index.ts`.
- **Component Props**: Always type your component props using TypeScript interfaces or types.
- When adding new services, create `src/app/services/*` (use `.ts` extension) and keep fetchers pure; inject into components via hooks or props.

## Quality and safety

- Use React Testing Library for behavior-first tests.
- Avoid editing generated configs unless necessary; prefer small, reviewable diffs.
- Document non-obvious decisions inline with concise comments.

## API Service Usage

### Direct API calls

The `api-wrapper.ts` service provides a typed, centralized way to make HTTP requests:

```typescript
import api from '../services/api-wrapper';

// GET request
const data = await api.get<User[]>('/users');

// POST request
const newUser = await api.post<User, CreateUserDto>('/users', { name: 'John' });

// With error handling
try {
  const result = await api.get('/endpoint');
} catch (error) {
  // error is typed as ApiError with status, message, details
  console.error(error.status, error.message);
}
```

### Using the useApi hook

For components that need loading and error states:

```typescript
import { useApi } from '../hooks/useApi';
import Loading from '../components/common/Loading';
import type { User } from '../types';

const MyComponent: React.FC = () => {
  const { data, loading, error, refetch } = useApi<User[]>('/api/users');
  
  if (loading) return <Loading />;
  if (error) return <div>Error: {error.message}</div>;
  
  return <div>{/* render data */}</div>;
};
```

The service automatically:
- Uses `REACT_APP_API_BASE_URL` from environment variables
- Sets JSON content-type headers
- Handles JSON parsing
- Throws typed errors for non-OK responses
- Supports AbortSignal for request cancellation

## TypeScript Guidelines

- **Components**: Always type props using interfaces or types
  ```typescript
  type MyComponentProps = {
    title: string;
    count?: number;
  };
  
  const MyComponent: React.FC<MyComponentProps> = ({ title, count = 0 }) => {
    // ...
  };
  ```

- **API Responses**: Type your API responses
  ```typescript
  type User = {
    id: number;
    name: string;
  };
  
  const users = await api.get<User[]>('/api/users');
  ```

- **Hooks**: Type hook return values and parameters
  ```typescript
  const { data, loading } = useApi<Debate[]>('/api/debates');
  ```

## How to help

- Before large refactors, note intent and file targets in the PR description.
- After code edits, run tests or `npm run build` to verify TypeScript compilation.
- When adding API endpoints, use the `api-wrapper.ts` service instead of raw `fetch`.
- Always use TypeScript types for props, API responses, and function parameters.
- Keep this guide updated when changing structure, commands, or adding new services.