Part A — 오버레이 라인 가시성: - Globe/Mercator 쌍끌이·FC·선단 라인 굵기 ~2x 증가 - Globe 범위 원·선단 원 대시 패턴 추가 - Globe 오버레이 호버 시 브리딩(breathing) 맥동 rAF Part B — 경고 선박 강조: - DashboardPage → Map3D alarmMmsiMap 전달 - Globe: 경고 맥동 레이어(ships-globe-alarm-pulse) + 배지(ships-globe-alarm-badge) - Mercator: Deck.gl ScatterplotLayer 맥동 + TextLayer 배지 - 경고 종류별 배지(이/수/환/휴/A), z-index 상향, 호버 스케일 확대 - 경고 필터 OFF 시 맥동/배지 자동 비표시 Part C — Mercator 선명 라벨: - MapLibre 라벨 → Deck.gl TextLayer 교체 (z-order 안정) Part D — 프로젝션 전환 안정화: - Globe→Mercator 전환 시 Globe custom layer 맵에서 분리 - alarm rAF에 projectionBusyRef 가드 추가 - overlay.setProps() stale WebGL 자원 참조 방지 Part E — 김개발(DEV) 모드 더미 데이터: - mockOverlayData.ts: 서해 12척 가상 선박 (5종 경고 시나리오) - 김개발 로그인 시 자동 주입, 일반 계정 미노출 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
419 lines
15 KiB
TypeScript
419 lines
15 KiB
TypeScript
import { useCallback, useEffect, useRef, type MutableRefObject } from 'react';
|
|
import type maplibregl from 'maplibre-gl';
|
|
import type { GeoJSONSource, GeoJSONSourceSpecification, LayerSpecification } from 'maplibre-gl';
|
|
import type { FcLink, FleetCircle } from '../../../features/legacyDashboard/model/types';
|
|
import type { MapToggleState } from '../../../features/mapToggles/MapToggles';
|
|
import type { DashSeg, MapProjectionId } from '../types';
|
|
import {
|
|
FC_LINE_NORMAL_ML, FC_LINE_SUSPICIOUS_ML,
|
|
FC_LINE_NORMAL_ML_HL, FC_LINE_SUSPICIOUS_ML_HL,
|
|
FLEET_FILL_ML_HL,
|
|
FLEET_LINE_ML, FLEET_LINE_ML_HL,
|
|
} from '../constants';
|
|
import { makeUniqueSorted } from '../lib/setUtils';
|
|
import {
|
|
makeFcSegmentFeatureId,
|
|
makeFleetCircleFeatureId,
|
|
} from '../lib/featureIds';
|
|
import {
|
|
makeMmsiAnyEndpointExpr,
|
|
makeFleetOwnerMatchExpr,
|
|
makeFleetMemberMatchExpr,
|
|
} from '../lib/mlExpressions';
|
|
import { kickRepaint, onMapStyleReady } from '../lib/mapCore';
|
|
import { circleRingLngLat } from '../lib/geometry';
|
|
import { guardedSetVisibility } from '../lib/layerHelpers';
|
|
import { dashifyLine } from '../lib/dashifyLine';
|
|
|
|
// ── Overlay line width constants ──
|
|
const FC_LINE_W_NORMAL = 2.2;
|
|
const FC_LINE_W_HL = 3.2;
|
|
const FLEET_LINE_W_NORMAL = 2.0;
|
|
const FLEET_LINE_W_HL = 3.0;
|
|
|
|
// ── Breathing animation constants ──
|
|
const BREATHE_AMP = 2.0;
|
|
const BREATHE_PERIOD_MS = 1200;
|
|
|
|
/** Globe FC lines + fleet circles 오버레이 */
|
|
export function useGlobeFcFleetOverlay(
|
|
mapRef: MutableRefObject<maplibregl.Map | null>,
|
|
projectionBusyRef: MutableRefObject<boolean>,
|
|
reorderGlobeFeatureLayers: () => void,
|
|
opts: {
|
|
overlays: MapToggleState;
|
|
fcLinks: FcLink[] | undefined;
|
|
fleetCircles: FleetCircle[] | undefined;
|
|
projection: MapProjectionId;
|
|
mapSyncEpoch: number;
|
|
hoveredFleetMmsiList: number[];
|
|
hoveredFleetOwnerKeyList: string[];
|
|
hoveredPairMmsiList: number[];
|
|
},
|
|
) {
|
|
const {
|
|
overlays, fcLinks, fleetCircles, projection, mapSyncEpoch,
|
|
hoveredFleetMmsiList, hoveredFleetOwnerKeyList, hoveredPairMmsiList,
|
|
} = opts;
|
|
const breatheRafRef = useRef<number>(0);
|
|
|
|
// FC lines
|
|
useEffect(() => {
|
|
const map = mapRef.current;
|
|
if (!map) return;
|
|
|
|
const srcId = 'fc-lines-ml-src';
|
|
const layerId = 'fc-lines-ml';
|
|
|
|
const remove = () => {
|
|
guardedSetVisibility(map, layerId, 'none');
|
|
};
|
|
|
|
const ensure = () => {
|
|
if (projectionBusyRef.current) return;
|
|
if (!map.isStyleLoaded()) return;
|
|
const fleetAwarePairMmsiList = makeUniqueSorted([...hoveredPairMmsiList, ...hoveredFleetMmsiList]);
|
|
const fcHoverActive = fleetAwarePairMmsiList.length > 0;
|
|
if (projection !== 'globe' || (!overlays.fcLines && !fcHoverActive)) {
|
|
remove();
|
|
return;
|
|
}
|
|
|
|
const segs: DashSeg[] = [];
|
|
for (const l of fcLinks || []) {
|
|
segs.push(...dashifyLine(l.from, l.to, l.suspicious, l.distanceNm, l.fcMmsi, l.otherMmsi));
|
|
}
|
|
if (segs.length === 0) {
|
|
remove();
|
|
return;
|
|
}
|
|
|
|
const fc: GeoJSON.FeatureCollection<GeoJSON.LineString> = {
|
|
type: 'FeatureCollection',
|
|
features: segs.map((s, idx) => ({
|
|
type: 'Feature',
|
|
id: makeFcSegmentFeatureId(s.fromMmsi ?? -1, s.toMmsi ?? -1, idx),
|
|
geometry: { type: 'LineString', coordinates: [s.from, s.to] },
|
|
properties: {
|
|
type: 'fc',
|
|
suspicious: s.suspicious,
|
|
distanceNm: s.distanceNm,
|
|
fcMmsi: s.fromMmsi ?? -1,
|
|
otherMmsi: s.toMmsi ?? -1,
|
|
},
|
|
})),
|
|
};
|
|
|
|
try {
|
|
const existing = map.getSource(srcId) as GeoJSONSource | undefined;
|
|
if (existing) existing.setData(fc);
|
|
else map.addSource(srcId, { type: 'geojson', data: fc } as GeoJSONSourceSpecification);
|
|
} catch (e) {
|
|
console.warn('FC lines source setup failed:', e);
|
|
return;
|
|
}
|
|
|
|
if (!map.getLayer(layerId)) {
|
|
try {
|
|
map.addLayer(
|
|
{
|
|
id: layerId,
|
|
type: 'line',
|
|
source: srcId,
|
|
layout: { 'line-cap': 'round', 'line-join': 'round', visibility: 'visible' },
|
|
paint: {
|
|
'line-color': [
|
|
'case',
|
|
['==', ['get', 'highlighted'], 1],
|
|
['case', ['boolean', ['get', 'suspicious'], false], FC_LINE_SUSPICIOUS_ML_HL, FC_LINE_NORMAL_ML_HL],
|
|
['boolean', ['get', 'suspicious'], false],
|
|
FC_LINE_SUSPICIOUS_ML,
|
|
FC_LINE_NORMAL_ML,
|
|
] as never,
|
|
'line-width': ['case', ['==', ['get', 'highlighted'], 1], FC_LINE_W_HL, FC_LINE_W_NORMAL] as never,
|
|
'line-opacity': 0.9,
|
|
},
|
|
} as unknown as LayerSpecification,
|
|
undefined,
|
|
);
|
|
} catch (e) {
|
|
console.warn('FC lines layer add failed:', e);
|
|
}
|
|
} else {
|
|
guardedSetVisibility(map, layerId, 'visible');
|
|
}
|
|
|
|
reorderGlobeFeatureLayers();
|
|
kickRepaint(map);
|
|
};
|
|
|
|
const stop = onMapStyleReady(map, ensure);
|
|
ensure();
|
|
return () => {
|
|
stop();
|
|
};
|
|
}, [
|
|
projection,
|
|
overlays.fcLines,
|
|
fcLinks,
|
|
hoveredPairMmsiList,
|
|
hoveredFleetMmsiList,
|
|
mapSyncEpoch,
|
|
reorderGlobeFeatureLayers,
|
|
]);
|
|
|
|
// Fleet circles
|
|
useEffect(() => {
|
|
const map = mapRef.current;
|
|
if (!map) return;
|
|
|
|
const srcId = 'fleet-circles-ml-src';
|
|
const fillSrcId = 'fleet-circles-ml-fill-src';
|
|
const layerId = 'fleet-circles-ml';
|
|
const fillLayerId = 'fleet-circles-ml-fill';
|
|
|
|
const remove = () => {
|
|
guardedSetVisibility(map, layerId, 'none');
|
|
guardedSetVisibility(map, fillLayerId, 'none');
|
|
};
|
|
|
|
const ensure = () => {
|
|
if (projectionBusyRef.current) return;
|
|
if (!map.isStyleLoaded()) return;
|
|
const fleetHoverActive = hoveredFleetOwnerKeyList.length > 0 || hoveredFleetMmsiList.length > 0;
|
|
if (projection !== 'globe' || (!overlays.fleetCircles && !fleetHoverActive) || (fleetCircles?.length ?? 0) === 0) {
|
|
remove();
|
|
return;
|
|
}
|
|
|
|
const circles = fleetCircles || [];
|
|
const isHighlightedFleet = (ownerKey: string, vesselMmsis: number[]) =>
|
|
hoveredFleetOwnerKeyList.includes(ownerKey) ||
|
|
(hoveredFleetMmsiList.length > 0 && vesselMmsis.some((mmsi) => hoveredFleetMmsiList.includes(mmsi)));
|
|
|
|
const fcLine: GeoJSON.FeatureCollection<GeoJSON.LineString> = {
|
|
type: 'FeatureCollection',
|
|
features: circles.map((c) => {
|
|
const ring = circleRingLngLat(c.center, c.radiusNm * 1852);
|
|
return {
|
|
type: 'Feature',
|
|
id: makeFleetCircleFeatureId(c.ownerKey),
|
|
geometry: { type: 'LineString', coordinates: ring },
|
|
properties: {
|
|
type: 'fleet',
|
|
ownerKey: c.ownerKey,
|
|
ownerLabel: c.ownerLabel,
|
|
count: c.count,
|
|
vesselMmsis: c.vesselMmsis,
|
|
highlighted: 0,
|
|
},
|
|
};
|
|
}),
|
|
};
|
|
|
|
const fcFill: GeoJSON.FeatureCollection<GeoJSON.Polygon> = {
|
|
type: 'FeatureCollection',
|
|
features: circles
|
|
.filter((c) => isHighlightedFleet(c.ownerKey, c.vesselMmsis))
|
|
.map((c) => ({
|
|
type: 'Feature',
|
|
id: makeFleetCircleFeatureId(`${c.ownerKey}-fill`),
|
|
geometry: {
|
|
type: 'Polygon',
|
|
coordinates: [circleRingLngLat(c.center, c.radiusNm * 1852, 24)],
|
|
},
|
|
properties: {
|
|
ownerKey: c.ownerKey,
|
|
},
|
|
})),
|
|
};
|
|
|
|
try {
|
|
const existing = map.getSource(srcId) as GeoJSONSource | undefined;
|
|
if (existing) existing.setData(fcLine);
|
|
else map.addSource(srcId, { type: 'geojson', data: fcLine } as GeoJSONSourceSpecification);
|
|
} catch (e) {
|
|
console.warn('Fleet circles source setup failed:', e);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const existingFill = map.getSource(fillSrcId) as GeoJSONSource | undefined;
|
|
if (existingFill) existingFill.setData(fcFill);
|
|
else map.addSource(fillSrcId, { type: 'geojson', data: fcFill } as GeoJSONSourceSpecification);
|
|
} catch (e) {
|
|
console.warn('Fleet circles fill source setup failed:', e);
|
|
}
|
|
|
|
if (!map.getLayer(layerId)) {
|
|
try {
|
|
map.addLayer(
|
|
{
|
|
id: layerId,
|
|
type: 'line',
|
|
source: srcId,
|
|
layout: { 'line-cap': 'round', 'line-join': 'round', visibility: 'visible' },
|
|
paint: {
|
|
'line-color': ['case', ['==', ['get', 'highlighted'], 1], FLEET_LINE_ML_HL, FLEET_LINE_ML] as never,
|
|
'line-width': ['case', ['==', ['get', 'highlighted'], 1], FLEET_LINE_W_HL, FLEET_LINE_W_NORMAL] as never,
|
|
'line-opacity': 0.85,
|
|
},
|
|
} as unknown as LayerSpecification,
|
|
undefined,
|
|
);
|
|
} catch (e) {
|
|
console.warn('Fleet circles layer add failed:', e);
|
|
}
|
|
} else {
|
|
guardedSetVisibility(map, layerId, 'visible');
|
|
}
|
|
|
|
if (!map.getLayer(fillLayerId)) {
|
|
try {
|
|
map.addLayer(
|
|
{
|
|
id: fillLayerId,
|
|
type: 'fill',
|
|
source: fillSrcId,
|
|
layout: { visibility: fcFill.features.length > 0 ? 'visible' : 'none' },
|
|
paint: {
|
|
'fill-color': FLEET_FILL_ML_HL,
|
|
},
|
|
} as unknown as LayerSpecification,
|
|
undefined,
|
|
);
|
|
} catch (e) {
|
|
console.warn('Fleet circles fill layer add failed:', e);
|
|
}
|
|
} else {
|
|
guardedSetVisibility(map, fillLayerId, fcFill.features.length > 0 ? 'visible' : 'none');
|
|
}
|
|
|
|
reorderGlobeFeatureLayers();
|
|
kickRepaint(map);
|
|
};
|
|
|
|
const stop = onMapStyleReady(map, ensure);
|
|
ensure();
|
|
return () => {
|
|
stop();
|
|
};
|
|
}, [
|
|
projection,
|
|
overlays.fleetCircles,
|
|
fleetCircles,
|
|
hoveredFleetOwnerKeyList,
|
|
hoveredFleetMmsiList,
|
|
mapSyncEpoch,
|
|
reorderGlobeFeatureLayers,
|
|
]);
|
|
|
|
// FC + Fleet paint state updates
|
|
// eslint-disable-next-line react-hooks/preserve-manual-memoization
|
|
const updateFcFleetPaintStates = useCallback(() => {
|
|
if (projection !== 'globe' || projectionBusyRef.current) return;
|
|
const map = mapRef.current;
|
|
if (!map || !map.isStyleLoaded()) return;
|
|
|
|
const fleetAwarePairMmsiList = makeUniqueSorted([...hoveredPairMmsiList, ...hoveredFleetMmsiList]);
|
|
|
|
const fcEndpointHighlightExpr = fleetAwarePairMmsiList.length > 0
|
|
? makeMmsiAnyEndpointExpr('fcMmsi', 'otherMmsi', fleetAwarePairMmsiList)
|
|
: false;
|
|
|
|
const fleetOwnerMatchExpr =
|
|
hoveredFleetOwnerKeyList.length > 0 ? makeFleetOwnerMatchExpr(hoveredFleetOwnerKeyList) : false;
|
|
const fleetMemberExpr =
|
|
hoveredFleetMmsiList.length > 0 ? makeFleetMemberMatchExpr(hoveredFleetMmsiList) : false;
|
|
const fleetHighlightExpr =
|
|
hoveredFleetOwnerKeyList.length > 0 || hoveredFleetMmsiList.length > 0
|
|
? (['any', fleetOwnerMatchExpr, fleetMemberExpr] as never)
|
|
: false;
|
|
|
|
try {
|
|
if (map.getLayer('fc-lines-ml')) {
|
|
map.setPaintProperty(
|
|
'fc-lines-ml', 'line-color',
|
|
['case', fcEndpointHighlightExpr, ['case', ['boolean', ['get', 'suspicious'], false], FC_LINE_SUSPICIOUS_ML_HL, FC_LINE_NORMAL_ML_HL], ['boolean', ['get', 'suspicious'], false], FC_LINE_SUSPICIOUS_ML, FC_LINE_NORMAL_ML] as never,
|
|
);
|
|
map.setPaintProperty(
|
|
'fc-lines-ml', 'line-width',
|
|
['case', fcEndpointHighlightExpr, FC_LINE_W_HL, FC_LINE_W_NORMAL] as never,
|
|
);
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
|
|
try {
|
|
if (map.getLayer('fleet-circles-ml')) {
|
|
map.setPaintProperty('fleet-circles-ml', 'line-color', ['case', fleetHighlightExpr, FLEET_LINE_ML_HL, FLEET_LINE_ML] as never);
|
|
map.setPaintProperty('fleet-circles-ml', 'line-width', ['case', fleetHighlightExpr, FLEET_LINE_W_HL, FLEET_LINE_W_NORMAL] as never);
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}, [projection, hoveredFleetMmsiList, hoveredFleetOwnerKeyList, hoveredPairMmsiList]);
|
|
|
|
useEffect(() => {
|
|
const map = mapRef.current;
|
|
if (!map) return;
|
|
const stop = onMapStyleReady(map, updateFcFleetPaintStates);
|
|
updateFcFleetPaintStates();
|
|
return () => {
|
|
stop();
|
|
};
|
|
}, [mapSyncEpoch, hoveredFleetMmsiList, hoveredFleetOwnerKeyList, hoveredPairMmsiList, projection, updateFcFleetPaintStates]);
|
|
|
|
// Breathing animation for highlighted fc/fleet overlays
|
|
useEffect(() => {
|
|
const map = mapRef.current;
|
|
const hasFleetHover = hoveredFleetOwnerKeyList.length > 0 || hoveredFleetMmsiList.length > 0;
|
|
const hasFcHover = hoveredPairMmsiList.length > 0 || hoveredFleetMmsiList.length > 0;
|
|
if (!map || (!hasFleetHover && !hasFcHover) || projection !== 'globe') {
|
|
if (breatheRafRef.current) cancelAnimationFrame(breatheRafRef.current);
|
|
breatheRafRef.current = 0;
|
|
return;
|
|
}
|
|
|
|
const fleetAwarePairMmsiList = makeUniqueSorted([...hoveredPairMmsiList, ...hoveredFleetMmsiList]);
|
|
const fcEndpointHighlightExpr = fleetAwarePairMmsiList.length > 0
|
|
? makeMmsiAnyEndpointExpr('fcMmsi', 'otherMmsi', fleetAwarePairMmsiList)
|
|
: false;
|
|
const fleetOwnerMatchExpr = hoveredFleetOwnerKeyList.length > 0 ? makeFleetOwnerMatchExpr(hoveredFleetOwnerKeyList) : false;
|
|
const fleetMemberExpr = hoveredFleetMmsiList.length > 0 ? makeFleetMemberMatchExpr(hoveredFleetMmsiList) : false;
|
|
const fleetHighlightExpr =
|
|
hoveredFleetOwnerKeyList.length > 0 || hoveredFleetMmsiList.length > 0
|
|
? (['any', fleetOwnerMatchExpr, fleetMemberExpr] as never)
|
|
: false;
|
|
|
|
const animate = () => {
|
|
if (!map.isStyleLoaded()) {
|
|
breatheRafRef.current = requestAnimationFrame(animate);
|
|
return;
|
|
}
|
|
const t = (Math.sin(Date.now() / BREATHE_PERIOD_MS * Math.PI * 2) + 1) / 2;
|
|
try {
|
|
if (map.getLayer('fc-lines-ml') && fcEndpointHighlightExpr !== false) {
|
|
const hlW = FC_LINE_W_HL + t * BREATHE_AMP;
|
|
map.setPaintProperty('fc-lines-ml', 'line-width',
|
|
['case', fcEndpointHighlightExpr, hlW, FC_LINE_W_NORMAL] as never);
|
|
}
|
|
if (map.getLayer('fleet-circles-ml') && fleetHighlightExpr !== false) {
|
|
const hlW = FLEET_LINE_W_HL + t * BREATHE_AMP;
|
|
map.setPaintProperty('fleet-circles-ml', 'line-width',
|
|
['case', fleetHighlightExpr, hlW, FLEET_LINE_W_NORMAL] as never);
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
breatheRafRef.current = requestAnimationFrame(animate);
|
|
};
|
|
breatheRafRef.current = requestAnimationFrame(animate);
|
|
return () => {
|
|
if (breatheRafRef.current) cancelAnimationFrame(breatheRafRef.current);
|
|
breatheRafRef.current = 0;
|
|
};
|
|
}, [hoveredFleetMmsiList, hoveredFleetOwnerKeyList, hoveredPairMmsiList, projection]);
|
|
}
|