UNPKG

@plteam/chat-ui

Version:

CUI Kit is a free and open-source library for creating AI assistant chat interfaces, built with React, Material UI, and TypeScript

41 lines (40 loc) 1.67 kB
import * as React from 'react'; import SpanSmoothAnimation from './SpanSmoothAnimation'; const stringToSmoothComponent = (childString, counter) => { return childString.split(' ').map((v) => !v // if `v` is empty, it means we removed a space and it needs to be restored ? ` ` : (React.createElement(SpanSmoothAnimation, { key: `smooth-${counter.i++}` }, `${v} `))); }; const forEachChildren = (children, counter) => { const resultValue = []; children.forEach((childValue) => { if (typeof childValue === 'string') { resultValue.push(...stringToSmoothComponent(childValue, counter)); } else if (Array.isArray(childValue)) { resultValue.push(...forEachChildren(childValue, counter)); } else { resultValue.push(childValue); } }); return resultValue; }; export const useChildrenSmootherParser = () => { return React.useCallback((children) => { const childrenArray = []; const counter = { i: 0 }; if (typeof children === 'string') { // A lone string child is pushed as-is (never wrapped in a pending span), so text that // renders without going through the smoother — e.g. reasoning paragraphs, whose // `inProgress` latches on while their stream has already ended — can never get stuck // at opacity 0. childrenArray.push(children); } else if (Array.isArray(children)) { childrenArray.push(...forEachChildren(children, counter)); } return childrenArray; }, []); };