import { useCallback, useEffect, useState } from 'react' import KetchupClient from 'ketchup-client' type Dispatch = (action: Action, timestamp?: number) => void export default function useKetchup( url: string, reducer: (state: State, action: Action) => State, ): [State | undefined, Dispatch] { const [proj, setProj] = useState() const [kc, setKc] = useState>() useEffect(() => { const newKc = new KetchupClient(url, reducer) setKc(newKc) newKc.on('projection', setProj) return () => { newKc.off('projection', setProj) } }, [url, reducer]) const dispatch = useCallback((a, ts) => kc?.dispatch(a, ts), [kc]) return [proj, dispatch, /* connected */] }