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