|
| 1 | +# Frontend (`client/`) |
| 2 | + |
| 3 | +Built with **Vite + React + TypeScript** |
| 4 | + |
| 5 | +## Technologies |
| 6 | + |
| 7 | +- **Vite** - build tool and dev server |
| 8 | +- **React 19** - UI library |
| 9 | +- **TypeScript** - type safety across all components |
| 10 | + |
| 11 | +## Structure |
| 12 | + |
| 13 | +``` |
| 14 | +src/ |
| 15 | + types.ts - shared Message interface |
| 16 | + App.tsx / App.css - root component, state, API calls |
| 17 | + components/ |
| 18 | + MessageList.tsx / .css - scrollable list of messages |
| 19 | + MessageForm.tsx / .css - name input, message textarea, send button |
| 20 | +``` |
| 21 | + |
| 22 | +## Implementation |
| 23 | + |
| 24 | +`types.ts` |
| 25 | + |
| 26 | +- Defines the `Message` interface with `id`, `name`, `text`, and `timestamp` |
| 27 | + |
| 28 | +`App.tsx` |
| 29 | + |
| 30 | +- Fetches all messages from `GET /messages` on page load |
| 31 | +- Opens a WebSocket connection to listen for new messages in real time |
| 32 | +- Persists the username in `localStorage` so it survives page refresh |
| 33 | +- Passes `handleSend` down to `MessageForm` which calls `POST /messages` |
| 34 | + |
| 35 | +`MessageList.tsx` |
| 36 | + |
| 37 | +- Receives messages as props and renders each one with name, timestamp, and text |
| 38 | +- Auto-scrolls to the latest message using a `ref` on the bottom of the list |
| 39 | + |
| 40 | +`MessageForm.tsx` |
| 41 | + |
| 42 | +- Controlled inputs for name and message |
| 43 | +- Name is remembered between sessions, message clears after sending |
| 44 | +- Send button is disabled if either field is empty |
| 45 | +- Displays total message count as an additional feature |
| 46 | + |
| 47 | +## What I Learned |
| 48 | + |
| 49 | +Building this project showed how the frontend and backend need a clear contract to work together, the REST endpoints handle loading and sending data, while WebSocket handles the real-time layer, and understanding when to use each was the key challenge. |
0 commit comments