forked from Dun-sin/Whisper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into implement-profile-picture-Dun-sin#343
- Loading branch information
Showing
10 changed files
with
132 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
VITE_IMPORTANT=test-d4a1c830-a65f-4c43-a871-abafcc31066d | ||
VITE_SOCKET_URL="http://localhost:4000" | ||
VITE_SECRET_KEY=decryption101 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/* | ||
* {timeInMilliseconds} is the time to track e.g 5 o'clock | ||
* {timeToCheck} is the time to track against also in ms e.g 3 minutes | ||
*/ | ||
export default (timeInMilliseconds, timeToCheck) => { | ||
const [timePassed, setTimePassed] = useState(false); | ||
const [timer, setTimer] = useState(null); | ||
|
||
// Function to clear the timer externally | ||
const clearTimer = () => { | ||
clearInterval(timer); | ||
return { timePassed: false, clearTimer }; | ||
}; | ||
|
||
useEffect(() => { | ||
if (!timeInMilliseconds) { | ||
return; | ||
} | ||
|
||
// check if the time if it's greater or equal to the `timeToCheck` | ||
const checkTime = () => { | ||
const currentTime = Date.now(); | ||
const timeDifference = currentTime - timeInMilliseconds; | ||
setTimePassed(timeDifference >= timeToCheck); | ||
}; | ||
|
||
// set the interval to state | ||
setTimer(setInterval(checkTime, 1000)); | ||
}, [timeInMilliseconds]); | ||
|
||
if (!timeInMilliseconds) { | ||
return { timePassed: false, clearTimer }; | ||
} | ||
|
||
return { timePassed, clearTimer }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { createBrowserNotification } from 'src/lib/browserNotification'; | ||
import useCheckTimePassed from './useCheckTimePassed'; | ||
import { isGreaterThan3Minutes } from 'src/lib/chatHelper'; | ||
|
||
const inactiveTimeThreshold = 180000; | ||
|
||
export default (getLastMessage, amITheSender) => { | ||
const [lastMessageTime, setLastMessageTime] = useState(null); | ||
const { timePassed, clearTimer } = useCheckTimePassed(lastMessageTime, inactiveTimeThreshold); | ||
|
||
|
||
useEffect(() => { | ||
if ( | ||
amITheSender === null | ||
|| amITheSender === undefined | ||
|| !getLastMessage) { | ||
return | ||
} | ||
|
||
// get the time of the last message | ||
const newMessageTime = getLastMessage.time; | ||
|
||
// if the lastMessageTime stored in state is the same as | ||
// the newmessagetime then we return | ||
// if the newMessageTime is greater than 3 minutes we also return | ||
// this is so that the hook doesn't always run on render | ||
if (newMessageTime === lastMessageTime | ||
|| isGreaterThan3Minutes(inactiveTimeThreshold, newMessageTime) | ||
) { | ||
return; | ||
} | ||
|
||
setLastMessageTime(newMessageTime); | ||
}, [getLastMessage]); | ||
|
||
|
||
useEffect(() => { | ||
if (timePassed) { | ||
if (amITheSender) { | ||
createBrowserNotification('Inactive Chat', `Your Partner isn't responding, want to leave?`) | ||
} else if (!amITheSender) { | ||
createBrowserNotification('Inactive Chat', `You haven't replied your partner yet`) | ||
} else { | ||
return | ||
} | ||
|
||
clearTimer(); | ||
setLastMessageTime(null); | ||
} | ||
}, [timePassed]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters