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

44 lines
940 B
TypeScript
Raw Normal View History

2024-10-30 16:01:24 +05:30
import { JSONValue } from 'ai';
import { Dispatch, memo, SetStateAction } from 'react';
2024-11-07 02:40:29 +03:00
import { UIBlock } from './block';
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;
}
2024-11-07 02:40:29 +03:00
export function PureBlockStreamHandler({
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,
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);