Skip to content

Support unlinked dive imports and quick creation - #291

Merged
kargig merged 2 commits into
mainfrom
feature/unlinked-dive-sites-quick-create
Oct 28, 2026
Merged

kargig merged 2 commits into
mainfrom
feature/unlinked-dive-sites-quick-create

Conversation

@kargig

@kargig kargig commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Allow dive logs (CSV, XML, JSON, FIT, DB) to import without blocking when dive sites are unknown in Divemap, persisting the raw site name as unlinked_site_name on the dive record.

Backend:

  • Add nullable unlinked_site_name column to dives (migration 0110).
  • Extract GPS coordinates from Subsurface XML into unmatched_dive_site.
  • Persist unlinked_site_name in confirm_import_dives when dive_site_id is null.
  • Implement server-side 3-tier geocoder in app/geo_utils.py (Geoapify autocomplete if GEOAPIFY_KEY is set -> Photon -> Nominatim).
  • Add /search-places and delegate /reverse-geocode to the 3-tier geocoder.

Frontend:

  • Implement satellite-first LocationPickerMap with 1km proximity duplicate detection and automatic reverse-geocoding of Country and Region.
  • Distinguish draggable user target pin (pulsing Blue) from nearby existing dive sites (Amber) with interactive Tooltips and Popups.
  • Separate free map exploration (panning and zooming) from pin placement, only re-centering the view when a pin is actually dropped.
  • Enable 1km proximity detection and suggestions on CreateDiveSite and EditDiveSite, replacing legacy duplicate banners.
  • Implement QuickCreateDiveSiteModal with batch linking and state injection.
  • Refactor ImportDivesModal to allow importing unlinked dives.
  • Integrate LocationPickerMap into CreateDiveSite and EditDiveSite.
  • Add Option 3A unlinked badge and LinkDiveSiteModal in logbook views.
  • Standardize frontend tests to co-located test files next to source.

Allow dive logs (CSV, XML, JSON, FIT, DB) to import without blocking
when dive sites are unknown in Divemap, persisting the raw site name
as unlinked_site_name on the dive record.

Backend:
- Add nullable unlinked_site_name column to dives (migration 0110).
- Extract GPS coordinates from Subsurface XML into unmatched_dive_site.
- Persist unlinked_site_name in confirm_import_dives when
  dive_site_id is null.
- Implement server-side 3-tier geocoder in app/geo_utils.py
  (Geoapify autocomplete if GEOAPIFY_KEY is set -> Photon ->
  Nominatim).
- Add /search-places and delegate /reverse-geocode to the 3-tier
  geocoder.

Frontend:
- Implement satellite-first LocationPickerMap with 1km proximity
  duplicate detection and automatic reverse-geocoding of Country and
  Region.
- Distinguish draggable user target pin (pulsing Blue) from nearby
  existing dive sites (Amber) with interactive Tooltips and Popups.
- Separate free map exploration (panning and zooming) from pin
  placement, only re-centering the view when a pin is actually dropped.
- Enable 1km proximity detection and suggestions on CreateDiveSite and
  EditDiveSite, replacing legacy duplicate banners.
- Implement QuickCreateDiveSiteModal with batch linking and state
  injection.
- Refactor ImportDivesModal to allow importing unlinked dives.
- Integrate LocationPickerMap into CreateDiveSite and EditDiveSite.
- Add Option 3A unlinked badge and LinkDiveSiteModal in logbook views.
- Standardize frontend tests to co-located test files next to source.
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Stale Closure

executeSearch is memoized with useCallback but calls handleSelectSearchResult, which is defined as a plain function after it and is not in the dependency array. Because handleSelectSearchResult is recreated every render, the executeSearch closure captures the first-render version. In the single-result auto-select path (items.length === 1), the captured handleSelectSearchResult still references the initial onChange/onLocationResolved props and initial state, so selecting a result may call a stale onChange or fail to update the parent correctly after props change.

const executeSearch = useCallback(
  async (rawQuery, isAuto = false) => {
    const query = (rawQuery || '').trim();
    if (!query) return;

    setIsSearching(true);
    try {
      const res = await api.get('/api/v1/dive-sites/search-places', {
        params: {
          q: query,
          latitude: hasValidCoords ? parsedLat : undefined,
          longitude: hasValidCoords ? parsedLng : undefined,
          limit: 5,
        },
      });

      const items = Array.isArray(res.data)
        ? res.data.map(d => ({
            display_name: d.display_name,
            name: d.name,
            lat: d.latitude,
            lon: d.longitude,
            country: d.country,
            region: d.region,
            type: d.type,
          }))
        : [];

      if (items.length === 0) {
        setSearchResults([]);
        setShowSearchDropdown(false);
        if (!isAuto) {
          toast.error(`No locations found for "${query}".`);
        }
      } else if (items.length === 1) {
        setSearchResults(items);
        handleSelectSearchResult(items[0]);
        if (isAuto) {
          toast.success(
            `Found location for "${items[0].name}" from map search. Adjust pin if needed.`
          );
        } else {
          toast.success(`Found and centered on: ${items[0].name}`);
        }
      } else {
        setSearchResults(items);
        if (!isAuto) {
          setShowSearchDropdown(true);
        }
      }
    } catch (err) {
      console.error('Place search failed:', err);
      setSearchResults([]);
      if (!isAuto) {
        toast.error('Place search failed. Please try again or click the map directly.');
      }
    } finally {
      setIsSearching(false);
    }
  },
  [hasValidCoords, parsedLat, parsedLng]
);
Missing PropType

onLocationResolved is used in the component (reverse-geocode effect and search result handler) but is not declared in LocationPickerMap.propTypes. This is a prop-validation gap; it also means the prop is undocumented for consumers. Add onLocationResolved: PropTypes.func to the propTypes block.

LocationPickerMap.propTypes = {
  latitude: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  longitude: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  onChange: PropTypes.func.isRequired,
  height: PropTypes.string,
  country: PropTypes.string,
  region: PropTypes.string,
  initialSearchQuery: PropTypes.string,
  autoSearchOnMount: PropTypes.bool,
  onSelectNearbySite: PropTypes.func,
  showSearch: PropTypes.bool,
  showNearbyAlert: PropTypes.bool,
  className: PropTypes.string,
};
Blocking I/O

perform_reverse_geocode and search_places perform synchronous requests.get calls (up to 5-6s each, and up to 3 sequential tiers) and are invoked directly from async def endpoints (reverse_geocode, search_places_endpoint). This blocks the event loop for the full duration of the outbound HTTP calls, degrading throughput under concurrent requests. Consider running these in a threadpool (e.g. run_in_threadpool) or making the calls async.

def perform_reverse_geocode(latitude: float, longitude: float) -> dict[str, str | None]:
    """
    3-tier reverse geocoder resolving country, region, and city from coordinates:
    Tier 1: Geoapify (if GEOAPIFY_KEY is set in environment)
    Tier 2: Photon (Komoot)
    Tier 3: OpenStreetMap Nominatim
    """
    result = {
        "country": None,
        "region": None,
        "county": None,
        "state": None,
        "city": None,
        "suburb": None,
    }

    # 1. Tier 1: Geoapify
    geoapify_key = os.getenv("GEOAPIFY_KEY")
    if geoapify_key:
        try:
            url = "https://api.geoapify.com/v1/geocode/reverse"
            params = {
                "lat": latitude,
                "lon": longitude,
                "apiKey": geoapify_key,
                "lang": "en",
            }
            resp = requests.get(url, params=params, timeout=5.0)
            if resp.status_code == 200:
                data = resp.json()
                features = data.get("features", [])
                if features:
                    props = features[0].get("properties", {})
                    country = props.get("country")
                    state = clean_regional_unit(props.get("state"))
                    county = clean_regional_unit(props.get("county"))
                    city = (
                        props.get("city")
                        or props.get("municipality")
                        or props.get("town")
                    )

                    region = state or county or props.get("region")

                    result.update(
                        {
                            "country": country,
                            "region": region,
                            "county": county,
                            "state": state,
                            "city": city,
                            "suburb": props.get("suburb"),
                        }
                    )
                    if result["country"]:
                        logger.info(
                            f"Geoapify reverse geocoded ({latitude}, {longitude}): {result}"
                        )
                        return result
        except Exception as e:
            logger.warning(f"Geoapify reverse geocoding failed: {e}")

    # 2. Tier 2: Photon (Komoot)
    try:
        url = "https://photon.komoot.io/reverse"
        params = {"lat": latitude, "lon": longitude, "lang": "en"}
        headers = {"User-Agent": "Divemap-Backend/1.0 (info@divemap.com)"}
        resp = requests.get(url, params=params, headers=headers, timeout=5.0)
        if resp.status_code == 200:
            data = resp.json()
            features = data.get("features", [])
            if features:
                props = features[0].get("properties", {})
                country = props.get("country")
                state = clean_regional_unit(props.get("state"))
                county = clean_regional_unit(props.get("county"))
                city = (
                    props.get("city") or props.get("district") or props.get("locality")
                )

                region = state or county or props.get("region")

                result.update(
                    {
                        "country": country,
                        "region": region,
                        "county": county,
                        "state": state,
                        "city": city,
                    }
                )
                if result["country"]:
                    logger.info(
                        f"Photon reverse geocoded ({latitude}, {longitude}): {result}"
                    )
                    return result
    except Exception as e:
        logger.warning(f"Photon reverse geocoding failed: {e}")

    # 3. Tier 3: Nominatim (OpenStreetMap)
    try:
        url = "https://nominatim.openstreetmap.org/reverse"
        params = {
            "lat": latitude,
            "lon": longitude,
            "format": "json",
            "addressdetails": 1,
            "zoom": 8,
            "accept-language": "en",
        }
        headers = {"User-Agent": "Divemap-Backend/1.0 (info@divemap.com)"}
        resp = requests.get(url, params=params, headers=headers, timeout=6.0)
        if resp.status_code == 200:
            data = resp.json()
            address = data.get("address", {})

            country = address.get("country")
            county = clean_regional_unit(address.get("county"))
            state_district = clean_regional_unit(address.get("state_district"))
            state = clean_regional_unit(address.get("state"))

            if county and state_district:
                region = f"{county}, {state_district}"
            else:
                region = (
                    state
                    or clean_regional_unit(address.get("province"))
                    or clean_regional_unit(address.get("region"))
                    or county
                )

            city = (
                address.get("city")
                or address.get("town")
                or address.get("village")
                or address.get("municipality")
            )

            result.update(
                {
                    "country": country,
                    "region": region,
                    "county": county,
                    "state": state,
                    "city": city,
                    "suburb": address.get("suburb"),
                }
            )
            if result["country"]:
                logger.info(
                    f"Nominatim reverse geocoded ({latitude}, {longitude}): {result}"
                )
                return result
    except Exception as e:
        logger.error(f"Nominatim reverse geocoding failed: {e}")

    return result


def search_places(
    query: str,
    lat: float | None = None,
    lon: float | None = None,
    limit: int = 5,
) -> list[dict]:
    """
    3-tier forward place search:
    Tier 1: Geoapify Autocomplete (if GEOAPIFY_KEY is set in environment)
    Tier 2: Photon (Komoot)
    Tier 3: OpenStreetMap Nominatim
    """
    clean_q = (query or "").strip()
    if not clean_q:
        return []

    # 1. Tier 1: Geoapify Autocomplete
    geoapify_key = os.getenv("GEOAPIFY_KEY")
    if geoapify_key:
        try:
            url = "https://api.geoapify.com/v1/geocode/autocomplete"
            params = {
                "text": clean_q,
                "apiKey": geoapify_key,
                "limit": limit,
            }
            if lat is not None and lon is not None:
                params["bias"] = f"proximity:{lon},{lat}"

            resp = requests.get(url, params=params, timeout=5.0)
            if resp.status_code == 200:
                data = resp.json()
                features = data.get("features", [])
                if features:
                    results = []
                    for f in features:
                        p = f.get("properties", {})
                        coords = f.get("geometry", {}).get("coordinates", [0, 0])
                        results.append(
                            {
                                "display_name": p.get("formatted")
                                or p.get("name")
                                or clean_q,
                                "name": p.get("name") or p.get("city") or clean_q,
                                "latitude": coords[1],
                                "longitude": coords[0],
                                "country": p.get("country"),
                                "region": clean_regional_unit(
                                    p.get("state") or p.get("county")
                                ),
                                "type": p.get("result_type"),
                            }
                        )
                    logger.info(
                        f"Geoapify place search for '{clean_q}' found {len(results)} items"
                    )
                    return results
        except Exception as e:
            logger.warning(f"Geoapify search failed: {e}")

    # 2. Tier 2: Photon (Komoot)
    try:
        url = "https://photon.komoot.io/api/"
        params = {"q": clean_q, "limit": limit, "lang": "en"}
        if lat is not None and lon is not None:
            params["lat"] = lat
            params["lon"] = lon

        headers = {"User-Agent": "Divemap-Backend/1.0 (info@divemap.com)"}
        resp = requests.get(url, params=params, headers=headers, timeout=5.0)
        if resp.status_code == 200:
            data = resp.json()
            features = data.get("features", [])
            if features:
                results = []
                for f in features:
                    p = f.get("properties", {})
                    coords = f.get("geometry", {}).get("coordinates", [0, 0])
                    parts = [
                        p.get("name"),
                        p.get("city"),
                        p.get("state"),
                        p.get("country"),
                    ]
                    display_name = ", ".join(filter(None, parts))
                    results.append(
                        {
                            "display_name": display_name,
                            "name": p.get("name") or clean_q,
                            "latitude": coords[1],
                            "longitude": coords[0],
                            "country": p.get("country"),
                            "region": clean_regional_unit(
                                p.get("state") or p.get("county")
                            ),
                            "type": p.get("type") or p.get("osm_value"),
                        }
                    )
                logger.info(
                    f"Photon place search for '{clean_q}' found {len(results)} items"
                )
                return results
    except Exception as e:
        logger.warning(f"Photon search failed: {e}")

    # 3. Tier 3: Nominatim (OpenStreetMap)
    try:
        url = "https://nominatim.openstreetmap.org/search"
        params = {
            "q": clean_q,
            "format": "json",
            "limit": limit,
            "addressdetails": 1,
            "accept-language": "en",
        }
        headers = {"User-Agent": "Divemap-Backend/1.0 (info@divemap.com)"}
        resp = requests.get(url, params=params, headers=headers, timeout=6.0)
        if resp.status_code == 200:
            data = resp.json()
            if isinstance(data, list) and data:
                results = []
                for d in data:
                    addr = d.get("address", {})
                    results.append(
                        {
                            "display_name": d.get("display_name"),
                            "name": d.get("display_name", "").split(",")[0],
                            "latitude": float(d["lat"]),
                            "longitude": float(d["lon"]),
                            "country": addr.get("country"),
                            "region": clean_regional_unit(
                                addr.get("state") or addr.get("county")
                            ),
                            "type": d.get("addresstype") or d.get("type"),
                        }
                    )
                logger.info(
                    f"Nominatim place search for '{clean_q}' found {len(results)} items"
                )
                return results
    except Exception as e:
        logger.error(f"Nominatim search failed: {e}")

    return []

⚠️ Review coverage: The following files were not included in this review because of the token budget:

  • frontend/src/components/ImportDivesModal.test.jsx
  • frontend/src/components/LinkDiveSiteModal.jsx
  • frontend/src/components/LocationPickerMap.test.jsx
  • frontend/src/pages/DiveDetail.jsx
  • frontend/src/components/QuickCreateDiveSiteModal.test.jsx
  • frontend/src/pages/Dives.jsx
  • frontend/src/pages/CreateDiveSiteMapPicker.test.jsx
  • frontend/src/pages/DivesUnlinkedSite.test.jsx
  • frontend/src/pages/EditDiveSite.jsx
  • frontend/src/components/DiveSiteSpecsCard.test.jsx
  • frontend/src/components/DiveSiteMediaPreview.test.jsx

Comment thread backend/app/geo_utils.py Fixed
Comment thread backend/app/geo_utils.py Fixed
Comment thread backend/app/geo_utils.py Fixed
- Change User-Agent email across geo_utils requests.
- Set Geoapify and Photon HTTP timeouts to at most 2.0s everywhere.
- Handle Timeout and ConnectionError in 3-tier geocoder, propagating
  HTTP 503 on service timeouts as expected by API tests.
- Remove clear-text logging of GPS coordinates and locations in
  geo_utils to resolve CodeQL sensitive information alerts.
- Memoize handleSelectSearchResult with useCallback before
  executeSearch in LocationPickerMap to eliminate stale closure.
@kargig
kargig merged commit 46f8e34 into main Oct 28, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants