UNPKG

@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

160 lines (150 loc) 4.36 kB
"use strict"; /** * @file EmbedButton.hooks.ts * @description Custom hooks for the EmbedButton component */ import { useCallback, useEffect, useRef, useState } from 'react'; import { getAgentData } from "../store/store.key.js"; import { checkPermissions } from "../utils/permision.js"; import { parseDelayMs } from "./EmbedButton.helpers.js"; // ==================== CONFIG DATA HOOK ==================== /** * Hook to fetch and manage agent configuration data */ export const useConfigData = () => { const [configData, setConfigData] = useState(null); useEffect(() => { const fetchConfig = async () => { try { const data = await 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 */ export const useCallDuration = connectionState => { const [callDuration, setCallDuration] = useState(0); const timerRef = useRef(null); 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 = 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) */ export const useInactivityBehavior = ({ configData, isOpen, isLoading, hasActiveToken, onStartCall }) => { const [isAutoOpen, setIsAutoOpen] = useState(false); 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 = 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) */ export const useCallManagement = ({ initializeVoiceAgent, endCall, muteMic, unmuteMic, isMicMuted, resetDuration, setIsOpen }) => { const handleStartCall = useCallback(async () => { await checkPermissions(); resetDuration(); await initializeVoiceAgent(); }, [initializeVoiceAgent, resetDuration]); const handleEndCall = useCallback(async () => { setIsOpen(false); resetDuration(); await endCall(); }, [endCall, resetDuration, setIsOpen]); const handleMicToggle = useCallback(() => { if (isMicMuted) { unmuteMic(); } else { muteMic(); } }, [isMicMuted, muteMic, unmuteMic]); return { handleStartCall, handleEndCall, handleMicToggle }; }; //# sourceMappingURL=EmbedButton.hooks.js.map