Portal Community

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

PropertyTypeDescription
sendInteraction(request)Function → PromisePublishes the interaction. Resolves with InteractionResponse when the user responds. Rejects on timeout, block, or error.
isSendingbooleantrue while an interaction is in-flight (published but no response yet)
lastErrorError | nullThe 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

ErrorWhen Thrown
InteractionTimeoutErrorUser did not respond within timeoutMs
InteractionBlockedErrorA pre-send hook blocked the interaction
InteractionRateLimitErrorRate limit exceeded for the target user
InteractionConnectionErrorEdgeStream not connected when sendInteraction was called