/** * 실시간 전략 STOMP 구독 — 화면과 무관하게 동작, 시그널은 알림 Context로 전달 */ import React, { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'; import { useLiveStrategyMarkers, type LiveMarker } from '../hooks/useLiveStrategyMarkers'; import { useTradeNotification } from '../contexts/TradeNotificationContext'; export interface LiveSignalNotifierHandle { clearMarkers: () => void; } interface Props { markets: string[]; /** 종목별 평가 분봉 — STOMP 구독 경로 */ marketSubscriptions?: { market: string; candleType: string }[]; activeMarket: string; enabled: boolean; popupEnabled: boolean; onMarkersChange: (markers: LiveMarker[]) => void; /** 차트 화면일 때만 마커를 차트에 반영 */ chartMarkersActive: boolean; } export const LiveSignalNotifier = forwardRef(function LiveSignalNotifier({ markets, marketSubscriptions, activeMarket, enabled, popupEnabled: _popupEnabled, onMarkersChange, chartMarkersActive, }, ref) { const { addNotification, refreshHistory } = useTradeNotification(); const onMarkersRef = useRef(onMarkersChange); onMarkersRef.current = onMarkersChange; useEffect(() => { if (!enabled || markets.length === 0) return; const id = window.setInterval(() => { void refreshHistory(); }, 20_000); return () => window.clearInterval(id); }, [enabled, markets.length, refreshHistory]); const { clearMarkers } = useLiveStrategyMarkers({ markets, subscriptions: marketSubscriptions, activeMarket, enabled: enabled && markets.length > 0, onMarkersChange: markers => { if (chartMarkersActive) onMarkersRef.current(markers); }, onSignal: (marker: LiveMarker) => { addNotification({ market: marker.market, signalType: marker.signal, price: marker.price, candleTime: marker.time, strategyId: marker.strategyId ?? undefined, executionType: marker.executionType ?? undefined, candleType: marker.candleType ?? '1m', }); window.setTimeout(() => { void refreshHistory(); }, 800); }, }); useImperativeHandle(ref, () => ({ clearMarkers }), [clearMarkers]); return null; }); export default LiveSignalNotifier;