# OrangeCat Developer Guide Welcome to the OrangeCat codebase! This guide will help you understand the project structure, find what you need, and contribute effectively. ## ๐Ÿš€ Quick Start ### For New Developers 1. **Read this guide** - Understand the project structure 2. **Set up your environment** - See [Environment Setup](#environment-setup) 3. **Run the project** - Use `pnpm run dev` 4. **Explore the codebase** - Start with the sections below ### For Contributors 1. **Understand the architecture** - See [Architecture](#architecture) 2. **Follow the patterns** - Check [Development Patterns](#development-patterns) 3. **Write tests** - See [Testing](#testing) 4. **Submit changes** - Follow [Contributing](#contributing) --- ## ๐Ÿ“ Project Structure ``` /home/g/dev/orangecat/ โ”œโ”€โ”€ ๐Ÿ“ src/ # Main application code โ”‚ โ”œโ”€โ”€ ๐Ÿ“ app/ # Next.js app router pages โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ (authenticated)/ # Protected routes โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ dashboard/ # Main dashboard โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ profile/ # User profile pages โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“ organizations/ # Organization pages โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ auth/ # Authentication pages โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ api/ # API routes โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“ blog/ # Blog pages โ”‚ โ”œโ”€โ”€ ๐Ÿ“ components/ # React components โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ ui/ # Base UI components โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ layout/ # Layout components โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ profile/ # Profile-related components โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ wallet/ # Wallet-related components โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“ funding/ # Funding-related components โ”‚ โ”œโ”€โ”€ ๐Ÿ“ services/ # Business logic services โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ supabase/ # Supabase integration โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ bitcoin/ # Bitcoin utilities โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“ search/ # Search functionality โ”‚ โ”œโ”€โ”€ ๐Ÿ“ types/ # TypeScript type definitions โ”‚ โ”œโ”€โ”€ ๐Ÿ“ utils/ # Utility functions โ”‚ โ”œโ”€โ”€ ๐Ÿ“ hooks/ # Custom React hooks โ”‚ โ”œโ”€โ”€ ๐Ÿ“ stores/ # State management โ”‚ โ””โ”€โ”€ ๐Ÿ“ lib/ # Library functions โ”œโ”€โ”€ ๐Ÿ“ content/ # Static content โ”‚ โ””โ”€โ”€ ๐Ÿ“ blog/ # Blog posts (MDX) โ”œโ”€โ”€ ๐Ÿ“ docs/ # Documentation โ”‚ โ”œโ”€โ”€ ๐Ÿ“ architecture/ # System architecture docs โ”‚ โ”œโ”€โ”€ ๐Ÿ“ development/ # Development guides โ”‚ โ”œโ”€โ”€ ๐Ÿ“ security/ # Security documentation โ”‚ โ””โ”€โ”€ ๐Ÿ“ deployment/ # Deployment guides โ”œโ”€โ”€ ๐Ÿ“ scripts/ # Utility scripts โ”‚ โ”œโ”€โ”€ ๐Ÿ“ db/ # Database scripts โ”‚ โ”œโ”€โ”€ ๐Ÿ“ deployment/ # Deployment scripts โ”‚ โ””โ”€โ”€ ๐Ÿ“ dev/ # Development scripts โ””โ”€โ”€ ๐Ÿ“ tests/ # Test files โ”œโ”€โ”€ ๐Ÿ“ e2e/ # End-to-end tests โ””โ”€โ”€ ๐Ÿ“ unit/ # Unit tests ``` --- ## ๐Ÿ”ง Environment Setup ### Prerequisites - **Node.js** 18+ (LTS recommended) - **npm** or **yarn** - **Git** - **Supabase CLI** (optional, for local development) ### Installation ```bash # Clone the repository git clone cd orangecat # Install dependencies pnpm install # Set up environment variables cp config/production.env.template .env.local # Edit .env.local with your Supabase credentials # Start development server pnpm run dev ``` ### Environment Variables **Required:** - `NEXT_PUBLIC_SUPABASE_URL` - Your Supabase project URL - `NEXT_PUBLIC_SUPABASE_ANON_KEY` - Your Supabase anon key **Optional:** - `SUPABASE_SERVICE_ROLE_KEY` - For admin operations - `SUPABASE_ACCESS_TOKEN` - For MCP tools --- ## ๐Ÿ—๏ธ Architecture Overview ### Core Technologies - **Frontend**: Next.js 16, React 19, TypeScript - **Backend**: Supabase (PostgreSQL + Auth + Storage) - **Styling**: Tailwind CSS - **State Management**: Zustand - **Testing**: Vitest, Playwright - **Deployment**: Self-hosted on Hetzner (bitbaum, behind Caddy) ### Key Features - **Bitcoin Funding Platform** - Crowdfunding with Bitcoin/Lightning - **Profile Management** - User profiles with verification - **Organization Support** - Multi-user organizations - **Wallet Integration** - Bitcoin wallet discovery and management - **Social Features** - Following, associations, networking ### Architecture Principles - **Modular Design** - Small, focused components and services - **Type Safety** - Comprehensive TypeScript coverage - **Security First** - Multi-layer security validation - **Performance Optimized** - Lazy loading and code splitting --- ## ๐Ÿ“– Where to Find What ### ๐Ÿงฉ **Components** - **UI Components**: `src/components/ui/` - Buttons, forms, cards, etc. - **Layout Components**: `src/components/layout/` - Header, footer, navigation - **Feature Components**: `src/components//` - Profile, wallet, funding, etc. ### ๐Ÿ”ง **Business Logic** - **Services**: `src/services/` - Supabase integration, Bitcoin utilities, search - **Utilities**: `src/utils/` - Validation, formatting, helpers - **Types**: `src/types/` - TypeScript definitions ### ๐Ÿ“„ **Pages & Routes** - **App Pages**: `src/app/` - Next.js pages organized by feature - **API Routes**: `src/app/api/` - Server-side API endpoints ### ๐Ÿ—„๏ธ **Data & Configuration** - **Blog Posts**: `content/blog/` - MDX blog posts - **Entity Registry**: `src/config/entity-registry.ts` - SSOT for every entity type - **Demo Data**: `src/data/` - Mock data for development ### ๐Ÿงช **Testing** - **Unit Tests**: `__tests__/` (repo root, Vitest) - **E2E Tests**: `tests/e2e/` - Playwright tests - **Smoke Tests**: `__tests__/smoke/` --- ## ๐Ÿš€ Development Workflow ### Creating New Features 1. **Plan the Feature** ```bash # Check existing patterns in similar features find src/components -name "*[feature]*" -type d ``` 2. **Create Component Structure** ```bash mkdir -p src/components/new-feature # Create index.tsx, types.ts, utils.ts as needed ``` 3. **Add to Navigation** ```typescript // Update navigation config // Add to appropriate dashboard config ``` 4. **Write Tests** ```bash pnpm test new-feature ``` ### Database Changes 1. **Create Migration** ```bash # Use Supabase migrations supabase migration new feature_name ``` 2. **Update Types** ```typescript // Update src/types/database.ts // Update validation schemas ``` 3. **Test Locally** ```bash pnpm run dev # Test the changes ``` ### Blog Posts 1. **Create MDX File** ```bash # Use the template cp scripts/blog/blog-template.mdx content/blog/new-post.mdx ``` 2. **Write Content** - Use the available MDX components - Add frontmatter with metadata - Use ``, ``, `` components 3. **Publish** ```typescript // Set published: true in frontmatter // Deploy to see live ``` --- ## ๐Ÿ”’ Security & Validation ### Bitcoin Address Validation ```typescript import { isValidBitcoinAddress } from '@/utils/validation'; // Validates format, length, and security const result = isValidBitcoinAddress('bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh'); ``` ### Profile Security ```typescript import { isValidUsername, isValidBio } from '@/utils/validation'; // Prevents impersonation and injection const usernameResult = isValidUsername('elonmusk'); // Blocked const bioResult = isValidBio(''); // Blocked ``` ### Authentication - Uses Supabase Auth with RLS policies - JWT tokens for API access - Protected routes for sensitive features --- ## ๐Ÿงช Testing Strategy ### Test Organization ``` tests/ โ”œโ”€โ”€ e2e/ # Playwright end-to-end tests โ”‚ โ”œโ”€โ”€ auth/ # Authentication flows โ”‚ โ”œโ”€โ”€ profile/ # Profile management โ”‚ โ””โ”€โ”€ funding/ # Campaign creation โ””โ”€โ”€ unit/ # Vitest unit tests โ”œโ”€โ”€ components/ # Component tests โ”œโ”€โ”€ utils/ # Utility tests โ””โ”€โ”€ services/ # Service tests ``` ### Running Tests ```bash # Run all tests pnpm test # Run specific test suites pnpm run test:unit # Full fast Vitest suite (unit + smoke + env โ€” what CI gates) pnpm run test:auth # Auth tests pnpm run test:security # Security tests # Run with coverage pnpm run test:ci # CI mode with coverage ``` ### Writing Tests ```typescript // Component test example import { render, screen } from '@testing-library/react' import MyComponent from './MyComponent' test('renders correctly', () => { render() expect(screen.getByText('Expected text')).toBeInTheDocument() }) ``` --- ## ๐Ÿš€ Deployment ### Production Deployment ```bash # Build for production pnpm run build:production # Deploy: self-hosted on Hetzner (bitbaum, behind Caddy). # See docs/operations/deployment/DEPLOYMENT_PROCESS.md for the on-box flow. ``` ### Environment Variables - **Development**: `.env.local` (gitignored) - **Production**: `/opt/orangecat/app/.env` on the Hetzner box ### Database Migrations ```bash # Migrations apply automatically on deploy (scripts/apply-migrations.sh) # Generate new migration pnpm exec supabase migration new feature_name ``` --- ## ๐Ÿ“š Documentation Structure ### ๐Ÿ“ **docs/** Directory Organization #### **Architecture** (`docs/architecture/`) - `ARCHITECTURE.md` - High-level system overview - `SUPABASE_SCHEMA_GUIDE.md` - Database tables and conventions - `services-architecture.md` - Service layer design #### **Development** (`docs/development/`) - `BEST_PRACTICES.md` - Code standards and patterns - `hooks-guide.md` - Custom hook patterns - `types-guide.md` - TypeScript usage guide - `utils-guide.md` - Utility function patterns #### **Security** (`docs/security/`) - `auth_system.md` - Authentication system details - `celebrity-impersonation-prevention.md` - Anti-impersonation measures #### **Deployment** (`docs/operations/deployment/`) - `DEPLOYMENT_PROCESS.md` - How a merge reaches production - Infrastructure and DevOps guides #### **Features** (`docs/features/`) - Feature-specific documentation and guides #### **Standards** (`docs/development/standards/`) - `documentation_standards.md` - Documentation guidelines - `datetime_standards.md` - Date/time handling standards ### ๐Ÿ“‹ **Key Documentation Files** | Topic | File | Purpose | | -------------------- | ------------------------------------------------------- | ------------------------------ | | **Project Overview** | `README.md` | High-level project description | | **Setup Guide** | `docs/development/README.md` | Getting started instructions | | **Architecture** | `docs/architecture/ARCHITECTURE.md` | System design overview | | **Security** | `SECURITY.md` | Reporting vulnerabilities | | **Standards** | `docs/development/standards/documentation_standards.md` | Documentation guidelines | | **Contributing** | `docs/development/contributing/CONTRIBUTING.md` | Contribution guidelines | --- ## ๐Ÿ› ๏ธ Common Development Tasks ### Adding a New Component 1. **Create the component file** ```bash # In src/components/feature/ touch NewComponent.tsx ``` 2. **Add proper TypeScript types** ```typescript interface NewComponentProps { // Define props } ``` 3. **Follow naming conventions** - Use PascalCase for components - Use camelCase for props and functions - Export as default 4. **Add to index file if needed** ```typescript // src/components/feature/index.ts export { default as NewComponent } from './NewComponent'; ``` ### Creating API Routes 1. **Create route file** ```bash # In src/app/api/feature/ touch route.ts ``` 2. **Implement proper error handling** ```typescript import { handleApiError } from '@/lib/errors'; ``` 3. **Add authentication if needed** ```typescript import { withAuth } from '@/lib/api/withAuth'; ``` ### Database Changes 1. **Create migration** ```bash supabase migration new add_new_table ``` 2. **Update TypeScript types** ```typescript // src/types/database.ts export interface NewTable { // Define new table structure } ``` 3. **Update validation schemas** ```typescript // src/lib/validation.ts export const newTableSchema = z.object({...}) ``` --- ## ๐Ÿ” Finding Information ### **Quick Reference** | I Need To... | Look In... | File/Path | | ---------------------- | -------------------- | ---------------------------------------------- | | **Create a component** | Component templates | `docs/development/templates/component.md` | | **Add validation** | Validation utilities | `src/lib/validation/` | | **Update database** | Migrations guide | `docs/operations/supabase/migrations-guide.md` | | **Add API route** | API templates | `docs/development/templates/api-endpoint.md` | | **Write tests** | Testing guide | `docs/testing/test-guide.md` | | **Deploy** | Deployment docs | `docs/operations/deployment/` | | **Security check** | Security docs | `docs/security/` | ### **Search Tips** - Use `grep` for finding specific code patterns - Check `docs/` for comprehensive guides - Look in `src/types/` for TypeScript definitions - Check `src/services/` for business logic --- ## ๐ŸŽฏ Best Practices ### Code Organization - **Single Responsibility** - One file, one purpose - **Consistent Naming** - Follow established patterns - **Type Safety** - Use TypeScript everywhere - **Error Handling** - Proper error boundaries and logging ### Performance - **Lazy Loading** - Use `React.lazy()` for large components - **Code Splitting** - Dynamic imports for routes - **Memoization** - Use `useMemo` and `useCallback` appropriately - **Bundle Analysis** - Run `pnpm run analyze` regularly ### Security - **Input Validation** - Always validate user input - **Authentication** - Use Supabase Auth properly - **Authorization** - Check permissions for all actions - **Data Sanitization** - Clean user data before processing ### Testing - **Unit Tests** - Test individual functions and components - **Integration Tests** - Test service interactions - **E2E Tests** - Test complete user flows - **Security Tests** - Test authentication and authorization --- ## ๐Ÿ“ž Getting Help ### **Common Issues** - **Build Errors**: Check TypeScript compilation with `pnpm run type-check` - **Runtime Errors**: Check browser console and server logs - **Database Issues**: Verify Supabase connection and migrations - **Authentication**: Check auth configuration and RLS policies ### **Resources** - **Project README**: `README.md` - High-level overview - **Architecture Docs**: `docs/architecture/` - System design - **Development Guide**: `docs/development/` - Day-to-day development - **Security Guide**: `docs/security/` - Security policies - **Standards**: `docs/development/standards/` - Coding standards ### **Community** - **GitHub Issues** - For bugs and feature requests - **Pull Requests** - For code contributions - **Documentation** - Help improve these guides --- ## ๐Ÿ”„ Keeping This Guide Updated This guide should evolve with the codebase. When you: - **Add new features** โ†’ Update the architecture section - **Change file structure** โ†’ Update the project structure section - **Add new patterns** โ†’ Update the development workflow section - **Find missing information** โ†’ Add it to the appropriate section **Remember:** A good developer guide is a living document that grows with the project! --- _Last Updated: $(date)_ _Version: 1.0.0_