위젯 기능 추가
This commit is contained in:
@@ -11,7 +11,7 @@ export interface RecentTrade {
|
||||
time: number;
|
||||
}
|
||||
|
||||
const MAX_TRADES = 12;
|
||||
const MAX_TRADES_DEFAULT = 12;
|
||||
|
||||
interface UpbitTradeRaw {
|
||||
market: string;
|
||||
@@ -30,9 +30,11 @@ interface UpbitTradeWs {
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export function useUpbitRecentTrades(market: string, enabled = true) {
|
||||
export function useUpbitRecentTrades(market: string, enabled = true, maxTrades = MAX_TRADES_DEFAULT) {
|
||||
const [trades, setTrades] = useState<RecentTrade[]>([]);
|
||||
const [strength, setStrength] = useState<number | null>(null);
|
||||
const maxRef = useRef(maxTrades);
|
||||
maxRef.current = maxTrades;
|
||||
|
||||
const mountedRef = useRef(true);
|
||||
const marketRef = useRef(market);
|
||||
@@ -40,7 +42,8 @@ export function useUpbitRecentTrades(market: string, enabled = true) {
|
||||
|
||||
const pushTrade = useCallback((t: RecentTrade) => {
|
||||
setTrades(prev => {
|
||||
const next = [t, ...prev].slice(0, MAX_TRADES);
|
||||
const cap = maxRef.current;
|
||||
const next = [t, ...prev].slice(0, cap);
|
||||
const buyVol = next.filter(x => x.side === 'bid').reduce((s, x) => s + x.volume, 0);
|
||||
const sellVol = next.filter(x => x.side === 'ask').reduce((s, x) => s + x.volume, 0);
|
||||
if (sellVol > 0) setStrength((buyVol / sellVol) * 100);
|
||||
@@ -51,7 +54,7 @@ export function useUpbitRecentTrades(market: string, enabled = true) {
|
||||
|
||||
const fetchSnapshot = useCallback(async () => {
|
||||
try {
|
||||
const res = await fetch(`/upbit-api/v1/trades/ticks?market=${marketRef.current}&count=${MAX_TRADES}`);
|
||||
const res = await fetch(`/upbit-api/v1/trades/ticks?market=${marketRef.current}&count=${maxRef.current}`);
|
||||
if (!res.ok) return;
|
||||
const data: UpbitTradeRaw[] = await res.json();
|
||||
const list: RecentTrade[] = data.map(d => ({
|
||||
|
||||
Reference in New Issue
Block a user