Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing Feature #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions @types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ export const enum IOEvents {
newMessage = "newMessage",
/** Number of connected clients updated */
clientsCount = "clientsCount",

typing = 'typing',

stoppedTyping = 'stopppedTyping',


}
39 changes: 37 additions & 2 deletions client/chat/ChatInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ function ChatInterface() {
const [textMessage, setTextMessage] = useState("");
const [messages, setMessages] = useState<any[]>([]);
const [notification, setNotification] = useState<string | null>(null);
const [isTyping, setIsTyping] = useState(false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using the isTyping to render a UI component?




let typingTimer: NodeJS.Timeout | null = null;

const typingStartedHandler = () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting is way off, can you please keep it consistent. You can use a plugin called prettier to format

setIsTyping(true)
clearTimeout(typingTimer);
typingTimer = setTimeout(() => {
setIsTyping(false);
}, 5000); // Adjust the debounce time as needed
};

const typingStoppedHandler = () => {


setIsTyping(false);

};


useEffect(() => {
socket.emit(IOEvents.joinRoom);
Expand All @@ -30,18 +51,22 @@ function ChatInterface() {
const leftRoomHandler = ({ userIds }: { userIds: string[] }) => {
if (userIds.length === 1) {
setIsWaiting(true);
setNotification("User left chat room")
setNotification("User left chat room");
}
};

socket.on(IOEvents.newMessage, newMessageHandler);
socket.on(IOEvents.joinedRoom, joinedRoomHandler);
socket.on(IOEvents.leftRoom, leftRoomHandler);
socket.on(IOEvents.typing, typingStartedHandler);
socket.on(IOEvents.stoppedTyping, typingStoppedHandler);

return () => {
socket.off(IOEvents.newMessage, newMessageHandler);
socket.off(IOEvents.joinedRoom, leftRoomHandler);
socket.off(IOEvents.leftRoom, joinedRoomHandler);
socket.off(IOEvents.typing, typingStartedHandler);
socket.off(IOEvents.stoppedTyping, typingStoppedHandler);
};
}, []);

Expand Down Expand Up @@ -120,13 +145,23 @@ function ChatInterface() {
</ul>

<form method="POST" action="/chat" onSubmit={sendMessage}>
{isTyping && <p>Other user is typing...</p>}

<div className="field has-addons block">
<p className="control is-expanded">
<input
type="text"
name="message"
value={textMessage}
onChange={({ target: { value } }) => setTextMessage(value)}
onChange={({ target: { value } }) => {
setTextMessage(value);
if (value.trim() !== "") {
typingStartedHandler();
} else {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird extra spacing here

typingStoppedHandler();
}
}}
className="input"
/>
</p>
Expand Down