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.

40.9 KB
Raw Blame
import React from 'react'
import { useTranslation } from '../i18n'
import Navbar from '../components/Layout/Navbar'
import DemoWelcome from '../components/Demo/DemoWelcome'
import TripFormModal from '../components/Trips/TripFormModal'
import ConfirmDialog from '../components/shared/ConfirmDialog'
import CopyTripDialog from '../components/shared/CopyTripDialog'
import CustomSelect from '../components/shared/CustomSelect'
import PlaceAvatar from '../components/shared/PlaceAvatar'
import EmptyState from '../components/shared/EmptyState'
import { Skeleton, SpotlightSkeleton, TripCardSkeleton } from '../components/shared/Skeleton'
import MobileTopBar from '../components/Layout/MobileTopBar'
import HelpAnchor from '../components/Help/HelpAnchor'
import { useDashboard } from './dashboard/useDashboard'
import {
  type DashboardTrip, type HeroBundle, type TravelStats, type UpcomingReservation,
  MOMENT_LABEL, MS_PER_DAY, daysUntil, fullDate, getTripStatus, upcomingKey,
} from './dashboard/dashboardModel'
import {
  Plus, Edit2, Trash2, Archive, ArchiveRestore, Copy, ArrowRight, MapPin,
  Plane, Hotel, Utensils, Clock, RefreshCw, ArrowRightLeft, Calendar,
  LayoutGrid, List, Ticket, X, CalendarPlus, ParkingSquare, LogIn, LogOut, Search,
} from 'lucide-react'
import { IcsSubscribeModal } from '../components/Planner/IcsSubscribeModal'
import CollectionsWidget from '../components/Dashboard/CollectionsWidget'
import { useCurrencyConverter } from '../components/Dashboard/useCurrencyConverter'
import { useDashboardPlugins } from '../components/Dashboard/useDashboardPlugins'
import { shortZone, useDashboardTimezones, zoneOptionLabel } from '../components/Dashboard/useDashboardTimezones'
import PluginWidgets from '../components/Plugins/PluginWidgets'
import PluginFrame from '../components/Plugins/PluginFrame'
import { TripCardBadges } from '../components/Plugins/TripCardBadges'
import type { TripCardBadge } from '../api/client'
import { usePluginStore } from '../store/pluginStore'
import { formatTime, splitReservationDateTime } from '../utils/formatters'
import { convertDistance, getDistanceUnitLabel } from '../utils/units'
import { useSettingsStore } from '../store/settingsStore'
import { useAddonStore } from '../store/addonStore'
import { normalizeAppearance } from '@trek/shared'
import '../styles/dashboard.css'

const GRADIENTS = [
  'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
  'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
  'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)',
  'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)',
  'linear-gradient(135deg, #fa709a 0%, #fee140 100%)',
  'linear-gradient(135deg, #a18cd1 0%, #fbc2eb 100%)',
  'linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%)',
  'linear-gradient(135deg, #96fbc4 0%, #f9f586 100%)',
]
function tripGradient(id: number): string { return GRADIENTS[id % GRADIENTS.length] }

// Day + short month for the boarding pass / cards, plus the year — but only
// when it isn't the current year (this year's trips stay clutter-free), e.g.
// { d: '10', m: 'Sep', y: '' } now vs { …, y: '2024' } for an older trip.
function splitDate(dateStr: string | null | undefined, locale: string): { d: string; m: string; y: string } | null {
  if (!dateStr) return null
  const date = new Date(dateStr + 'T00:00:00Z')
  if (Number.isNaN(date.getTime())) return null // malformed date — render a dash, never crash
  const otherYear = date.getUTCFullYear() !== new Date().getUTCFullYear()
  return {
    d: date.toLocaleDateString(locale, { day: 'numeric', timeZone: 'UTC' }),
    m: date.toLocaleDateString(locale, { month: 'short', timeZone: 'UTC' }),
    y: otherYear ? date.toLocaleDateString(locale, { year: 'numeric', timeZone: 'UTC' }) : '',
  }
}

function buddyColor(seed: number): string {
  const pairs = [
    ['#6366f1', '#8b5cf6'], ['#10b981', '#059669'], ['#f59e0b', '#d97706'],
    ['#ec4899', '#be185d'], ['#0ea5e9', '#2563eb'], ['#14b8a6', '#0d9488'],
  ]
  const [a, b] = pairs[seed % pairs.length]
  return `linear-gradient(135deg, ${a}, ${b})`
}

function initials(name: string | null | undefined): string {
  if (!name) return '?'
  const parts = name.trim().split(/\s+/)
  if (parts.length >= 2) return (parts[0][0] + parts[1][0]).toUpperCase()
  return name.slice(0, 2).toUpperCase()
}

const RES_ICON: Record<string, React.ReactElement> = {
  flight: <Plane size={16} />, hotel: <Hotel size={16} />, restaurant: <Utensils size={16} />, parking: <ParkingSquare size={16} />,
  // A stay's two moments (#1934) — the arrow says which way you are going, on
  // the same green the hotel tile already uses.
  checkin: <LogIn size={16} />, checkout: <LogOut size={16} />,
}
const RES_TYPE_CLASS: Record<string, string> = {
  flight: 'flight', hotel: 'hotel', restaurant: 'food', checkin: 'hotel', checkout: 'hotel',
}

export default function DashboardPage(): React.ReactElement {
  // ViewportRoute in App.tsx picks the branch now, so the phone screen is a
  // chunk of its own instead of a dead limb in this one.
  return <DashboardPageDesktop />
}

function DashboardPageDesktop(): React.ReactElement {
  // Page = wiring container: all state, data loading and mutations live in the
  // useDashboard data hook; this component only renders what it returns.
  const {
    demoMode, locale, t, navigate,
    spotlight, heroBundle, stats, upcoming, gridTrips, isLoading,
    loadError, retryLoad,
    tripFilter, setTripFilter, viewMode, toggleViewMode,
    showForm, setShowForm, editingTrip, setEditingTrip,
    deleteTrip, setDeleteTrip, copyTrip, setCopyTrip, applyCoverUpdate,
    handleCreate, handleUpdate, confirmDelete, handleArchive, handleUnarchive, confirmCopy,
    allSubOpen, setAllSubOpen, search,
  } = useDashboard()

  // Dashboard widget visibility (from the appearance config). Phones never reach this
  // component — DashboardPage routes them to MDashboard — so only the desktop set applies.
  const appearanceCfg = useSettingsStore(s => s.settings.appearance)
  const dashCfg = normalizeAppearance(appearanceCfg).dashboard
  const sideWidgets = dashCfg.desktop
  const showCurrency = sideWidgets.currency
  const showTimezones = sideWidgets.timezones
  const showUpcoming = sideWidgets.upcomingReservations
  // Collections is double-gated: the admin addon AND the per-user widget flag.
  const isAddonEnabled = useAddonStore(s => s.isEnabled)
  const showCollections = isAddonEnabled('collections') && sideWidgets.collections
  // Desktop has a master toggle for the whole right column; off → centered layout.
  // Only true dashboard widgets belong here — hero mounts on the boarding pass, and
  // place-detail/day-detail widgets live inside the planner panels, not the sidebar.
  // Plugin-contributed badges on the trip cards (tripCardProvider hook). One fetch for
  // all visible cards; only runs when at least one plugin is active. Fail-safe.
  const { widgetPlugins, badgesFor } = useDashboardPlugins(gridTrips.map(t => t.id))
  const sidebarVisible = dashCfg.desktop.sidebar && (showCurrency || showCollections || showTimezones || showUpcoming || widgetPlugins.length > 0)

  return (
    <>
      {/* Navbar lives outside .trek-dash so it keeps the app-wide font + button
          styling instead of inheriting the dashboard scope's font and the
          `.trek-dash button` reset (which shifted the bell icon + menu items). */}
      <Navbar />
      <HelpAnchor id="dashboard" />
      <div className="trek-dash trek-dash-shell">
      {demoMode && <DemoWelcome />}
      <div className="trek-dash-scroll">
        <MobileTopBar />
        <main className="page" data-no-sidebar={sidebarVisible ? undefined : 'true'}>
          <div className="page-main">
            {loadError && (
              <div className="dash-error" role="alert">
                <span className="dash-error-txt">{t('dashboard.loadErrorBanner')}</span>
                <button type="button" className="dash-error-retry" onClick={retryLoad}>
                  <RefreshCw size={15} />
                  {t('dashboard.retry')}
                </button>
              </div>
            )}
            {/* The hero and the cards below stand in for themselves while the trips load.
                Without them the page rendered its finished empty state: no hero, no
                cards, and a stats row reading zero, which reads as "your trips are
                gone" rather than "not here yet" on a slow connection (#2115). */}
            {isLoading && !spotlight && gridTrips.length === 0 && <SpotlightSkeleton />}
            {spotlight && (
              <BoardingPassHero
                trip={spotlight}
                bundle={heroBundle}
                locale={locale}
                onOpen={() => navigate(`/trips/${spotlight.id}`)}
                onEdit={() => { setEditingTrip(spotlight); setShowForm(true) }}
                onCopy={() => setCopyTrip(spotlight)}
                onArchive={() => spotlight.is_archived ? handleUnarchive(spotlight.id) : handleArchive(spotlight.id)}
                onDelete={() => setDeleteTrip(spotlight)}
              />
            )}

            <AtlasStats stats={stats} />

            <section>
              <div className="sec-head">
                <h3 className="sec-title">{t('dashboard.title')}</h3>
                <div className="sec-tools">
                  {/* Trip search (#2190): every trip, by title, date or a place on it */}
                  <label className={`dash-search${search.active ? ' on' : ''}`}>
                    <Search size={15} strokeWidth={2.2} aria-hidden />
                    <input
                      type="search"
                      value={search.query}
                      onChange={e => search.setQuery(e.target.value)}
                      onKeyDown={e => { if (e.key === 'Escape') search.setQuery('') }}
                      placeholder={t('dashboard.search.placeholder')}
                      aria-label={t('dashboard.search.label')}
                    />
                    {search.active && (
                      <button type="button" className="dash-search-clear" onClick={() => search.setQuery('')} aria-label={t('dashboard.search.clear')}>
                        <X size={13} strokeWidth={2.4} />
                      </button>
                    )}
                  </label>
                  <div className={`seg${search.active ? ' is-muted' : ''}`}>
                    <button type="button" className={tripFilter === 'planned' ? 'on' : ''} onClick={() => setTripFilter('planned')}>{t('dashboard.filter.planned')}</button>
                    <button type="button" className={tripFilter === 'archive' ? 'on' : ''} onClick={() => setTripFilter('archive')}>{t('dashboard.archived')}</button>
                    <button type="button" className={tripFilter === 'completed' ? 'on' : ''} onClick={() => setTripFilter('completed')}>{t('dashboard.mobile.completed')}</button>
                  </div>
                  <button type="button"
                    className="tool-action"
                    aria-label={t('dashboard.subscribeAllTrips')}
                    title={t('dashboard.subscribeAllTrips')}
                    onClick={() => setAllSubOpen(true)}
                    style={{ width: 38, height: 38, borderRadius: 11 }}
                  >
                    <CalendarPlus size={17} />
                  </button>
                  <button type="button" className="tool-action" aria-label={t('dashboard.aria.toggleView')} onClick={toggleViewMode} style={{ width: 38, height: 38, borderRadius: 11 }}>
                    {viewMode === 'grid' ? <List size={17} /> : <LayoutGrid size={17} />}
                  </button>
                </div>
              </div>
              {allSubOpen && (
                <IcsSubscribeModal
                  endpoint="/api/feed/user"
                  title={t('dashboard.subscribeAllTrips')}
                  description={t('dashboard.subscribeAllTripsDesc')}
                  onClose={() => setAllSubOpen(false)}
                />
              )}

              {/* "No trips yet" only when there really are none — a user whose trips are
                  all finished has a hero, and telling them to create their first trip is
                  simply wrong (#1706). Same condition the mobile dashboard already uses. */}
              {search.active && gridTrips.length === 0 && (
                <div className="trips-empty">
                  <EmptyState scene="dashboard" title={t('dashboard.search.empty', { query: search.query.trim() })} />
                </div>
              )}

              {!search.active && gridTrips.length === 0 && !spotlight && tripFilter === 'planned' && !isLoading && !loadError && (
                <div className="trips-empty">
                  <EmptyState scene="dashboard" title={t('dashboard.emptyTitle')} />
                </div>
              )}

              <div className={`trips${viewMode === 'list' ? ' list-view' : ''}`}>
                {gridTrips.map(trip => (
                  <TripCard
                    key={trip.id}
                    trip={trip}
                    locale={locale}
                    badges={badgesFor(trip.id)}
                    matchedPlaces={search.active ? search.placeHits.get(trip.id) : undefined}
                    onOpen={() => navigate(`/trips/${trip.id}`)}
                    onEdit={() => { setEditingTrip(trip); setShowForm(true) }}
                    onCopy={() => setCopyTrip(trip)}
                    onArchive={() => trip.is_archived ? handleUnarchive(trip.id) : handleArchive(trip.id)}
                    onDelete={() => setDeleteTrip(trip)}
                  />
                ))}
                {isLoading && gridTrips.length === 0 && (
                  <>
                    <TripCardSkeleton />
                    <TripCardSkeleton />
                    <TripCardSkeleton />
                  </>
                )}
                {tripFilter === 'planned' && !search.active && !isLoading && (
                  <button type="button" className="add-trip-card" onClick={() => { setEditingTrip(null); setShowForm(true) }}>
                    <div>
                      <div className="circ"><Plus size={20} /></div>
                      <div className="ttl">{t('dashboard.newTrip')}</div>
                      <div className="sub">{t('dashboard.newTripSub')}</div>
                    </div>
                  </button>
                )}
              </div>
            </section>
          </div>

          {sidebarVisible && (
            <aside className="page-sidebar">
              {showCurrency && <CurrencyTool />}
              {showCollections && <CollectionsWidget onOpen={() => navigate('/collections')} />}
              {showTimezones && <TimezoneTool locale={locale} />}
              {showUpcoming && <UpcomingTool items={upcoming} locale={locale} onOpen={(tripId) => navigate(`/trips/${tripId}`)} />}
              <PluginWidgets plugins={widgetPlugins} tripId={spotlight ? String(spotlight.id) : null} />
            </aside>
          )}
        </main>
      </div>

      <button type="button"
        className="fab-new-trip"
        onClick={() => { setEditingTrip(null); setShowForm(true) }}
        aria-label={t('dashboard.newTrip')}
        title={t('dashboard.newTrip')}
      >
        <Plus size={22} strokeWidth={2.4} />
        <span className="fab-label">{t('dashboard.newTrip')}</span>
      </button>

      {showForm && (
        <TripFormModal
          isOpen={showForm}
          trip={editingTrip}
          onClose={() => { setShowForm(false); setEditingTrip(null) }}
          onSave={editingTrip ? handleUpdate : handleCreate}
          onCoverUpdate={applyCoverUpdate}
        />
      )}
      {deleteTrip && (
        <ConfirmDialog
          isOpen={!!deleteTrip}
          title={t('common.delete')}
          message={t('dashboard.confirm.delete', { title: deleteTrip.title })}
          confirmLabel={t('common.delete')}
          onConfirm={confirmDelete}
          onClose={() => setDeleteTrip(null)}
          danger
        />
      )}
      {copyTrip && (
        <CopyTripDialog
          isOpen={!!copyTrip}
          tripTitle={copyTrip.title}
          onConfirm={confirmCopy}
          onClose={() => setCopyTrip(null)}
        />
      )}
      </div>
    </>
  )
}

// ── Boarding-pass hero ───────────────────────────────────────────────────────
function BoardingPassHero({ trip, bundle, locale, onOpen, onEdit, onCopy, onArchive, onDelete }: {
  trip: DashboardTrip; bundle: HeroBundle | null; locale: string; onOpen: () => void
  onEdit: () => void; onCopy: () => void; onArchive: () => void; onDelete: () => void
}): React.ReactElement {
  const { t } = useTranslation()
  const heroPlugins = usePluginStore(s => s.plugins).filter(p => p.type === 'widget' && p.slot === 'hero')
  const stop = (e: React.MouseEvent, fn: () => void) => { e.stopPropagation(); fn() }
  const status = getTripStatus(trip)
  const archiveLabel = trip.is_archived ? t('dashboard.restore') : t('dashboard.archive')
  const start = splitDate(trip.start_date, locale)
  const end = splitDate(trip.end_date, locale)

  // Countdown cell — plain text in the same style as the trip-dates cell:
  // days remaining while the trip runs, days until departure before it starts.
  const until = daysUntil(trip.start_date)
  const ongoing = status === 'ongoing'
  let countdownTop = ''
  let countdownNumber = ''
  let countdownLabel = ''
  if (ongoing && trip.end_date) {
    const todayMid = new Date(); todayMid.setHours(0, 0, 0, 0)
    const endMid = new Date(trip.end_date + 'T00:00:00')
    const daysLeft = Math.max(0, Math.round((endMid.getTime() - todayMid.getTime()) / MS_PER_DAY))
    countdownTop = t('dashboard.status.ongoing')
    countdownNumber = String(daysLeft)
    countdownLabel = daysLeft === 0 ? t('dashboard.hero.lastDay') : t('dashboard.hero.daysLeft', { count: daysLeft })
  } else if (until !== null && until >= 0) {
    countdownTop = t('dashboard.hero.startsIn')
    countdownNumber = String(until)
    countdownLabel = t('dashboard.hero.dayUnit', { count: until })
  }

  const members = bundle?.members || []
  const places = bundle?.places || []
  const buddyCount = trip.shared_count != null ? trip.shared_count + 1 : members.length
  const placeCount = trip.place_count || places.length

  const badge = status === 'ongoing' ? t('dashboard.hero.badgeLive')
    : status === 'today' ? t('dashboard.hero.badgeToday')
    : status === 'tomorrow' ? t('dashboard.hero.badgeTomorrow')
    : status === 'future' ? t('dashboard.hero.badgeNext')
    : t('dashboard.hero.badgeRecent')

  const passCells = (
    <>
      <div className="pass-cell buddies">
        <div className="pass-label">{t('dashboard.members')}</div>
        <div className="buddies-avatars">
          {members.slice(0, 4).map((m, i) => (
            m.avatar_url
              ? <img key={m.id} className="buddy-avatar" src={m.avatar_url} alt={m.username} style={{ objectFit: 'cover' }} />
              : <div key={m.id} className="buddy-avatar" style={{ background: buddyColor(i) }}>{initials(m.username)}</div>
          ))}
          {members.length > 4 && <div className="buddy-more">+{members.length - 4}</div>}
          {members.length === 0 && <div className="buddy-avatar" style={{ background: buddyColor(0) }}>{initials(trip.owner_username)}</div>}
        </div>
        <div className="date-month">{t('dashboard.hero.travelers', { count: buddyCount })}</div>
      </div>

      <div className="pass-cell dates-combined">
        <div className="pass-label">{t('dashboard.hero.tripDates')}</div>
        <div className="dates-row">
          {start ? <div className="date-block"><div className="date-num mono">{start.d}</div><div className="date-month">{start.m}{start.y ? ` ${start.y}` : ''}</div></div>
            : <div className="date-block"><div className="date-num">—</div></div>}
          <div className="date-arrow"><ArrowRight /></div>
          {end ? <div className="date-block"><div className="date-num mono">{end.d}</div><div className="date-month">{end.m}{end.y ? ` ${end.y}` : ''}</div></div>
            : <div className="date-block"><div className="date-num">—</div></div>}
        </div>
      </div>

      <div className="pass-cell countdown">
        {countdownNumber && (
          <>
            <div className="pass-label">{countdownTop}</div>
            <div className="date-num mono">{countdownNumber}</div>
            <div className="date-month">{countdownLabel}</div>
          </>
        )}
      </div>

      <div className="pass-cell places">
        <div className="pass-label">{t('dashboard.pass.places')}</div>
        <div className="places-preview">
          {places.slice(0, 3).map(p => (
            <div key={p.id} className="place-av">
              <PlaceAvatar place={p} size={32} category={{ color: p.category_color ?? undefined, icon: p.category_icon ?? undefined }} />
            </div>
          ))}
          {places.length === 0 && <div className="place-more"><MapPin size={15} /></div>}
          {places.length > 3 && <div className="place-more">+{places.length - 3}</div>}
        </div>
        <div className="date-month">{t('dashboard.hero.destinations', { count: placeCount })}</div>
      </div>
    </>
  )

  return (
    <div
      className="hero-trip"
      onClick={onOpen}
      role="button"
      // No press-scale on composite cards: shrinking the surface mid-click slides
      // the inner menu buttons out from under the pointer (#2158).
      data-no-press
      tabIndex={0}
      onKeyDown={(e) => { if (e.target === e.currentTarget && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); onOpen() } }}
    >
      {trip.cover_image
        ? <img className="bg" src={trip.cover_image} alt={trip.title} />
        : <div className="bg" style={{ background: tripGradient(trip.id) }} />}
      <div className="scrim" />
      <div className="hero-content">
        <div className="hero-top">
          <div className="hero-badge">
            {status === 'ongoing' && <span className="pulse" />}
            {badge}
          </div>
          <div className="hero-tools">
            <button type="button" className="hero-tool" aria-label={t('common.edit')} title={t('common.edit')} onClick={(e) => stop(e, onEdit)}><Edit2 size={17} strokeWidth={2.2} /></button>
            <button type="button" className="hero-tool" aria-label={t('dashboard.aria.duplicate')} title={t('dashboard.aria.duplicate')} onClick={(e) => stop(e, onCopy)}><Copy size={17} strokeWidth={2.2} /></button>
            <button type="button" className="hero-tool" aria-label={archiveLabel} title={archiveLabel} onClick={(e) => stop(e, onArchive)}>{trip.is_archived ? <ArchiveRestore size={17} strokeWidth={2.2} /> : <Archive size={17} strokeWidth={2.2} />}</button>
            <button type="button" className="hero-tool" aria-label={t('common.delete')} title={t('common.delete')} onClick={(e) => stop(e, onDelete)}><Trash2 size={17} strokeWidth={2.2} /></button>
          </div>
        </div>

        <div className="hero-title-block">
          <h2 className="hero-title">{trip.title}</h2>
        </div>

        <div className="hero-pass-wrap">
          {heroPlugins.length > 0 && (
            <div className="hero-pass-overlay" aria-hidden="true">
              {heroPlugins.map(p => (
                <PluginFrame key={p.id} pluginId={p.id} tripId={String(trip.id)} title={p.name} surface="dashboard-widget" className="hero-overlay-frame" />
              ))}
            </div>
          )}
          <div className="hero-pass" role="presentation" onClick={(e) => { e.stopPropagation(); onOpen() }}>
            <div className="hero-pass-inner">{passCells}</div>
          </div>
        </div>
      </div>
    </div>
  )
}

// ── Atlas / stats row ────────────────────────────────────────────────────────
function formatCompactDistance(value: number): string {
  const safeValue = Number.isFinite(value) ? Math.max(0, value) : 0
  // String() keeps a '.' decimal regardless of locale (no "1,5k" in non-English UIs).
  if (safeValue >= 1000) {
    return `${String(Math.round(safeValue / 100) / 10)}k`
  }
  const rounded = Math.round(safeValue * 10) / 10
  if (safeValue > 0 && rounded === 0) return '<0.1'
  return String(rounded)
}

function AtlasStats({ stats }: { stats: TravelStats | null }): React.ReactElement | null {
  const { t } = useTranslation()
  const distanceUnit = useSettingsStore(s => s.settings.distance_unit) || 'metric'
  const appearance = useSettingsStore(s => s.settings.appearance)
  const dash = normalizeAppearance(appearance).dashboard

  // Widget visibility — this row only ever renders on the desktop page.
  const showAtlas = dash.desktop.atlas
  const showTrips = dash.desktop.tripsTotal
  const showDays = dash.desktop.daysTraveled
  const showDistance = dash.desktop.distanceFlown
  if (!showAtlas && !showTrips && !showDays && !showDistance) return null

  // Reflow: the grid spreads the visible tiles to full width (the passport stays
  // proportionally wider). Set as CSS vars so the responsive media queries still win.
  const atlasTemplate =
    [dash.desktop.atlas && '1.5fr', dash.desktop.tripsTotal && '1fr', dash.desktop.daysTraveled && '1fr', dash.desktop.distanceFlown && '1fr']
      .filter(Boolean).join(' ') || '1fr'
  const atlasTemplateM =
    [dash.mobile.tripsTotal && '1fr', dash.mobile.daysTraveled && '1fr'].filter(Boolean).join(' ') || '1fr'

  // stats stays null until travelStats() answers, and that call carries no loading
  // flag of its own, so every tile below used to render its zero fallback in the
  // meantime. On a slow connection that reads as "no trips", which is the whole of
  // #2115. A dash placeholder says "not yet" where a 0 says "none".
  const statsPending = stats === null
  const countries = stats?.countries || []
  const distanceKm = stats?.totalDistanceKm || 0
  const distance = convertDistance(distanceKm, distanceUnit)
  const distanceText = formatCompactDistance(distance)
  const equatorDistance = convertDistance(40075, distanceUnit)
  const equatorTimes = (distance / equatorDistance).toFixed(2)
  const distanceLabel = getDistanceUnitLabel(distanceUnit)

  return (
    <section className="atlas" style={{ '--atlas-template': atlasTemplate, '--atlas-template-m': atlasTemplateM } as React.CSSProperties}>
      {showAtlas && (
        <div className="atlas-card passport">
          <div className="label">{t('dashboard.atlas.countriesVisited')}</div>
          <div className="value mono">
            {statsPending ? <Skeleton width={44} height={30} radius={6} /> : countries.length}
            {' '}<span className="unit text-[oklch(1_0_0_/_.55)]">{t('dashboard.atlas.ofTotal', { total: 195 })}</span>
          </div>
          <div className="passport-flags">
            {countries.slice(0, 5).map((c, i) => (
              <span key={i} className="flag" title={c}>
                <img src={`https://flagcdn.com/w40/${c.toLowerCase()}.png`} alt={c} loading="lazy" />
              </span>
            ))}
            {countries.length > 5 && <span className="flag more">+{countries.length - 5}</span>}
          </div>
          <div className="delta" />
        </div>
      )}

      {showTrips && (
        <div className="atlas-card">
          <div className="label">{t('dashboard.atlas.tripsTotal')}</div>
          <div className="value mono">{statsPending ? <Skeleton width={44} height={30} radius={6} /> : stats.totalTrips ?? 0}</div>
          <div className="delta">{statsPending ? <span className="trek-skeleton delta-pending">{t('dashboard.atlas.placesMapped', { count: 0 })}</span> : t('dashboard.atlas.placesMapped', { count: stats.totalPlaces ?? 0 })}</div>
          <svg className="spark" width="80" height="36" viewBox="0 0 80 36">
            <polyline points="0,30 12,26 22,28 32,18 44,22 56,10 68,14 80,4" fill="none" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
          </svg>
        </div>
      )}

      {showDays && (
        <div className="atlas-card">
          <div className="label">{t('dashboard.atlas.daysTraveled')}</div>
          <div className="value mono">
            {statsPending ? <Skeleton width={44} height={30} radius={6} /> : stats.totalDays ?? 0}
            {' '}<span className="unit">{t('dashboard.atlas.daysUnit')}</span>
          </div>
          <div className="delta">{t('dashboard.atlas.acrossAllTrips')}</div>
          <svg className="spark" width="80" height="36" viewBox="0 0 80 36">
            <path d="M0 30 Q10 24 20 26 T40 20 T60 14 T80 10" fill="none" strokeWidth="2" strokeLinecap="round" />
          </svg>
        </div>
      )}

      {showDistance && (
        <div className="atlas-card">
          <div className="label">{t('dashboard.atlas.distanceFlown')}</div>
          <div className="value mono">
            {statsPending ? <Skeleton width={60} height={30} radius={6} /> : distanceText}
            {' '}<span className="unit">{distanceLabel}</span>
          </div>
          <div className="delta">{statsPending ? <span className="trek-skeleton delta-pending">{t('dashboard.atlas.aroundEquator', { count: '0.00' })}</span> : t('dashboard.atlas.aroundEquator', { count: equatorTimes })}</div>
          <svg className="spark" width="80" height="36" viewBox="0 0 80 36">
            <circle cx="40" cy="18" r="14" fill="none" stroke="oklch(0.88 0.01 70)" strokeWidth="2" />
            <circle cx="40" cy="18" r="14" fill="none" strokeWidth="2" strokeDasharray="58 88" strokeLinecap="round" transform="rotate(-90 40 18)" />
          </svg>
        </div>
      )}
    </section>
  )
}

// ── Trip card ────────────────────────────────────────────────────────────────
function TripCard({ trip, locale, badges, matchedPlaces, onOpen, onEdit, onCopy, onArchive, onDelete }: {
  trip: DashboardTrip; locale: string; badges?: TripCardBadge[]; onOpen: () => void
  /** The places that made this trip a search result (#2190). */
  matchedPlaces?: string[]
  onEdit: () => void; onCopy: () => void; onArchive: () => void; onDelete: () => void
}): React.ReactElement {
  const { t } = useTranslation()
  const status = getTripStatus(trip)
  const archiveLabel = trip.is_archived ? t('dashboard.restore') : t('dashboard.archive')
  const start = splitDate(trip.start_date, locale)
  const end = splitDate(trip.end_date, locale)
  const until = daysUntil(trip.start_date)

  const statusClass = status === 'ongoing' ? '' : status === 'past' ? 'completed' : status === 'future' || status === 'today' || status === 'tomorrow' ? 'upcoming' : 'idea'
  const statusLabel = status === 'ongoing' ? t('dashboard.mobile.liveNow')
    : status === 'today' ? t('dashboard.status.today')
    : status === 'tomorrow' ? t('dashboard.status.tomorrow')
    : status === 'future' && until !== null ? (until > 60 ? t('dashboard.mobile.inMonths', { count: Math.round(until / 30) }) : t('dashboard.mobile.inDays', { count: until }))
    : status === 'past' ? t('dashboard.mobile.completed')
    : t('dashboard.card.idea')

  const stop = (e: React.MouseEvent, fn: () => void) => { e.stopPropagation(); fn() }

  return (
    <div
      className="trip-card"
      onClick={onOpen}
      role="button"
      // Same press-scale opt-out as the hero card (#2158).
      data-no-press
      tabIndex={0}
      onKeyDown={(e) => { if (e.target === e.currentTarget && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); onOpen() } }}
    >
      <div className="trip-cover">
        {trip.cover_image
          ? <img src={trip.cover_image} alt={trip.title} />
          : <div style={{ width: '100%', height: '100%', background: tripGradient(trip.id) }} />}
        <div className={`trip-status ${statusClass}`}><span className="indicator" /> {statusLabel}</div>
        <div className="trip-actions">
          <button type="button" className="trip-action-btn" aria-label={t('common.edit')} title={t('common.edit')} onClick={(e) => stop(e, onEdit)}><Edit2 size={17} strokeWidth={2.2} /></button>
          <button type="button" className="trip-action-btn" aria-label={t('dashboard.aria.duplicate')} title={t('dashboard.aria.duplicate')} onClick={(e) => stop(e, onCopy)}><Copy size={17} strokeWidth={2.2} /></button>
          <button type="button" className="trip-action-btn" aria-label={archiveLabel} title={archiveLabel} onClick={(e) => stop(e, onArchive)}>{trip.is_archived ? <ArchiveRestore size={17} strokeWidth={2.2} /> : <Archive size={17} strokeWidth={2.2} />}</button>
          <button type="button" className="trip-action-btn" aria-label={t('common.delete')} title={t('common.delete')} onClick={(e) => stop(e, onDelete)}><Trash2 size={17} strokeWidth={2.2} /></button>
        </div>
        <div className="trip-cover-content">
          <h3 className="trip-name">{trip.title}</h3>
        </div>
      </div>
      <div className="trip-body">
        <div className="trip-dates">
          {start && end ? (
            <>
              <span className="date-num">{fullDate(trip.start_date, locale)}</span>
              <span className="date-arrow"><ArrowRight size={11} /></span>
              <span className="date-num">{fullDate(trip.end_date, locale)}</span>
            </>
          ) : <span>{t('dashboard.hero.noDates')}</span>}
        </div>
        {matchedPlaces && matchedPlaces.length > 0 && (
          <div className="trip-match" title={matchedPlaces.join(', ')}>
            <MapPin size={12} strokeWidth={2.2} aria-hidden />
            <span>{matchedPlaces.join(', ')}</span>
          </div>
        )}
        <div className="trip-meta" style={{ gridTemplateColumns: 'repeat(3, 1fr)' }}>
          <div><span className="n mono">{trip.day_count ?? 0}</span><span className="k">{t('dashboard.days', { count: trip.day_count ?? 0 })}</span></div>
          <div><span className="n mono">{trip.place_count ?? 0}</span><span className="k">{t('dashboard.places', { count: trip.place_count ?? 0 })}</span></div>
          <div><span className="n mono">{trip.shared_count ?? 0}</span><span className="k">{t('dashboard.card.buddies', { count: trip.shared_count ?? 0 })}</span></div>
        </div>
        <TripCardBadges items={badges ?? []} />
      </div>
    </div>
  )
}

// ── Currency tool (self-contained, mirrors the design's fx widget) ───────────
function CurrencyTool(): React.ReactElement {
  const { t } = useTranslation()
  const { from, to, setFrom, setTo, amount, setAmount, currencies, rate, converted, swap, fetchRates: fetchRate, pairPending, ratePending } = useCurrencyConverter()
  const ccyOptions = currencies.map(c => ({ value: c, label: c }))

  return (
    <div className="tool">
      <div className="tool-head">
        <div className="tool-title"><RefreshCw size={14} /> {t('dashboard.currency')}</div>
        <button type="button" className="tool-action" aria-label={t('dashboard.aria.refreshRates')} onClick={fetchRate}><RefreshCw size={14} /></button>
      </div>
      {/* Held invisible until the saved pair is known, so the defaults never flash first (#3009). */}
      <div className="fx-input" style={pairPending ? { visibility: 'hidden' } : undefined}>
        <div className="fx-field">
          <div className="lbl">{t('dashboard.fx.from')}</div>
          <input className="amt mono" value={amount} onChange={e => setAmount(e.target.value)} inputMode="decimal" />
          <CustomSelect value={from} onChange={v => setFrom(String(v))} options={ccyOptions} searchable size="sm" style={{ marginTop: 6 }} />
        </div>
        <button type="button" className="fx-swap" aria-label={t('dashboard.aria.swapCurrencies')} onClick={swap}><ArrowRightLeft size={14} /></button>
        <div className="fx-field">
          <div className="lbl">{t('dashboard.fx.to')}</div>
          <input className="amt mono" value={converted != null ? converted.toFixed(2) : '—'} readOnly />
          <CustomSelect value={to} onChange={v => setTo(String(v))} options={ccyOptions} searchable size="sm" style={{ marginTop: 6 }} />
        </div>
      </div>
      <div className="fx-rate">
        {pairPending || ratePending
          ? <span className="trek-skeleton" aria-hidden>{`1 ${from} = 0.0000 ${to}`}</span>
          : <span>{rate != null ? `1 ${from} = ${rate.toFixed(4)} ${to}` : t('dashboard.fx.unavailable')}</span>}
      </div>
    </div>
  )
}

// ── Timezone tool ────────────────────────────────────────────────────────────
function TimezoneTool({ locale }: { locale: string }): React.ReactElement {
  const { t } = useTranslation()
  const { zones, adding, setAdding, allZones, addZone, removeZone, timeIn, offsetLabel } = useDashboardTimezones(locale)

  const tzOptions = allZones
    .filter(z => !zones.includes(z))
    .map(z => ({ value: z, label: zoneOptionLabel(z), searchLabel: `${z} ${zoneOptionLabel(z)}` }))

  return (
    <div className="tool">
      <div className="tool-head">
        <div className="tool-title"><Clock size={14} /> {t('dashboard.timezone')}</div>
        <button type="button" className="tool-action" aria-label={t('dashboard.aria.addTimezone')} onClick={() => setAdding(a => !a)}>
          {adding ? <X size={14} /> : <Plus size={14} />}
        </button>
      </div>
      {adding && (
        <div style={{ marginBottom: 14 }}>
          <CustomSelect value="" onChange={addZone} options={tzOptions} searchable size="sm" placeholder={t('dashboard.tz.searchPlaceholder')} />
        </div>
      )}
      <div className="tz-list">
        {zones.map(tz => (
          <div className="tz-row" key={tz}>
            <div className="tz-dot">{shortZone(tz)[0]?.toUpperCase()}</div>
            <div>
              <div className="tz-city">{shortZone(tz)}</div>
              <div className="tz-sub">{offsetLabel(tz)}</div>
            </div>
            <div className="tz-time mono">{timeIn(tz)}</div>
            <button type="button" className="tz-del" aria-label={t('dashboard.aria.removeTimezone', { city: shortZone(tz) })} onClick={() => removeZone(tz)}><X size={13} /></button>
          </div>
        ))}
        {zones.length === 0 && (
          <div className="tz-empty">{t('dashboard.tz.empty')}</div>
        )}
      </div>
    </div>
  )
}

// ── Upcoming reservations tool ───────────────────────────────────────────────
function UpcomingTool({ items, locale, onOpen }: {
  items: UpcomingReservation[]; locale: string; onOpen: (tripId: number) => void
}): React.ReactElement {
  const { t } = useTranslation()
  const timeFormat = useSettingsStore(s => s.settings.time_format)
  return (
    <div className="tool">
      <div className="tool-head">
        <div className="tool-title"><Calendar size={14} /> {t('dashboard.upcoming.title')}</div>
      </div>
      {items.length === 0 ? (
        <div style={{ fontSize: 'calc(13px * var(--fs-scale-body, 1))', color: 'var(--ink-3)' }}>{t('dashboard.upcoming.empty')}</div>
      ) : (
        <div className="upc-list">
          {items.map(r => {
            // Read the date/time straight from the stored string parts. Going through
            // new Date(...).toISOString() reinterprets the naive local time as UTC and
            // can roll the displayed day forward/back in non-UTC timezones.
            const parsed = splitReservationDateTime(r.reservation_time)
            const datePart = parsed.date || r.day_date || null
            const dateStr = datePart ? splitDate(datePart, locale) : null
            const timeStr = parsed.time ? formatTime(parsed.time, locale, timeFormat) : null
            const typeClass = RES_TYPE_CLASS[r.type] || 'other'
            const moment = MOMENT_LABEL[r.type]
            const where = moment ? t(moment) : (r.location || r.place_name || r.trip_title)
            return (
              <div className="upc-item" key={upcomingKey(r)} onClick={() => onOpen(r.trip_id)}
                role="button" data-no-press tabIndex={0}
                onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); onOpen(r.trip_id) } }}>
                <div className="upc-date"><div className="d mono">{dateStr?.d ?? '–'}</div><div className="m">{dateStr?.m ?? ''}</div></div>
                <div className="upc-info">
                  <div className="t">
                    {r.title}
                  </div>
                  <div className="s">
                    {timeStr && <span className="upc-chip"><Clock size={11} />{timeStr}</span>}
                    {where && <span className="upc-chip"><span className="upc-chip-text">{where}</span></span>}
                  </div>
                </div>
                <div className={`upc-type ${typeClass}`}>
                  {RES_ICON[r.type] || <Ticket size={16} />}
                  {r.status === 'pending' && (
                    <span className="upc-pending" title={t('reservations.pending')} aria-label={t('reservations.pending')}>
                      {t('reservations.pending').charAt(0).toUpperCase()}
                    </span>
                  )}
                </div>
              </div>
            )
          })}
        </div>
      )}
    </div>
  )
}