Streaming with status
Stream a reply without announcing every word. The scroller follows growth while the reader is at the end, and a single status message announces when the reply is complete. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.
"use client"
import * as React from "react"
import { Button } from "@/components/ui/button"
import { Bubble, BubbleContent } from "@/components/ui/bubble"
import {
Message,
MessageContent,
MessageGroup,
} from "@/components/ui/message"
import {
MessageScroller,
MessageScrollerButton,
MessageScrollerContent,
MessageScrollerViewport,
} from "@/components/ui/message-scroller"
const history = [
{ author: "Support", text: "Hi! How can we help with your order today?" },
{ author: "You", text: "My package shows delivered but it is not here." },
{ author: "Support", text: "Sorry about that. Could you check with your neighbours or building reception first?" },
{ author: "You", text: "I did, nobody has it." },
]
const reply =
"Thanks for checking. I have opened a delivery investigation. You will get an update by email within two business days, and if the package is not found we will send a replacement at no cost."
function StreamingReply() {
const [streamed, setStreamed] = React.useState<string | null>(null)
const [status, setStatus] = React.useState("")
const words = reply.split(" ")
const streaming = streamed !== null && streamed !== reply
React.useEffect(() => {
if (!streaming) return
const timer = window.setTimeout(() => {
const next = words.slice(0, (streamed?.split(" ").filter(Boolean).length ?? 0) + 2).join(" ")
setStreamed(next)
if (next === reply) setStatus("Support replied.")
}, 120)
return () => window.clearTimeout(timer)
}, [streamed, streaming, words])
return (
<div className="flex h-80 w-full max-w-md flex-col overflow-hidden rounded-16 border border-soft-200 bg-white-0">
<MessageScroller>
<MessageScrollerViewport role="region" aria-label="Support conversation">
<MessageScrollerContent>
<MessageGroup>
{history.map((message, index) => (
<Message key={index} align={message.author === "You" ? "end" : "start"}>
<MessageContent>
<Bubble
variant={message.author === "You" ? "primary" : "muted"}
align={message.author === "You" ? "end" : "start"}
>
<BubbleContent>
<span className="sr-only">{message.author}: </span>
{message.text}
</BubbleContent>
</Bubble>
</MessageContent>
</Message>
))}
{streamed !== null && (
<Message aria-busy={streaming || undefined}>
<MessageContent>
<Bubble variant="muted">
<BubbleContent>
<span className="sr-only">Support: </span>
{streamed || "…"}
</BubbleContent>
</Bubble>
</MessageContent>
</Message>
)}
</MessageGroup>
</MessageScrollerContent>
</MessageScrollerViewport>
<MessageScrollerButton />
</MessageScroller>
<p role="status" className="sr-only">
{status}
</p>
<div className="border-t border-soft-200 p-3">
<Button
type="button"
size="xs"
emphasis="stroke"
intent="neutral"
className="w-full"
loading={streaming}
onClick={() => {
setStatus("")
setStreamed("")
}}
>
Stream the support reply
</Button>
</div>
</div>
)
}
export function MessageScrollerStreamingWithStatus() {
return (
<div className="flex w-full max-w-md flex-col gap-10">
<StreamingReply />
</div>
)
}