Developer Guide
Overview
Section titled “Overview”This guide provides comprehensive instructions for developers setting up and working with the TangleCat project locally. It covers both the Next.js frontend application and the Sanity Studio CMS.
Prerequisites
Section titled “Prerequisites”Required Software
Section titled “Required Software”- Node.js 20+ - Download here
- Git - Download here
- Docker & Docker Compose - Download here
Optional Software
Section titled “Optional Software”- VS Code with recommended extensions
- Postman or similar for API testing
- Chrome DevTools for debugging
Project Architecture
Section titled “Project Architecture”The TangleCat project consists of three main components:
- Next.js Frontend (
/app
) - React-based web application - Sanity Studio (
/app/sanity
) - Content management system - Documentation Site (
/website
) - This documentation
Detailed Setup Instructions
Section titled “Detailed Setup Instructions”1. Repository Setup
Section titled “1. Repository Setup”# Clone the repositorygit clone <your-repository-url>cd leaderboard-challenges
# Verify the structurels -la# Should show: app/, website/, docker-compose.yml, etc.
2. Environment Configuration
Section titled “2. Environment Configuration”Create a .env
file in the root directory:
# Copy the example environment filecp .env.example .env
# Edit with your valuesnano .env
Required Environment Variables:
# EnvironmentNODE_ENV=developmentPORT=8080
# Sanity ConfigurationNEXT_PUBLIC_SANITY_PROJECT_ID=your_project_idNEXT_PUBLIC_SANITY_DATASET=productionNEXT_PUBLIC_SANITY_DATASET_DEV=developmentSANITY_API_VERSION=2024-03-21SANITY_API_TOKEN=your_api_token
# Sanity Studio Environment VariablesSANITY_STUDIO_PUBLIC_SANITY_PROJECT_ID=your_project_idSANITY_STUDIO_PUBLIC_SANITY_DATASET=productionSANITY_STUDIO_PUBLIC_SANITY_DATASET_DEV=development
3. Sanity Project Setup
Section titled “3. Sanity Project Setup”Create Sanity Account
Section titled “Create Sanity Account”- Go to sanity.io and create an account
- Create a new organization (if you don’t have one)
- Create a new project with a descriptive name
Configure Project
Section titled “Configure Project”-
Note your Project ID from the dashboard
-
Create Datasets:
- Go to “Datasets” tab
- Create
production
dataset - Create
development
dataset - Create
testing
dataset (optional)
-
Configure CORS Origins:
- Go to “API” tab
- Add these URLs to CORS origins:
http://localhost:8080http://localhost:3000http://localhost:3333http://localhost:4321
-
Create API Token:
- Go to “API” tab
- Click “Add API token”
- Name: “Development Token”
- Role: “Editor”
- Copy the token (you won’t see it again)
4. Development Environment Setup
Section titled “4. Development Environment Setup”Option A: Docker Setup (Recommended)
Section titled “Option A: Docker Setup (Recommended)”# Start both servicesdocker compose up
# Or start individuallydocker compose up web # Frontend onlydocker compose up sanity # Sanity Studio only
# View logsdocker compose logs -f webdocker compose logs -f sanity
Option B: Manual Setup
Section titled “Option B: Manual Setup”Frontend Setup:
cd appnpm installnpm run dev
Sanity Studio Setup:
cd app/sanitynpm installnpm run dev
Documentation Site Setup:
cd websitenpm installnpm run dev
5. Verify Installation
Section titled “5. Verify Installation”After setup, you should be able to access:
- Frontend: http://localhost:8080 (Docker) or http://localhost:3000 (manual)
- Sanity Studio: http://localhost:3333
- Documentation: http://localhost:4321
Development Workflow
Section titled “Development Workflow”Daily Development
Section titled “Daily Development”# Start development environmentdocker compose up
# In another terminal, make code changes# Changes will hot-reload automatically
# View logsdocker compose logs -f
Code Changes
Section titled “Code Changes”Frontend Changes
Section titled “Frontend Changes”- Edit files in
/app/src/
- Changes auto-reload in browser
- Check console for errors
Sanity Schema Changes
Section titled “Sanity Schema Changes”- Edit files in
/app/sanity/schemaTypes/
- Restart Sanity Studio after schema changes
- Test in Sanity Studio interface
API Changes
Section titled “API Changes”- Edit files in
/app/src/app/api/
- Test endpoints with Postman or browser
- Check server logs for errors
Database Management
Section titled “Database Management”Access Sanity Studio
Section titled “Access Sanity Studio”- Go to http://localhost:3333
- Log in with your Sanity account
- Select your project and dataset
Content Operations
Section titled “Content Operations”- Create: Add new challenges, awards, users
- Read: View existing content
- Update: Modify content through the interface
- Delete: Remove content (be careful!)
Import Demo Data
Section titled “Import Demo Data”cd app/sanitynpm run demo-data
Project Structure Deep Dive
Section titled “Project Structure Deep Dive”Frontend (/app
)
Section titled “Frontend (/app)”app/├── src/│ ├── app/ # Next.js App Router│ │ ├── (admin)/ # Admin routes│ │ ├── (protected)/ # Protected routes│ │ ├── api/ # API endpoints│ │ └── globals.css # Global styles│ ├── components/ # React components│ │ ├── ui/ # UI components│ │ └── *.tsx # Feature components│ ├── lib/ # Utilities and configurations│ │ ├── auth.ts # Authentication logic│ │ ├── sanity.ts # Sanity client│ │ └── utils/ # Helper functions│ └── types/ # TypeScript definitions├── sanity/ # Sanity Studio│ ├── schemaTypes/ # Content schemas│ ├── sanity.config.ts # Studio configuration│ └── sanity.cli.ts # CLI configuration└── package.json # Dependencies
Key Files
Section titled “Key Files”/app/src/app/layout.tsx
- Root layout component/app/src/app/page.tsx
- Home page/app/src/lib/sanity.ts
- Sanity client configuration/app/src/middleware.ts
- Authentication middleware/app/sanity/schemaTypes/
- Content model definitions
Testing and Debugging
Section titled “Testing and Debugging”Frontend Testing
Section titled “Frontend Testing”# Run lintingcd appnpm run lint
# Check TypeScriptnpx tsc --noEmit
# Run tests (if configured)npm test
API Testing
Section titled “API Testing”# Test authentication endpointcurl -X POST http://localhost:8080/api/auth/signup \ -H "Content-Type: application/json" \ -d '{"email":"test@example.com","name":"Test User"}'
# Test challenges endpointcurl http://localhost:8080/api/challenges
Database Testing
Section titled “Database Testing”- Sanity Studio: http://localhost:3333
- Sanity Vision: http://localhost:3333/vision
- API Explorer: Use Sanity’s built-in tools
Common Development Tasks
Section titled “Common Development Tasks”Adding New Content Types
Section titled “Adding New Content Types”- Create Schema in
/app/sanity/schemaTypes/
- Add to Index in
/app/sanity/schemaTypes/index.ts
- Restart Sanity Studio
- Create Content in Sanity Studio
- Query Data in frontend
Adding New API Endpoints
Section titled “Adding New API Endpoints”- Create Route File in
/app/src/app/api/
- Implement Handler function
- Add Types in
/app/src/types/
- Test Endpoint with Postman or browser
Styling Changes
Section titled “Styling Changes”- Edit CSS in
/app/src/app/globals.css
- Use Tailwind classes in components
- Custom Components in
/app/src/components/ui/
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”Port Conflicts
Section titled “Port Conflicts”# Check what's using portslsof -i :8080lsof -i :3333lsof -i :3000
# Kill processes or change ports in docker-compose.yml
Sanity Connection Issues
Section titled “Sanity Connection Issues”# Verify environment variablesecho $NEXT_PUBLIC_SANITY_PROJECT_IDecho $SANITY_API_TOKEN
# Check CORS origins in Sanity dashboard# Ensure API token has correct permissions
Docker Issues
Section titled “Docker Issues”# Rebuild containersdocker compose downdocker compose up --build
# Clear Docker cachedocker system prune -a
# Check Docker logsdocker compose logs -f
Node.js Issues
Section titled “Node.js Issues”# Clear node_modulesrm -rf node_modules package-lock.jsonnpm install
# Check Node versionnode --version # Should be 20+
Debug Mode
Section titled “Debug Mode”Frontend Debugging
Section titled “Frontend Debugging”# Enable debug loggingDEBUG=* npm run dev
# Check browser console# Use React DevTools extension
Sanity Debugging
Section titled “Sanity Debugging”# Enable Sanity debug modecd app/sanityDEBUG=sanity:* npm run dev
# Check Sanity Studio console# Use Sanity Vision for query testing
Performance Optimization
Section titled “Performance Optimization”Frontend
Section titled “Frontend”- Use React.memo for expensive components
- Implement proper loading states
- Optimize images with next/image
- Use dynamic imports for code splitting
Database
Section titled “Database”- Implement proper caching strategies
- Use GROQ projections to limit data
- Optimize queries with indexes
- Monitor query performance
Security Considerations
Section titled “Security Considerations”Environment Variables
Section titled “Environment Variables”- Never commit
.env
files - Use strong, unique API tokens
- Rotate tokens regularly
- Limit token permissions
API Security
Section titled “API Security”- Implement proper authentication
- Validate all inputs
- Use HTTPS in production
- Implement rate limiting
Content Security
Section titled “Content Security”- Sanitize user inputs
- Validate file uploads
- Implement proper access controls
- Monitor for suspicious activity
Deployment Preparation
Section titled “Deployment Preparation”Production Environment
Section titled “Production Environment”# Build production imagesENVIRONMENT=production docker compose up --build
# Test production build locallyENVIRONMENT=production docker compose up
Environment Variables
Section titled “Environment Variables”Ensure production environment variables are set:
- Production Sanity project ID
- Production API tokens
- Production URLs
- Analytics and monitoring keys
Getting Help
Section titled “Getting Help”Resources
Section titled “Resources”Community
Section titled “Community”Support
Section titled “Support”For technical support or questions:
- Create a GitHub issue
- Contact the development team
- Check the troubleshooting section above
Next Steps
Section titled “Next Steps”After completing this setup:
- Explore the codebase to understand the architecture
- Create test content in Sanity Studio
- Test the complete user flow from registration to completion
- Customize the application for your specific event
- Set up monitoring and analytics
- Plan your production deployment
Remember to check the Getting Started guide for a quick overview and the other guides for specific use cases.