Architecture Cleanup Analysis
**Issue**: Multiple overlapping implementations of route optimization algorithms - **Impact**: - Code duplication - Maintenance burden - Confusion about which to use - Potential bugs from inconsistent implementations - **Recommendation**: - Audit which implementations are actually used - Consolidate into a single, well-tested optimization engine - Remove unused variants
Full Public Reader
# Architecture Cleanup Analysis
## Milkmen Delivery - Code Quality & Dead Weight Assessment
### Project Overview
Objective: Sales route optimization and territory management platform for coffee delivery sales teams. Manages locations, agents, territories, route planning, and store visits.
Tech Stack: React + TypeScript + Vite + Supabase + Google Maps
---
๐ด CRITICAL ISSUES - Immediate Action Needed
### 1. Unused Import in Entry Point
File: `src/main.tsx`
- Issue: Line 4 imports `Planner` but never uses it
- Impact: Unnecessary bundle size, confusion
- Fix: Remove line 4: `import Planner from "./pages/routes/Planner";`
### 2. Duplicate Route Optimization Implementations
Files:
- `partner-delivery-route/route-calculator.ts` (root level - 628 lines)
- `src/lib/optimization/partner-route-calculator.ts` (src/lib - 353 lines)
- `src/services/route-optimization/route-calculator.ts` (simpler version)
- `src/lib/advanced-route-optimizer.ts` (443 lines)
- `src/lib/route-strategy-engine.ts` (454 lines)
- `src/lib/optimization/multi-stage-route-optimizer.ts` (1308 lines!)
Issue: Multiple overlapping implementations of route optimization algorithms
- Impact:
- Code duplication
- Maintenance burden
- Confusion about which to use
- Potential bugs from inconsistent implementations
- Recommendation:
- Audit which implementations are actually used
- Consolidate into a single, well-tested optimization engine
- Remove unused variants
### 3. Deprecated API Key Management
File: `src/lib/services/api-keys.ts`
- Issue: Entire file marked as `@deprecated` with console.warn calls
- Impact: Dead code that still runs, adds bundle size
- Recommendation:
- Check if any code still imports/uses these functions
- If unused, delete the entire file
- If used, create migration path and remove after migration
---
๐ก MEDIUM PRIORITY - Cleanup Opportunities
### 4. Backup Files
Files:
- `src/utils/partner-proximity-priority.ts.backup` (394 lines)
- Issue: Backup file in source control
- Recommendation: Delete if original is working, or move to archive outside repo
### 5. Temporary Export Scripts in Root
Files:
- `export-carsan-territory.js`
- `export-fidi-lower-manhattan.js`
- `export-lower-manhattan-locations.js`
- `generate-carsan-bushwick-routes.js`
- `generate-fidi-lower-manhattan-routes.js`
- Issue: One-off scripts mixed with project code
- Recommendation:
- Move to `scripts/` directory
- Or delete if no longer needed
- Add to `.gitignore` if temporary
### 6. CSV Export Files in Root
Files:
- `bushwick-locations.csv`
- `carsan-bushwick-100-routes.csv`
- `carsan-bushwick-200-routes.csv`
- `carsan-territory-locations.csv`
- `fidi-lower-manhattan-200-routes.csv`
- `lower-manhattan-locations.csv`
- Issue: Generated data files in repository root
- Recommendation:
- Add to `.gitignore`
- Move to `exports/` or `data/exports/` directory
- Or delete if not needed in repo
### 7. SQL Debug/Fix Scripts
Location: `sql/` directory (34 files)
Files to Review:
- `fix_salswee_region.sql` (root level)
- `get_mohamed_territory_count.sql` (root level)
- `sql/debug-*.sql` (multiple debug files)
- `sql/fix-*.sql` (multiple fix files)
- `sql/RUN-THIS-TO-FIX-SALA.sql` (temporary fix)
- Issue: One-off SQL scripts mixed with migrations
- Recommendation:
- Keep only permanent migrations in `supabase/migrations/`
- Move one-off fixes to `sql/archive/` or delete
- Document which fixes have been applied
### 8. Documentation Files (Potential Junk)
Files:
- `BULK_PASTE_SEARCH_TERMS_FEATURE.md`
- `ENHANCED_PARTNER_DELIVERY_ROUTE.md`
- `FIX_MAP_LOCATION_LA.md`
- `OPTIMIZATION_RESULTS_ERROR_FIX.md`
- `PARTNER_DELIVERY_ROUTE_INTEGRATION.md`
- `PARTNER_DELIVERY_TAB_FIX.md`
- `PARTNER_SHOPS_SETUP_GUIDE.md`
- Issue: Feature docs and fix notes in root
- Recommendation:
- Consolidate into `docs/` directory
- Archive old fix documentation
- Keep only current/active documentation
### 9. Sample/Mock Data Files
Files:
- `src/data/sample-accounts-data.ts` (199 lines)
- `src/data/store-visit-data.ts` (204 lines)
- Usage:
- `sample-accounts-data.ts` imported in `useLocationsForAccounts.ts`
- `store-visit-data.ts` imported in `useStoreVisitsData.ts`
- Issue: Mock data in production codebase
- Recommendation:
- Verify if these are still used (or if real data is used)
- If unused, remove
- If used for development/testing, move to `src/data/mocks/` or `src/__mocks__/`
### 10. Mock Data in Hooks
Files:
- `src/hooks/useInventoryData.ts` - Contains 99 lines of mock inventory data
- `src/hooks/useSalesTeamData.ts` - Contains mock sales rep data
- Issue: Mock data embedded in hooks
- Recommendation:
- Extract to separate mock data files
- Or verify if real API calls should replace these
---
๐ข LOW PRIORITY - Code Quality Improvements
### 11. Unused Dependencies
Potential candidates (needs verification):
- `@hello-pangea/dnd` - Check if both `@dnd-kit` and `@hello-pangea/dnd` are needed
- `react-window` + `react-window-infinite-loader` - Verify usage
- `immer` - Check if actually used
- `input-otp` - Verify usage
- `vaul` - Verify usage
Recommendation: Run `depcheck` or similar to identify truly unused dependencies
### 12. TypeScript Files in Docs
Files:
- `src/docs/MapsIntegrationGuide.ts`
- `src/docs/RouteManagementGuide.ts`
- Issue: Documentation as `.ts` files (should be `.md`)
- Recommendation: Convert to `.md` files
### 13. Duplicate Distance Calculation Functions
Found in multiple files:
- `src/lib/utils/geo.ts`
- `src/lib/utils/location.ts`
- `src/services/route-optimization/utils/distance-utils.ts`
- `src/components/optimization/schedule/calendar/utils.ts`
- Multiple other locations
- Issue: Same Haversine formula implemented multiple times
- Recommendation: Consolidate to single utility function
### 14. Large Component Files
Files:
- `src/components/optimization/schedule/territory-weekly-calendar.tsx` (2396 lines!)
- `src/components/optimization/schedule/interactive-route-builder.tsx` (1622 lines)
- Issue: Extremely large components, hard to maintain
- Recommendation:
- Break into smaller, focused components
- Extract custom hooks
- Split into logical sub-components
### 15. Unused Route in App.tsx
File: `src/App.tsx` line 187
- Issue: Documentation route appears incomplete (missing closing brace)
- Recommendation: Fix syntax or remove if unused
---
๐ SUMMARY STATISTICS
### File Count Analysis
- Total Components: ~400+ files
- Route Optimization Files: 6+ duplicate implementations
- SQL Scripts: 34+ (many one-off fixes)
- Export Scripts: 5+ in root
- CSV Files: 6+ in root
- Documentation: 7+ markdown files in root
### Estimated Cleanup Impact
- Dead Code Removal: ~2000-3000 lines
- Bundle Size Reduction: ~50-100KB (after removing unused imports/deps)
- Maintenance Burden: Significant reduction by consolidating route optimization
- Code Clarity: Major improvement from removing duplicates
---
๐ฏ RECOMMENDED ACTION PLAN
### Phase 1: Quick Wins (1-2 hours)
1. โ
Remove unused `Planner` import from `main.tsx`
2. โ
Delete `.backup` file
3. โ
Move/delete CSV export files
4. โ
Move export scripts to `scripts/` directory
5. โ
Fix incomplete route in `App.tsx`
### Phase 2: Code Consolidation (4-8 hours)
1. โ
Audit route optimization implementations
2. โ
Consolidate to single optimization engine
3. โ
Remove deprecated API key functions (if unused)
4. โ
Consolidate distance calculation functions
### Phase 3: Documentation & Organization (2-4 hours)
1. โ
Organize SQL scripts (migrations vs one-offs)
2. โ
Consolidate documentation files
3. โ
Review and organize sample/mock data
4. โ
Clean up root directory
### Phase 4: Dependency Audit (2-3 hours)
1. โ
Run dependency checker
2. โ
Remove unused npm packages
3. โ
Update package.json
### Phase 5: Component Refactoring (Ongoing)
1. โ
Break down large components (2396 lines โ multiple smaller files)
2. โ
Extract custom hooks from large components
3. โ
Improve code organization
---
๐ VERIFICATION CHECKLIST
Before deleting anything, verify:
- [ ] No imports reference the file/function
- [ ] No tests depend on it
- [ ] Not used in production workflows
- [ ] Not referenced in documentation
- [ ] Git history preserved (if needed for reference)
---
๐ก ADDITIONAL OBSERVATIONS
### Positive Aspects
- โ
Good TypeScript usage
- โ
Well-organized component structure
- โ
Comprehensive feature set
- โ
Good use of hooks pattern
### Areas for Improvement
- โ ๏ธ Code duplication (especially route optimization)
- โ ๏ธ Large monolithic components
- โ ๏ธ Mixing temporary scripts with source code
- โ ๏ธ Inconsistent file organization
---
๐ NOTES
This analysis is based on static code review. For complete accuracy:
1. Run dynamic analysis (e.g., webpack-bundle-analyzer)
2. Check actual runtime usage
3. Review test coverage
4. Consult with team about deprecated features
Promotion Decision
Promote into a technical note or architecture paper with implementation anchors.
Source Anchor
milkmendelivery/docs/archive/ARCHITECTURE_CLEANUP_ANALYSIS.md
Detected Structure
Method ยท Evaluation ยท Code Anchors ยท Architecture