Skip to content

Commit

Permalink
Add animejs library and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne committed Feb 15, 2024
1 parent 142e944 commit ad32a70
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 66 deletions.
6 changes: 4 additions & 2 deletions backend/src/routes/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ io.on("connection", (socket) => {
socket.on("users", (roomId) => {
if (roomUsers[roomId]) {
const userIds = roomUsers[roomId];
const userNamesList = userIds.map((userId) => userNames[userId] || 'Anonyme');
const userNamesList = userIds.map(
(userId) => userNames[userId] || "Anonyme"
);
socket.emit("usersList", userNamesList);
} else {
socket.emit("error", `Room with ID: ${roomId} does not exist.`);
Expand Down Expand Up @@ -251,7 +253,7 @@ io.on("connection", (socket) => {
}

io.emit("receive_message", {
text: username + " quit the room. ➡️🚪",
text: username + " as quit the room. ➡️🚪",
socketId: "Kurama-chat",
roomId: roomId,
systemMessage: true,
Expand Down
24 changes: 24 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@types/react-dom": "18.2.4",
"@types/socket.io": "^3.0.2",
"@types/socket.io-client": "^3.0.0",
"animejs": "^3.2.2",
"autoprefixer": "10.4.14",
"clipboard": "^2.0.11",
"core-js-pure": "^3.35.0",
Expand All @@ -41,6 +42,7 @@
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/animejs": "^3.1.12",
"@types/toastr": "^2.1.43",
"@types/uuid": "^9.0.1",
"bufferutil": "^4.0.7",
Expand Down
70 changes: 14 additions & 56 deletions frontend/src/components/Chat/ChatBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import { useSocket } from "@/contexts/SocketContext";
import { useUser } from "@/contexts/UserContext";
import React, { useEffect, useRef, useState } from "react";
import Avatar from "react-avatar";
import ChatImage from "./ChatImage";
import Message from "./messages";

function ChatBody({ roomId }: { roomId: string }) {
const [typing, setTyping] = useState<string>("");
Expand Down Expand Up @@ -82,62 +81,21 @@ function ChatBody({ roomId }: { roomId: string }) {
}, [socket, roomId]);

return (
<div className="basis-[85%] overflow-y-scroll p-5 w-full flex flex-col gap-2 custom-scrollbar rounded-lg">
{messages[roomId]?.map((message: any, index: number) =>
message.systemMessage ? (
<div className="flex self-center" key={index}>
<div className="flex justify-center items-center dark:text-gray-300">
<p>{message.text}</p>
</div>
</div>
) : message.userId === currentUserId ? ( // Compare the message user ID with the current user ID
<div className="flex self-end flex-col items-end" key={index}>
{message.text && (
<div className="flex justify-center items-center px-3 py-1 text-white rounded-full rounded-br-none bg-primary dark:bg-purple-900">
<p className="font-sans">{message.text}</p>
</div>
)}
{message.image && <ChatImage imgURL={message.image} />}
</div>
) : (
<div className="flex gap-2 self-start dark:text-gray-200" key={index}>
<div className="self-center">
<Avatar
name={message.name}
round={true}
size="30"
className="text-sm"
/>
</div>
<div>
<p className="pl-2 text-sm align-bottom">{message.name}</p>
{message.text && (
<div
className={`px-3 py-1 bg-gray-200 rounded-full dark:bg-blue-950 ${
message.image ? "rounded-bl-none" : "rounded-tl-none"
} w-fit`}
>
<p className="font-sans">{message.text}</p>
</div>
)}
{message.image && <ChatImage imgURL={message.image} />}
<p className="py-2 pl-2 text-xs font-light dark:text-white">
{message.date
? new Date(message.date).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
})
: "En direct"}
</p>
</div>
</div>
)
)}
<div ref={lastMessageRef} className="mt-auto text-slate-500">
{typing}
<div style={{ flexGrow: 1, overflowY: "auto" }}>
<div className="basis_[85%] overflow-y-scroll p-5 w-full flex flex-col gap-2 custom-scrollbar rounded-lg">
{messages[roomId]?.map((message: any, index: number) => (
<Message
message={message}
currentUserId={currentUserId ?? ""}
index={index}
messages={messages[roomId]}
/>
))}
<div ref={lastMessageRef} className="mt-auto text-slate-500">
{typing}
</div>
</div>
</div>
);
}

export default ChatBody;
4 changes: 2 additions & 2 deletions frontend/src/components/Chat/ChatFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function ChatFooter({ roomId }: { roomId: string }) {
<img src={image} className="w-full h-full object-contain" />
</div>
)}
<div className="basis-[8%] p-2 flex items-center dark:bg-neutral-800 bg-slate-200 gap-4 rounded-lg">
<div className="basis_[8%] p-2 flex items-center dark:bg-neutral-800 bg-slate-200 gap-4 rounded-lg bottom-0 w-full ">
{message.length === 0 && (
<>
<AiFillPlusCircle
Expand Down Expand Up @@ -319,7 +319,7 @@ function ChatFooter({ roomId }: { roomId: string }) {
<AiFillLike
size={28}
className="cursor-pointer text-red-600"
onClick={(e) => handleSendMessage(e, "👍 <Bon toutou>")}
onClick={(e) => handleSendMessage(e, "👍")}
/>
) : (
<IoMdSend
Expand Down
108 changes: 108 additions & 0 deletions frontend/src/components/Chat/messages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { useSocket } from "@/contexts/SocketContext";
import { useUser } from "@/contexts/UserContext";
import React, { useEffect, useRef, useState } from "react";
import Avatar from "react-avatar";
import ChatImage from "./ChatImage";
import anime from "animejs";
import { convertToObject } from "typescript";

function Message({
messages,
message,
currentUserId,
index,
}: {
messages: any;
message: any;
currentUserId: string;
index: number;
}) {
const messageRef = useRef<HTMLDivElement>(null);
const previousMessage = messages && index > 0 ? messages[index - 1] : null;
const nextMessage =
messages && index < messages.length - 1 ? messages[index + 1] : null;

useEffect(() => {
anime({
targets: messageRef.current,
translateY: [-20, 0],
opacity: [0, 1],
duration: 500,
});
}, [message]);

const showUsername =
!previousMessage ||
previousMessage.userId !== message.userId ||
(previousMessage &&
previousMessage.userId === message.userId &&
Math.abs(
new Date(message.date).getTime() -
new Date(previousMessage.date).getTime()
) >
5 * 60 * 1000);
const showDate = !nextMessage || nextMessage.userId !== message.userId;

return message.systemMessage ? (
<div className="flex self-center" key={index} ref={messageRef}>
<div className="flex justify-center items-center dark:text-gray-300">
<p>{message.text}</p>
</div>
</div>
) : message.userId === currentUserId ? (
<div
className="flex self-end flex-col items-end"
key={index}
ref={messageRef}
>
{message.text && (
<div className="flex justify-center items-center px-3 py-1 text-white rounded-full rounded-br-none bg-primary dark:bg-purple-900">
<p className="font-sans">{message.text}</p>
</div>
)}
{message.image && <ChatImage imgURL={message.image} />}
</div>
) : (
<div
className="flex gap-2 self-start dark:text-gray-200"
key={index}
ref={messageRef}
>
<div className="self-center">
<Avatar
name={message.name}
round={true}
size="30"
className="text-sm"
/>
</div>
<div>
{showUsername && (
<p className="pl-2 text-sm align-bottom">{message.name}</p>
)}
{message.text && (
<div
className={`px-3 py-1 bg-gray-200 rounded-full dark:bg-blue-950 ${
message.image ? "rounded-bl-none" : "rounded-tl-none"
} w-fit`}
>
<p className="font-sans">{message.text}</p>
</div>
)}
{message.image && <ChatImage imgURL={message.image} />}
{showDate && (
<p className="py-2 pl-2 text-xs font-light dark:text-white">
{message.date
? new Date(message.date).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
})
: "En direct"}
</p>
)}
</div>
</div>
);
}

export default Message;
9 changes: 3 additions & 6 deletions frontend/src/components/Home/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ function LoginForm() {
e.preventDefault();
setIsLoading(true);
if (name) {
let userId = localStorage.getItem("userId");
if (!userId) {
userId =
Math.random().toString(36).substring(2) + Date.now().toString(36);
localStorage.setItem("userId", userId);
}
let userId =
Math.random().toString(36).substring(2) + Date.now().toString(36);
localStorage.setItem("userId", userId);
localStorage.setItem("name", name);
setUsername(name);
}
Expand Down

0 comments on commit ad32a70

Please sign in to comment.