Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

10.3 KB
Raw Blame
import { useEffect, useState } from 'react'
import { useNavigate, useSearchParams } from 'react-router'
import { tripsApi, authApi, reservationsApi } from '../../api/client'
import { tripRepo } from '../../repo/tripRepo'
import { isEffectivelyOffline } from '../../sync/networkMode'
import { useServerPing } from '../../sync/useServerPing'
import { useAuthStore } from '../../store/authStore'
import { useTranslation } from '../../i18n'
import { useToast } from '../../components/shared/Toast'
import { getApiErrorMessage } from '../../types'
import { localIsoToday } from './dashboardModel'
import { heroTrip } from '../../utils/heroTrip'
import { useTripSearch } from './useTripSearch'
import type { TripCreateRequest } from '@trek/shared'
import {
  type DashboardTrip,
  type TravelStats,
  type UpcomingReservation,
  type HeroBundle,
  getTripStatus,
  sortTrips,
} from './dashboardModel'

/**
 * Dashboard data hook — owns every bit of the page's state, data loading and
 * mutations (trip CRUD, archive/copy, travel stats, upcoming reservations,
 * the spotlight hero bundle) and exposes derived values + handlers. The
 * DashboardPage component is a pure wiring container that renders what this
 * returns. Behaviour is identical to the previous in-component logic.
 */
export function useDashboard() {
  const [trips, setTrips] = useState<DashboardTrip[]>([])
  const [archivedTrips, setArchivedTrips] = useState<DashboardTrip[]>([])
  const [isLoading, setIsLoading] = useState<boolean>(true)
  const [showForm, setShowForm] = useState<boolean>(false)
  const [editingTrip, setEditingTrip] = useState<DashboardTrip | null>(null)
  const [viewMode, setViewMode] = useState<'grid' | 'list'>(() => (localStorage.getItem('trek_dashboard_view') as 'grid' | 'list') || 'grid')
  const [deleteTrip, setDeleteTrip] = useState<DashboardTrip | null>(null)
  const [copyTrip, setCopyTrip] = useState<DashboardTrip | null>(null)
  const [tripFilter, setTripFilter] = useState<'planned' | 'archive' | 'completed'>('planned')
  const [allSubOpen, setAllSubOpen] = useState<boolean>(false)
  const [loadError, setLoadError] = useState<boolean>(false)

  const [stats, setStats] = useState<TravelStats | null>(null)
  const [upcoming, setUpcoming] = useState<UpcomingReservation[]>([])
  const [heroBundle, setHeroBundle] = useState<HeroBundle | null>(null)

  const navigate = useNavigate()
  const [searchParams, setSearchParams] = useSearchParams()
  const toast = useToast()
  const { t, locale } = useTranslation()
  const { demoMode, authCheckFailed, loadUser } = useAuthStore()

  const toggleViewMode = () => {
    setViewMode(prev => {
      const next = prev === 'grid' ? 'list' : 'grid'
      localStorage.setItem('trek_dashboard_view', next)
      return next
    })
  }

  useEffect(() => {
    if (searchParams.get('create') === '1') {
      setShowForm(true)
      setSearchParams({}, { replace: true })
    }
  }, [searchParams])

  useEffect(() => { void loadTrips() }, [])

  // Travel stats + upcoming reservations power the atlas row and the sidebar.
  // Both are best-effort: a failure just leaves that section empty. Every trip
  // mutation below reloads them, so the counters and the booking dates don't
  // lag behind a created, copied or redated trip until the next visit. #2604
  const loadSideData = () => {
    authApi.travelStats().then(setStats).catch(() => {})
    reservationsApi.upcoming().then((r: { reservations: UpcomingReservation[] }) => setUpcoming(r.reservations || [])).catch(() => {})
  }

  useEffect(() => { loadSideData() }, [])

  const loadTrips = async () => {
    setIsLoading(true)
    try {
      const { trips, archivedTrips } = await tripRepo.list()
      setTrips(sortTrips(trips))
      setArchivedTrips(sortTrips(archivedTrips))
      setLoadError(false)
    } catch {
      setLoadError(true)
      toast.error(t('dashboard.toast.loadError'))
    } finally {
      setIsLoading(false)
    }
  }

  // A trip created, copied or deleted by this user elsewhere (another tab, an MCP
  // client) is read back quietly, one read per burst: no loading state and no
  // toast, since nobody here asked for it. trips:changed goes to the acting user
  // only; trip:deleted goes to the trip's room, which this page is never in.
  const refreshQuietly = async () => {
    try {
      const { trips, archivedTrips } = await tripRepo.list()
      setTrips(sortTrips(trips))
      setArchivedTrips(sortTrips(archivedTrips))
      loadSideData()
    } catch {
      // The list on screen stays; the next visit reads it again.
    }
  }
  useServerPing('trips:changed', true, refreshQuietly)

  // Re-run both the trip fetch and the auth check so a recovered backend clears
  // the error banner (loadUser resets authCheckFailed on success). #1283
  const retryLoad = () => {
    loadUser({ silent: true })
    void loadTrips()
  }

  const today = localIsoToday()
  // A trip the hero features on its own merit: one that is running, else the next
  // one coming up. Only that one is taken out of the grid below, so the same trip
  // isn't shown twice.
  const featured = heroTrip(trips, today)
  // With neither, the hero still shows something rather than sitting empty — but
  // that trip is only borrowed for the header and must stay in the grid, or a user
  // whose trips are all finished (or undated) sees "No trips yet". #1706
  const spotlight = featured || trips[0] || null
  const rest = featured ? trips.filter(t => t.id !== featured.id) : trips

  // Pull the spotlight trip's members + places so the boarding pass can show
  // real buddies and place thumbnails instead of placeholders.
  useEffect(() => {
    if (!spotlight) { setHeroBundle(null); return }
    let cancelled = false
    tripsApi.bundle(spotlight.id)
      .then((b: HeroBundle) => { if (!cancelled) setHeroBundle({ members: b.members || [], places: b.places || [] }) })
      .catch(() => { if (!cancelled) setHeroBundle(null) })
    return () => { cancelled = true }
  }, [spotlight?.id])

  const handleCreate = async (tripData: TripCreateRequest) => {
    try {
      const data = await tripsApi.create(tripData)
      setTrips(prev => sortTrips([data.trip, ...prev]))
      toast.success(t('dashboard.toast.created'))
      loadSideData()
      return data
    } catch (err: unknown) {
      throw new Error(getApiErrorMessage(err, t('dashboard.toast.createError')))
    }
  }

  const handleUpdate = async (tripData: TripCreateRequest) => {
    if (!editingTrip) return
    try {
      const data = await tripRepo.update(editingTrip.id, tripData)
      setTrips(prev => sortTrips(prev.map(t => t.id === editingTrip.id ? data.trip : t)))
      toast.success(t('dashboard.toast.updated'))
      loadSideData()
    } catch (err: unknown) {
      throw new Error(getApiErrorMessage(err, t('dashboard.toast.updateError')))
    }
  }

  // A refusal says why (no right to archive or delete this trip, say) rather
  // than only that it failed; the generic line stays for a failure without one.
  const confirmDelete = async () => {
    if (!deleteTrip) return
    // Deleting, copying and creating a trip are the server's to do; archiving
    // and editing its name wait in the offline queue.
    if (isEffectivelyOffline()) { toast.error(t('common.needsConnection')); setDeleteTrip(null); return }
    try {
      await tripsApi.delete(deleteTrip.id)
      setTrips(prev => prev.filter(t => t.id !== deleteTrip.id))
      setArchivedTrips(prev => prev.filter(t => t.id !== deleteTrip.id))
      toast.success(t('dashboard.toast.deleted'))
      loadSideData()
    } catch (err: unknown) {
      toast.error(getApiErrorMessage(err, t('dashboard.toast.deleteError')))
    }
    setDeleteTrip(null)
  }

  const handleArchive = async (id: number) => {
    try {
      const data = await tripRepo.update(id, { is_archived: true })
      setTrips(prev => prev.filter(t => t.id !== id))
      setArchivedTrips(prev => sortTrips([data.trip, ...prev]))
      toast.success(t('dashboard.toast.archived'))
      loadSideData()
    } catch (err: unknown) {
      toast.error(getApiErrorMessage(err, t('dashboard.toast.archiveError')))
    }
  }

  const handleUnarchive = async (id: number) => {
    try {
      const data = await tripRepo.update(id, { is_archived: false })
      setArchivedTrips(prev => prev.filter(t => t.id !== id))
      setTrips(prev => sortTrips([data.trip, ...prev]))
      toast.success(t('dashboard.toast.restored'))
      loadSideData()
    } catch (err: unknown) {
      toast.error(getApiErrorMessage(err, t('dashboard.toast.restoreError')))
    }
  }

  const confirmCopy = async () => {
    if (!copyTrip) return
    if (isEffectivelyOffline()) { toast.error(t('common.needsConnection')); setCopyTrip(null); return }
    try {
      const data = await tripsApi.copy(copyTrip.id, { title: `${copyTrip.title} (${t('dashboard.copySuffix')})` })
      setTrips(prev => sortTrips([data.trip, ...prev]))
      toast.success(t('dashboard.toast.copied'))
      loadSideData()
    } catch (err: unknown) {
      toast.error(getApiErrorMessage(err, t('dashboard.toast.copyError')))
    }
    setCopyTrip(null)
  }

  // The cover can be swapped from the archive filter too, so patch both lists.
  const applyCoverUpdate = (tripId: number, coverUrl: string) => {
    const patch = (list: DashboardTrip[]) => list.map(t => t.id === tripId ? { ...t, cover_image: coverUrl } : t)
    setTrips(patch)
    setArchivedTrips(patch)
  }

  // While a search runs it spans every trip, archived ones included (#2190).
  const search = useTripSearch(trips, archivedTrips, locale)
  const gridTrips = search.active ? search.results
    : tripFilter === 'archive' ? archivedTrips
    : tripFilter === 'completed' ? rest.filter(t => getTripStatus(t) === 'past')
    : rest.filter(t => getTripStatus(t) !== 'past')

  return {
    // cross-cutting
    demoMode, locale, t, navigate,
    // data + derived
    spotlight, heroBundle, stats, upcoming, gridTrips, isLoading,
    loadError: loadError || authCheckFailed, retryLoad,
    // Pull-to-refresh on mobile: the same quiet read, no loading state.
    refreshQuietly,
    // ui state
    tripFilter, setTripFilter, viewMode, toggleViewMode,
    showForm, setShowForm, editingTrip, setEditingTrip,
    deleteTrip, setDeleteTrip, copyTrip, setCopyTrip, applyCoverUpdate,
    allSubOpen, setAllSubOpen,
    search,
    // actions
    handleCreate, handleUpdate, confirmDelete, handleArchive, handleUnarchive, confirmCopy,
  }
}