@revrag-ai/embed-react-native
Version:
A powerful React Native library for integrating AI-powered voice agents into mobile applications. Features real-time voice communication, intelligent speech processing, customizable UI components, and comprehensive event handling for building conversation
165 lines (158 loc) • 4.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useInactivityBehavior = exports.useConfigData = exports.useCallManagement = exports.useCallDuration = void 0;
var _react = require("react");
var _storeKey = require("../store/store.key.js");
var _permision = require("../utils/permision.js");
var _EmbedButtonHelpers = require("./EmbedButton.helpers.js");
/**
* @file EmbedButton.hooks.ts
* @description Custom hooks for the EmbedButton component
*/
// ==================== CONFIG DATA HOOK ====================
/**
* Hook to fetch and manage agent configuration data
*/
const useConfigData = () => {
const [configData, setConfigData] = (0, _react.useState)(null);
(0, _react.useEffect)(() => {
const fetchConfig = async () => {
try {
const data = await (0, _storeKey.getAgentData)('@config_data');
setConfigData(data?.ui_config || null);
} catch (error) {
console.error('Failed to fetch config data:', error);
}
};
fetchConfig();
}, []);
return configData;
};
// ==================== CALL DURATION HOOK ====================
/**
* Hook to track call duration when connected
*/
exports.useConfigData = useConfigData;
const useCallDuration = connectionState => {
const [callDuration, setCallDuration] = (0, _react.useState)(0);
const timerRef = (0, _react.useRef)(null);
(0, _react.useEffect)(() => {
if (connectionState === 'connected' && !timerRef.current) {
// Start timer
timerRef.current = setInterval(() => {
setCallDuration(prev => prev + 1);
}, 1000);
} else if (connectionState !== 'connected' && timerRef.current) {
// Stop timer
clearInterval(timerRef.current);
timerRef.current = null;
}
return () => {
if (timerRef.current) {
clearInterval(timerRef.current);
timerRef.current = null;
}
};
}, [connectionState]);
const resetDuration = (0, _react.useCallback)(() => {
setCallDuration(0);
if (timerRef.current) {
clearInterval(timerRef.current);
timerRef.current = null;
}
}, []);
return {
callDuration,
resetDuration
};
};
// ==================== INACTIVITY BEHAVIOR HOOK ====================
/**
* Hook to handle inactivity behavior (show_popup or auto_trigger)
*/
exports.useCallDuration = useCallDuration;
const useInactivityBehavior = ({
configData,
isOpen,
isLoading,
hasActiveToken,
onStartCall
}) => {
const [isAutoOpen, setIsAutoOpen] = (0, _react.useState)(false);
(0, _react.useEffect)(() => {
const behavior = configData?.inactivity_behaviour;
// Only act when explicitly configured
if (behavior !== 'show_popup' && behavior !== 'auto_trigger') {
return;
}
// Parse delay with fallback
const delayMs = (0, _EmbedButtonHelpers.parseDelayMs)(configData?.popup_delay_ms);
const timer = setTimeout(async () => {
if (isOpen) return;
if (behavior === 'show_popup') {
// Show popup notification
if (configData?.popup_description) {
setIsAutoOpen(true);
} else {
console.warn('Popup behavior configured but no description provided');
}
} else if (behavior === 'auto_trigger') {
// Auto-start the call
setIsAutoOpen(false);
if (!hasActiveToken && !isLoading) {
try {
await onStartCall();
} catch (error) {
console.error('Auto-trigger call failed:', error);
}
}
}
}, delayMs);
return () => clearTimeout(timer);
}, [configData?.inactivity_behaviour, configData?.popup_delay_ms, configData?.popup_description, isOpen, isLoading, hasActiveToken, onStartCall]);
return {
isAutoOpen,
setIsAutoOpen
};
};
// ==================== CALL MANAGEMENT HOOK ====================
/**
* Hook to manage call lifecycle (start, end, mute)
*/
exports.useInactivityBehavior = useInactivityBehavior;
const useCallManagement = ({
initializeVoiceAgent,
endCall,
muteMic,
unmuteMic,
isMicMuted,
resetDuration,
setIsOpen
}) => {
const handleStartCall = (0, _react.useCallback)(async () => {
await (0, _permision.checkPermissions)();
resetDuration();
await initializeVoiceAgent();
}, [initializeVoiceAgent, resetDuration]);
const handleEndCall = (0, _react.useCallback)(async () => {
setIsOpen(false);
resetDuration();
await endCall();
}, [endCall, resetDuration, setIsOpen]);
const handleMicToggle = (0, _react.useCallback)(() => {
if (isMicMuted) {
unmuteMic();
} else {
muteMic();
}
}, [isMicMuted, muteMic, unmuteMic]);
return {
handleStartCall,
handleEndCall,
handleMicToggle
};
};
exports.useCallManagement = useCallManagement;
//# sourceMappingURL=EmbedButton.hooks.js.map