2026-02-16 02:28:11 +09:00
|
|
|
import { useEffect, useMemo, useRef, type MutableRefObject } from 'react';
|
2026-02-16 02:11:37 +09:00
|
|
|
import maplibregl, { type LayerSpecification } from 'maplibre-gl';
|
|
|
|
|
import type { SubcableGeoJson } from '../../../entities/subcable/model/types';
|
|
|
|
|
import type { MapToggleState } from '../../../features/mapToggles/MapToggles';
|
|
|
|
|
import type { MapProjectionId } from '../types';
|
|
|
|
|
import { ensureGeoJsonSource, ensureLayer, setLayerVisibility, cleanupLayers } from '../lib/layerHelpers';
|
|
|
|
|
import { kickRepaint, onMapStyleReady } from '../lib/mapCore';
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
/* ── Layer / Source IDs ─────────────────────────────────────────────── */
|
2026-02-16 02:11:37 +09:00
|
|
|
const SRC_ID = 'subcables-src';
|
2026-02-16 02:28:11 +09:00
|
|
|
const POINTS_SRC_ID = 'subcables-pts-src';
|
|
|
|
|
|
|
|
|
|
const HITAREA_ID = 'subcables-hitarea';
|
|
|
|
|
const CASING_ID = 'subcables-casing';
|
2026-02-16 02:11:37 +09:00
|
|
|
const LINE_ID = 'subcables-line';
|
2026-02-16 02:28:11 +09:00
|
|
|
const GLOW_ID = 'subcables-glow';
|
|
|
|
|
const POINTS_ID = 'subcables-points';
|
2026-02-16 02:11:37 +09:00
|
|
|
const LABEL_ID = 'subcables-label';
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
const ALL_LAYER_IDS = [LABEL_ID, POINTS_ID, GLOW_ID, LINE_ID, CASING_ID, HITAREA_ID];
|
|
|
|
|
const ALL_SOURCE_IDS = [POINTS_SRC_ID, SRC_ID];
|
|
|
|
|
|
2026-02-16 02:11:37 +09:00
|
|
|
export function useSubcablesLayer(
|
|
|
|
|
mapRef: MutableRefObject<maplibregl.Map | null>,
|
|
|
|
|
projectionBusyRef: MutableRefObject<boolean>,
|
|
|
|
|
reorderGlobeFeatureLayers: () => void,
|
|
|
|
|
opts: {
|
|
|
|
|
subcableGeo: SubcableGeoJson | null;
|
|
|
|
|
overlays: MapToggleState;
|
|
|
|
|
projection: MapProjectionId;
|
|
|
|
|
mapSyncEpoch: number;
|
|
|
|
|
hoveredCableId: string | null;
|
|
|
|
|
onHoverCable: (cableId: string | null) => void;
|
|
|
|
|
onClickCable: (cableId: string | null) => void;
|
|
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
|
const { subcableGeo, overlays, projection, mapSyncEpoch, hoveredCableId, onHoverCable, onClickCable } = opts;
|
|
|
|
|
|
|
|
|
|
const onHoverRef = useRef(onHoverCable);
|
|
|
|
|
const onClickRef = useRef(onClickCable);
|
|
|
|
|
onHoverRef.current = onHoverCable;
|
|
|
|
|
onClickRef.current = onClickCable;
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
/* ── Derived point features (cable midpoints for circle markers) ── */
|
|
|
|
|
const pointsGeoJson = useMemo<GeoJSON.FeatureCollection>(() => {
|
|
|
|
|
if (!subcableGeo) return { type: 'FeatureCollection', features: [] };
|
|
|
|
|
const features: GeoJSON.Feature<GeoJSON.Point>[] = [];
|
|
|
|
|
for (const f of subcableGeo.features) {
|
|
|
|
|
const coords = f.properties.coordinates;
|
|
|
|
|
if (!coords || coords.length < 2) continue;
|
|
|
|
|
features.push({
|
|
|
|
|
type: 'Feature',
|
|
|
|
|
properties: { id: f.properties.id, color: f.properties.color },
|
|
|
|
|
geometry: { type: 'Point', coordinates: coords },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return { type: 'FeatureCollection', features };
|
|
|
|
|
}, [subcableGeo]);
|
|
|
|
|
|
|
|
|
|
/* ── Main layer setup effect ──────────────────────────────────────── */
|
2026-02-16 02:11:37 +09:00
|
|
|
useEffect(() => {
|
|
|
|
|
const map = mapRef.current;
|
|
|
|
|
if (!map) return;
|
|
|
|
|
|
|
|
|
|
const ensure = () => {
|
|
|
|
|
if (projectionBusyRef.current) return;
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
const visible = overlays.subcables;
|
|
|
|
|
for (const id of ALL_LAYER_IDS) {
|
|
|
|
|
setLayerVisibility(map, id, visible);
|
|
|
|
|
}
|
2026-02-16 02:11:37 +09:00
|
|
|
|
|
|
|
|
if (!subcableGeo) return;
|
|
|
|
|
if (!map.isStyleLoaded()) return;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ensureGeoJsonSource(map, SRC_ID, subcableGeo);
|
2026-02-16 02:28:11 +09:00
|
|
|
ensureGeoJsonSource(map, POINTS_SRC_ID, pointsGeoJson);
|
2026-02-16 02:11:37 +09:00
|
|
|
|
|
|
|
|
const before = map.getLayer('zones-fill')
|
|
|
|
|
? 'zones-fill'
|
|
|
|
|
: map.getLayer('deck-globe')
|
|
|
|
|
? 'deck-globe'
|
|
|
|
|
: undefined;
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
const vis = visible ? 'visible' : 'none';
|
|
|
|
|
|
|
|
|
|
/* 1) Hit-area — invisible wide line for easy hover detection */
|
|
|
|
|
ensureLayer(
|
|
|
|
|
map,
|
|
|
|
|
{
|
|
|
|
|
id: HITAREA_ID,
|
|
|
|
|
type: 'line',
|
|
|
|
|
source: SRC_ID,
|
|
|
|
|
paint: {
|
|
|
|
|
'line-color': 'rgba(0,0,0,0)',
|
|
|
|
|
'line-width': 14,
|
|
|
|
|
'line-opacity': 0,
|
|
|
|
|
},
|
|
|
|
|
layout: { visibility: vis, 'line-cap': 'round', 'line-join': 'round' },
|
|
|
|
|
} as unknown as LayerSpecification,
|
|
|
|
|
{ before },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/* 2) Dark casing behind cable for contrast */
|
|
|
|
|
ensureLayer(
|
|
|
|
|
map,
|
|
|
|
|
{
|
|
|
|
|
id: CASING_ID,
|
|
|
|
|
type: 'line',
|
|
|
|
|
source: SRC_ID,
|
|
|
|
|
paint: {
|
|
|
|
|
'line-color': 'rgba(0,0,0,0.55)',
|
|
|
|
|
'line-width': ['interpolate', ['linear'], ['zoom'], 2, 2.4, 6, 3.6, 10, 5.5],
|
|
|
|
|
'line-opacity': ['interpolate', ['linear'], ['zoom'], 2, 0.3, 5, 0.5, 8, 0.65],
|
|
|
|
|
},
|
|
|
|
|
layout: { visibility: vis, 'line-cap': 'round', 'line-join': 'round' },
|
|
|
|
|
} as unknown as LayerSpecification,
|
|
|
|
|
{ before },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/* 3) Main cable line — vivid color */
|
2026-02-16 02:11:37 +09:00
|
|
|
ensureLayer(
|
|
|
|
|
map,
|
|
|
|
|
{
|
|
|
|
|
id: LINE_ID,
|
|
|
|
|
type: 'line',
|
|
|
|
|
source: SRC_ID,
|
|
|
|
|
paint: {
|
|
|
|
|
'line-color': ['get', 'color'],
|
2026-02-16 02:28:11 +09:00
|
|
|
'line-opacity': ['interpolate', ['linear'], ['zoom'], 2, 0.7, 6, 0.82, 10, 0.92],
|
|
|
|
|
'line-width': ['interpolate', ['linear'], ['zoom'], 2, 1.6, 6, 2.5, 10, 4.0],
|
2026-02-16 02:11:37 +09:00
|
|
|
},
|
2026-02-16 02:28:11 +09:00
|
|
|
layout: { visibility: vis, 'line-cap': 'round', 'line-join': 'round' },
|
2026-02-16 02:11:37 +09:00
|
|
|
} as unknown as LayerSpecification,
|
|
|
|
|
{ before },
|
|
|
|
|
);
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
/* 4) Glow — visible only on hover */
|
2026-02-16 02:11:37 +09:00
|
|
|
ensureLayer(
|
|
|
|
|
map,
|
|
|
|
|
{
|
2026-02-16 02:28:11 +09:00
|
|
|
id: GLOW_ID,
|
2026-02-16 02:11:37 +09:00
|
|
|
type: 'line',
|
|
|
|
|
source: SRC_ID,
|
|
|
|
|
paint: {
|
|
|
|
|
'line-color': ['get', 'color'],
|
|
|
|
|
'line-opacity': 0,
|
2026-02-16 02:28:11 +09:00
|
|
|
'line-width': ['interpolate', ['linear'], ['zoom'], 2, 5, 6, 8, 10, 12],
|
|
|
|
|
'line-blur': ['interpolate', ['linear'], ['zoom'], 2, 3, 6, 5, 10, 7],
|
2026-02-16 02:11:37 +09:00
|
|
|
},
|
|
|
|
|
filter: ['==', ['get', 'id'], ''],
|
2026-02-16 02:28:11 +09:00
|
|
|
layout: { visibility: vis, 'line-cap': 'round', 'line-join': 'round' },
|
2026-02-16 02:11:37 +09:00
|
|
|
} as unknown as LayerSpecification,
|
|
|
|
|
{ before },
|
|
|
|
|
);
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
/* 5) Point markers at cable representative coordinates */
|
|
|
|
|
ensureLayer(
|
|
|
|
|
map,
|
|
|
|
|
{
|
|
|
|
|
id: POINTS_ID,
|
|
|
|
|
type: 'circle',
|
|
|
|
|
source: POINTS_SRC_ID,
|
|
|
|
|
paint: {
|
|
|
|
|
'circle-radius': ['interpolate', ['linear'], ['zoom'], 2, 1.5, 6, 2.5, 10, 4],
|
|
|
|
|
'circle-color': ['get', 'color'],
|
|
|
|
|
'circle-opacity': ['interpolate', ['linear'], ['zoom'], 2, 0.5, 5, 0.7, 8, 0.85],
|
|
|
|
|
'circle-stroke-color': 'rgba(0,0,0,0.5)',
|
|
|
|
|
'circle-stroke-width': 0.5,
|
|
|
|
|
},
|
|
|
|
|
layout: { visibility: vis },
|
|
|
|
|
minzoom: 3,
|
|
|
|
|
} as unknown as LayerSpecification,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/* 6) Cable name label along line */
|
2026-02-16 02:11:37 +09:00
|
|
|
ensureLayer(
|
|
|
|
|
map,
|
|
|
|
|
{
|
|
|
|
|
id: LABEL_ID,
|
|
|
|
|
type: 'symbol',
|
|
|
|
|
source: SRC_ID,
|
|
|
|
|
layout: {
|
2026-02-16 02:28:11 +09:00
|
|
|
visibility: vis,
|
2026-02-16 02:11:37 +09:00
|
|
|
'symbol-placement': 'line',
|
|
|
|
|
'text-field': ['get', 'name'],
|
|
|
|
|
'text-size': ['interpolate', ['linear'], ['zoom'], 4, 9, 8, 11, 12, 13],
|
|
|
|
|
'text-font': ['Noto Sans Regular', 'Open Sans Regular'],
|
|
|
|
|
'text-allow-overlap': false,
|
|
|
|
|
'text-padding': 8,
|
|
|
|
|
'text-rotation-alignment': 'map',
|
|
|
|
|
},
|
|
|
|
|
paint: {
|
2026-02-16 02:28:11 +09:00
|
|
|
'text-color': 'rgba(220,232,245,0.82)',
|
|
|
|
|
'text-halo-color': 'rgba(2,6,23,0.9)',
|
|
|
|
|
'text-halo-width': 1.2,
|
|
|
|
|
'text-halo-blur': 0.5,
|
|
|
|
|
'text-opacity': ['interpolate', ['linear'], ['zoom'], 4, 0, 5, 0.7, 8, 0.88],
|
2026-02-16 02:11:37 +09:00
|
|
|
},
|
|
|
|
|
minzoom: 4,
|
|
|
|
|
} as unknown as LayerSpecification,
|
|
|
|
|
);
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
/* ── Hover highlight (flat values — no nested interpolate) ── */
|
2026-02-16 02:11:37 +09:00
|
|
|
if (hoveredCableId) {
|
2026-02-16 02:28:11 +09:00
|
|
|
const matchExpr = ['==', ['get', 'id'], hoveredCableId];
|
|
|
|
|
|
|
|
|
|
// Main line: hovered=bright+thick, rest=dimmed+thin
|
2026-02-16 02:11:37 +09:00
|
|
|
if (map.getLayer(LINE_ID)) {
|
2026-02-16 02:28:11 +09:00
|
|
|
map.setPaintProperty(LINE_ID, 'line-opacity', ['case', matchExpr, 1.0, 0.2] as never);
|
|
|
|
|
map.setPaintProperty(LINE_ID, 'line-width', ['case', matchExpr, 4.5, 1.2] as never);
|
2026-02-16 02:11:37 +09:00
|
|
|
}
|
2026-02-16 02:28:11 +09:00
|
|
|
// Casing: dim non-hovered
|
|
|
|
|
if (map.getLayer(CASING_ID)) {
|
|
|
|
|
map.setPaintProperty(CASING_ID, 'line-opacity', ['case', matchExpr, 0.7, 0.12] as never);
|
|
|
|
|
map.setPaintProperty(CASING_ID, 'line-width', ['case', matchExpr, 6.5, 2.0] as never);
|
|
|
|
|
}
|
|
|
|
|
// Glow: show only on hovered cable
|
|
|
|
|
if (map.getLayer(GLOW_ID)) {
|
|
|
|
|
map.setFilter(GLOW_ID, matchExpr as never);
|
|
|
|
|
map.setPaintProperty(GLOW_ID, 'line-opacity', 0.35);
|
|
|
|
|
}
|
|
|
|
|
// Points: dim non-hovered
|
|
|
|
|
if (map.getLayer(POINTS_ID)) {
|
|
|
|
|
map.setPaintProperty(POINTS_ID, 'circle-opacity', ['case', matchExpr, 1.0, 0.15] as never);
|
|
|
|
|
map.setPaintProperty(POINTS_ID, 'circle-radius', ['case', matchExpr, 4, 1.5] as never);
|
2026-02-16 02:11:37 +09:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-02-16 02:28:11 +09:00
|
|
|
// Restore zoom-based interpolation defaults
|
2026-02-16 02:11:37 +09:00
|
|
|
if (map.getLayer(LINE_ID)) {
|
|
|
|
|
map.setPaintProperty(
|
2026-02-16 02:28:11 +09:00
|
|
|
LINE_ID, 'line-opacity',
|
|
|
|
|
['interpolate', ['linear'], ['zoom'], 2, 0.7, 6, 0.82, 10, 0.92] as never,
|
|
|
|
|
);
|
|
|
|
|
map.setPaintProperty(
|
|
|
|
|
LINE_ID, 'line-width',
|
|
|
|
|
['interpolate', ['linear'], ['zoom'], 2, 1.6, 6, 2.5, 10, 4.0] as never,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (map.getLayer(CASING_ID)) {
|
|
|
|
|
map.setPaintProperty(
|
|
|
|
|
CASING_ID, 'line-opacity',
|
|
|
|
|
['interpolate', ['linear'], ['zoom'], 2, 0.3, 5, 0.5, 8, 0.65] as never,
|
2026-02-16 02:11:37 +09:00
|
|
|
);
|
|
|
|
|
map.setPaintProperty(
|
2026-02-16 02:28:11 +09:00
|
|
|
CASING_ID, 'line-width',
|
|
|
|
|
['interpolate', ['linear'], ['zoom'], 2, 2.4, 6, 3.6, 10, 5.5] as never,
|
2026-02-16 02:11:37 +09:00
|
|
|
);
|
|
|
|
|
}
|
2026-02-16 02:28:11 +09:00
|
|
|
if (map.getLayer(GLOW_ID)) {
|
|
|
|
|
map.setFilter(GLOW_ID, ['==', ['get', 'id'], ''] as never);
|
|
|
|
|
map.setPaintProperty(GLOW_ID, 'line-opacity', 0);
|
|
|
|
|
}
|
|
|
|
|
if (map.getLayer(POINTS_ID)) {
|
|
|
|
|
map.setPaintProperty(
|
|
|
|
|
POINTS_ID, 'circle-opacity',
|
|
|
|
|
['interpolate', ['linear'], ['zoom'], 2, 0.5, 5, 0.7, 8, 0.85] as never,
|
|
|
|
|
);
|
|
|
|
|
map.setPaintProperty(
|
|
|
|
|
POINTS_ID, 'circle-radius',
|
|
|
|
|
['interpolate', ['linear'], ['zoom'], 2, 1.5, 6, 2.5, 10, 4] as never,
|
|
|
|
|
);
|
2026-02-16 02:11:37 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.warn('Subcables layer setup failed:', e);
|
|
|
|
|
} finally {
|
|
|
|
|
reorderGlobeFeatureLayers();
|
|
|
|
|
kickRepaint(map);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const stop = onMapStyleReady(map, ensure);
|
|
|
|
|
return () => {
|
|
|
|
|
stop();
|
|
|
|
|
};
|
2026-02-16 02:28:11 +09:00
|
|
|
}, [subcableGeo, pointsGeoJson, overlays.subcables, projection, mapSyncEpoch, hoveredCableId, reorderGlobeFeatureLayers]);
|
2026-02-16 02:11:37 +09:00
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
/* ── Mouse events (bind to hit-area layer for easy hovering) ───── */
|
2026-02-16 02:11:37 +09:00
|
|
|
useEffect(() => {
|
|
|
|
|
const map = mapRef.current;
|
|
|
|
|
if (!map) return;
|
|
|
|
|
if (!overlays.subcables) return;
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
const onMouseMove = (e: maplibregl.MapMouseEvent & { features?: maplibregl.MapGeoJSONFeature[] }) => {
|
2026-02-16 02:11:37 +09:00
|
|
|
const cableId = e.features?.[0]?.properties?.id;
|
|
|
|
|
if (typeof cableId === 'string' && cableId) {
|
|
|
|
|
map.getCanvas().style.cursor = 'pointer';
|
|
|
|
|
onHoverRef.current(cableId);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onMouseLeave = () => {
|
|
|
|
|
map.getCanvas().style.cursor = '';
|
|
|
|
|
onHoverRef.current(null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onClick = (e: maplibregl.MapMouseEvent & { features?: maplibregl.MapGeoJSONFeature[] }) => {
|
|
|
|
|
const cableId = e.features?.[0]?.properties?.id;
|
|
|
|
|
if (typeof cableId === 'string' && cableId) {
|
|
|
|
|
onClickRef.current(cableId);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const addEvents = () => {
|
2026-02-16 02:28:11 +09:00
|
|
|
// Bind to hit-area for wider hover target, fallback to main line
|
|
|
|
|
const targetLayer = map.getLayer(HITAREA_ID) ? HITAREA_ID : LINE_ID;
|
|
|
|
|
if (!map.getLayer(targetLayer)) return;
|
|
|
|
|
map.on('mousemove', targetLayer, onMouseMove);
|
|
|
|
|
map.on('mouseleave', targetLayer, onMouseLeave);
|
|
|
|
|
map.on('click', targetLayer, onClick);
|
2026-02-16 02:11:37 +09:00
|
|
|
};
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
if (map.isStyleLoaded() && (map.getLayer(HITAREA_ID) || map.getLayer(LINE_ID))) {
|
2026-02-16 02:11:37 +09:00
|
|
|
addEvents();
|
|
|
|
|
} else {
|
|
|
|
|
map.once('idle', addEvents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
try {
|
2026-02-16 02:28:11 +09:00
|
|
|
map.off('mousemove', HITAREA_ID, onMouseMove);
|
|
|
|
|
map.off('mouseleave', HITAREA_ID, onMouseLeave);
|
|
|
|
|
map.off('click', HITAREA_ID, onClick);
|
|
|
|
|
map.off('mousemove', LINE_ID, onMouseMove);
|
2026-02-16 02:11:37 +09:00
|
|
|
map.off('mouseleave', LINE_ID, onMouseLeave);
|
|
|
|
|
map.off('click', LINE_ID, onClick);
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, [overlays.subcables, mapSyncEpoch]);
|
|
|
|
|
|
2026-02-16 02:28:11 +09:00
|
|
|
/* ── Cleanup on unmount ───────────────────────────────────────────── */
|
2026-02-16 02:11:37 +09:00
|
|
|
useEffect(() => {
|
|
|
|
|
const mapInstance = mapRef.current;
|
|
|
|
|
return () => {
|
|
|
|
|
if (!mapInstance) return;
|
2026-02-16 02:28:11 +09:00
|
|
|
cleanupLayers(mapInstance, ALL_LAYER_IDS, ALL_SOURCE_IDS);
|
2026-02-16 02:11:37 +09:00
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
}
|