Fix world map country selection handling and preserve map during re-fetch

This commit is contained in:
2026-04-05 22:44:50 +02:00
parent 15d53a8e96
commit 6e2abe9d97
2 changed files with 35 additions and 15 deletions

View File

@@ -208,10 +208,16 @@ export function WorldMap({
[onSelectCountry, selectedCountry],
);
/** SVG-level click handler — paths never receive click when pointer capture
* is active on the SVG, so we resolve the target via the data-cc attribute. */
const handleSvgClick = useCallback((event: React.MouseEvent<SVGSVGElement>) => {
const target = (event.target as Element).closest('[data-cc]');
const cc = target?.getAttribute('data-cc') ?? null;
if (cc) handleCountrySelect(cc);
}, [handleCountrySelect]);
const handlePointerDown = useCallback((event: React.PointerEvent<SVGSVGElement>) => {
if (event.button !== 0) return;
event.currentTarget.setPointerCapture(event.pointerId);
dragStateRef.current = {
active: true,
startX: event.clientX,
@@ -231,6 +237,7 @@ export function WorldMap({
if (!drag.moved && Math.hypot(dx, dy) > PAN_THRESHOLD) {
drag.moved = true;
clickSuppressedRef.current = true;
event.currentTarget.setPointerCapture(event.pointerId);
}
setCenter([drag.startCenter[0] + dx, drag.startCenter[1] + dy]);
@@ -332,6 +339,7 @@ export function WorldMap({
onPointerUp={handlePointerUp}
onPointerLeave={handlePointerUp}
onWheel={handleWheel}
onClick={handleSvgClick}
>
<g transform={`translate(${center[0]} ${center[1]}) scale(${zoom})`}>
{countryFeatures.map((featureItem) => {
@@ -350,6 +358,7 @@ export function WorldMap({
<g key={String(rawId)}>
<path
d={pathString}
data-cc={cc ?? undefined}
role={cc ? "button" : undefined}
tabIndex={cc ? 0 : undefined}
aria-label={
@@ -373,9 +382,7 @@ export function WorldMap({
} as React.CSSProperties
}
onClick={(): void => {
if (cc) {
handleCountrySelect(cc);
}
if (cc) handleCountrySelect(cc);
}}
onKeyDown={(event): void => {
if (cc && (event.key === "Enter" || event.key === " ")) {