EdgeInteract
useInteraction
useInteraction() is the hook for sending an interaction from a React component. It returns a sendInteraction function that publishes the request and returns a Promise that resolves with the response (or rejects on timeout or block).
Signature
function useInteraction<TPayload = unknown, TData = unknown>(): {
sendInteraction: (
request: Omit<InteractionRequest<TPayload>, 'interactionId'>
) => Promise<InteractionResponse<TData>>;
isSending: boolean;
lastError: Error | null;
}
Basic Usage
import { useInteraction } from 'edge-interact-react';
function SendApprovalButton({ invoiceId, managerId }: Props) {
const { sendInteraction, isSending, lastError } = useInteraction();
const handleRequest = async () => {
try {
const response = await sendInteraction({
type: 'approval',
targetUserId: managerId,
title: 'Invoice Approval Required',
payload: {
context: `Invoice ${invoiceId} requires approval.`,
fields: [{ key: 'id', label: 'Invoice', value: invoiceId }]
},
timeoutMs: 86_400_000
});
if (response.outcome === 'approved') {
toast.success(`Approved by ${response.respondedBy}`);
} else {
toast.warning(`Rejected: ${response.data?.comment}`);
}
} catch (err) {
if (err instanceof InteractionTimeoutError) {
toast.error('No response received — interaction timed out.');
} else {
toast.error('Failed to send approval request.');
}
}
};
return (
<button onClick={handleRequest} disabled={isSending}>
{isSending ? 'Sending...' : 'Request Manager Approval'}
</button>
);
}
Return Values
| Property | Type | Description |
|---|---|---|
sendInteraction(request) | Function → Promise | Publishes the interaction. Resolves with InteractionResponse when the user responds. Rejects on timeout, block, or error. |
isSending | boolean | true while an interaction is in-flight (published but no response yet) |
lastError | Error | null | The last error from sendInteraction. Reset to null on the next call. |
Typed Usage with Generics
import { useInteraction } from 'edge-interact-react';
import type { ApprovalPayload } from 'edge-interact-core';
// TPayload = ApprovalPayload, TData = { comment: string }
const { sendInteraction } = useInteraction<ApprovalPayload, { comment: string }>();
const response = await sendInteraction({
type: 'approval',
targetUserId: managerId,
title: 'Approve',
payload: {
context: '...', // typed as ApprovalPayload
fields: [...]
},
timeoutMs: 86_400_000
});
// response.data is typed as { comment: string }
console.log(response.data?.comment);
Error Types
| Error | When Thrown |
|---|---|
InteractionTimeoutError | User did not respond within timeoutMs |
InteractionBlockedError | A pre-send hook blocked the interaction |
InteractionRateLimitError | Rate limit exceeded for the target user |
InteractionConnectionError | EdgeStream not connected when sendInteraction was called |