-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
15 changed files
with
429 additions
and
159 deletions.
There are no files selected for viewing
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
Binary file not shown.
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
Binary file not shown.
Binary file not shown.
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,128 @@ | ||
import InfoOutlinedIcon from "@material-ui/icons/InfoOutlined"; | ||
import StarBorderOutlinedIcon from "@material-ui/icons/StarBorderOutlined"; | ||
import React, { useEffect, useRef } from "react"; | ||
import { useCollection, useDocument } from "react-firebase-hooks/firestore"; | ||
import { useSelector } from "react-redux"; | ||
import styled from "styled-components"; | ||
import { selectRoomId } from "../features/roomSlice"; | ||
import { db } from "../firebase"; | ||
import { MessageContent } from "../types"; | ||
import { ChatInput } from "./ChatInput"; | ||
import { Message } from "./Message"; | ||
|
||
export const Chat: React.FC = () => { | ||
const chatRef = useRef<HTMLDivElement>(null); | ||
|
||
const roomId = useSelector(selectRoomId); | ||
|
||
const [roomDetails] = useDocument( | ||
roomId && db.collection("rooms").doc(roomId) | ||
); | ||
|
||
const [roomMessages, loading] = useCollection( | ||
roomId && | ||
db | ||
.collection("rooms") | ||
.doc(roomId) | ||
.collection("messages") | ||
.orderBy("timestamp", "asc") | ||
); | ||
|
||
useEffect(() => { | ||
chatRef?.current?.scrollIntoView({ | ||
behavior: "smooth", | ||
}); | ||
}, [roomId, loading]); | ||
|
||
return ( | ||
<ChatContainer> | ||
{roomDetails && roomMessages && ( | ||
<> | ||
<ChatHeader> | ||
<ChatHeaderLeft> | ||
<h4> | ||
<strong>#{roomDetails?.data().name}</strong> | ||
<StarBorderOutlinedIcon /> | ||
</h4> | ||
</ChatHeaderLeft> | ||
<ChatHeaderRight> | ||
<p> | ||
<InfoOutlinedIcon /> Details | ||
</p> | ||
</ChatHeaderRight> | ||
</ChatHeader> | ||
<ChatMessages> | ||
{roomMessages?.docs.map((doc: MessageContent) => { | ||
const { message, timestamp, user, userImg } = doc.data(); | ||
|
||
return ( | ||
<Message | ||
key={doc.id} | ||
message={message} | ||
timestamp={timestamp} | ||
user={user} | ||
userImg={userImg} | ||
/> | ||
); | ||
})} | ||
<ChatBottom ref={chatRef} /> | ||
</ChatMessages> | ||
<ChatInput | ||
chatRef={chatRef} | ||
channelId={roomId} | ||
channelName={roomDetails?.data().name} | ||
/> | ||
</> | ||
)} | ||
</ChatContainer> | ||
); | ||
}; | ||
|
||
const ChatContainer = styled.div` | ||
flex: 0.7; | ||
flex-grow: 1; | ||
overflow-y: scroll; | ||
margin-top: 70px; | ||
`; | ||
|
||
const ChatHeader = styled.div` | ||
display: flex; | ||
justify-content: space-between; | ||
padding: 20px; | ||
border-bottom: 1px solid lightgray; | ||
`; | ||
|
||
const ChatHeaderLeft = styled.div` | ||
display: flex; | ||
align-items: center; | ||
> h4 { | ||
display: flex; | ||
text-transform: lowercase; | ||
margin-right: 1px; | ||
} | ||
> h4 > svg { | ||
margin-left: 10px; | ||
font-style: 18px; | ||
} | ||
`; | ||
|
||
const ChatHeaderRight = styled.div` | ||
> p { | ||
display: flex; | ||
align-items: center; | ||
font-size: 14px; | ||
} | ||
> p > svg { | ||
margin-right: 5px; | ||
font-size: 16px; | ||
} | ||
`; | ||
|
||
const ChatMessages = styled.div``; | ||
|
||
const ChatBottom = styled.div` | ||
padding-bottom: 200px; | ||
`; |
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,86 @@ | ||
import { Button } from "@material-ui/core"; | ||
import React, { useRef } from "react"; | ||
import { auth, db } from "../firebase"; | ||
import styled from "styled-components"; | ||
import firebase from "firebase/app"; | ||
import { useAuthState } from "react-firebase-hooks/auth"; | ||
|
||
interface ChatInputProps { | ||
chatRef: React.RefObject<HTMLDivElement>; | ||
channelId: string | null; | ||
channelName: string; | ||
} | ||
|
||
export const ChatInput: React.FC<ChatInputProps> = ({ | ||
chatRef, | ||
channelId, | ||
channelName, | ||
}) => { | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const [user] = useAuthState(auth); | ||
|
||
const sendMessage = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { | ||
e.preventDefault(); | ||
|
||
if (!channelId || !inputRef.current) { | ||
return; | ||
} | ||
|
||
if (inputRef.current.value === "") { | ||
return; | ||
} | ||
|
||
db.collection("rooms").doc(channelId).collection("messages").add({ | ||
message: inputRef.current.value, | ||
timestamp: firebase.firestore.FieldValue.serverTimestamp(), | ||
user: user?.displayName, | ||
userImg: user?.photoURL, | ||
}); | ||
|
||
chatRef.current?.scrollIntoView({ | ||
behavior: "smooth", | ||
}); | ||
|
||
inputRef.current.value = ""; | ||
}; | ||
|
||
return ( | ||
<ChatInputCointainer> | ||
<form> | ||
<input | ||
ref={inputRef} | ||
type="text" | ||
placeholder={`Message #${channelName}`} | ||
/> | ||
<Button hidden type="submit" onClick={sendMessage}> | ||
SEND | ||
</Button> | ||
</form> | ||
</ChatInputCointainer> | ||
); | ||
}; | ||
|
||
const ChatInputCointainer = styled.div` | ||
border-radius: 20px; | ||
> form { | ||
position: relative; | ||
display: flex; | ||
justify-content: center; | ||
} | ||
> form > input { | ||
position: fixed; | ||
bottom: 30px; | ||
width: 60%; | ||
border: 1px solid gray; | ||
border-radius: 3px; | ||
padding: 20px; | ||
outline: none; | ||
} | ||
> form > button { | ||
display: none !important; | ||
} | ||
`; |
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,51 @@ | ||
import React from "react"; | ||
import styled from "styled-components"; | ||
|
||
interface MessageProps { | ||
message: string; | ||
timestamp: any; | ||
user: string; | ||
userImg: string | null; | ||
} | ||
|
||
export const Message: React.FC<MessageProps> = ({ | ||
message, | ||
timestamp, | ||
user, | ||
userImg, | ||
}) => { | ||
return ( | ||
<MessageContainer> | ||
<img src={userImg || ""} alt="profile" /> | ||
<MessageInfo> | ||
<h4> | ||
{user} | ||
<span>{new Date(timestamp?.toDate()).toUTCString()}</span> | ||
</h4> | ||
<p>{message}</p> | ||
</MessageInfo> | ||
</MessageContainer> | ||
); | ||
}; | ||
|
||
const MessageContainer = styled.div` | ||
display: flex; | ||
align-items: center; | ||
padding: 20px; | ||
> img { | ||
height: 50px; | ||
border-radius: 8px; | ||
} | ||
`; | ||
|
||
const MessageInfo = styled.div` | ||
padding-left: 10px; | ||
> h4 > span { | ||
color: gray; | ||
font-weight: 300; | ||
margin-left: 4px; | ||
font-size: 10px; | ||
} | ||
`; |
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
Oops, something went wrong.