chatbot-template/components/block-stream-handler.tsx
2024-11-15 13:00:15 -05:00

43 lines
961 B
TypeScript

import type { JSONValue } from 'ai';
import { type Dispatch, memo, type SetStateAction } from 'react';
import type { UIBlock } from './block';
import { useBlockStream } from './use-block-stream';
interface BlockStreamHandlerProps {
setBlock: Dispatch<SetStateAction<UIBlock>>;
streamingData: JSONValue[] | undefined;
}
export function PureBlockStreamHandler({
setBlock,
streamingData,
}: BlockStreamHandlerProps) {
useBlockStream({
streamingData,
setBlock,
});
return null;
}
function areEqual(
prevProps: BlockStreamHandlerProps,
nextProps: BlockStreamHandlerProps,
) {
if (!prevProps.streamingData && !nextProps.streamingData) {
return true;
}
if (!prevProps.streamingData || !nextProps.streamingData) {
return false;
}
if (prevProps.streamingData.length !== nextProps.streamingData.length) {
return false;
}
return true;
}
export const BlockStreamHandler = memo(PureBlockStreamHandler, areEqual);