chatbot-template/components/block-stream-handler.tsx

44 lines
954 B
TypeScript
Raw Normal View History

2024-11-15 12:18:17 -05:00
import type { JSONValue } from 'ai';
import { type Dispatch, memo, type SetStateAction } from 'react';
2024-10-30 16:01:24 +05:30
2024-11-15 12:18:17 -05:00
import type { UIBlock } from './block';
2024-11-07 02:40:29 +03:00
import { useBlockStream } from './use-block-stream';
2024-10-30 16:01:24 +05:30
2024-11-07 02:40:29 +03:00
interface BlockStreamHandlerProps {
setBlock: Dispatch<SetStateAction<UIBlock>>;
2024-10-30 16:01:24 +05:30
streamingData: JSONValue[] | undefined;
}
function PureBlockStreamHandler({
2024-11-07 02:40:29 +03:00
setBlock,
2024-10-30 16:01:24 +05:30
streamingData,
2024-11-07 02:40:29 +03:00
}: BlockStreamHandlerProps) {
useBlockStream({
2024-10-30 16:01:24 +05:30
streamingData,
2024-11-07 02:40:29 +03:00
setBlock,
2024-10-30 16:01:24 +05:30
});
return null;
}
function areEqual(
2024-11-07 02:40:29 +03:00
prevProps: BlockStreamHandlerProps,
2024-11-15 13:00:15 -05:00
nextProps: BlockStreamHandlerProps,
2024-10-30 16:01:24 +05:30
) {
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;
}
2024-11-07 02:40:29 +03:00
export const BlockStreamHandler = memo(PureBlockStreamHandler, areEqual);