|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +HabitTrove is a gamified habit tracking PWA built with Next.js 15, TypeScript, and Jotai state management. Users earn coins for completing habits and can redeem them for rewards. Features multi-user support with admin capabilities and shared ownership of habits/wishlist items. |
| 8 | + |
| 9 | +## Essential Commands |
| 10 | + |
| 11 | +### Development |
| 12 | +- `npm run dev` - Start development server with Turbopack |
| 13 | +- `npm run setup:dev` - Full setup: installs bun, dependencies, runs typecheck and lint |
| 14 | +- `npm install --force` - Install dependencies (force flag required) |
| 15 | + |
| 16 | +### Quality Assurance (Run these before committing) |
| 17 | +- `npm run typecheck` - TypeScript type checking (required) |
| 18 | +- `npm run lint` - ESLint code linting (required) |
| 19 | +- `npm test` - Run tests with Bun |
| 20 | +- `npm run build` - Build production version |
| 21 | + |
| 22 | +### Docker Deployment |
| 23 | +- `npm run docker-build` - Build Docker image locally |
| 24 | +- `docker compose up -d` - Run with docker-compose (recommended) |
| 25 | +- Requires `AUTH_SECRET` environment variable: `openssl rand -base64 32` |
| 26 | + |
| 27 | +## Architecture Overview |
| 28 | + |
| 29 | +### State Management (Jotai) |
| 30 | +- **Central atoms**: `habitsAtom`, `coinsAtom`, `wishlistAtom`, `usersAtom` in `lib/atoms.ts` |
| 31 | +- **Derived atoms**: Computed values like `dailyHabitsAtom`, `coinsBalanceAtom` |
| 32 | +- **Business logic hooks**: `useHabits`, `useCoins`, `useWishlist` in `/hooks` |
| 33 | + |
| 34 | +### Data Models & Ownership |
| 35 | +- **Individual ownership**: `CoinTransaction` has single `userId` |
| 36 | +- **Shared ownership**: `Habit` and `WishlistItemType` have `userIds: string[]` array |
| 37 | +- **Admin features**: Admin users can view/manage any user's data via dropdown selectors |
| 38 | +- **Data persistence**: JSON files in `/data` directory with automatic `/backups` |
| 39 | + |
| 40 | +### Key Components Structure |
| 41 | +- **Feature components**: `HabitList`, `CoinsManager`, `WishlistManager` - main page components |
| 42 | +- **Modal components**: `AddEditHabitModal`, `AddEditWishlistItemModal`, `UserSelectModal` |
| 43 | +- **UI components**: `/components/ui` - shadcn/ui based components |
| 44 | + |
| 45 | +### Authentication & Users |
| 46 | +- NextAuth.js v5 with multi-user support |
| 47 | +- User permissions: regular users vs admin users |
| 48 | +- Admin dropdown patterns: Similar implementation across Habits/Wishlist pages (reference CoinsManager for pattern) |
| 49 | + |
| 50 | +### Internationalization |
| 51 | +- `next-intl` with messages in `/messages/*.json` |
| 52 | +- Supported languages: English, Spanish, German, French, Russian, Chinese, Japanese |
| 53 | + |
| 54 | +## Code Patterns |
| 55 | + |
| 56 | +### Component Structure |
| 57 | +```typescript |
| 58 | +// Standard component pattern: |
| 59 | +export default function ComponentName() { |
| 60 | + const [data, setData] = useAtom(dataAtom) |
| 61 | + const { businessLogicFunction } = useCustomHook() |
| 62 | + // Component logic |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### Hook Patterns |
| 67 | +- Custom hooks accept options: `useHabits({ selectedUser?: string })` |
| 68 | +- Return destructured functions and computed values |
| 69 | +- Handle both individual and shared ownership models |
| 70 | + |
| 71 | +### Shared Ownership Pattern |
| 72 | +```typescript |
| 73 | +// Filtering for shared ownership: |
| 74 | +const userItems = allItems.filter(item => |
| 75 | + item.userIds && item.userIds.includes(targetUserId) |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +### Admin Dropdown Pattern |
| 80 | +Reference `CoinsManager.tsx:107-119` for admin user selection implementation. Similar pattern should be applied to Habits and Wishlist pages. |
| 81 | + |
| 82 | +## Data Safety |
| 83 | +- Always backup `/data` before major changes |
| 84 | +- Test with existing data files to prevent data loss |
| 85 | +- Validate user permissions for all data operations |
| 86 | +- Handle migration scripts carefully (see PLAN.md for shared ownership migration) |
| 87 | + |
| 88 | +## Performance Considerations |
| 89 | +- State updates use immutable patterns |
| 90 | +- Large dataset filtering happens at hook level |
| 91 | +- Derived atoms prevent unnecessary re-renders |
0 commit comments