Docs / built-apps / useThreads

useThreads

hooksince 0.2.2

List a built-app user's private message threads, with unread counts and consent control.

import { useThreads } from 'flowstack-sdk'

Lists the authenticated user's private DM threads from the server-owned, ACL'd DM store — the backend only ever returns threads the caller participates in. Each thread carries the counterpart's user key, the last message, and an unread count.

Private messaging is a built-app capability: it requires an appScope on the FlowstackProvider config. In a Casino personal session the backend returns 403 and this hook surfaces an error. Treat the type signatures in packages/flowstack-sdk/src/hooks/useThreads.ts as the source of truth.

Signature

const { threads, isLoading, error, refresh, openThread } = useThreads(options?)

Parameters

ParamTypeNotesDescription
options.refreshIntervalnumbersince 0.2.2Auto-poll interval in ms. No polling by default.
options.enabledbooleandefault true, since 0.2.2Set false to skip the initial fetch.

Returns

FieldTypeDescription
threadsDmThread[]The caller's threads — each { pair_key, with_user_key, status, last_message, unread_count, updated_at }.
isLoadingbooleanTrue while a fetch is in flight.
errorstring | nullError message, e.g. a 403 when used outside a built-app (no appScope).
refresh() => Promise<void>Refetch the thread list.
openThread(withUserKey: string) => Promise<'pending' | 'open' | null>Record the caller's consent to open a thread. The thread becomes sendable only once BOTH parties consent. Idempotent per caller; refetches on success.

Examples

List threads with polling
const { threads, isLoading, openThread } = useThreads({ refreshInterval: 5000 });

threads.map((t) => (
  <ThreadRow key={t.pair_key} counterpart={t.with_user_key} unread={t.unread_count} />
));

// Consent to open a new conversation
const status = await openThread(otherUserKey); // 'pending' | 'open' | null
WarningRequires a built-app appScope. Outside a built app the backend returns 403 and error is set.

See also

useMessages · listThreads · openThread · DmThread

Source: README.md#private-messaging-dms · Also available in llms-full.txt and registry.json.