A React application that provides interactive statistics and visualizations for trivia questions from the Open Trivia Database API.
- Category Selection: Browse and select from various trivia categories
- Interactive Statistics: View data visualizations for:
- Distribution by difficulty level (Easy, Medium, Hard)
- Distribution by question type (Multiple Choice, True/False)
- Distribution by category (when viewing all categories)
- Real-time Data: Fetches fresh data from the Open Trivia Database API
- Responsive Design: Modern UI with CSS modules and responsive layout
- Loading States: Skeleton loading components for better UX
- Error Handling: Graceful error handling with retry logic for rate limits
- React 18 with TypeScript
- Vite for build tooling
- React Router for navigation
- Recharts for data visualization
- Axios for API requests
- CSS Modules for styling
- ESLint for code quality
- Node.js (version 16 or higher)
- npm or yarn
- Clone the repository:
git clone <repository-url>
cd jetbrains_task- Install dependencies:
npm install- Start the development server:
npm run dev- Open your browser and navigate to
http://localhost:5173
npm run dev- Start development servernpm run build- Build for productionnpm run preview- Preview production buildnpm run lint- Run ESLint
src/
├── api/ # API layer
│ └── api.ts # API functions and hooks
├── components/ # Reusable UI components
│ ├── CategoriesList/ # Category selection component
│ ├── Statistics/ # Main statistics container
│ ├── DistributionBy*/ # Chart components for different data types
│ ├── Header/ # Application header
│ ├── Skeleton/ # Loading skeleton component
│ └── ui/ # Basic UI components (Button)
├── hooks/ # Custom React hooks
│ └── useFetch.ts # API fetching hook with retry logic
├── pages/ # Page components
│ ├── Home.tsx # Main application page
│ └── NotFound/ # 404 error page
├── types/ # TypeScript type definitions
├── utils/ # Utility functions
├── consts/ # Application constants
└── main.tsx # Application entry point
The application integrates with the Open Trivia Database API to fetch trivia questions and generate statistics. The API provides:
- Questions across multiple categories
- Different difficulty levels (easy, medium, hard)
- Various question types (multiple choice, true/false)
The application uses environment variables for API configuration:
VITE_BASE_URL: Base URL for the Open Trivia Database API
The src/api/api.ts file contains:
useFetchQuestions(categoryId?): Fetches trivia questions (50 questions, optionally filtered by category)useFetchCategories(): Fetches available trivia categories
Both functions use the custom useFetch hook with built-in retry logic and error handling.
Centralized API functions that handle all external data fetching:
useFetchQuestions(categoryId?): Fetches 50 trivia questions, optionally filtered by categoryuseFetchCategories(): Fetches the list of available trivia categories- Uses environment variables for flexible API endpoint configuration
- Built on top of the custom
useFetchhook for consistent error handling
The main component that orchestrates data fetching and visualization. It:
- Uses the API layer to fetch questions and categories
- Groups data by difficulty, type, and category
- Renders appropriate charts based on the selected category
A custom hook that handles API requests with:
- Loading states
- Error handling
- Automatic retry logic for rate limiting (429 errors)
- Cleanup to prevent memory leaks
Three specialized chart components using Recharts:
DistributionByDifficulty: Bar chart showing question distribution by difficultyDistributionByType: Bar chart showing question distribution by typeDistributionByCategory: Bar chart showing question distribution by category
The application uses CSS Modules for component-scoped styling, ensuring:
- No style conflicts
- Better maintainability
- Improved performance
The application includes comprehensive error handling:
- Network error recovery
- API rate limit handling with exponential backoff
- User-friendly error messages
- Graceful fallbacks for missing data
For production deployment, ensure the following environment variable is set:
VITE_BASE_URL: The base URL for the Open Trivia Database API
The application will use the default value https://opentdb.com/ if the environment variable is not set.
Netlify:
- Go to Site settings → Environment variables
- Add
VITE_BASE_URLwith valuehttps://opentdb.com/
Vercel:
- Go to Project settings → Environment Variables
- Add
VITE_BASE_URLwith valuehttps://opentdb.com/
Heroku:
heroku config:set VITE_BASE_URL=https://opentdb.com/The application uses Vite for building:
npm run buildThis creates an optimized production build in the dist/ directory.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.