254 lines
6.2 KiB
Markdown
254 lines
6.2 KiB
Markdown
# Newzy Frontend
|
|
|
|
Modern news platform built with React, TypeScript, Vite, and Tailwind CSS.
|
|
|
|
## Tech Stack
|
|
|
|
- **React** - UI library
|
|
- **TypeScript** - Type safety
|
|
- **Vite** - Build tool and dev server
|
|
- **Tailwind CSS** - Utility-first CSS framework
|
|
- **Lucide React** - Beautiful icon library
|
|
- **React Router v6.x** - Client-side routing
|
|
- **Vitest** - Unit testing framework
|
|
- **Playwright** - E2E testing
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
frontend/
|
|
├── public/ # Static assets and index.html
|
|
├── src/
|
|
│ ├── assets/ # Images, styles, logo
|
|
│ │ └── styles/ # Global CSS with Tailwind
|
|
│ ├── components/ # Reusable React components
|
|
│ │ ├── ui/ # Generic UI components (Header, Layout)
|
|
│ │ └── articles/ # Article-specific components
|
|
│ ├── pages/ # Page-level components (routes)
|
|
│ ├── types/ # TypeScript type definitions
|
|
│ ├── api/ # API client functions
|
|
│ └── tests/ # Unit tests (Vitest)
|
|
├── playwright/e2e/ # E2E tests (Playwright)
|
|
├── Dockerfile # Production build config
|
|
├── nginx.conf # Nginx configuration for serving
|
|
├── vite.config.ts # Vite configuration
|
|
├── package.json # Dependencies and scripts
|
|
└── tsconfig.json # TypeScript configuration
|
|
```
|
|
|
|
## Getting Started
|
|
|
|
### Prerequisites
|
|
|
|
- Node.js 18 or later
|
|
- npm, yarn, or pnpm
|
|
- Git
|
|
|
|
### Local Development
|
|
|
|
1. **Install dependencies**
|
|
```bash
|
|
npm install
|
|
```
|
|
|
|
2. **Start development server**
|
|
```bash
|
|
npm run dev
|
|
```
|
|
|
|
The app will open at http://localhost:5173
|
|
|
|
### Building for Production
|
|
|
|
```bash
|
|
npm run build
|
|
```
|
|
|
|
Build output will be in the `dist/` directory.
|
|
|
|
### Preview Production Build
|
|
|
|
```bash
|
|
npm run preview
|
|
```
|
|
|
|
## API Usage
|
|
|
|
The frontend connects to the backend via `/api` routes. The API client is configured in `src/api/client.ts`.
|
|
|
|
Available endpoints:
|
|
- `GET /api/articles` - List all articles
|
|
- `GET /api/articles/:id` - Get specific article by ID
|
|
- `GET /api/authors` - List all authors
|
|
- `GET /api/categories` - List all categories
|
|
|
|
## Testing
|
|
|
|
### Unit Tests (Vitest)
|
|
|
|
```bash
|
|
npm test
|
|
```
|
|
|
|
Tests are located in `src/tests/`.
|
|
|
|
### E2E Tests (Playwright)
|
|
|
|
First, install Playwright browsers:
|
|
```bash
|
|
npx playwright install
|
|
```
|
|
|
|
Run tests:
|
|
```bash
|
|
npm run e2e
|
|
```
|
|
|
|
## Lucide React Icons
|
|
|
|
This project uses [Lucide React](https://lucide.dev/) for beautiful SVG icons.
|
|
|
|
Available icons used in this project:
|
|
- **Home** - Home icon for navigation
|
|
- **FileText** - Articles/documents icon
|
|
- **Settings** - Settings/cog icon
|
|
- **Search** - Search magnifying glass
|
|
|
|
Import and use like:
|
|
```tsx
|
|
import { Home, FileText, Settings } from 'lucide-react';
|
|
|
|
function MyComponent() {
|
|
return (
|
|
<button>
|
|
<Home className="w-5 h-5" />
|
|
</button>
|
|
);
|
|
}
|
|
```
|
|
|
|
## Scripts Reference
|
|
|
|
| Command | Description |
|
|
|---------|-------------|
|
|
| `npm run dev` | Start development server on port 5173 |
|
|
| `npm run build` | Build for production (outputs to dist/) |
|
|
| `npm run preview` | Preview production build locally |
|
|
| `npm test` | Run Vitest unit tests |
|
|
| `npm run e2e` | Run Playwright E2E tests |
|
|
| `npm run lint` | Run ESLint on codebase |
|
|
|
|
## Docker Usage
|
|
|
|
### Build and Run Frontend Only
|
|
|
|
```bash
|
|
cd frontend
|
|
docker build -t newzy-frontend .
|
|
docker run -p 3000:80 newzy-frontend
|
|
```
|
|
|
|
### Full Application (with backend)
|
|
|
|
From the project root:
|
|
```bash
|
|
docker-compose up --build
|
|
```
|
|
|
|
This will start both frontend and backend services.
|
|
|
|
## Architecture
|
|
|
|
### Component Organization
|
|
|
|
**pages/** - Page-level components, one per route:
|
|
- `Home.tsx` → `/` main landing page
|
|
- `ArticlePage.tsx` → `/articles/:id` individual article view
|
|
- `CategoriesPage.tsx` → `/categories` categories listing
|
|
|
|
**components/ui/** - Generic UI with lucide icons:
|
|
- `Header.tsx` → Navigation with lucide icons + React Router `<Link>`
|
|
- `Layout.tsx` → Page wrapper structure
|
|
|
|
**components/articles/** - Domain-specific components:
|
|
- `ArticleCard.tsx` → Article preview for feeds
|
|
- `AuthorAvatar.tsx` → Author profile indicator
|
|
- `CategoryBadge.tsx` → Category label pill
|
|
|
|
### Routing
|
|
|
|
The app uses React Router v6.x for client-side navigation:
|
|
|
|
```tsx
|
|
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
|
|
|
|
function App() {
|
|
return (
|
|
<BrowserRouter>
|
|
<Routes>
|
|
<Route path="/" element={<Home />} />
|
|
<Route path="/articles" element={<ArticlePage />} />
|
|
</Routes>
|
|
</BrowserRouter>
|
|
);
|
|
}
|
|
```
|
|
|
|
Navigation uses `<Link>` components, not anchor tags:
|
|
|
|
```tsx
|
|
import { Link } from 'react-router-dom';
|
|
|
|
<Link to="/">Home</Link>
|
|
<Link to="/articles">Articles</Link>
|
|
```
|
|
|
|
## ESLint Configuration
|
|
|
|
ESLint rules are configured for React, TypeScript, and react-router best practices:
|
|
|
|
- No console.log in production code
|
|
- Proper async/await usage
|
|
- No any types unless necessary
|
|
- JSX props must be explicitly typed
|
|
- Link elements should use <Link>, not <a> tags
|
|
|
|
## Development Tips
|
|
|
|
1. **Component Naming**: Use PascalCase for components (Header, ArticleCard)
|
|
2. **File Organization**: Keep page-level logic in `pages/`, reusable UI in `components/ui/`
|
|
3. **Type Safety**: All props should be typed using TypeScript interfaces
|
|
4. **Icons**: Import lucide-react icons at the top level of their component
|
|
5. **API Calls**: Use the api client in `src/api/client.ts`, don't use raw fetch
|
|
|
|
## Environment Variables
|
|
|
|
The frontend currently doesn't require environment variables, but you can add them:
|
|
|
|
- `VITE_API_URL` - Override default API base URL
|
|
- `VITE_APP_VERSION` - Application version string
|
|
|
|
Example `.env.local`:
|
|
```bash
|
|
VITE_API_URL=http://localhost:8000/api
|
|
```
|
|
|
|
## Known Limitations
|
|
|
|
- No authentication flow implemented yet
|
|
- TypeScript path aliases not configured (using relative imports)
|
|
- Playwright E2E tests may fail when backend service not available
|
|
- React Router state preservation not implemented
|
|
- Static file serving requires nginx restart on rebuild
|
|
|
|
## Security Notes
|
|
|
|
- No sensitive data in client-side code
|
|
- API credentials should use backend proxy
|
|
- CORS must be configured on backend to accept frontend origin
|
|
- CSRF protection TBD for future milestones
|
|
|
|
## Contributing
|
|
|
|
See [../CONTRIBUTING.md](../CONTRIBUTING.md) for contribution guidelines.
|