Spaces:
Running
Running
File size: 10,957 Bytes
1bcf29b |
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# π Building a React App with shadcn/ui Styling & Sorting Tables
## π Table of Contents
1. [Project Overview](#project-overview)
2. [Prerequisites](#prerequisites)
3. [Step-by-Step Build Process](#step-by-step-build-process)
4. [File Structure & Purpose](#file-structure--purpose)
5. [Key Implementation Details](#key-implementation-details)
6. [Customization Guide](#customization-guide)
7. [Deployment to Hugging Face](#deployment-to-hugging-face)
8. [Troubleshooting](#troubleshooting)
---
## π― Project Overview
This guide shows you how to build a **MongoDB Data Viewer** - a React application with:
- β¨ **Modern shadcn/ui styling** (custom CSS implementation)
- π **Interactive data tables** with sorting functionality
- π **Real-time data refresh** capabilities
- π± **Responsive design** for all devices
- π¨ **Professional UI/UX** without external CSS frameworks
**Why This Approach?**
- **No Tailwind CSS dependencies** - avoids compatibility issues
- **Custom CSS variables** - easy theming and customization
- **Modular components** - maintainable and scalable
- **Clean architecture** - follows React best practices
---
## π§ Prerequisites
### **Required Software:**
- **Node.js** (version 14+ recommended)
- **npm** (comes with Node.js)
- **Git** (for version control)
- **Code editor** (VS Code recommended)
### **Knowledge Requirements:**
- Basic **React** concepts (components, hooks, state)
- Basic **JavaScript** (ES6+, async/await)
- Basic **CSS** understanding
- Basic **Git** commands
---
## ποΈ Step-by-Step Build Process
### **Phase 1: Project Setup**
#### **Step 1: Create React App**
```bash
npx create-react-app mongodb-data-viewer
cd mongodb-data-viewer
```
#### **Step 2: Clean Up Default Files**
Remove unnecessary files:
- `src/logo.svg`
- `src/reportWebVitals.js`
- `src/setupTests.js`
- `public/logo*.png`
- `public/manifest.json`
- `public/robots.txt`
#### **Step 3: Install Dependencies**
```bash
npm install @radix-ui/react-slot
npm install -D tailwindcss postcss autoprefixer
```
### **Phase 2: CSS Architecture**
#### **Step 4: Create Custom CSS System**
Create `src/index.css` with:
- CSS variables for theming
- Utility classes (similar to Tailwind)
- Component-specific styles
- Responsive design rules
#### **Step 5: Set Up PostCSS (Optional)**
```bash
npx tailwindcss init -p
```
*Note: We'll use custom CSS instead of Tailwind*
### **Phase 3: Component Architecture**
#### **Step 6: Create Utility Functions**
Create `src/lib/utils.js`:
```javascript
import { clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs) {
return twMerge(clsx(inputs))
}
```
#### **Step 7: Build UI Components**
Create component files:
- `src/components/ui/button.jsx`
- `src/components/ui/table.jsx`
#### **Step 8: Create Main Data Component**
Create `src/components/DataTable.jsx` with:
- State management for data
- Sorting functionality
- Loading states
- Error handling
### **Phase 4: App Integration**
#### **Step 9: Update Main App**
Modify `src/App.js` to:
- Import custom components
- Use custom CSS classes
- Create responsive layout
#### **Step 10: Configure Entry Point**
Update `src/index.js` to:
- Remove unused imports
- Import custom CSS
- Render main App component
---
## π File Structure & Purpose
```
mongodb-data-viewer/
βββ .git/ # Git repository
βββ .gitignore # Git ignore rules
βββ package.json # Dependencies & scripts
βββ package-lock.json # Locked dependency versions
βββ README.md # Project documentation
βββ BUILD_GUIDE.md # This guide
βββ public/ # Static assets
β βββ index.html # Main HTML template
β βββ favicon.ico # Browser icon
βββ src/ # Source code
βββ index.js # App entry point
βββ index.css # Custom CSS system
βββ App.js # Main app component
βββ App.css # Basic React styling (required for Streamlit)
βββ App.test.js # Test file
βββ components/ # Reusable components
βββ DataTable.jsx # Main data display component
βββ ui/ # UI component library
βββ button.jsx # Button component
βββ table.jsx # Table components
```
### **File Purpose Breakdown:**
#### **Root Level Files:**
- **`package.json`**: Defines dependencies, scripts, and project metadata
- **`.gitignore`**: Specifies which files Git should ignore
- **`README.md`**: Project documentation and Hugging Face configuration
#### **Public Directory:**
- **`index.html`**: HTML template that React renders into
- **`favicon.ico`**: Browser tab icon (required for professional appearance)
#### **Source Directory:**
- **`index.js`**: JavaScript entry point that bootstraps the React app
- **`index.css`**: Custom CSS system with shadcn/ui design tokens
- **`App.js`**: Main application component that orchestrates the UI
- **`App.css`**: Basic React styling (required for Streamlit compatibility)
- **`App.test.js`**: Test file for the main App component
#### **Components Directory:**
- **`DataTable.jsx`**: Core business logic for displaying and sorting data
- **`ui/button.jsx`**: Reusable button component with consistent styling
- **`ui/table.jsx`**: Table components for structured data display
---
## π Key Implementation Details
### **1. Custom CSS System**
```css
:root {
--background: #ffffff;
--foreground: #020817;
--primary: #0f172a;
--primary-foreground: #f8fafc;
/* ... more variables */
}
```
**Why Custom CSS?**
- **No framework dependencies** - avoids compatibility issues
- **Full control** - customize every aspect of styling
- **Performance** - no unused CSS classes
- **Maintainability** - clear, organized structure
### **2. Component State Management**
```javascript
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [sortConfig, setSortConfig] = useState({ key: null, direction: 'asc' });
```
**State Structure:**
- **`data`**: Array of objects to display in table
- **`loading`**: Boolean for loading states
- **`error`**: String for error messages
- **`sortConfig`**: Object tracking sort column and direction
### **3. Sorting Algorithm**
```javascript
const sortData = (key) => {
let direction = 'asc';
if (sortConfig.key === key) {
if (sortConfig.direction === 'asc') {
direction = 'desc';
} else if (sortConfig.direction === 'desc') {
setSortConfig({ key: null, direction: 'none' });
return;
}
}
setSortConfig({ key, direction });
};
```
**Three-State Sorting:**
1. **Ascending** (β): First click, sorts AβZ
2. **Descending** (β): Second click, sorts ZβA
3. **None** (βοΈ): Third click, returns to original order
### **4. Responsive Design**
```css
.container {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-left: 2rem;
padding-right: 2rem;
}
@media (min-width: 1400px) {
.container {
max-width: 1400px;
}
}
```
**Responsive Features:**
- **Mobile-first** approach
- **Flexible containers** that adapt to screen size
- **Consistent spacing** across devices
- **Touch-friendly** interactions
---
## π¨ Customization Guide
### **Changing Colors**
Modify CSS variables in `src/index.css`:
```css
:root {
--primary: #your-color-here;
--background: #your-bg-color;
/* ... */
}
```
### **Adding New Columns**
1. **Update mock data** in `DataTable.jsx`
2. **Add table header** with sorting capability
3. **Add table cell** for new data
4. **Update sorting logic** if needed
### **Modifying Table Styling**
Edit table-specific CSS in `src/index.css`:
```css
.table th {
/* Custom header styles */
}
.table td {
/* Custom cell styles */
}
```
### **Adding New Features**
- **Search**: Add input field and filter logic
- **Pagination**: Implement page-based data loading
- **Export**: Add CSV/PDF download functionality
- **Charts**: Integrate data visualization libraries
---
## π Deployment to Hugging Face
### **1. Configure README.md**
```yaml
---
title: MongoDB Data Viewer
emoji: π
colorFrom: blue
colorTo: indigo
sdk: static
pinned: false
app_build_command: npm run build
app_file: build/index.html
---
```
### **2. Build the App**
```bash
npm run build
```
### **3. Push to Git Repository**
```bash
git add .
git commit -m "Add MongoDB data viewer with sorting"
git push origin main
```
### **4. Hugging Face Integration**
- **Create new Space** on Hugging Face
- **Connect Git repository**
- **Automatic deployment** will begin
- **Monitor build logs** for any issues
---
## π§ Troubleshooting
### **Common Issues & Solutions:**
#### **Build Errors**
- **Problem**: Missing dependencies
- **Solution**: Run `npm install` and check `package.json`
#### **Styling Issues**
- **Problem**: CSS not loading
- **Solution**: Verify `index.css` import in `index.js`
#### **Component Errors**
- **Problem**: Import/export mismatches
- **Solution**: Check file paths and component names
#### **Deployment Issues**
- **Problem**: Hugging Face build fails
- **Solution**: Verify `app_file` points to `build/index.html`
### **Performance Optimization:**
- **Lazy loading** for large datasets
- **Memoization** for expensive calculations
- **Debouncing** for search inputs
- **Virtual scrolling** for very long tables
---
## π Next Steps & Enhancements
### **Immediate Improvements:**
1. **Add search functionality** across all columns
2. **Implement pagination** for large datasets
3. **Add data export** (CSV, JSON)
4. **Create dark mode** toggle
### **Advanced Features:**
1. **Real-time updates** with WebSocket
2. **Advanced filtering** with multiple criteria
3. **Data visualization** with charts
4. **User authentication** and permissions
### **Backend Integration:**
1. **MongoDB connection** with Node.js/Express
2. **RESTful API** endpoints
3. **Database optimization** with indexing
4. **Caching layer** for performance
---
## π― Summary
This guide has walked you through building a **professional-grade React application** with:
β
**Clean architecture** following React best practices
β
**Custom CSS system** without external dependencies
β
**Interactive data tables** with sorting functionality
β
**Responsive design** for all devices
β
**Professional styling** that matches shadcn/ui design system
β
**Easy deployment** to Hugging Face Spaces
The result is a **maintainable, scalable, and beautiful** application that you can easily customize and extend for your specific needs.
**Happy coding! π**
|