Refactor to use ai/rsc (#253)
This commit is contained in:
parent
69ca8fcc22
commit
e85ba803dd
66 changed files with 2799 additions and 740 deletions
26
lib/hooks/use-streamable-text.ts
Normal file
26
lib/hooks/use-streamable-text.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { StreamableValue, readStreamableValue } from 'ai/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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue