Utilities
Small hooks and helpers for timing, clipboard access, and formatting.
React hooks are exported from the /hooks entry point, and pure formatting helpers from the package root. These utilities do not make API requests. Browser-dependent hooks should run in client-rendered components.
Debounce a value
useDebounce(value, delay?) returns the latest value after it remains unchanged for the delay. The default delay is 500 ms.
import { useState } from "react";
import { useDebounce } from "@scalereal/sr-voice-engine-react-sdk/hooks";
function SearchBox() {
const [search, setSearch] = useState("");
const settledSearch = useDebounce(search, 300);
return (
<label>
Search
<input value={search} onChange={(event) => setSearch(event.target.value)} />
<span>Query: {settledSearch}</span>
</label>
);
}Import useState from React in this example's file.
Debounce a callback
useDebouncedCallback(callback, delay) returns call, cancel, and flush. Calling call resets the timer; cancel drops the pending call; flush runs it immediately.
Copy text to the clipboard
useClipboard(timeout?) returns copied, copy(text), and reset(). copied remains true for 2 seconds by default after a successful copy. If the Clipboard API is unavailable, copy is a no-op.
import { useClipboard } from "@scalereal/sr-voice-engine-react-sdk/hooks";
function CopyCallId({ callId }: { callId: string }) {
const clipboard = useClipboard();
return (
<button type="button" onClick={() => void clipboard.copy(callId)}>
{clipboard.copied ? "Copied" : "Copy call ID"}
</button>
);
}Track call duration
useCallDuration(isActive) returns elapsed time formatted as MM:SS. The timer runs while active and freezes while inactive.
Delay a transient indicator
useDelayedTrue(value, delayMs?) returns true only when value remains true for the delay. It returns false immediately when value becomes false. The default is THINKING_INDICATOR_SHOW_DELAY_MS (300 ms).
Format values
The root package also exports pure helpers:
formatDateTime,formatTime, andformatDurationfor call timestamps and durations.formatCreditsandformatCreditsValuefor credit amounts.buildCallReportCsv,flattenCallHistoryRow,escapeCsvCell, anddownloadCsvTextfor CSV generation and download.buildCallReportPdfanddownloadPdffor PDF report generation and download.- Transcript helpers including
normalizeTranscriptMessages,getTranscriptTextContent,isCompletedFunctionCall,shouldIncludeTranscriptMessage,getNextThinkingSide, andisTranscriptWebSocketMessage.
Custom attributes helpers
The SDK exports helpers for building query filters and reading nested custom attributes:
Read nested attributes
getCustomAttribute(attributes, path) safely retrieves a value from a custom_attributes object using dot notation without throwing if parent keys are missing:
import { getCustomAttribute } from "@scalereal/sr-voice-engine-react-sdk";
const city = getCustomAttribute(call.custom_attributes, "user.address.city");Filter builders and helpers
attr: typed helper functions (attr.eq,attr.btw,attr.like,attr.contains,attr.rootIsNull, etc.) that produceCustomAttributeFilterobjects foruseCallHistory.customAttributeQuery(): chainable builder returningCustomAttributeQueryBuilderwith.eq(),.btw(),.exists(),.rootIsNull(),.toFilters(),.toQueryParams(), and.toQueryString().buildCustomAttributeQuery(filters): convertsCustomAttributeFilter[]into flat query parameters (e.g.{ "custom_attributes.plan__eq": "pro" }).buildCustomAttributeQueryString(filters): converts filters directly into a URL query string without the leading?.customAttributeParamKey(filter): returns the query key for a filter (e.g.custom_attributes.age__gtorcustom_attributes__isnull).serializeCustomAttributeFilter(filter): converts one filter into a[key, value]tuple.CUSTOM_ATTRIBUTES_PARAM: the"custom_attributes"query parameter prefix constant.
See Call History for end-to-end filtering and query examples.