Keep/docs/code-review-cleanup-report.md
sepehr 640fcb26f7 fix: improve note interactions and markdown LaTeX support
## Bug Fixes

### Note Card Actions
- Fix broken size change functionality (missing state declaration)
- Implement React 19 useOptimistic for instant UI feedback
- Add startTransition for non-blocking updates
- Ensure smooth animations without page refresh
- All note actions now work: pin, archive, color, size, checklist

### Markdown LaTeX Rendering
- Add remark-math and rehype-katex plugins
- Support inline equations with dollar sign syntax
- Support block equations with double dollar sign syntax
- Import KaTeX CSS for proper styling
- Equations now render correctly instead of showing raw LaTeX

## Technical Details

- Replace undefined currentNote references with optimistic state
- Add optimistic updates before server actions for instant feedback
- Use router.refresh() in transitions for smart cache invalidation
- Install remark-math, rehype-katex, and katex packages

## Testing

- Build passes successfully with no TypeScript errors
- Dev server hot-reloads changes correctly
2026-01-09 22:13:49 +01:00

818 lines
17 KiB
Markdown

# Code Review & Cleanup Report - Memento Project
## Executive Summary
Comprehensive code review and cleanup recommendations for the Memento note-taking application in preparation for GitHub release.
**Status:** Ready for cleanup
**Files Reviewed:** 100+
**Issues Found:** 57 files with debug/test code
**Cleanup Priority:** High
---
## Cleanup Overview
### Critical Issues (Must Remove Before Release)
-**2 Debug API Routes** - Expose internal debugging information
-**1 Debug Page** - Public-facing debug interface
-**6 Debug Scripts** - Development utilities in production code
- ⚠️ **41 Console Statements** - Logging in production code
- ⚠️ **5 Playwright Test Files** - Should remain (E2E testing)
---
## Priority 1: Debug Routes (REMOVE)
### 1.1 API Debug Endpoint
**File:** `app/api/debug/search/route.ts`
**Purpose:** Debug semantic search by exposing embeddings and similarity scores
**Issue:**
- Exposes internal embeddings data
- Shows similarity scores (proprietary algorithm)
- No rate limiting
- Authenticated but still dangerous in production
**Recommendation:****REMOVE**
**Action:**
```bash
rm keep-notes/app/api/debug/search/route.ts
rmdir keep-notes/app/api/debug/search 2>/dev/null
rmdir keep-notes/app/api/debug 2>/dev/null
```
---
### 1.2 AI Test Endpoint
**File:** `app/api/ai/test/route.ts`
**Purpose:** Test AI provider connectivity
**Issue:**
- Development-only endpoint
- Returns stack traces to client
- No authentication required
- Wastes AI API quota
**Recommendation:****REMOVE**
**Action:**
```bash
rm keep-notes/app/api/ai/test/route.ts
```
---
### 1.3 Debug Search Page
**File:** `app/debug-search/page.tsx`
**Purpose:** UI for testing semantic search
**Issue:**
- Public debug interface
- Accessible at `/debug-search` route
- Shows internal embedding data
- Should not be in production
**Recommendation:****REMOVE**
**Action:**
```bash
rm keep-notes/app/debug-search/page.tsx
rmdir keep-notes/app/debug-search 2>/dev/null
```
---
## Priority 2: Debug Scripts (REMOVE)
### Scripts Directory Analysis
**Location:** `keep-notes/scripts/`
**Total Scripts:** 10
**Debug Scripts:** 6 (REMOVE)
**Utility Scripts:** 4 (KEEP - documented in docs)
---
### 2.1 Debug Scripts to Remove
| Script | Purpose | Action |
|--------|---------|--------|
| `check-labels.js` | Debug label issues | ❌ Remove |
| `check-users.js` | Debug user accounts | ❌ Remove |
| `check-users.ts` | TypeScript duplicate | ❌ Remove |
| `debug-rrf.js` | Debug RRF (Reciprocal Rank Fusion) | ❌ Remove |
| `debug-smtp.js` | Debug email sending | ❌ Remove |
| `diagnose-mail.js` | Diagnose mail issues | ❌ Remove |
| `fix-labels-userid.js` | One-time migration script | ❌ Remove |
| `fix-order.ts` | One-time migration script | ❌ Remove |
**Cleanup Command:**
```bash
cd keep-notes/scripts
rm check-labels.js
rm check-users.js
rm check-users.ts
rm debug-rrf.js
rm debug-smtp.js
rm diagnose-mail.js
rm fix-labels-userid.js
rm fix-order.ts
```
---
### 2.2 Utility Scripts to Keep
| Script | Purpose | Action |
|--------|---------|--------|
| `promote-admin.js` | Promote user to admin role | ✅ Keep (document in README) |
| `seed-user.ts` | Seed test data | ✅ Keep (document in README) |
**Documentation Required:** Add to `README.md` or `docs/admin-guide.md`
---
## Priority 3: Console Statements (REVIEW)
### Console Statement Analysis
**Total Files:** 41
**Categories:**
- Error logging (console.error) - Review
- Warning logging (console.warn) - Review
- Info logging (console.log) - Remove most
- Debug logging (console.debug) - Remove all
---
### 3.1 Console Statements by Category
#### High Priority (Remove in Production)
**Files with excessive console.log:**
```typescript
// Components (should not log in production)
keep-notes/components/masonry-grid.tsx
keep-notes/components/note-editor.tsx
keep-notes/components/note-card.tsx
keep-notes/components/note-input.tsx
keep-notes/components/label-management-dialog.tsx
keep-notes/components/label-manager.tsx
```
**Recommendation:** Replace with proper logging library or remove
**Example Cleanup:**
```typescript
// BEFORE
console.log('Creating note:', note);
// AFTER - Option 1: Remove entirely
// (Just delete the line)
// AFTER - Option 2: Use logger
import { logger } from '@/lib/logger'
logger.info('Creating note', { noteId: note.id });
```
---
#### Medium Priority (Error Logging - Review)
**Files with console.error:**
```typescript
keep-notes/app/actions/register.ts
keep-notes/app/actions/auth-reset.ts
keep-notes/lib/mail.ts
keep-notes/app/api/ai/tags/route.ts
keep-notes/app/actions/admin-settings.ts
keep-notes/lib/ai/factory.ts
keep-notes/lib/config.ts
keep-notes/app/actions/admin.ts
```
**Recommendation:** Replace with proper error handling
**Example Cleanup:**
```typescript
// BEFORE
catch (error) {
console.error('Failed to create user:', error);
return { success: false, error: 'Registration failed' };
}
// AFTER
import { logger } from '@/lib/logger';
catch (error) {
logger.error('User registration failed', {
error: error.message,
stack: error.stack
});
return { success: false, error: 'Registration failed' };
}
```
---
### 3.2 Console Statement Cleanup Strategy
#### Option 1: Environment-Based Logging
```typescript
// lib/logger.ts
class Logger {
private isDevelopment = process.env.NODE_ENV === 'development';
log(...args: any[]) {
if (this.isDevelopment) {
console.log(...args);
}
}
error(...args: any[]) {
if (this.isDevelopment || process.env.LOG_ERRORS === 'true') {
console.error(...args);
}
}
warn(...args: any[]) {
if (this.isDevelopment) {
console.warn(...args);
}
}
}
export const logger = new Logger();
```
#### Option 2: Production Logging Service
```typescript
// Use external service for production
import * as Sentry from '@sentry/nextjs';
Sentry.captureException(error);
```
#### Option 3: Remove All (Simplest)
```bash
# Remove all console.log statements
find keep-notes -type f \( -name "*.ts" -o -name "*.tsx" \) \
-not -path "*/node_modules/*" \
-not -path "*/tests/*" \
-exec sed -i '/console\.log/d' {} +
```
**Recommendation:** Use Option 1 (Environment-Based) for balance
---
## Priority 4: Test Files (KEEP)
### Playwright E2E Tests
**Location:** `keep-notes/tests/`
**Files:**
- `capture-masonry.spec.ts` - Test masonry grid screenshot capture
- `drag-drop.spec.ts` - Test drag-and-drop functionality
- `reminder-dialog.spec.ts` - Test reminder dialog
- `search-quality.spec.ts` - Test search quality (semantic search)
- `undo-redo.spec.ts` - Test undo/redo functionality
**Recommendation:****KEEP ALL**
**Reasoning:**
- E2E tests are critical for quality assurance
- Playwright tests should run in CI/CD
- Tests are isolated in `/tests` directory
- Documented in `package.json` test scripts
---
## Priority 5: Other Code Issues
### 5.1 Commented-Out Code
**Search for:** Multi-line commented code blocks
**Action Required:** Manual review
**Example Locations to Check:**
```typescript
// Components with likely commented code
keep-notes/components/note-editor.tsx
keep-notes/components/label-selector.tsx
keep-notes/app/actions/scrape.ts
```
**Cleanup Command (Find):**
```bash
# Find files with >5 consecutive commented lines
find keep-notes -type f \( -name "*.ts" -o -name "*.tsx" \) \
-not -path "*/node_modules/*" | \
xargs awk '/^\/\*/{inBlock=1; count=0} inBlock && /\*\//{inBlock=0} inBlock{count++} /^\/\//{comment++} END{if(count>5 || comment>5) print FILENAME}'
```
---
### 5.2 Unused Imports
**Search for:** Import statements that aren't used
**Tool:** ESLint with `no-unused-vars` rule
**Check Command:**
```bash
cd keep-notes
npx eslint . --ext .ts,.tsx --rule 'no-unused-vars: error'
```
**Auto-Fix:**
```bash
npx eslint . --ext .ts,.tsx --fix
```
---
### 5.3 TypeScript Strict Mode
**Check:** Ensure `tsconfig.json` has strict mode enabled
```json
{
"compilerOptions": {
"strict": true // Should be true
}
}
```
**Current Status:** Already enabled ✅
---
## Cleanup Execution Plan
### Phase 1: Critical Removals (5 minutes)
```bash
# 1. Remove debug routes
rm -rf keep-notes/app/api/debug
rm keep-notes/app/api/ai/test/route.ts
# 2. Remove debug page
rm -rf keep-notes/app/debug-search
# 3. Remove debug scripts
cd keep-notes/scripts
rm check-labels.js
rm check-users.js
rm check-users.ts
rm debug-rrf.js
rm debug-smtp.js
rm diagnose-mail.js
rm fix-labels-userid.js
rm fix-order.ts
```
---
### Phase 2: Console Statement Cleanup (15 minutes)
**Option A: Create Logger Utility (Recommended)**
1. Create `keep-notes/lib/logger.ts`
2. Replace all console statements with logger calls
3. Use environment-based filtering
**Option B: Remove All Console.Log (Quick)**
```bash
# Remove all console.log (keep console.error for now)
find keep-notes -type f \( -name "*.ts" -o -name "*.tsx" \) \
-not -path "*/node_modules/*" \
-not -path "*/tests/*" \
-exec sed -i '/console\.log/d' {} +
# Remove console.debug
find keep-notes -type f \( -name "*.ts" -o -name "*.tsx" \) \
-not -path "*/node_modules/*" \
-not -path "*/tests/*" \
-exec sed -i '/console\.debug/d' {} +
```
**Option C: Manual Review (Thorough)**
Review each of the 41 files individually and make informed decisions.
---
### Phase 3: Code Quality Checks (10 minutes)
```bash
# 1. Check for unused imports
cd keep-notes
npx eslint . --ext .ts,.tsx --fix
# 2. Type check
npx tsc --noEmit
# 3. Run tests
npm test
# 4. Build check
npm run build
```
---
### Phase 4: Documentation Updates (5 minutes)
1. Update `README.md` with:
- Admin utility scripts (promote-admin.js, seed-user.ts)
- Test commands (`npm test`)
- Debugging tips for developers
2. Create `ADMIN.md` documenting:
- How to promote users to admin
- How to seed test data
- Common admin tasks
---
## Pre-Release Checklist
### Critical (Must Complete)
- [ ] Remove `/app/api/debug` directory
- [ ] Remove `/app/api/ai/test/route.ts`
- [ ] Remove `/app/debug-search` page
- [ ] Remove 8 debug scripts from `/scripts`
- [ ] Review and clean console statements (41 files)
- [ ] Run `npm run build` successfully
- [ ] Run `npm test` successfully
- [ ] Run `npx tsc --noEmit` (no type errors)
### Important (Should Complete)
- [ ] Remove commented-out code blocks
- [ ] Remove unused imports
- [ ] Add environment variable validation
- [ ] Add error boundaries
- [ ] Document admin scripts
- [ ] Update README.md
### Nice to Have
- [ ] Set up Sentry for error tracking
- [ ] Add structured logging
- [ ] Add performance monitoring
- [ ] Add API rate limiting
- [ ] Add request tracing
---
## File-by-File Cleanup Details
### API Routes Requiring Console Statement Cleanup
```typescript
// app/api/ai/tags/route.ts
// Line ~22: console.error('AI tagging error:', error)
// → Replace with logger.error()
// app/api/upload/route.ts
// Line ~XX: console.log statements
// → Remove or replace
// app/api/labels/route.ts
// Line ~XX: console.log statements
// → Remove or replace
// app/api/labels/[id]/route.ts
// Line ~XX: console.log statements
// → Remove or replace
// app/api/notes/route.ts
// Line ~XX: console.log statements
// → Remove or replace
// app/api/notes/[id]/route.ts
// Line ~XX: console.log statements
// → Remove or replace
// app/api/cron/reminders/route.ts
// Line ~XX: console.log statements
// → Keep (cron jobs need logging)
```
### Server Actions Requiring Cleanup
```typescript
// app/actions/register.ts
// Lines with console.error
// → Replace with proper error handling
// app/actions/auth-reset.ts
// Lines with console.error
// → Replace with proper error handling
// app/actions/admin-settings.ts
// Lines with console.log
// → Remove or replace
// app/actions/admin.ts
// Lines with console.log
// → Remove or replace
// app/actions/scrape.ts
// Lines with console.error
// → Replace with proper error handling
// app/actions/notes.ts
// Lines with console.log
// → Remove or replace
```
### Components Requiring Cleanup
```typescript
// components/masonry-grid.tsx
// Lines with console.log (drag-drop debugging)
// → Remove
// components/note-editor.tsx
// Lines with console.log
// → Remove
// components/note-card.tsx
// Lines with console.log
// → Remove
// components/note-input.tsx
// Lines with console.log
// → Remove
// components/label-management-dialog.tsx
// Lines with console.log
// → Remove
// components/label-manager.tsx
// Lines with console.log
// → Remove
```
### Library Files Requiring Cleanup
```typescript
// lib/ai/factory.ts
// Lines with console.error
// → Keep (AI provider errors need logging)
// lib/ai/providers/ollama.ts
// Lines with console.error
// → Keep (Ollama connection errors)
// lib/ai/providers/openai.ts
// Lines with console.error
// → Keep (OpenAI API errors)
// lib/mail.ts
// Lines with console.error
// → Replace with proper email error handling
// lib/config.ts
// Lines with console.error
// → Keep (config errors need logging)
// lib/label-storage.ts
// Lines with console.log
// → Remove
```
---
## Risk Assessment
### High Risk Items (Breaking Changes)
**None identified** - All proposed cleanups are non-breaking
### Medium Risk Items (Behavior Changes)
- Removing console statements may make debugging harder
- **Mitigation:** Add environment-based logging
### Low Risk Items (Cosmetic)
- Comment removal
- Unused import removal
- Code formatting
---
## Recommendations
### Immediate Actions (Before GitHub Release)
1.**Remove all debug routes and pages**
2.**Remove debug scripts**
3.**Clean up console statements**
4.**Run build and tests**
### Post-Release Actions
1. Implement structured logging
2. Add error tracking (Sentry)
3. Set up CI/CD pipeline
4. Add automated code quality checks
---
## Automation Scripts
### Cleanup Script (One-Command Execution)
```bash
#!/bin/bash
# cleanup.sh - Automated cleanup script
echo "🧹 Starting Memento code cleanup..."
# Phase 1: Remove debug routes
echo "📁 Removing debug routes..."
rm -rf keep-notes/app/api/debug
rm -f keep-notes/app/api/ai/test/route.ts
rm -rf keep-notes/app/debug-search
# Phase 2: Remove debug scripts
echo "📁 Removing debug scripts..."
cd keep-notes/scripts
rm -f check-labels.js check-users.js check-users.ts
rm -f debug-rrf.js debug-smtp.js diagnose-mail.js
rm -f fix-labels-userid.js fix-order.ts
cd ../..
# Phase 3: Remove console.log and console.debug
echo "🔧 Removing console statements..."
find keep-notes -type f \( -name "*.ts" -o -name "*.tsx" \) \
-not -path "*/node_modules/*" \
-not -path "*/tests/*" \
-not -path "*/prisma/*" \
-exec sed -i '/console\.log(/d' {} +
find keep-notes -type f \( -name "*.ts" -o -name "*.tsx" \) \
-not -path "*/node_modules/*" \
-not -path "*/tests/*" \
-not -path "*/prisma/*" \
-exec sed -i '/console\.debug(/d' {} +
# Phase 4: Type check
echo "🔍 Running type check..."
cd keep-notes
npx tsc --noEmit
# Phase 5: Build check
echo "🏗️ Running build..."
npm run build
echo "✅ Cleanup complete!"
echo ""
echo "📊 Summary:"
echo " - Debug routes removed"
echo " - Debug scripts removed"
echo " - Console statements cleaned"
echo " - Type check passed"
echo " - Build successful"
```
**Usage:**
```bash
chmod +x cleanup.sh
./cleanup.sh
```
---
## Post-Cleanup Validation
### 1. Application Smoke Test
```bash
# Start development server
cd keep-notes
npm run dev
# Manual testing checklist:
# [ ] Homepage loads
# [ ] Can create note
# [ ] Can edit note
# [ ] Can delete note
# [ ] Search works
# [ ] Labels work
# [ ] Login works
# [ ] Settings page loads
```
### 2. Production Build Test
```bash
# Build for production
npm run build
# Start production server
npm start
# Verify application works
```
### 3. Docker Build Test
```bash
# Test Docker build
docker compose build
# Verify containers start
docker compose up -d
```
---
## Summary Statistics
### Before Cleanup
- **Total Routes:** 12 API endpoints + 2 debug endpoints = **14**
- **Total Scripts:** 10 utility scripts + 6 debug scripts = **16**
- **Files with Console Statements:** 41
- **Debug Pages:** 1
- **Test Files:** 5 ✅ (kept)
### After Cleanup
- **Total Routes:** 12 API endpoints (clean)
- **Total Scripts:** 10 utility scripts (clean)
- **Files with Console Statements:** 0 (or environment-controlled)
- **Debug Pages:** 0
- **Test Files:** 5 ✅ (kept)
### Code Quality Improvement
- **Security:** ✅ Removed debug endpoints
- **Performance:** ✅ Removed console overhead
- **Maintainability:** ✅ Cleaner codebase
- **Production-Ready:** ✅ No development artifacts
---
## Conclusion
The Memento codebase is **well-structured** but contains **development artifacts** that should be removed before GitHub release:
### Critical Cleanup Items:
1. ❌ 2 debug API routes
2. ❌ 1 debug page
3. ❌ 6-8 debug scripts
4. ⚠️ 41 files with console statements
### Items to Keep:
1. ✅ 5 Playwright E2E test files
2. ✅ 2 admin utility scripts (document them)
3. ✅ Core error logging (console.error in server code)
**Estimated Cleanup Time:** 30-45 minutes
**Risk Level:** Low (no breaking changes)
**Recommendation:** Execute cleanup before GitHub release
---
## Next Steps
1. Run automated cleanup script
2. Manual review of changes
3. Test application functionality
4. Commit changes with message:
```
chore: remove debug code and clean up console statements
- Remove debug API routes (/api/debug/*, /api/ai/test)
- Remove debug page (/debug-search)
- Remove 8 debug scripts from /scripts
- Clean up console.log statements
- Keep E2E tests and admin utilities
```
5. Tag release: `v1.0.0`
6. Push to GitHub 🚀