diff --git a/src/components/Chat2.tsx b/src/components/Chat2.tsx index 54d51b9..eb2f9ce 100644 --- a/src/components/Chat2.tsx +++ b/src/components/Chat2.tsx @@ -6,28 +6,44 @@ import CloseIcon from "./icons/CloseIcon"; import SendChatIcon from "./icons/SendChatIcon"; import Button from "./ui/Button"; import useStreamUserStore from "../stores/useStreamUserStore"; -import { FormEvent, useRef, useState } from "react"; -import IMessage from "../types/IMessage"; -import useStateRef from "react-usestateref"; +import { FormEvent, useEffect, useRef, useState } from "react"; +import useSocketStore from "../stores/useSocketStore"; +import useChatStore from "../stores/useChatStore"; interface Props { onClose: () => void; } function Chat2({ onClose }: Props) { -// const { socket } = useSocketStore(); + const { socket } = useSocketStore(); const { me, users } = useStreamUserStore(); - const [messages] = useStateRef([]); + // const [messages] = useStateRef([]); const messagesRef = useRef(null); const [messageText, setMessageText] = useState(""); const messageTextRef = useRef(null); + const { messages } = useChatStore(); function sendMessage(e: FormEvent) { e.preventDefault(); + + socket?.emit("message", messageText); + + setMessageText(""); } + useEffect(() => { + if (!messages.length) return; + + messagesRef.current?.scrollTo({ + top: messagesRef.current.scrollHeight, + behavior: "smooth", + }); + }, [messages]); + return ( -
+

Чат @@ -47,14 +63,16 @@ function Chat2({ onClose }: Props) {

user.id === me?.id)?.name === message.name + users.find((user) => user.id === me?.id)?.id === message.userId ? "bg-[#C4DDF5]" : "bg-[#F0F1F2]" }`} > - {users.find((user) => user.id === me?.id)?.name !== - message.name && ( -

{message.name}

+ {users.find((user) => user.id === me?.id)?.id !== + message.userId && ( +

+ {users.find((user) => user.id === message.userId)?.name} +

)}

{message.text}

@@ -70,7 +88,7 @@ function Chat2({ onClose }: Props) { ref={messageTextRef} type="text" placeholder={t("writeAMessage")} - className="w-full bg-transparent text-sm outline-none" + className="w-full text-sm bg-transparent outline-none" value={messageText} onChange={(e) => setMessageText(e.target.value)} /> diff --git a/src/components/icons/DesktopIcon.tsx b/src/components/icons/DesktopIcon.tsx index 14b3a3f..2c49848 100644 --- a/src/components/icons/DesktopIcon.tsx +++ b/src/components/icons/DesktopIcon.tsx @@ -9,13 +9,13 @@ function DesktopIcon() { > diff --git a/src/components/icons/MobileIcon.tsx b/src/components/icons/MobileIcon.tsx index d70c5f4..e37d325 100644 --- a/src/components/icons/MobileIcon.tsx +++ b/src/components/icons/MobileIcon.tsx @@ -9,7 +9,7 @@ function MobileIcon() { > diff --git a/src/pages/StreamPage3.tsx b/src/pages/StreamPage3.tsx index 1c960e0..1ebe3e6 100644 --- a/src/pages/StreamPage3.tsx +++ b/src/pages/StreamPage3.tsx @@ -25,7 +25,7 @@ import MicroOffIcon from "../components/icons/MicroOffIcon"; import CameraOnIcon from "../components/icons/CameraOnIcon"; import CameraOffIcon from "../components/icons/CameraOffIcon"; import { Trans } from "react-i18next"; -import { isIOS, useMobileOrientation } from "react-device-detect"; +import { isIOS, isMobile, useMobileOrientation } from "react-device-detect"; import WindowIcon from "../components/icons/WindowIcon"; import FullscreenIcon from "../components/icons/FullscreenIcon"; import ShareIcon from "../components/icons/ShareIcon"; @@ -39,6 +39,10 @@ import useStreamUserStore from "../stores/useStreamUserStore"; import Chat2 from "../components/Chat2"; import Rotate64Icon from "../components/icons/Rotate64Icon"; import Draggable from "react-draggable"; +import DesktopIcon from "../components/icons/DesktopIcon"; +import MobileIcon from "../components/icons/MobileIcon"; +import ChatIcon from "../components/icons/ChatIcon"; +import useChatStore from "../stores/useChatStore"; // import ChatIcon from "../components/icons/ChatIcon"; // import MoreIcon from "../components/icons/MoreIcon"; @@ -79,6 +83,7 @@ function StreamPage3() { const [step, setStep] = useState(1); const [isShowChat, setIsShowChat] = useState(false); const { isPortrait } = useMobileOrientation(); + const { setMessages } = useChatStore(); async function startCall(remotePeerId: string) { if (!peerInstance) return; @@ -131,6 +136,7 @@ function StreamPage3() { function initPeer() { const peer = new Peer({ + host: "stream.graff.tech", config: { iceServers: [ { @@ -212,6 +218,10 @@ function StreamPage3() { toast.info(`Вы получили разрешение на управление`); }); + socket.on("message", ({ userId, text }) => { + setMessages([...useChatStore.getState().messages, { userId, text }]); + }); + socket.on("connect", () => { setSocket(socket); }); @@ -267,7 +277,7 @@ function StreamPage3() { setIsEnded(false); setWSUrl( - `wss://${activeSession.location}.sess.stream.graff.tech/${activeSession.name}/${activeSession.cirrusPort}/` + `wss://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:${activeSession.cirrusPort}` ); setModal(); @@ -310,6 +320,7 @@ function StreamPage3() { useEffect(() => { getWSUrl(); + // initSocket(); }, []); useEffect(() => { @@ -347,16 +358,16 @@ function StreamPage3() { > {isEnded === false && ( <> -
+
-
+

{name[0]?.toUpperCase()} @@ -366,8 +377,11 @@ function StreamPage3() { )}

{name}

+
+ {isMobile ? : } +
-
+
*/} +
-
+
{WSUrl && ( -
+