Skip to content

Commit

Permalink
finished basic web
Browse files Browse the repository at this point in the history
  • Loading branch information
MartsTech committed Mar 10, 2021
1 parent 01c0b8a commit a766e39
Show file tree
Hide file tree
Showing 15 changed files with 429 additions and 159 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"react-dom": "^17.0.1",
"react-firebase-hooks": "^2.2.0",
"react-redux": "^7.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"react-spinkit": "^3.0.0",
"styled-components": "^5.2.1"
},
"scripts": {
Expand All @@ -40,7 +40,7 @@
"@types/react": "^17.0.2",
"@types/react-dom": "^17.0.1",
"@types/react-redux": "^7.1.16",
"@types/react-router-dom": "^5.1.7",
"@types/react-spinkit": "^3.0.6",
"@types/styled-components": "^5.1.7",
"typescript": "^4.2.2"
}
Expand Down
Binary file modified public/favicon.ico
Binary file not shown.
32 changes: 3 additions & 29 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,13 @@
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<meta name="description" content="Slack clone" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/favicon.ico" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React Redux App</title>
<title>Slack Clone</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file removed public/logo192.png
Binary file not shown.
Binary file removed public/logo512.png
Binary file not shown.
14 changes: 2 additions & 12 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"short_name": "Slack Clone",
"name": "Slack clone",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
Expand Down
128 changes: 128 additions & 0 deletions src/components/Chat.tsx
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;
`;
86 changes: 86 additions & 0 deletions src/components/ChatInput.tsx
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;
}
`;
10 changes: 9 additions & 1 deletion src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ import AccessTimeIcon from "@material-ui/icons/AccessTime";
import HelpOutlinedIcon from "@material-ui/icons/HelpOutline";
import SearchIcon from "@material-ui/icons/Search";
import React from "react";
import { useAuthState } from "react-firebase-hooks/auth";
import { auth } from "../firebase";
import styled from "styled-components";

export const Header: React.FC = () => {
const [user] = useAuthState(auth);

return (
<HeaderContainer>
<HeaderLeft>
<HeaderAvatar alt="avatar" />
<HeaderAvatar
onClick={() => auth.signOut()}
src={user?.photoURL}
alt={user?.displayName}
/>
<AccessTimeIcon />
</HeaderLeft>
<HeaderSearch>
Expand Down
51 changes: 51 additions & 0 deletions src/components/Message.tsx
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;
}
`;
6 changes: 4 additions & 2 deletions src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import styled from "styled-components";
import { SidebarOption } from "./SidebarOption";
import { StatusBadge } from "./StatusBadge";
import { useCollection } from "react-firebase-hooks/firestore";
import { db } from "../firebase";
import { auth, db } from "../firebase";
import { useAuthState } from "react-firebase-hooks/auth";

export const Sidebar: React.FC = () => {
const [user] = useAuthState(auth);
const [channels] = useCollection(db.collection("rooms"));

return (
Expand All @@ -28,7 +30,7 @@ export const Sidebar: React.FC = () => {
<SidebarStatus>
<StatusBadge />
</SidebarStatus>
Martin Velkov
{user?.displayName}
</h3>
</SidebarInfo>
<CreateIcon />
Expand Down
Loading

0 comments on commit a766e39

Please sign in to comment.