diff --git a/package-lock.json b/package-lock.json index b9b2d12..0b56755 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "dependencies": { "@auth/prisma-adapter": "^2.1.0", + "@chakra-ui/icons": "^2.1.1", "@chakra-ui/next-js": "^2.1.2", "@chakra-ui/react": "^2.5.5", "@emotion/react": "^11.10.6", @@ -631,6 +632,30 @@ "react": ">=18" } }, + "node_modules/@chakra-ui/icons": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/@chakra-ui/icons/-/icons-2.1.1.tgz", + "integrity": "sha512-3p30hdo4LlRZTT5CwoAJq3G9fHI0wDc0pBaMHj4SUn0yomO+RcDRlzhdXqdr5cVnzax44sqXJVnf3oQG0eI+4g==", + "dependencies": { + "@chakra-ui/icon": "3.2.0" + }, + "peerDependencies": { + "@chakra-ui/system": ">=2.0.0", + "react": ">=18" + } + }, + "node_modules/@chakra-ui/icons/node_modules/@chakra-ui/icon": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/@chakra-ui/icon/-/icon-3.2.0.tgz", + "integrity": "sha512-xxjGLvlX2Ys4H0iHrI16t74rG9EBcpFvJ3Y3B7KMQTrnW34Kf7Da/UC8J67Gtx85mTHW020ml85SVPKORWNNKQ==", + "dependencies": { + "@chakra-ui/shared-utils": "2.0.5" + }, + "peerDependencies": { + "@chakra-ui/system": ">=2.0.0", + "react": ">=18" + } + }, "node_modules/@chakra-ui/image": { "version": "2.0.15", "resolved": "https://registry.npmjs.org/@chakra-ui/image/-/image-2.0.15.tgz", diff --git a/package.json b/package.json index ce7cea7..62e4570 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ }, "dependencies": { "@auth/prisma-adapter": "^2.1.0", + "@chakra-ui/icons": "^2.1.1", "@chakra-ui/next-js": "^2.1.2", "@chakra-ui/react": "^2.5.5", "@emotion/react": "^11.10.6", diff --git a/pages/Chatnow.tsx b/pages/Chatnow.tsx index 7541bfb..a67f5fd 100644 --- a/pages/Chatnow.tsx +++ b/pages/Chatnow.tsx @@ -1,178 +1,170 @@ import React, { useState, useEffect } from "react"; -import { v4 as uuidv4 } from "uuid"; import { Box, Container, - Hero, Text, Button, Input, VStack, HStack, - useColorModeValue, -} from "@/lib/ui"; -import Head from "next/head"; -import Image from "next/image"; -import { NextPage } from "next"; -import chatBackground from "@/public/assets/heroImages/chatBackground.svg"; // Adjust according to your actual path + IconButton, +} from "@chakra-ui/react"; +import { AddIcon, CloseIcon } from "@chakra-ui/icons"; +import { v4 as uuidv4 } from "uuid"; interface Message { text: string; from: "user" | "system"; } -const ChatNow: NextPage = () => { - const [interest, setInterest] = useState(""); +const ChatNow = () => { + const [interests, setInterests] = useState([""]); const [inChat, setInChat] = useState(false); const [messages, setMessages] = useState([]); const [message, setMessage] = useState(""); const [ws, setWs] = useState(null); const [matchId, setMatchId] = useState(null); + const [loading, setLoading] = useState(false); useEffect(() => { if (inChat && !ws) { - const websocket = new WebSocket("ws://localhost:3001"); // Adjust to your server address + const websocket = new WebSocket("ws://localhost:8080/ws"); // Adjust to your Go server address + const userId = uuidv4(); websocket.onopen = () => { websocket.send( - JSON.stringify({ type: "register", id: userId, interest }) + JSON.stringify({ type: "register", id: userId, interests }) ); + setLoading(true); }; websocket.onmessage = (event) => { const data = JSON.parse(event.data); if (data.type === "match") { setMatchId(data.id); + setLoading(false); + setMessages([ + { text: "You've been matched! Start chatting.", from: "system" }, + ]); } else if (data.type === "message") { const receivedMessage: Message = { text: data.text, from: "system" }; setMessages((prevMessages) => [...prevMessages, receivedMessage]); - } else if (data.type === "system") { - const systemMessage: Message = { text: data.text, from: "system" }; - setMessages((prevMessages) => [...prevMessages, systemMessage]); } }; - websocket.onclose = () => + websocket.onclose = () => { console.log("Disconnected from WebSocket server"); + setLoading(false); + setInChat(false); + }; setWs(websocket); } - }, [inChat, ws, interest]); + }, [inChat, ws, interests]); const handleStartChat = () => { - setInChat(true); - setMessages([ - { - text: "Looking for someone to chat about " + interest + "...", - from: "system", - }, - ]); + if (interests.some((interest) => interest.trim() !== "")) { + setInChat(true); + setLoading(true); + } }; const handleMessageSend = () => { - if (message.trim() !== "" && matchId) { + if (message.trim() !== "" && matchId && ws) { const userMessage: Message = { text: message, from: "user" }; setMessages([...messages, userMessage]); - ws?.send(JSON.stringify({ type: "message", to: matchId, text: message })); - setMessage(""); // Clear input after send + ws.send(JSON.stringify({ type: "message", to: matchId, text: message })); + setMessage(""); } }; const handleEndChat = () => { if (ws) { ws.send(JSON.stringify({ type: "end" })); - setMessages([{ text: "Looking for a new match...", from: "system" }]); + ws.close(); + setWs(null); + setMatchId(null); + setLoading(false); + setInChat(false); + setMessages([]); + } + }; + + const addInterest = () => { + if (interests.length < 10) { + setInterests([...interests, ""]); } }; - const bgUser = useColorModeValue("blue.100", "blue.900"); - const bgSystem = useColorModeValue("gray.200", "gray.700"); + const removeInterest = (index: number) => { + if (interests.length > 1) { + const updatedInterests = interests.filter((_, i) => i !== index); + setInterests(updatedInterests); + } + }; return ( - <> - - ChatNow - Connect Instantly - - - + + {!inChat ? ( - - - Enter Your Interest - - setInterest(e.target.value)} - mb={6} - /> - - + // UI to enter interests + + {interests.map((interest, index) => ( + + { + let newInterests = [...interests]; + newInterests[index] = e.target.value; + setInterests(newInterests); + }} + placeholder="Enter interest" + /> + } + onClick={() => removeInterest(index)} + /> + + ))} + {interests.length < 10 && ( + + )} + + + ) : loading ? ( + // Loading message + Loading, searching for a match... ) : ( - + // Chat interface + {messages.map((msg, index) => ( - - {msg.text} - + {msg.text} ))} - + setMessage(e.target.value)} - onKeyDown={(e: React.KeyboardEvent) => { - if (e.key === "Enter") { - handleMessageSend(); - } - }} - size="sm" - flexGrow={1} - h="36px" // Reduced height of the input box + placeholder="Type a message..." + flex="1" /> - + )} - - + + ); }; diff --git a/yarn.lock b/yarn.lock index 2d582a9..5edbc5f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -321,6 +321,20 @@ dependencies: "@chakra-ui/shared-utils" "2.0.5" +"@chakra-ui/icon@3.2.0": + version "3.2.0" + resolved "https://registry.npmjs.org/@chakra-ui/icon/-/icon-3.2.0.tgz" + integrity sha512-xxjGLvlX2Ys4H0iHrI16t74rG9EBcpFvJ3Y3B7KMQTrnW34Kf7Da/UC8J67Gtx85mTHW020ml85SVPKORWNNKQ== + dependencies: + "@chakra-ui/shared-utils" "2.0.5" + +"@chakra-ui/icons@^2.1.1": + version "2.1.1" + resolved "https://registry.npmjs.org/@chakra-ui/icons/-/icons-2.1.1.tgz" + integrity sha512-3p30hdo4LlRZTT5CwoAJq3G9fHI0wDc0pBaMHj4SUn0yomO+RcDRlzhdXqdr5cVnzax44sqXJVnf3oQG0eI+4g== + dependencies: + "@chakra-ui/icon" "3.2.0" + "@chakra-ui/image@2.0.15": version "2.0.15" resolved "https://registry.npmjs.org/@chakra-ui/image/-/image-2.0.15.tgz"