2024-12-30 01:47:21 +01:00
|
|
|
import React from 'react';
|
2024-12-21 14:36:01 +01:00
|
|
|
import { useRecoilState } from 'recoil';
|
2024-12-30 01:47:21 +01:00
|
|
|
import { Phone, PhoneOff } from 'lucide-react';
|
2024-12-21 14:36:01 +01:00
|
|
|
import { OGDialog, OGDialogContent, Button } from '~/components';
|
2024-12-30 01:47:21 +01:00
|
|
|
import { useWebSocket, useCall } from '~/hooks';
|
2024-12-21 14:36:01 +01:00
|
|
|
import store from '~/store';
|
|
|
|
|
|
|
|
|
|
export const Call: React.FC = () => {
|
2024-12-30 01:47:21 +01:00
|
|
|
const { isConnected, sendMessage } = useWebSocket();
|
|
|
|
|
const { isCalling, isProcessing, startCall, hangUp } = useCall();
|
2024-12-21 14:36:01 +01:00
|
|
|
|
|
|
|
|
const [open, setOpen] = useRecoilState(store.callDialogOpen(0));
|
|
|
|
|
|
2024-12-30 01:47:21 +01:00
|
|
|
const [eventLog, setEventLog] = React.useState<string[]>([]);
|
|
|
|
|
|
|
|
|
|
const logEvent = (message: string) => {
|
|
|
|
|
console.log(message);
|
|
|
|
|
setEventLog((prev) => [...prev, message]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (isConnected) {
|
|
|
|
|
logEvent('Connected to server.');
|
|
|
|
|
} else {
|
|
|
|
|
logEvent('Disconnected from server.');
|
|
|
|
|
}
|
|
|
|
|
}, [isConnected]);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (isCalling) {
|
|
|
|
|
logEvent('Call started.');
|
|
|
|
|
} else if (isProcessing) {
|
|
|
|
|
logEvent('Processing audio...');
|
|
|
|
|
} else {
|
|
|
|
|
logEvent('Call ended.');
|
|
|
|
|
}
|
|
|
|
|
}, [isCalling, isProcessing]);
|
|
|
|
|
|
|
|
|
|
const handleStartCall = () => {
|
|
|
|
|
logEvent('Attempting to start call...');
|
|
|
|
|
startCall();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleHangUp = () => {
|
|
|
|
|
logEvent('Attempting to hang up call...');
|
|
|
|
|
hangUp();
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-21 14:36:01 +01:00
|
|
|
return (
|
|
|
|
|
<OGDialog open={open} onOpenChange={setOpen}>
|
|
|
|
|
<OGDialogContent className="w-96 p-8">
|
|
|
|
|
<div className="flex flex-col items-center gap-6">
|
|
|
|
|
<div
|
|
|
|
|
className={`flex items-center gap-2 rounded-full px-4 py-2 ${
|
|
|
|
|
isConnected ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className={`h-2 w-2 rounded-full ${isConnected ? 'bg-green-500' : 'bg-red-500'}`}
|
|
|
|
|
/>
|
|
|
|
|
<span className="text-sm font-medium">
|
|
|
|
|
{isConnected ? 'Connected' : 'Disconnected'}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-12-30 01:47:21 +01:00
|
|
|
{isCalling ? (
|
2024-12-21 14:36:01 +01:00
|
|
|
<Button
|
2024-12-30 01:47:21 +01:00
|
|
|
onClick={handleHangUp}
|
|
|
|
|
className="flex items-center gap-2 rounded-full bg-red-500 px-6 py-3 text-white hover:bg-red-600"
|
2024-12-21 14:36:01 +01:00
|
|
|
>
|
2024-12-30 01:47:21 +01:00
|
|
|
<PhoneOff size={20} />
|
|
|
|
|
<span>End Call</span>
|
2024-12-21 14:36:01 +01:00
|
|
|
</Button>
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
2024-12-30 01:47:21 +01:00
|
|
|
onClick={handleStartCall}
|
|
|
|
|
disabled={!isConnected}
|
|
|
|
|
className="flex items-center gap-2 rounded-full bg-green-500 px-6 py-3 text-white hover:bg-green-600 disabled:opacity-50"
|
2024-12-21 14:36:01 +01:00
|
|
|
>
|
2024-12-30 01:47:21 +01:00
|
|
|
<Phone size={20} />
|
|
|
|
|
<span>Start Call</span>
|
2024-12-21 14:36:01 +01:00
|
|
|
</Button>
|
|
|
|
|
)}
|
2024-12-30 01:47:21 +01:00
|
|
|
|
|
|
|
|
{/* Debugging Information */}
|
|
|
|
|
<div className="mt-4 w-full rounded-md bg-gray-100 p-4 shadow-sm">
|
|
|
|
|
<h3 className="mb-2 text-lg font-medium">Event Log</h3>
|
|
|
|
|
<ul className="h-32 overflow-y-auto text-xs text-gray-600">
|
|
|
|
|
{eventLog.map((log, index) => (
|
|
|
|
|
<li key={index}>{log}</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
2024-12-21 14:36:01 +01:00
|
|
|
</div>
|
|
|
|
|
</OGDialogContent>
|
|
|
|
|
</OGDialog>
|
|
|
|
|
);
|
|
|
|
|
};
|