useThread

One conversation's live state: polled history, the conversation record, and a send() with optimistic updates and idempotent retries.

Usage

import { useThread } from "@handset/react";

const thread = useThread(conversationId);

// Reply — from/to derive from the conversation itself:
await thread.send({ body: "On our way!" });

Sends append an optimistic message immediately (pending: true), then swap in the API's accepted message. On failure the bubble flips to failed instead of vanishing. The optimistic local id doubles as the Idempotency-Key, so a retried request can never double-send.

Options

Options
PropTypeDefaultDescription
conversationIdstring | nullFirst argument. null renders nothing and stops polling.
pollMsnumber3000Poll interval. 0 disables polling.
limitnumber100Max messages fetched per poll.

Returns

Return value
PropTypeDefaultDescription
conversationConversation | nullThe conversation record, including opted_out.
messagesOutgoingMessage[]Oldest first; includes optimistic entries.
send(input: SendInput) => Promise<Message>Reply in this conversation. Throws on failure.
isSendingbooleanTrue while a send is in flight.
isLoadingbooleanTrue until the first fetch resolves.
errorError | nullLast fetch error.
refresh() => Promise<void>Force an immediate re-fetch.