Files
TREK/client/src/components/Map/usePlaceNameLayout.ts
T
Maurice 82165855c7 feat(collections): own order, KML/KMZ import, copy options, position search, place names on the trip map
- A list keeps its own order: drag a place by its grip (desktop and phone) or
  move it with the arrow keys. Reordering is off while a search or filter hides
  places and the list says why. New PUT /collections/:id/places/reorder and the
  MCP tool reorder_collection_places; new places go to the end. Client and
  server now order ties the same way (sort_order, oldest first, id).
- Copying saved places into a trip no longer takes their notes and tags along
  unasked. The picker offers both; a tag taken along becomes a trip tag (a trip
  tag of the same name is reused), never someone's personal tag.
- The source trip and place a saved place names are only kept when the user can
  open that trip and the place belongs to it.
- A KML or KMZ map can be read into a list: the dialog lists every placemark to
  keep or leave out and says which entries were skipped and why
  (POST /collections/map/read writes nothing; the import does).
- A saved place without coordinates says so and can be looked up on the map
  from its edit form, on the phone too, where a missing position could not be
  added at all. A typed address without a pick says it will not be on the map.
- The trip map can show the place names beside the pins (desktop and phone,
  both renderers). Names that would cover another pin or a control give way,
  the selected place and the planned stops first.
2026-11-29 14:57:00 +01:00

72 lines
2.7 KiB
TypeScript

import { useCallback, useEffect } from 'react';
import { useSettingsStore } from '../../store/settingsStore';
import { controlBoxes, layoutPlaceNames, PLACE_NAMES_CONTROLS } from './placeNameLabels';
/** Whether the trip map shows its place names, the reader's own setting (#2796). */
export function usePlaceNamesSetting(): boolean {
return useSettingsStore((s) => s.settings.map_place_labels) === true;
}
/**
* Keeps the place names of one map laid out: once after every change of the markers
* (a cluster opening, a day picked) and after every move or zoom the renderer reports
* through `listen`. Both renderers share it; only how they report a move differs.
*/
export function usePlaceNameLayout(
enabled: boolean,
/** The element the markers live in, or null while the map is not up yet. Keep it stable (useCallback). */
getContainer: () => HTMLElement | null,
/** Subscribes `run` to the renderer's move and zoom ends and returns the unsubscribe. Keep it stable too. */
listen: (run: () => void) => () => void
): void {
useEffect(() => {
const container = getContainer();
if (!container) return;
let frame = 0;
const run = () => {
cancelAnimationFrame(frame);
frame = requestAnimationFrame(() => {
const scope = container.parentElement ?? container;
const controls = enabled ? controlBoxes(scope, PLACE_NAMES_CONTROLS).filter((b) => b.right > b.left) : [];
layoutPlaceNames(container, enabled, controls);
});
};
run();
if (!enabled) return () => cancelAnimationFrame(frame);
const stop = listen(run);
// A marker added or taken away (a cluster splitting, a selection rebuilding the pins).
const observer = new MutationObserver(run);
observer.observe(container, { childList: true, subtree: true });
return () => {
cancelAnimationFrame(frame);
stop();
observer.disconnect();
};
}, [enabled, getContainer, listen]);
}
/** The part of either GL map the names need. */
interface NamesGlMap {
getContainer: () => HTMLElement;
on: (type: 'moveend', listener: () => void) => unknown;
off: (type: 'moveend', listener: () => void) => unknown;
}
/** The GL map's wiring: its container, and a layout after every move or zoom ends. */
export function useGlPlaceNames(map: NamesGlMap | null): void {
const enabled = usePlaceNamesSetting();
const getContainer = useCallback(() => map?.getContainer() ?? null, [map]);
const listen = useCallback(
(run: () => void) => {
if (!map) return () => {};
map.on('moveend', run);
return () => {
map.off('moveend', run);
};
},
[map]
);
usePlaceNameLayout(enabled, getContainer, listen);
}