- Move useDeviceId from apps/web to packages/store/src/device-id.ts - Update imports in chat.tsx and use-gateway.ts to use @multica/store - Add uuid dependency to @multica/store Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { useEffect, useRef, useState, useCallback } from "react"
|
|
import { GatewayClient, type ConnectionState, type RoutedMessage } from "@multica/sdk"
|
|
import { useDeviceId } from "@multica/store"
|
|
import { GATEWAY_URL } from "../lib/config"
|
|
|
|
interface UseGatewayOptions {
|
|
onMessage?: (msg: RoutedMessage) => void
|
|
}
|
|
|
|
export function useGateway(options?: UseGatewayOptions) {
|
|
const deviceId = useDeviceId()
|
|
const [state, setState] = useState<ConnectionState>("disconnected")
|
|
const clientRef = useRef<GatewayClient | null>(null)
|
|
const onMessageRef = useRef(options?.onMessage)
|
|
|
|
useEffect(() => {
|
|
onMessageRef.current = options?.onMessage
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (!deviceId) return
|
|
|
|
const client = new GatewayClient({
|
|
url: GATEWAY_URL,
|
|
deviceId,
|
|
deviceType: "client",
|
|
})
|
|
.onStateChange(setState)
|
|
.onMessage(msg => onMessageRef.current?.(msg))
|
|
|
|
clientRef.current = client
|
|
client.connect()
|
|
return () => { client.disconnect() }
|
|
}, [deviceId])
|
|
|
|
const send = useCallback(
|
|
(to: string, action: string, payload: unknown) => {
|
|
clientRef.current?.send(to, action, payload)
|
|
},
|
|
[]
|
|
)
|
|
|
|
return { state, send }
|
|
}
|