Files
auto-trade/features/trade/hooks/useKisTradeWebSocket.ts

80 lines
2.7 KiB
TypeScript
Raw Normal View History

2026-02-13 12:17:35 +09:00
import { useEffect, useMemo } from "react";
import { type KisRuntimeCredentials } from "@/features/settings/store/use-kis-runtime-store";
2026-02-10 11:16:39 +09:00
import type {
DashboardRealtimeTradeTick,
DashboardStockOrderBookResponse,
2026-02-11 16:31:28 +09:00
} from "@/features/trade/types/trade.types";
2026-02-13 12:17:35 +09:00
import { resolveTradeTrIds } from "@/features/trade/utils/kisRealtimeUtils";
import { useKisWebSocketStore } from "@/features/kis-realtime/stores/kisWebSocketStore";
import { useDomesticSession } from "@/features/trade/hooks/useDomesticSession";
import { useTradeTickSubscription } from "@/features/trade/hooks/useTradeTickSubscription";
import { useOrderbookSubscription } from "@/features/trade/hooks/useOrderbookSubscription";
2026-02-12 10:24:03 +09:00
/**
2026-02-13 12:17:35 +09:00
* @description KIS / .
* @summary
* - `useDomesticSession`: (, )
* - `useTradeTickSubscription`: (Tick)
* - `useOrderbookSubscription`: (Orderbook)
* - `useKisWebSocketStore`:
*
* (Composition) .
2026-02-10 11:16:39 +09:00
*/
export function useKisTradeWebSocket(
symbol: string | undefined,
credentials: KisRuntimeCredentials | null,
isVerified: boolean,
onTick?: (tick: DashboardRealtimeTradeTick) => void,
options?: {
orderBookSymbol?: string;
2026-02-23 15:37:22 +09:00
orderBookMarket?: "KOSPI" | "KOSDAQ";
2026-02-10 11:16:39 +09:00
onOrderBookMessage?: (data: DashboardStockOrderBookResponse) => void;
},
) {
2026-02-13 12:17:35 +09:00
const marketSession = useDomesticSession();
const { isConnected, error } = useKisWebSocketStore();
const { latestTick, recentTradeTicks, lastTickAt } = useTradeTickSubscription(
{
symbol,
isVerified,
credentials,
marketSession,
onTick,
},
2026-02-11 11:18:15 +09:00
);
2026-02-10 11:16:39 +09:00
2026-02-13 12:17:35 +09:00
useOrderbookSubscription({
symbol: options?.orderBookSymbol,
2026-02-23 15:37:22 +09:00
market: options?.orderBookMarket,
2026-02-13 12:17:35 +09:00
isVerified,
credentials,
marketSession,
onOrderBookMessage: options?.onOrderBookMessage,
});
2026-02-10 11:16:39 +09:00
2026-02-13 12:17:35 +09:00
// Connection/Data warning
2026-02-10 11:16:39 +09:00
useEffect(() => {
2026-02-13 12:17:35 +09:00
if (!isConnected || lastTickAt || !symbol) return;
2026-02-10 11:16:39 +09:00
const timer = window.setTimeout(() => {
2026-02-13 12:17:35 +09:00
// Just a warning, not blocking
2026-02-10 11:16:39 +09:00
}, 8000);
return () => window.clearTimeout(timer);
2026-02-13 12:17:35 +09:00
}, [isConnected, lastTickAt, symbol]);
2026-02-10 11:16:39 +09:00
2026-02-13 12:17:35 +09:00
const realtimeTrId = useMemo(() => {
if (!credentials) return "H0STCNT0";
const ids = resolveTradeTrIds(credentials.tradingEnv, marketSession);
return ids[0] ?? "H0STCNT0";
}, [credentials, marketSession]);
2026-02-10 11:16:39 +09:00
return {
latestTick,
recentTradeTicks,
isConnected,
error,
lastTickAt,
2026-02-11 11:18:15 +09:00
realtimeTrId,
2026-02-10 11:16:39 +09:00
};
}