import React, { useEffect, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import { useTranslation } from '../i18n'
import Navbar from '../components/Layout/Navbar'
import CustomSelect from '../components/shared/CustomSelect'
import EmptyState from '../components/shared/EmptyState'
import { Globe, MapPin, Briefcase, Calendar, Flag, PanelLeftOpen, PanelLeftClose, X, Star, Plus, Trash2, Search, Check, ArrowLeft, ChevronRight, Footprints, type LucideIcon } from 'lucide-react'
import type { TranslationFn } from '../types'
import { Tooltip } from '../components/shared/Tooltip'
import { DialogButton, DialogFooter, DialogHeader, DialogShell, DialogTile, FooterSpacer, NEUTRAL_TINT, fs } from '../components/shared/DialogShell'
import { EditorField, GRID_2 } from '../components/shared/dialogParts'
import DawarichAtlasSidePanel from '../components/Dawarich/DawarichAtlasSidePanel'
import CountryFlag from '../components/shared/CountryFlag'
import { A2_TO_A3, countryCodeToFlag, visitedRegionCount, type AtlasCountry, type AtlasStats, type AtlasData, type CountryDetail } from './atlas/atlasModel'
import { useAtlas } from './atlas/useAtlas'
import { bucketMonthTarget, useAtlasCountryActions, type AtlasCountryActionsOptions } from './atlas/useAtlasCountryActions'
import AtlasCountrySearch from './atlas/AtlasCountrySearch'
import AtlasLayerToggle from './atlas/AtlasLayerToggle'
import HelpAnchor from '../components/Help/HelpAnchor'
import AtlasCountryPlaces from '../components/Atlas/AtlasCountryPlaces'
import { AtlasTourActivityPane } from '../components/Atlas/tourActivity/AtlasTourActivityPane'
import { useAddonStore } from '../store/addonStore'
import { getIntlLanguage } from '@trek/shared'
// Fixed ids rather than useId: the page body stays free of hooks, and there is only one atlas.
const COUNTRY_DIALOG_TITLE = 'atlas-country-dialog-title'
const BUCKET_MONTH_ID = 'atlas-bucket-month'
const BUCKET_YEAR_ID = 'atlas-bucket-year'
/** The footer's destructive answer, filled like ConfirmDialog's. */
const DANGER_BUTTON = 'inline-flex items-center gap-1.5 rounded-[10px] bg-danger px-4 py-2 font-medium text-white hover:opacity-90' // theme-lint-disable: white on the danger fill, as ConfirmDialog draws it
export default function AtlasPage(): 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 (
<>
>
)
}
function AtlasPageDesktop(): React.ReactElement {
// Page = wiring container: the whole interactive globe (map lifecycle, atlas +
// bucket data, mark/unmark flows, country search) lives in useAtlas. The page
// only wires that state into JSX and its presentational SidebarContent helper.
const {
t, language, navigate, resolveName, dark, loading,
mapRef, regionTooltipRef, panelRef, glareRef, borderGlareRef,
handlePanelMouseMove, handlePanelMouseLeave,
data, setData, stats, countries, selectedCountry, countryDetail,
showPlanned, togglePlanned,
loadCountryDetail, handleUnmarkCountry, select_country_from_search,
visitedRegions, setVisitedRegions, regionsVisited, placesOpen, setPlacesOpen,
atlas_country_search, set_atlas_country_search,
atlas_country_results, set_atlas_country_results,
atlas_country_open, set_atlas_country_open, atlas_country_options,
atlas_place_results, atlas_places_loading, search_places, select_place_from_search,
confirmAction, setConfirmAction, executeConfirmAction,
bucketMonth, setBucketMonth, bucketYear, setBucketYear,
bucketList, setBucketList, bucketTab, setBucketTab,
showBucketAdd, setShowBucketAdd, bucketForm, setBucketForm,
handleAddBucketItem, handleDeleteBucketItem, handleClearBucketVisit, handleBucketPoiSearch, handleSelectBucketPoi,
bucketSearchResults, setBucketSearchResults,
bucketPoiMonth, setBucketPoiMonth, bucketPoiYear, setBucketPoiYear,
bucketSearching, bucketSearch, setBucketSearch, reloadAfterDawarich,
} = useAtlas()
// Solid surfaces when the user disabled transparency (read at render — the
// attribute is already set by applyAppearance before navigating here).
const noTransparency = typeof document !== 'undefined' && document.documentElement.hasAttribute('data-no-transparency')
if (loading) {
return (
)
}
// The glass both bottom panels are cut from. One object, because two panels
// side by side that differ by a blur radius look like a mistake.
const glassPanel: React.CSSProperties = {
background: noTransparency ? (dark ? '#15151c' : '#ffffff') : (dark ? 'rgba(10,10,15,0.55)' : 'rgba(255,255,255,0.2)'),
backdropFilter: 'blur(24px) saturate(180%)',
WebkitBackdropFilter: 'blur(24px) saturate(180%)',
border: '1px solid ' + (dark ? 'rgba(255,255,255,0.08)' : 'rgba(0,0,0,0.06)'),
borderRadius: 20,
boxShadow: dark ? '0 8px 32px rgba(0,0,0,0.3)' : '0 8px 32px rgba(0,0,0,0.08)',
}
return (
{/* Desktop Panels — bottom center, glass effect.
Three columns so the Atlas panel stays exactly centred over the map
however wide its neighbour is: Dawarich sits in the left column,
right-aligned against the middle one, and stretches to its height. */}
{/* Country action popup: the flag and the name at the head, what can be
done with the country below. A region names its country under its own name. */}
{(() => {
const code = item.country_code?.length === 2 ? item.country_code : (Object.entries(A2_TO_A3).find(([, v]) => v === item.country_code)?.[0] || '')
return code ? (
) :
})()}
{item.name}
{item.target_date && (() => {
const [y, m] = item.target_date.split('-')
const label = m ? new Date(Number(y), Number(m) - 1).toLocaleString(getIntlLanguage(language), { month: 'short', year: 'numeric' }) : y
return {label}
})()}
{!item.target_date && item.notes && {item.notes}}
{/* Reached (#2279). A tick rather than removing the entry: a wish that
came true is the part of the list worth keeping, and the source is
named so a recording-derived tick is not mistaken for a manual one. */}
{item.visited_at && (
{/* A tick that came from a recording can be taken back here: the
wishlist is curated by hand, and a suggestion nobody can undo
is not a suggestion. A tick somebody set themselves is text. */}
{item.visited_source === 'dawarich' ? (
) : (
{new Date(item.visited_at).toLocaleDateString(getIntlLanguage(language))}
)}
)}
{/* Portalled to the body: the panel that hosts this form clips its
overflow, which used to swallow the top of the list (#1899). */}
{bucketSearchResults.length > 0 && createPortal(
{
const rect = bucketSearchRowRef.current?.getBoundingClientRect()
if (!rect) return { left: 0, top: 0, width: 240, maxHeight: 160 }
const above = rect.top - 12
const below = window.innerHeight - rect.bottom - 12
const openUp = above >= below
return {
left: rect.left,
width: rect.width,
// Never taller than the room on that side, so the list scrolls
// instead of running off the edge of the viewport.
maxHeight: Math.min(160, openUp ? above : below),
...(openUp ? { bottom: window.innerHeight - rect.top + 4 } : { top: rect.bottom + 4 }),
}
})(),
zIndex: 99999, borderRadius: 8, boxShadow: '0 4px 12px rgba(0,0,0,0.12)', overflowY: 'auto',
}}>
{bucketSearchResults.slice(0, 6).map((r, i) => (
))}
,
document.body
)}
{/* Selected place indicator */}
{bucketForm.lat && bucketForm.lng && (
{/* Antarctica only joins the row once someone has actually been — CONTINENT_MAP has
always known AQ, but a permanent zero column would be dead space for everyone else. */}
{['Europe', 'Asia', 'North America', 'South America', 'Africa', 'Oceania', ...((continents?.['Antarctica'] || 0) > 0 ? ['Antarctica'] : [])].map((cont) => {
const count = continents?.[cont] || 0
const active = count > 0
return (
{/* Twemoji where the platform has no flag glyphs: Windows draws the emoji as two letters. */}
{/^[A-Za-z]{2}$/.test(selectedCountry)
?
: {countryCodeToFlag(selectedCountry)}}