Skip to content

Commit

Permalink
Improved the incoming call flow
Browse files Browse the repository at this point in the history
  • Loading branch information
PezhmanGhavami committed Jun 11, 2023
1 parent 11f13c3 commit 7360016
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
9 changes: 9 additions & 0 deletions client/src/context/call.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface ICallContext {
localCameraEnabled: boolean;
localMicrophoneEnabled: boolean;
callStarted: boolean;
isConnecting: boolean;
isRecivingCall: boolean;
isCallInitiator: boolean;
endCall: () => void;
Expand All @@ -49,6 +50,7 @@ const callContextInit: ICallContext = {
localCameraEnabled: true,
localMicrophoneEnabled: true,
callStarted: false,
isConnecting: false,
isRecivingCall: false,
isCallInitiator: false,
remoteUser: { displayName: "", id: "", stream: null, signalData: null },
Expand All @@ -66,6 +68,9 @@ const CallProvider = ({ children }: { children: ReactNode }) => {
const [remoteUser, setRemoteUser] = useState({
...callContextInit.remoteUser,
});
const [isConnecting, setIsConnecting] = useState(
callContextInit.isConnecting,
);
const [isRecivingCall, setIsRecivingCall] = useState(
callContextInit.isRecivingCall,
);
Expand Down Expand Up @@ -190,6 +195,7 @@ const CallProvider = ({ children }: { children: ReactNode }) => {

const answerCall = async () => {
console.log("answering call...");
setIsConnecting(true);

const newPeer = new Peer({
initiator: false,
Expand All @@ -216,6 +222,7 @@ const CallProvider = ({ children }: { children: ReactNode }) => {
newPeer.on("connect", () => {
console.log("connected.");
setCallStarted(true);
setIsConnecting(false);
});

newPeer.on("error", (error) => {
Expand All @@ -230,6 +237,7 @@ const CallProvider = ({ children }: { children: ReactNode }) => {
peer?.destroy();
setPeer(null);
setCallStarted(false);
setIsConnecting(false);
setIsRecivingCall(false);
setIsCallInitiator(false);
setRemoteUser({ ...callContextInit.remoteUser });
Expand Down Expand Up @@ -278,6 +286,7 @@ const CallProvider = ({ children }: { children: ReactNode }) => {
localCameraEnabled,
localMicrophoneEnabled,
callStarted,
isConnecting,
isRecivingCall,
isCallInitiator,
endCall,
Expand Down
23 changes: 17 additions & 6 deletions client/src/pages/call/call.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function CallStarted() {

const {
localStream,
isConnecting,
localCameraEnabled,
localMicrophoneEnabled,
remoteUser,
Expand Down Expand Up @@ -111,7 +112,11 @@ function CallStarted() {
{callStarted ? (
<p>{remoteUser.displayName}</p>
) : (
<p>Calling {remoteUser.displayName}...</p>
<p>
{`${isConnecting ? "Connecting to" : "Calling"} ${
remoteUser.displayName
}...`}
</p>
)}
</div>

Expand Down Expand Up @@ -181,12 +186,18 @@ function CallStarted() {
}

function IncomingCall() {
const { localStream, callStarted, remoteUser, endCall, answerCall } =
useContext(CallContext);
const {
isConnecting,
localStream,
callStarted,
remoteUser,
endCall,
answerCall,
} = useContext(CallContext);

useEffect(() => {
const timeout = setTimeout(() => {
if (!callStarted) {
if (!callStarted && !isConnecting) {
endCall();
}
}, 60 * 1000);
Expand Down Expand Up @@ -221,7 +232,7 @@ function IncomingCall() {
}

function Call() {
const { isCallInitiator, isRecivingCall, callStarted } =
const { isConnecting, isCallInitiator, isRecivingCall, callStarted } =
useContext(CallContext);

const navigate = useNavigate();
Expand All @@ -232,7 +243,7 @@ function Call() {
}
}, [isCallInitiator, isRecivingCall, callStarted]);

if (isRecivingCall && !callStarted) {
if (isRecivingCall && !callStarted && !isConnecting) {
return <IncomingCall />;
}

Expand Down

0 comments on commit 7360016

Please sign in to comment.