Skip to content

Light Lantern Flow Refactor - Implementation Complete ✅

What Was Refactored

This document describes the refactor that was implemented to consolidate the Light Lantern flows and eliminate code duplication.

Previous State (Before Refactor)

Both Flow 1 and Flow 2 did the same thing (light a lantern) but used completely different code paths:

Flow 1 (Venue Detail)

  • Lives in Dashboard.jsx using view state machine
  • Entry: handleLightLantern() → set view='form'
  • Form: LightLanternForm embedded directly
  • Light: confirmLantern() → calls lightLantern() service → set view='active'
  • Success: Separate view='success' screen before view='active' (duplicate!)

Flow 2 (Fire Icon Modal)

  • Lives in LightLanternModal.jsx component
  • Entry: handleLanternHubLightLantern() → open modal
  • Form: LightLanternForm inside modal
  • Light: handleFormConfirm() → calls lightLantern() service → set step='success'
  • Success: Built into modal with 50 lines of duplicate UI

Problems

IssueImpact
Duplicated Firebase callsHard to maintain, inconsistent error handling
Different success screensUser gets different UX depending on entry point
Separate state managementbugs in one flow don't fix the other
~200 lines of parallel codeWaste and maintenance burden
Modal re-fetches locationUnnecessary API calls

Implementation (Completed 2026-01-07)

1. Created Shared Hook: useLightLantern

File: src/hooks/useLightLantern.js

Centralizes the Firebase lighting logic:

javascript
const { lightLanternFlow, loading, error } = useLightLantern()

await lightLanternFlow({
  userId: currentUser.uid,
  venueId: venue.id,
  location: { lat, lng },
  formData: { mood, interest },
  onSuccess: (lantern) => { ... },
  onError: (error) => { ... }
})

Benefits:

  • Single source of truth for lantern creation
  • Consistent loading and error states
  • Callbacks for both flows to handle success/error

2. Refactored Flow 1 (Dashboard.jsx)

Changes to confirmLantern():

  • Now uses useLightLantern hook instead of direct Firebase call
  • Removed 15 lines of duplicate code
  • Error handling moved to hook callback
javascript
// Before: 20 lines of Firebase logic
// After: Uses hook, much simpler
await lightLanternFlow({
  userId: currentUser.uid,
  venueId: selectedVenue.id,
  location: userLocation,
  formData: data,
  onSuccess: (lantern) => {
    setView('active')
  },
  onError: (error) => {
    alert('Failed: ' + error.message)
  }
})

3. Refactored Flow 2 (LightLanternModal.jsx)

Changes:

  • Now uses useLightLantern hook
  • Removed success step from modal (~50 lines deleted)
  • Modal simplified to just 2 steps: venue → form → close
  • Dashboard's onLanternLit callback handles navigation to ActiveLanternView

Before:

  • 3 steps: venue → form → success (built into modal)
  • 50 lines of success UI duplication
  • Complex state: selectedVenue, formData, litLantern

After:

  • 2 steps: venue → form
  • Modal closes on success, no success screen needed
  • Simplified state: selectedVenue, lighting
  • Uses hook: const step = selectedVenue ? 'form' : 'venue'

4. Bug Fixes

Fixed Flow 1 duplicate success screens:

  • Removed intermediate view='success' state
  • Goes directly to ActiveLanternView after lighting

Fixed Flow 2 blank screen issue:

  • Modal's onLanternLit callback now always sets view='active'
  • ActiveLanternView handles missing selectedVenue gracefully

Results

Code Metrics

MetricBeforeAfterChange
Lines in LightLanternModal296190-106 lines ⬇️
Duplicate Firebase calls21-50% 🎯
Success screens2 different1 sharedUnified UX ✅
Hook-based refactoring0%100%DRY principle ✅

Architecture

User clicks Light Lantern (anywhere)

Flow 1 (venue detail)   OR   Flow 2 (fire icon modal)
      ↓                              ↓
confirmLantern()   OR   handleFormConfirm()
      ↓                              ↓
   useLightLantern hook

  Firebase: lightLantern()

   SUCCESS

Dashboard: setView('active')

ActiveLanternView (SAME SUCCESS SCREEN FOR BOTH)

Benefits Achieved

Single source of truth - Both flows use useLightLantern hook
Code reduction - 106 lines removed from modal, 60+ lines of duplication eliminated
Converged UX - Both flows end at same ActiveLanternView
Easier to maintain - Change lighting logic once, affects both flows instantly
Simpler modal - No success state management, just data collection
Bugs fixed - Blank screen, duplicate success screens, setMyLantern error
Type safety - Consistent parameters across both flows
Scalability - If we add Flow 3, reuse same hook and success screen


Testing Verification

Both flows tested and working:

✅ Flow 1: Venue detail → form → success screen
✅ Flow 2: Fire icon → modal → form → success screen
✅ Success screen shows correct lantern details
✅ Fire icon "View Details" shows lantern properly
✅ Real-time updates work correctly


Files Modified


Future Improvements

If we add new flows:

  • Reuse useLightLantern hook
  • Reuse ActiveLanternView component
  • New flows will automatically follow the same pattern

No need to duplicate lighting logic again!

Built with VitePress