hts/apps/migrant/lib/hooks/use-streamable-text.ts

28 lines
677 B
TypeScript

import { StreamableValue, readStreamableValue } from '@aigxion/isdk/rsc'
import { useEffect, useState } from 'react'
export const useStreamableText = (
content: string | StreamableValue<string>
) => {
const [rawContent, setRawContent] = useState(
typeof content === 'string' ? content : ''
)
useEffect(() => {
; (async () => {
if (typeof content === 'object') {
let value = ''
for await (const delta of readStreamableValue(content)) {
console.log(delta)
if (typeof delta === 'string') {
setRawContent((value = value + delta))
}
}
}
})()
}, [content])
return rawContent
}