import React, { useState, useEffect } from 'react'; import {SafeAreaView, View, Text, Button, StyleSheet, ScrollView} from 'react-native'; import Daily from "@daily-co/react-native-daily-js"; import { API_BASE_URL } from "@env"; const CallScreen = () => { const [connectionStatus, setConnectionStatus] = useState('Disconnected'); const [isConnected, setIsConnected] = useState(false); const [callObject, setCallObject] = useState(null); const [logs, setLogs] = useState([]); useEffect(() => { if (callObject) { setupTrackListeners(callObject); } }, [callObject]); const log = (message) => { setLogs((prevLogs) => [...prevLogs, `${new Date().toISOString()} - ${message}`]); console.log(message); }; const setupTrackListeners = (callObject) => { callObject.on("joined-meeting", () => { setConnectionStatus('Connected'); setIsConnected(true); log('Client connected'); }); callObject.on("left-meeting", () => { setConnectionStatus('Disconnected'); setIsConnected(false); log('Client disconnected'); }); callObject.on("participant-left", () => { // When the bot leaves, we are also disconnecting from the call disconnect().catch((err) => { log(`Failed to disconnect ${err}`); }) }); // Trigger so the bot can start sending audio callObject.on("track-started", (evt) => { if (evt.track.kind === "audio" && evt.participant.local === false) { handleEventToConsole(evt) log("Sending the message that will trigger the bot to play the audio.") callObject.sendAppMessage("playable") } }); callObject.on("error", (evt) => log(`Error: ${evt.error}`)); // Other events just for awareness callObject.on("track-stopped", handleEventToConsole); callObject.on("participant-joined", handleEventToConsole); callObject.on("participant-updated", handleEventToConsole); }; const handleEventToConsole = (evt) => { log(`Received event: ${evt.action}`); }; const connect = async () => { try { const callObject = Daily.createCallObject({ subscribeToTracksAutomatically: true }); setCallObject(callObject); const connectionUrl = `${API_BASE_URL}/connect` const res = await fetch(connectionUrl, { method: "POST", headers: { "Content-Type": "application/json" } }); const roomInfo = await res.json(); await callObject.join({ url: roomInfo.room_url }); } catch (error) { log(`Error connecting: ${error.message}`); } }; const disconnect = async () => { if (callObject) { try { await callObject.leave(); await callObject.destroy(); setCallObject(null); } catch (error) { log(`Error disconnecting: ${error.message}`); } } }; return ( Status: {connectionStatus}