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 | null—First argument. null renders nothing and stops polling.
pollMsnumber3000Poll interval. 0 disables polling.
limitnumber100Max messages fetched per poll.

Returns

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