diff --git a/examples/nextjs/next.config.ts b/examples/nextjs/next.config.ts index 5e891cf..508543d 100644 --- a/examples/nextjs/next.config.ts +++ b/examples/nextjs/next.config.ts @@ -2,6 +2,7 @@ import type { NextConfig } from 'next'; const nextConfig: NextConfig = { /* config options here */ + reactStrictMode: false, }; export default nextConfig; diff --git a/examples/nextjs/src/app/flow/OutputView.tsx b/examples/nextjs/src/app/flow/OutputView.tsx index 7b890ca..10409d8 100644 --- a/examples/nextjs/src/app/flow/OutputView.tsx +++ b/examples/nextjs/src/app/flow/OutputView.tsx @@ -1,11 +1,22 @@ +'use client'; import { useErrorBoundary } from 'react-error-boundary'; -import { useFlowEventListener } from '@speechmatics/flow-client-react'; +import { + type Message, + useFlowEventListener, +} from '@speechmatics/flow-client-react'; import { useFlowTranscript } from '@speechmatics/flow-client-react'; +import { useRef } from 'react'; export function OutputView() { useErrorView(); + console.log('RENDER'); + const { messages, handleEvent } = useFlowTranscript(); + // lastMessagesRef.current ??= messages; + // console.log(messages === lastMessagesRef.current); + + // console.log('messages=', messages); useFlowEventListener('message', ({ data }) => { if ( @@ -21,7 +32,7 @@ export function OutputView() { return (
Output
-
+ {/*
{messages.map((message) => (
{message.speaker}
@@ -31,7 +42,7 @@ export function OutputView() {
))} -
+
*/}
); } diff --git a/packages/flow-client-react/package.json b/packages/flow-client-react/package.json index c4a88a8..b6e249c 100644 --- a/packages/flow-client-react/package.json +++ b/packages/flow-client-react/package.json @@ -1,6 +1,6 @@ { "name": "@speechmatics/flow-client-react", - "version": "0.1.2-rc4", + "version": "0.1.2-rc5", "description": "React hooks for interacting with the Speechmatics Flow API", "main": "dist/index.cjs", "module": "dist/index.js", diff --git a/packages/flow-client-react/src/use-flow-transcript.tsx b/packages/flow-client-react/src/use-flow-transcript.tsx index d081777..73a9109 100644 --- a/packages/flow-client-react/src/use-flow-transcript.tsx +++ b/packages/flow-client-react/src/use-flow-transcript.tsx @@ -1,3 +1,4 @@ +'use client'; import type { AddPartialTranscriptMessage, AddTranscriptMessage, @@ -5,15 +6,23 @@ import type { ResponseStartedMessage, ResponseInterruptedMessage, } from '@speechmatics/flow-client'; -import { useReducer } from 'react'; +import { useCallback, useMemo, useReducer } from 'react'; export function useFlowTranscript() { const [state, dispatch] = useReducer(messagesReducer, []); - return { - messages: state, - handleEvent: dispatch, - }; + const handleEvent = useCallback((ev: IncomingEvent) => { + if (!eventIsEmpty(ev)) { + dispatch(ev); + } + }, []); + + return useMemo(() => { + return { + messages: state, + handleEvent, + }; + }, [state, handleEvent]); } export type Message = {