-
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.
- Loading branch information
1 parent
0e70150
commit 57090f7
Showing
18 changed files
with
232 additions
and
52 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
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,11 @@ | ||
import React from "react"; | ||
import { Outlet, Navigate } from "react-router-dom"; | ||
import {authtoken} from "../constants" | ||
|
||
export const ProtectedPages = () => { | ||
return authtoken ? <Outlet /> : <Navigate to={"/"} />; | ||
}; | ||
|
||
export const ProtectedAuthPages = () => { | ||
return authtoken ? <Navigate to={"/chat"} /> : <Outlet />; | ||
}; |
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 @@ | ||
export const authtoken = localStorage.getItem("authtoken") |
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,30 @@ | ||
import React, { useContext, useEffect, useState } from "react"; | ||
import { io } from "socket.io-client"; | ||
|
||
const SocketContext = React.createContext(null); | ||
|
||
export const useSocket = () => { | ||
const state = useContext(SocketContext); | ||
if (!state) throw new Error(`state is undefined`); | ||
return state; | ||
}; | ||
|
||
// useCallback hook to prevent unneccesary rerender | ||
|
||
export const SocketProvider = ({ children }) => { | ||
const [socket, setSocket] = useState(null); | ||
useEffect(() => { | ||
const _socket = io(process.env.REACT_APP_SOCKET_URL); | ||
setSocket(_socket); | ||
return () => { | ||
_socket.disconnect(); | ||
setSocket(undefined); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<SocketContext.Provider value={{socket}}> | ||
{children} | ||
</SocketContext.Provider> | ||
); | ||
}; |
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,23 @@ | ||
import React, { useContext, useState } from "react"; | ||
import { decodeToken } from "react-jwt"; | ||
import { authtoken } from "../constants"; | ||
|
||
const UserContext = React.createContext(null); | ||
|
||
export const useUserContext = () => { | ||
const state = useContext(UserContext); | ||
if (!state) throw new Error(`state is undefined`); | ||
return state; | ||
}; | ||
|
||
// useCallback hook to prevent unneccesary rerender | ||
|
||
export const UserDetails = ({ children }) => { | ||
console.log(decodeToken(authtoken)); | ||
const [user, setUser] = useState(decodeToken(authtoken)); | ||
return ( | ||
<UserContext.Provider value={{ user, setUser }}> | ||
{children} | ||
</UserContext.Provider> | ||
); | ||
}; |
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,95 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { useRef } from "react"; | ||
import moment from "moment"; | ||
import { Container } from "@nextui-org/react"; | ||
import { decodeToken } from "react-jwt"; | ||
import { authtoken } from "../../constants"; | ||
import { SocketProvider, useSocket } from "../../context/Socket"; | ||
import { useUserContext } from "../../context/UserDetails"; | ||
import MessageForm from "../../components/MessageForm"; | ||
import { useParams } from "react-router-dom"; | ||
const Index = ({ }) => { | ||
const { socket } = useSocket(); | ||
const { user } = useUserContext(); | ||
const [msg, setMsg] = useState(""); | ||
const { receiverId } = useParams(); | ||
const [msgRec,setmsgRec] = useState([]) | ||
const msgref = useRef(); | ||
const scrolltoView = () => { | ||
let windowHeight = window.innerHeight; | ||
if (msgref.current?.lastChild) { | ||
msgref.current?.lastChild.scrollIntoView() || | ||
window.scrollTo(0, windowHeight * windowHeight); | ||
} | ||
}; | ||
const tapToScroll = (event) => { | ||
if (msgref.current?.contains(event.target)) { | ||
scrolltoView(); | ||
} | ||
}; | ||
useEffect(() => { | ||
scrolltoView(); | ||
}, [msgRec, msgref.current?.lastChild]); | ||
|
||
const sendMsg = async () => { | ||
socket.emit("send_personal_msg", { msg, Usrname:user.name, receiverId, id: user.id }); | ||
setMsg(""); | ||
}; | ||
useEffect(() => { | ||
if (!socket) { | ||
return; | ||
} | ||
// Send message to specific user | ||
// socket.on("send_personal_msg", async (data) => { | ||
// const { id, msg, Usrname } = data; | ||
// let socketIdTosend = onlineUser.get(id); | ||
// const newMsg = new Msg(data); | ||
// await newMsg.save(); | ||
// }); | ||
|
||
socket.on("received_personal_msg", (data) => { | ||
console.log(data); | ||
setmsgRec((prev) => [...prev, data]); | ||
}); | ||
return () => {}; | ||
}, [socket]); | ||
|
||
return ( | ||
<> | ||
<div className="home-page"> | ||
<Container className=" message-container"> | ||
<div | ||
ref={msgref} | ||
className="my-1 msg-container" | ||
id="message-container-child" | ||
onClick={tapToScroll} | ||
> | ||
{msgRec?.map((data, index) => { | ||
return ( | ||
<div | ||
className={ | ||
data?.id === user?.id | ||
? "outgoing-msg" | ||
: "upcoming-message" | ||
} | ||
key={index} | ||
> | ||
<span className="recMsgusername" style={{ fontSize: "12px" }}> | ||
{data?.Usrname} | ||
</span> | ||
<li> {data?.msg}</li> | ||
<span className="messageTimestamp"> | ||
{moment(data?.timeStamp).format("LTS").toString()} | ||
</span> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</Container> | ||
</div> | ||
<MessageForm sendMsg={sendMsg} setMsg={setMsg} msg={msg} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default Index; |
Oops, something went wrong.