30 lines
855 B
TypeScript
30 lines
855 B
TypeScript
'use client'
|
|
|
|
import { useWsSubscribe } from './wsContext'
|
|
|
|
interface Handlers {
|
|
// BUG-08 fix: backend now broadcasts SOL/TRUMP/BNB/DOGE/LINK/AAVE ticks
|
|
// in addition to BTC/ETH — widen asset to string.
|
|
onPrice?: (asset: string, price: number) => void
|
|
onNewPost?: (post: object) => void
|
|
}
|
|
|
|
type PriceMsg = { type: 'price'; asset: string; price: number }
|
|
type PostMsg = { type: 'new_post'; post: object }
|
|
|
|
/**
|
|
* Subscribe to price ticks and new-post events from the shared WebSocket.
|
|
* Delegates to WsProvider's singleton connection — no new socket is opened.
|
|
*/
|
|
export function usePriceSocket(handlers: Handlers) {
|
|
useWsSubscribe('price', (msg) => {
|
|
const m = msg as PriceMsg
|
|
handlers.onPrice?.(m.asset, m.price)
|
|
})
|
|
|
|
useWsSubscribe('new_post', (msg) => {
|
|
const m = msg as PostMsg
|
|
handlers.onNewPost?.(m.post)
|
|
})
|
|
}
|