Repository: WebDevSimplified/Whatsapp-Clone Branch: master Commit: df4cfbfbfa6a Files: 23 Total size: 21.6 KB Directory structure: gitextract_8mbsqtrl/ ├── client/ │ ├── .gitignore │ ├── README.md │ ├── package.json │ ├── public/ │ │ ├── index.html │ │ ├── manifest.json │ │ └── robots.txt │ └── src/ │ ├── components/ │ │ ├── App.js │ │ ├── Contacts.js │ │ ├── Conversations.js │ │ ├── Dashboard.js │ │ ├── Login.js │ │ ├── NewContactModal.js │ │ ├── NewConversationModal.js │ │ ├── OpenConversation.js │ │ └── Sidebar.js │ ├── contexts/ │ │ ├── ContactsProvider.js │ │ ├── ConversationsProvider.js │ │ └── SocketProvider.js │ ├── hooks/ │ │ └── useLocalStorage.js │ └── index.js └── server/ ├── .gitignore ├── package.json └── server.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: client/.gitignore ================================================ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies /node_modules /.pnp .pnp.js # testing /coverage # production /build # misc .DS_Store .env.local .env.development.local .env.test.local .env.production.local npm-debug.log* yarn-debug.log* yarn-error.log* ================================================ FILE: client/README.md ================================================ This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). ## Available Scripts In the project directory, you can run: ### `npm start` Runs the app in the development mode.
Open [http://localhost:3000](http://localhost:3000) to view it in the browser. The page will reload if you make edits.
You will also see any lint errors in the console. ### `npm test` Launches the test runner in the interactive watch mode.
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. ### `npm run build` Builds the app for production to the `build` folder.
It correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes.
Your app is ready to be deployed! See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. ### `npm run eject` **Note: this is a one-way operation. Once you `eject`, you can’t go back!** If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. ## Learn More You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). To learn React, check out the [React documentation](https://reactjs.org/). ### Code Splitting This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting ### Analyzing the Bundle Size This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size ### Making a Progressive Web App This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app ### Advanced Configuration This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration ### Deployment This section has moved here: https://facebook.github.io/create-react-app/docs/deployment ### `npm run build` fails to minify This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify ================================================ FILE: client/package.json ================================================ { "name": "client", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.5.0", "@testing-library/user-event": "^7.2.1", "bootstrap": "^4.5.2", "react": "^16.13.1", "react-bootstrap": "^1.3.0", "react-dom": "^16.13.1", "react-scripts": "3.4.3", "socket.io-client": "^2.3.0", "uuid": "^8.3.0" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": "react-app" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } } ================================================ FILE: client/public/index.html ================================================ React App
================================================ FILE: client/public/manifest.json ================================================ { "short_name": "React App", "name": "Create React App Sample", "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": ".", "display": "standalone", "theme_color": "#000000", "background_color": "#ffffff" } ================================================ FILE: client/public/robots.txt ================================================ # https://www.robotstxt.org/robotstxt.html User-agent: * Disallow: ================================================ FILE: client/src/components/App.js ================================================ import React from 'react' import Login from './Login' import useLocalStorage from '../hooks/useLocalStorage'; import Dashboard from './Dashboard' import { ContactsProvider } from '../contexts/ContactsProvider' import { ConversationsProvider } from '../contexts/ConversationsProvider'; import { SocketProvider } from '../contexts/SocketProvider'; function App() { const [id, setId] = useLocalStorage('id') const dashboard = ( ) return ( id ? dashboard : ) } export default App; ================================================ FILE: client/src/components/Contacts.js ================================================ import React from 'react' import { ListGroup } from 'react-bootstrap' import { useContacts } from '../contexts/ContactsProvider'; export default function Contacts() { const { contacts } = useContacts() return ( {contacts.map(contact => ( {contact.name} ))} ) } ================================================ FILE: client/src/components/Conversations.js ================================================ import React from 'react' import { ListGroup } from 'react-bootstrap' import { useConversations } from '../contexts/ConversationsProvider'; export default function Conversations() { const { conversations, selectConversationIndex } = useConversations() return ( {conversations.map((conversation, index) => ( selectConversationIndex(index)} active={conversation.selected} > {conversation.recipients.map(r => r.name).join(', ')} ))} ) } ================================================ FILE: client/src/components/Dashboard.js ================================================ import React from 'react' import Sidebar from './Sidebar'; import OpenConversation from './OpenConversation'; import { useConversations } from '../contexts/ConversationsProvider'; export default function Dashboard({ id }) { const { selectedConversation } = useConversations() return (
{selectedConversation && }
) } ================================================ FILE: client/src/components/Login.js ================================================ import React, { useRef } from 'react' import { Container, Form, Button } from 'react-bootstrap' import { v4 as uuidV4 } from 'uuid' export default function Login({ onIdSubmit }) { const idRef = useRef() function handleSubmit(e) { e.preventDefault() onIdSubmit(idRef.current.value) } function createNewId() { onIdSubmit(uuidV4()) } return (
Enter Your Id
) } ================================================ FILE: client/src/components/NewContactModal.js ================================================ import React, { useRef } from 'react' import { Modal, Form, Button } from 'react-bootstrap' import { useContacts } from '../contexts/ContactsProvider' export default function NewContactModal({ closeModal }) { const idRef = useRef() const nameRef = useRef() const { createContact } = useContacts() function handleSubmit(e) { e.preventDefault() createContact(idRef.current.value, nameRef.current.value) closeModal() } return ( <> Create Contact
Id Name
) } ================================================ FILE: client/src/components/NewConversationModal.js ================================================ import React, { useState } from 'react' import { Modal, Form, Button } from 'react-bootstrap' import { useContacts } from '../contexts/ContactsProvider' import { useConversations } from '../contexts/ConversationsProvider' export default function NewConversationModal({ closeModal }) { const [selectedContactIds, setSelectedContactIds] = useState([]) const { contacts } = useContacts() const { createConversation } = useConversations() function handleSubmit(e) { e.preventDefault() createConversation(selectedContactIds) closeModal() } function handleCheckboxChange(contactId) { setSelectedContactIds(prevSelectedContactIds => { if (prevSelectedContactIds.includes(contactId)) { return prevSelectedContactIds.filter(prevId => { return contactId !== prevId }) } else { return [...prevSelectedContactIds, contactId] } }) } return ( <> Create Conversation
{contacts.map(contact => ( handleCheckboxChange(contact.id)} /> ))}
) } ================================================ FILE: client/src/components/OpenConversation.js ================================================ import React, { useState, useCallback } from 'react' import { Form, InputGroup, Button } from 'react-bootstrap' import { useConversations } from '../contexts/ConversationsProvider'; export default function OpenConversation() { const [text, setText] = useState('') const setRef = useCallback(node => { if (node) { node.scrollIntoView({ smooth: true }) } }, []) const { sendMessage, selectedConversation } = useConversations() function handleSubmit(e) { e.preventDefault() sendMessage( selectedConversation.recipients.map(r => r.id), text ) setText('') } return (
{selectedConversation.messages.map((message, index) => { const lastMessage = selectedConversation.messages.length - 1 === index return (
{message.text}
{message.fromMe ? 'You' : message.senderName}
) })}
setText(e.target.value)} style={{ height: '75px', resize: 'none' }} />
) } ================================================ FILE: client/src/components/Sidebar.js ================================================ import React, { useState } from 'react' import { Tab, Nav, Button, Modal } from 'react-bootstrap' import Conversations from './Conversations' import Contacts from './Contacts' import NewContactModal from './NewContactModal' import NewConversationModal from './NewConversationModal' const CONVERSATIONS_KEY = 'conversations' const CONTACTS_KEY = 'contacts' export default function Sidebar({ id }) { const [activeKey, setActiveKey] = useState(CONVERSATIONS_KEY) const [modalOpen, setModalOpen] = useState(false) const conversationsOpen = activeKey === CONVERSATIONS_KEY function closeModal() { setModalOpen(false) } return (
Your Id: {id}
{conversationsOpen ? : }
) } ================================================ FILE: client/src/contexts/ContactsProvider.js ================================================ import React, { useContext } from 'react' import useLocalStorage from '../hooks/useLocalStorage'; const ContactsContext = React.createContext() export function useContacts() { return useContext(ContactsContext) } export function ContactsProvider({ children }) { const [contacts, setContacts] = useLocalStorage('contacts', []) function createContact(id, name) { setContacts(prevContacts => { return [...prevContacts, { id, name }] }) } return ( {children} ) } ================================================ FILE: client/src/contexts/ConversationsProvider.js ================================================ import React, { useContext, useState, useEffect, useCallback } from 'react' import useLocalStorage from '../hooks/useLocalStorage'; import { useContacts } from './ContactsProvider'; import { useSocket } from './SocketProvider'; const ConversationsContext = React.createContext() export function useConversations() { return useContext(ConversationsContext) } export function ConversationsProvider({ id, children }) { const [conversations, setConversations] = useLocalStorage('conversations', []) const [selectedConversationIndex, setSelectedConversationIndex] = useState(0) const { contacts } = useContacts() const socket = useSocket() function createConversation(recipients) { setConversations(prevConversations => { return [...prevConversations, { recipients, messages: [] }] }) } const addMessageToConversation = useCallback(({ recipients, text, sender }) => { setConversations(prevConversations => { let madeChange = false const newMessage = { sender, text } const newConversations = prevConversations.map(conversation => { if (arrayEquality(conversation.recipients, recipients)) { madeChange = true return { ...conversation, messages: [...conversation.messages, newMessage] } } return conversation }) if (madeChange) { return newConversations } else { return [ ...prevConversations, { recipients, messages: [newMessage] } ] } }) }, [setConversations]) useEffect(() => { if (socket == null) return socket.on('receive-message', addMessageToConversation) return () => socket.off('receive-message') }, [socket, addMessageToConversation]) function sendMessage(recipients, text) { socket.emit('send-message', { recipients, text }) addMessageToConversation({ recipients, text, sender: id }) } const formattedConversations = conversations.map((conversation, index) => { const recipients = conversation.recipients.map(recipient => { const contact = contacts.find(contact => { return contact.id === recipient }) const name = (contact && contact.name) || recipient return { id: recipient, name } }) const messages = conversation.messages.map(message => { const contact = contacts.find(contact => { return contact.id === message.sender }) const name = (contact && contact.name) || message.sender const fromMe = id === message.sender return { ...message, senderName: name, fromMe } }) const selected = index === selectedConversationIndex return { ...conversation, messages, recipients, selected } }) const value = { conversations: formattedConversations, selectedConversation: formattedConversations[selectedConversationIndex], sendMessage, selectConversationIndex: setSelectedConversationIndex, createConversation } return ( {children} ) } function arrayEquality(a, b) { if (a.length !== b.length) return false a.sort() b.sort() return a.every((element, index) => { return element === b[index] }) } ================================================ FILE: client/src/contexts/SocketProvider.js ================================================ import React, { useContext, useEffect, useState } from 'react' import io from 'socket.io-client' const SocketContext = React.createContext() export function useSocket() { return useContext(SocketContext) } export function SocketProvider({ id, children }) { const [socket, setSocket] = useState() useEffect(() => { const newSocket = io( 'http://localhost:5000', { query: { id } } ) setSocket(newSocket) return () => newSocket.close() }, [id]) return ( {children} ) } ================================================ FILE: client/src/hooks/useLocalStorage.js ================================================ import { useEffect, useState } from 'react' const PREFIX = 'whatsapp-clone-' export default function useLocalStorage(key, initialValue) { const prefixedKey = PREFIX + key const [value, setValue] = useState(() => { const jsonValue = localStorage.getItem(prefixedKey) if (jsonValue != null) return JSON.parse(jsonValue) if (typeof initialValue === 'function') { return initialValue() } else { return initialValue } }) useEffect(() => { localStorage.setItem(prefixedKey, JSON.stringify(value)) }, [prefixedKey, value]) return [value, setValue] } ================================================ FILE: client/src/index.js ================================================ import React from 'react'; import ReactDOM from 'react-dom'; import App from './components/App'; import 'bootstrap/dist/css/bootstrap.min.css' ReactDOM.render( , document.getElementById('root') ); ================================================ FILE: server/.gitignore ================================================ node_modules ================================================ FILE: server/package.json ================================================ { "name": "server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "devStart": "nodemon server.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "socket.io": "^2.3.0" }, "devDependencies": { "nodemon": "^2.0.4" } } ================================================ FILE: server/server.js ================================================ const io = require('socket.io')(5000) io.on('connection', socket => { const id = socket.handshake.query.id socket.join(id) socket.on('send-message', ({ recipients, text }) => { recipients.forEach(recipient => { const newRecipients = recipients.filter(r => r !== recipient) newRecipients.push(id) socket.broadcast.to(recipient).emit('receive-message', { recipients: newRecipients, sender: id, text }) }) }) })