├── src ├── react-app-env.d.ts ├── App.scss ├── app │ ├── hooks.ts │ └── store.ts ├── Types.ts ├── components │ ├── Message.scss │ ├── ErrorFallBack.tsx │ ├── SidebarChannel.scss │ ├── Login.scss │ ├── Login.tsx │ ├── Message.tsx │ ├── Chat.scss │ ├── ChatHeader.scss │ ├── ChatHeader.tsx │ ├── SidebarChannle.tsx │ ├── Sidebar.scss │ ├── Sidebar.tsx │ └── Chat.tsx ├── index.tsx ├── index.scss ├── features │ ├── userSlice.ts │ └── appSlice.ts ├── firebase.ts ├── hooks │ ├── useFirebase.tsx │ └── useSubCollection.tsx └── App.tsx ├── .firebaserc ├── public ├── icon.png ├── favicon.ico ├── logo192.png ├── logo512.png ├── robots.txt ├── discordLogo.png ├── manifest.json └── index.html ├── firebase.json ├── .gitignore ├── tsconfig.json ├── .firebase └── hosting.YnVpbGQ.cache ├── package.json └── README.md /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/App.scss: -------------------------------------------------------------------------------- 1 | .App { 2 | display: flex; 3 | overflow-y: hidden; 4 | } 5 | -------------------------------------------------------------------------------- /.firebaserc: -------------------------------------------------------------------------------- 1 | { 2 | "projects": { 3 | "default": "discord-clone-83b08" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /public/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shin-sibainu/discord-clone/HEAD/public/icon.png -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shin-sibainu/discord-clone/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shin-sibainu/discord-clone/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shin-sibainu/discord-clone/HEAD/public/logo512.png -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/discordLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shin-sibainu/discord-clone/HEAD/public/discordLogo.png -------------------------------------------------------------------------------- /src/app/hooks.ts: -------------------------------------------------------------------------------- 1 | import { TypedUseSelectorHook, useSelector, useDispatch } from "react-redux"; 2 | import { AppDispatch, RootState } from "./store"; 3 | 4 | export const useAppDispatch: () => AppDispatch = useDispatch; 5 | export const useAppSelector: TypedUseSelectorHook = useSelector; 6 | -------------------------------------------------------------------------------- /firebase.json: -------------------------------------------------------------------------------- 1 | { 2 | "hosting": { 3 | "public": "build", 4 | "ignore": [ 5 | "firebase.json", 6 | "**/.*", 7 | "**/node_modules/**" 8 | ], 9 | "rewrites": [ 10 | { 11 | "source": "**", 12 | "destination": "/index.html" 13 | } 14 | ] 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Types.ts: -------------------------------------------------------------------------------- 1 | export interface InitialState { 2 | displayName: string; 3 | photo: string | undefined; 4 | user: null | { 5 | uid: string; 6 | photo: string; 7 | email: string; 8 | displayName: string; 9 | }; 10 | } 11 | 12 | export interface InitialAppState { 13 | channelId: string | null; 14 | channelName: string | null; 15 | } 16 | -------------------------------------------------------------------------------- /src/components/Message.scss: -------------------------------------------------------------------------------- 1 | .message { 2 | display: flex; 3 | align-items: center; 4 | padding: 15px; 5 | color: white; 6 | margin-bottom: -15px; 7 | 8 | .messageInfo { 9 | padding: 20px 20px 20px 10px; 10 | } 11 | 12 | .messageTimestamp { 13 | color: #7b7c85; 14 | margin-left: 20px; 15 | font-size: 16px; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /src/components/ErrorFallBack.tsx: -------------------------------------------------------------------------------- 1 | import { FallbackProps } from "react-error-boundary"; 2 | 3 | export const ErrorFallback = ({ 4 | error, 5 | resetErrorBoundary, 6 | }: FallbackProps): JSX.Element => { 7 | return ( 8 |
9 |

Error Message

10 |
{error!.message}
11 | 12 |
13 | ); 14 | }; 15 | -------------------------------------------------------------------------------- /src/components/SidebarChannel.scss: -------------------------------------------------------------------------------- 1 | .sidebarChannel { 2 | padding-left: 20px; 3 | h4 { 4 | color: #7b7c85; 5 | display: flex; 6 | padding: 5px; 7 | align-items: center; 8 | cursor: pointer; 9 | 10 | &:hover { 11 | color: white; 12 | background-color: #33363d; 13 | border-radius: 12px; 14 | } 15 | } 16 | 17 | .sidebarChannelHash { 18 | font-size: 22px; 19 | padding-right: 10px; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom/client"; 3 | import "./index.scss"; 4 | import App from "./App"; 5 | import { Provider } from "react-redux"; 6 | import { store } from "./app/store"; 7 | 8 | const root = ReactDOM.createRoot( 9 | document.getElementById("root") as HTMLElement 10 | ); 11 | root.render( 12 | 13 | 14 | 15 | 16 | 17 | ); 18 | -------------------------------------------------------------------------------- /src/index.scss: -------------------------------------------------------------------------------- 1 | * { 2 | margin: 0; 3 | font-family: "Noto Sans JP"; 4 | } 5 | 6 | body { 7 | margin: 0; 8 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 9 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 10 | sans-serif; 11 | -webkit-font-smoothing: antialiased; 12 | -moz-osx-font-smoothing: grayscale; 13 | overflow: hidden; 14 | } 15 | 16 | code { 17 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 18 | monospace; 19 | } 20 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /src/components/Login.scss: -------------------------------------------------------------------------------- 1 | .login { 2 | display: flex; 3 | flex-direction: column; 4 | justify-content: center; 5 | align-items: center; 6 | gap: 30px; 7 | height: 100vh; 8 | width: 100%; 9 | background-color: #33363d; 10 | 11 | button { 12 | width: 200px; 13 | background-color: #738adb; 14 | color: #eff2f5; 15 | font-weight: 800; 16 | 17 | &:hover { 18 | background-color: black; 19 | color: #738adb; 20 | } 21 | } 22 | 23 | .loginLogo { 24 | img { 25 | object-fit: cover; 26 | height: 150px; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "noFallthroughCasesInSwitch": true, 16 | "module": "esnext", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "noEmit": true, 21 | "jsx": "react-jsx" 22 | }, 23 | "include": [ 24 | "src" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /src/features/userSlice.ts: -------------------------------------------------------------------------------- 1 | import { createSlice, PayloadAction } from "@reduxjs/toolkit"; 2 | import { InitialState } from "../Types"; 3 | 4 | const initialState: InitialState = { 5 | user: null, 6 | displayName: "", 7 | photo: undefined, 8 | }; 9 | 10 | export const userSlice = createSlice({ 11 | name: "user", 12 | initialState, 13 | /* https://zenn.dev/luvmini511/articles/819d8c7fa13101 */ 14 | reducers: { 15 | login: (state, action) => { 16 | state.user = action.payload; 17 | }, 18 | logout: (state) => { 19 | state.user = null; 20 | }, 21 | }, 22 | }); 23 | 24 | export const { login, logout } = userSlice.actions; 25 | export default userSlice.reducer; 26 | -------------------------------------------------------------------------------- /src/app/store.ts: -------------------------------------------------------------------------------- 1 | import { configureStore } from "@reduxjs/toolkit"; 2 | import userReducer from "../features/userSlice"; 3 | import appReducer from "../features/appSlice"; 4 | import { 5 | useSelector as rawUseSelector, 6 | TypedUseSelectorHook, 7 | } from "react-redux"; 8 | 9 | export const store = configureStore({ 10 | reducer: { 11 | user: userReducer, 12 | app: appReducer, 13 | }, 14 | }); 15 | 16 | /* https://zenn.dev/engstt/articles/293e7420c93a18 */ 17 | export type AppDispatch = typeof store.dispatch; 18 | //storeの現在の状態の型を取得 19 | export type RootState = ReturnType; //getState...現在のstateを取得できる 20 | export const useSelector: TypedUseSelectorHook = rawUseSelector; 21 | -------------------------------------------------------------------------------- /src/components/Login.tsx: -------------------------------------------------------------------------------- 1 | import { Button } from "@mui/material"; 2 | import { signInWithPopup } from "firebase/auth"; 3 | import React from "react"; 4 | import { auth, provider } from "../firebase"; 5 | import "./Login.scss"; 6 | 7 | const Login = () => { 8 | const signIn = () => { 9 | signInWithPopup(auth, provider).catch((error) => { 10 | alert(error.message); 11 | }); 12 | }; 13 | 14 | return ( 15 |
16 | {/*

ログインページです。

*/} 17 | 18 |
19 | 20 |
21 | 22 | 23 |
24 | ); 25 | }; 26 | 27 | export default Login; 28 | -------------------------------------------------------------------------------- /src/features/appSlice.ts: -------------------------------------------------------------------------------- 1 | import { createSlice, PayloadAction } from "@reduxjs/toolkit"; 2 | import { InitialAppState } from "../Types"; 3 | 4 | const initialState: InitialAppState = { 5 | channelId: null, 6 | channelName: null, 7 | }; 8 | 9 | export const appSlice = createSlice({ 10 | name: "app", 11 | initialState, 12 | reducers: { 13 | setChannelId: (state, action) => { 14 | state.channelId += action.payload; 15 | }, 16 | setChannelInfo: (state, action) => { 17 | state.channelId = action.payload.channelId; 18 | state.channelName = action.payload.channelName; 19 | }, 20 | }, 21 | }); 22 | 23 | export const { setChannelId, setChannelInfo } = appSlice.actions; 24 | 25 | export default appSlice.reducer; 26 | -------------------------------------------------------------------------------- /src/firebase.ts: -------------------------------------------------------------------------------- 1 | import { initializeApp } from "firebase/app"; 2 | import { getFirestore } from "firebase/firestore"; 3 | import { getAuth, GoogleAuthProvider } from "firebase/auth"; 4 | 5 | const firebaseConfig = { 6 | apiKey: "AIzaSyBRV5jJRu7oQoQ9hNcmvcVdT6IzvvD7BF4", 7 | authDomain: "discord-clone-83b08.firebaseapp.com", 8 | projectId: "discord-clone-83b08", 9 | storageBucket: "discord-clone-83b08.appspot.com", 10 | messagingSenderId: "393399833089", 11 | appId: "1:393399833089:web:f60b51803644790a16e600", 12 | }; 13 | 14 | const app = initializeApp(firebaseConfig); 15 | const db = getFirestore(app); 16 | const auth = getAuth(app); 17 | const provider = new GoogleAuthProvider(); 18 | 19 | export { auth, provider, db }; 20 | // export default db; 21 | -------------------------------------------------------------------------------- /src/components/Message.tsx: -------------------------------------------------------------------------------- 1 | import { Avatar } from "@mui/material"; 2 | import { FieldValue, Timestamp } from "firebase/firestore"; 3 | import React from "react"; 4 | import "./Message.scss"; 5 | 6 | type Props = { 7 | message: string; 8 | timestamp: Timestamp; 9 | user: { 10 | uid: string; 11 | photo: string; 12 | email: string; 13 | displayName: string; 14 | }; 15 | }; 16 | 17 | const Message = (props: Props) => { 18 | const { message, timestamp, user } = props; 19 | // console.log(timestamp.seconds.toDate()); 20 | 21 | return ( 22 |
23 | 24 |
25 |

26 | {user?.displayName} 27 | 28 | {new Date(timestamp?.toDate()).toLocaleString()} 29 | 30 |

31 | 32 |

{message}

33 |
34 |
35 | ); 36 | }; 37 | 38 | export default Message; 39 | -------------------------------------------------------------------------------- /src/hooks/useFirebase.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | import { db } from "../firebase"; 3 | import { QueryDocumentSnapshot } from "firebase/firestore/lite"; 4 | import { 5 | onSnapshot, 6 | collection, 7 | DocumentData, 8 | CollectionReference, 9 | } from "firebase/firestore"; 10 | 11 | interface Channels { 12 | id: string; 13 | channel: QueryDocumentSnapshot; 14 | } 15 | 16 | const useFirebase = (data: string) => { 17 | const [documents, setDocuments] = useState([]); 18 | 19 | useEffect(() => { 20 | let collectionRef: CollectionReference = collection(db, data); 21 | onSnapshot(collectionRef, (snapshot) => { 22 | let results: Channels[] = []; 23 | snapshot.docs.forEach((doc) => { 24 | results.push({ id: doc.id, channel: doc.data() as any }); 25 | }); 26 | setDocuments(results); 27 | }); 28 | }, [data]); 29 | 30 | return { documents }; 31 | }; 32 | 33 | export default useFirebase; 34 | -------------------------------------------------------------------------------- /src/components/Chat.scss: -------------------------------------------------------------------------------- 1 | .chat { 2 | display: flex; 3 | flex-direction: column; 4 | flex-grow: 1; 5 | background-color: #34373c; 6 | position: relative; 7 | height: 100vh; 8 | 9 | .chatMessages { 10 | // flex-grow: 1; 11 | height: 100vh; 12 | overflow-y: scroll; 13 | } 14 | 15 | .chatInput { 16 | color: lightgray; 17 | display: flex; 18 | align-items: center; 19 | justify-content: space-between; 20 | padding: 15px; 21 | background-color: #474b53; 22 | border-radius: 5px; 23 | margin: 20px; 24 | 25 | form { 26 | flex-grow: 1; 27 | input { 28 | padding: 15px; 29 | background: transparent; 30 | border: none; 31 | outline: 0; 32 | color: white; 33 | font-size: large; 34 | width: 100%; 35 | } 36 | .chatInputButton { 37 | display: none; 38 | } 39 | } 40 | 41 | .chatInputIcons { 42 | .MuiSvgIcon-root { 43 | padding: 5px; 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/components/ChatHeader.scss: -------------------------------------------------------------------------------- 1 | .chatHeader { 2 | display: flex; 3 | align-items: center; 4 | justify-content: space-between; 5 | width: 100%; 6 | padding-top: 10px; 7 | 8 | .chatHeaderLeft { 9 | padding-left: 15px; 10 | 11 | .chatHeaderHash { 12 | color: #7b7c85; 13 | display: flex; 14 | align-items: center; 15 | padding-right: 7px; 16 | } 17 | 18 | h3 { 19 | color: #ffffff; 20 | display: flex; 21 | align-items: center; 22 | } 23 | } 24 | 25 | .chatHeaderRight { 26 | padding-right: 15px; 27 | display: flex; 28 | align-items: center; 29 | gap: 13px; 30 | color: #7b7c85; 31 | 32 | .chatHeaderSearch { 33 | display: flex; 34 | align-items: center; 35 | color: gray; 36 | background-color: #40404a; 37 | border-radius: 3px; 38 | padding: 3px; 39 | 40 | input { 41 | background: transparent; 42 | outline: 0; 43 | color: white; 44 | border: none; 45 | } 46 | } 47 | 48 | .MuiSvgIcon-root { 49 | cursor: pointer; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/components/ChatHeader.tsx: -------------------------------------------------------------------------------- 1 | import { 2 | HelpRounded, 3 | Notifications, 4 | SearchRounded, 5 | SendRounded, 6 | } from "@mui/icons-material"; 7 | import PushPinIcon from "@mui/icons-material/PushPin"; 8 | import PeopleAltIcon from "@mui/icons-material/PeopleAlt"; 9 | import React from "react"; 10 | import "./ChatHeader.scss"; 11 | 12 | type Props = { 13 | channelName: string | null; 14 | }; 15 | 16 | const ChatHeader = (props: Props) => { 17 | const { channelName } = props; 18 | return ( 19 |
20 |
21 |

22 | # 23 | {channelName} 24 |

25 |
26 | 27 |
28 | 29 | 30 | 31 |
32 | 33 | 34 |
35 | 36 | 37 |
38 |
39 | ); 40 | }; 41 | 42 | export default ChatHeader; 43 | -------------------------------------------------------------------------------- /src/components/SidebarChannle.tsx: -------------------------------------------------------------------------------- 1 | import { DocumentData } from "firebase/firestore/lite"; 2 | import React, { useEffect } from "react"; 3 | import { useAppDispatch } from "../app/hooks"; 4 | import { setChannelInfo } from "../features/appSlice"; 5 | import "./SidebarChannel.scss"; 6 | 7 | type Props = { 8 | id: string; 9 | channel: DocumentData; 10 | }; 11 | 12 | const SidebarChannle = (props: Props) => { 13 | const { id, channel } = props; 14 | const dispatch = useAppDispatch(); 15 | 16 | useEffect(() => { 17 | dispatch( 18 | setChannelInfo({ 19 | channelId: id, 20 | channelName: channel.channel.channelName, 21 | }) 22 | ); 23 | }, []); 24 | 25 | return ( 26 |
29 | dispatch( 30 | setChannelInfo({ 31 | channelId: id, 32 | channelName: channel.channel.channelName, 33 | }) 34 | ) 35 | } 36 | > 37 |

38 | # 39 | {channel.channel.channelName} 40 |

41 |
42 | ); 43 | }; 44 | 45 | export default SidebarChannle; 46 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 14 | 15 | 19 | React App 20 | 21 | 22 | 23 |
24 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /.firebase/hosting.YnVpbGQ.cache: -------------------------------------------------------------------------------- 1 | favicon.ico,1670301154382,eae62e993eb980ec8a25058c39d5a51feab118bd2100c4deebb2a9c158ec11f9 2 | index.html,1675317218737,2f2f3382a0b1b836cb832eec790033b887d0cd92d424bd7dbf5fa7de1700e36f 3 | logo192.png,1670301155225,3ee59515172ee198f3be375979df15ac5345183e656720a381b8872b2a39dc8b 4 | logo512.png,1670301155346,ee7e2f3fdb8209c4b6fd7bef6ba50d1b9dba30a25bb5c3126df057e1cb6f5331 5 | robots.txt,1670301155853,bfe106a3fb878dc83461c86818bf74fc1bdc7f28538ba613cd3e775516ce8b49 6 | manifest.json,1670301154551,aff3449bdc238776f5d6d967f19ec491b36aed5fb7f23ccff6500736fd58494a 7 | static/css/main.df00eb62.css.map,1675317218745,f5ef25f17ee855e1f9bf63cccf3714598630be3a77452be3f8e26cc8fe4af388 8 | static/css/main.df00eb62.css,1675317218745,c8a34225d707c87a4058ca4f7c5a4a6073acd739e19132614580554ceb22f17e 9 | static/js/main.b23fd4ab.js.LICENSE.txt,1675317218745,5cc47a93184f2ad57843c1da18a3cf23f1c0f2d31c15418d087e5912d8cbc2e6 10 | asset-manifest.json,1675317218744,e717203f7ce275a5093eb6aa8a9c34057707354db4869ed9e1dea9566aca9d39 11 | icon.png,1640763550869,ad63ed722c2c5262f665d5166fa9196e2184dcd16f1e4c758d701579329df8c6 12 | discordLogo.png,1671689094850,38add0dae2fd7ece60a7dc099c463bf7f8f690f292626599bbca7077cba12340 13 | static/js/main.b23fd4ab.js,1675317218746,4ec92713310b298636bf866c9b49a2171863ad1a02ca5a777c040aa6e8e0bf0f 14 | static/js/main.b23fd4ab.js.map,1675317218746,babf3c6c9f5b83de114389495cac055ce6ba933ef721ab3230d28da3497ec3fa 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "discord-clone", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@emotion/react": "^11.10.5", 7 | "@emotion/styled": "^11.10.5", 8 | "@mui/icons-material": "^5.10.16", 9 | "@mui/material": "^5.10.17", 10 | "@reduxjs/toolkit": "^1.9.1", 11 | "@testing-library/jest-dom": "^5.16.5", 12 | "@testing-library/react": "^13.4.0", 13 | "@testing-library/user-event": "^13.5.0", 14 | "@types/jest": "^27.5.2", 15 | "@types/node": "^16.18.6", 16 | "@types/react": "^18.0.26", 17 | "@types/react-dom": "^18.0.9", 18 | "@types/react-redux": "^7.1.24", 19 | "firebase": "^9.15.0", 20 | "node-sass": "^7.0.3", 21 | "react": "^18.2.0", 22 | "react-dom": "^18.2.0", 23 | "react-error-boundary": "^3.1.4", 24 | "react-redux": "^8.0.5", 25 | "react-scripts": "5.0.1", 26 | "typescript": "^4.9.3", 27 | "web-vitals": "^2.1.4" 28 | }, 29 | "scripts": { 30 | "dev": "react-scripts start", 31 | "build": "react-scripts build", 32 | "test": "react-scripts test", 33 | "eject": "react-scripts eject" 34 | }, 35 | "eslintConfig": { 36 | "extends": [ 37 | "react-app", 38 | "react-app/jest" 39 | ] 40 | }, 41 | "browserslist": { 42 | "production": [ 43 | ">0.2%", 44 | "not dead", 45 | "not op_mini all" 46 | ], 47 | "development": [ 48 | "last 1 chrome version", 49 | "last 1 firefox version", 50 | "last 1 safari version" 51 | ] 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect } from "react"; 2 | import { ErrorBoundary } from "react-error-boundary"; 3 | import "./App.scss"; 4 | import { useAppSelector, useAppDispatch } from "./app/hooks"; 5 | import Chat from "./components/Chat"; 6 | import { ErrorFallback } from "./components/ErrorFallBack"; 7 | import Login from "./components/Login"; 8 | import Sidebar from "./components/Sidebar"; 9 | import { login, logout } from "./features/userSlice"; 10 | import { auth } from "./firebase"; 11 | import { Suspense } from "react"; 12 | 13 | function App() { 14 | const user = useAppSelector((state) => state.user.user); 15 | // console.log(user); 16 | const dispatch = useAppDispatch(); 17 | 18 | useEffect(() => { 19 | auth.onAuthStateChanged((authUser) => { 20 | console.log(authUser); 21 | if (authUser) { 22 | dispatch( 23 | login({ 24 | uid: authUser.uid, 25 | photo: authUser.photoURL, 26 | email: authUser.email, 27 | displayName: authUser.displayName, 28 | }) 29 | ); 30 | } else { 31 | dispatch(logout()); 32 | } 33 | }); 34 | }, [dispatch]); 35 | 36 | return ( 37 |
38 | {user ? ( 39 | <> 40 | {/* sidebar */} 41 | 42 | {/* ...Loading
}> */} 43 | 44 | {/* */} 45 | 46 | {/* home */} 47 | 48 | 49 | ) : ( 50 | <> 51 | 52 | 53 | )} 54 | 55 | ); 56 | } 57 | 58 | export default App; 59 | -------------------------------------------------------------------------------- /src/hooks/useSubCollection.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | import { db } from "../firebase"; 3 | import { QueryDocumentSnapshot } from "firebase/firestore/lite"; 4 | import { 5 | onSnapshot, 6 | collection, 7 | DocumentData, 8 | CollectionReference, 9 | Timestamp, 10 | query, 11 | orderBy, 12 | } from "firebase/firestore"; 13 | import { useAppSelector } from "../app/hooks"; 14 | 15 | interface Messages { 16 | timestamp: Timestamp; 17 | message: string; 18 | user: { 19 | uid: string; 20 | photo: string; 21 | email: string; 22 | displayName: string; 23 | }; 24 | } 25 | 26 | const useSubCollection = ( 27 | collectionName: string, 28 | subCollectionName: string 29 | ) => { 30 | const channelId = useAppSelector((state) => state.app.channelId); 31 | const [subDocuments, setSubDocuments] = useState([]); 32 | 33 | useEffect(() => { 34 | let collectionRef = collection( 35 | db, 36 | collectionName, 37 | String(channelId), 38 | subCollectionName 39 | ); 40 | 41 | let collectionRefOrderBy = query( 42 | collectionRef, 43 | orderBy("timestamp", "desc") 44 | ); 45 | 46 | onSnapshot(collectionRefOrderBy, (snapshot) => { 47 | let results: Messages[] = []; 48 | snapshot.docs.forEach((doc: QueryDocumentSnapshot) => { 49 | results.push({ 50 | timestamp: doc.data().timestamp, 51 | message: doc.data().message, 52 | user: doc.data().user, 53 | }); 54 | }); 55 | setSubDocuments(results); 56 | }); 57 | }, [channelId]); 58 | return { subDocuments }; 59 | }; 60 | 61 | export default useSubCollection; 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 35 | 36 | 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. 37 | 38 | 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. 39 | 40 | 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. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | # discord-clone 48 | -------------------------------------------------------------------------------- /src/components/Sidebar.scss: -------------------------------------------------------------------------------- 1 | .sidebar { 2 | display: flex; 3 | flex: 0.35; 4 | height: 100vh; 5 | 6 | .sidebarLeft { 7 | display: flex; 8 | flex-direction: column; 9 | align-items: center; 10 | // margin-top: 15px; 11 | background-color: #1a1c20; 12 | padding: 7px 15px; 13 | 14 | .serverIcon { 15 | margin-bottom: 10px; 16 | margin-top: 7px; 17 | width: 60px; 18 | height: 60px; 19 | background-color: #2c2a33; 20 | border-radius: 9999px; 21 | position: relative; 22 | 23 | img { 24 | width: 50px; 25 | position: absolute; 26 | top: 50%; 27 | left: 50%; 28 | transform: translate(-50%, -50%); 29 | } 30 | } 31 | } 32 | 33 | .sidebarRight { 34 | display: flex; 35 | flex-direction: column; 36 | background-color: #2b2d33; 37 | width: 300px; 38 | position: relative; 39 | 40 | .sidebarTop { 41 | display: flex; 42 | justify-content: space-between; 43 | align-items: center; 44 | padding: 20px; 45 | color: #ffffff; 46 | } 47 | 48 | .sidebarChannels { 49 | padding: 13px; 50 | 51 | .sidebarChannelsHeader { 52 | color: white; 53 | display: flex; 54 | justify-content: space-between; 55 | margin-bottom: 5px; 56 | 57 | .sidebarHeader { 58 | display: flex; 59 | } 60 | 61 | .sidebarAddChannel { 62 | cursor: pointer; 63 | } 64 | } 65 | } 66 | 67 | .sidebarSettings { 68 | position: absolute; 69 | bottom: 0; 70 | display: flex; 71 | align-items: center; 72 | justify-content: space-between; 73 | width: 93%; 74 | padding-bottom: 10px; 75 | border-top: 1px solid #686a6e; 76 | padding-top: 10px; 77 | margin-left: -3px; 78 | 79 | .sidebarAccount { 80 | display: flex; 81 | align-items: center; 82 | 83 | img { 84 | width: 55px; 85 | border-radius: 50%; 86 | cursor: pointer; 87 | } 88 | 89 | .accountName { 90 | margin-left: 5px; 91 | 92 | h4 { 93 | color: white; 94 | font-weight: 500; 95 | } 96 | span { 97 | color: #686a6e; 98 | } 99 | } 100 | } 101 | 102 | .sidebarVoice { 103 | display: flex; 104 | gap: 12px; 105 | color: #686a6e; 106 | } 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/components/Sidebar.tsx: -------------------------------------------------------------------------------- 1 | import { ExpandMoreOutlined, Settings } from "@mui/icons-material"; 2 | import AddIcon from "@mui/icons-material/Add"; 3 | import MicIcon from "@mui/icons-material/Mic"; 4 | import HeadphonesIcon from "@mui/icons-material/Headphones"; 5 | import React, { useEffect, useState } from "react"; 6 | import "./Sidebar.scss"; 7 | import SidebarChannle from "./SidebarChannle"; 8 | import { useAppSelector } from "../app/hooks"; 9 | import { db, auth } from "../firebase"; 10 | import { 11 | collection, 12 | addDoc, 13 | DocumentData, 14 | DocumentReference, 15 | } from "firebase/firestore"; 16 | import useFirebase from "../hooks/useFirebase"; 17 | 18 | const Sidebar = () => { 19 | const user = useAppSelector((state) => state.user.user); 20 | // const [channels, setChannels] = useState([]); 21 | 22 | const { documents: channels } = useFirebase("channels"); 23 | console.log(channels); 24 | 25 | const addChannel = async () => { 26 | let channelName = prompt("新しいチャンネルを作成します"); 27 | 28 | if (channelName) { 29 | const docRef: DocumentReference = await addDoc( 30 | collection(db, "channels"), 31 | { 32 | channelName: channelName, 33 | } 34 | ); 35 | // console.log(docRef); 36 | } 37 | }; 38 | 39 | return ( 40 |
41 |
42 | {/* discrodIcon */} 43 |
44 | 45 |
46 |
47 | 48 |
49 |
50 | 51 |
52 |
53 |

Discord

54 | 55 |
56 | 57 |
58 |
59 |
60 | 61 |

プログラミングチャンネル

62 |
63 | 64 |
65 | 66 |
67 | {channels.map((channel) => ( 68 | 73 | ))} 74 | {/* 75 | 76 | */} 77 |
78 | 79 |
80 |
81 | account auth.signOut()} 85 | /> 86 |
87 |

{user?.displayName}

88 | #{user?.uid.substring(0, 4)} 89 |
90 |
91 | 92 |
93 | 94 | 95 | 96 |
97 |
98 |
99 |
100 |
101 | ); 102 | }; 103 | 104 | export default Sidebar; 105 | -------------------------------------------------------------------------------- /src/components/Chat.tsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import ChatHeader from "./ChatHeader"; 3 | import "./Chat.scss"; 4 | import { 5 | AddCircleOutline, 6 | CardGiftcardOutlined, 7 | EmojiEmotionsOutlined, 8 | } from "@mui/icons-material"; 9 | import GifIcon from "@mui/icons-material/Gif"; 10 | import Message from "./Message"; 11 | import { useAppSelector } from "../app/hooks"; 12 | import { db } from "../firebase"; 13 | import { 14 | addDoc, 15 | collection, 16 | CollectionReference, 17 | DocumentData, 18 | DocumentReference, 19 | FieldValue, 20 | Firestore, 21 | onSnapshot, 22 | orderBy, 23 | query, 24 | QueryDocumentSnapshot, 25 | QuerySnapshot, 26 | serverTimestamp, 27 | Timestamp, 28 | } from "firebase/firestore"; 29 | import useFirebase from "../hooks/useFirebase"; 30 | import useSubCollection from "../hooks/useSubCollection"; 31 | 32 | interface Messages { 33 | timestamp: Timestamp; 34 | message: string; 35 | user: { 36 | uid: string; 37 | photo: string; 38 | email: string; 39 | displayName: string; 40 | }; 41 | } 42 | 43 | //51:ディスコードチャット欄にメッセージを表示してみよう 44 | //52:メッセージを投稿した順番にソートして表示してみよう 45 | //54:【補足】サブコレクションデータ取得をカスタムフックスで切り出してみよう 46 | 47 | const Chat = () => { 48 | const user = useAppSelector((state) => state.user.user); 49 | const channelId = useAppSelector((state) => state.app.channelId); 50 | const channelName = useAppSelector((state) => state.app.channelName); 51 | 52 | const [inputText, setInputText] = useState(""); 53 | const { subDocuments: messages } = useSubCollection("channels", "messages"); 54 | // const [messages, setMessages] = useState([]); 55 | 56 | // useEffect(() => { 57 | // let collectionRef = collection( 58 | // db, 59 | // "channels", 60 | // String(channelId), 61 | // "messages" 62 | // ); 63 | 64 | // let collectionRefOrderBy = query( 65 | // collectionRef, 66 | // orderBy("timestamp", "desc") 67 | // ); 68 | 69 | // onSnapshot(collectionRefOrderBy, (snapshot) => { 70 | // let results: Messages[] = []; 71 | // snapshot.docs.forEach((doc: QueryDocumentSnapshot) => { 72 | // results.push({ 73 | // timestamp: doc.data().timestamp, 74 | // message: doc.data().message, 75 | // user: doc.data().user, 76 | // }); 77 | // }); 78 | // setMessages(results); 79 | // }); 80 | // }, [channelId]); 81 | 82 | const sendMessage = async (e: React.MouseEvent) => { 83 | e.preventDefault(); 84 | 85 | //channlesの中のmessageコレクションの中に新しくデータを入れる。 86 | const collectionRef = collection( 87 | db, 88 | "channels", 89 | String(channelId), 90 | "messages" 91 | ); 92 | const docRef: DocumentReference = await addDoc( 93 | collectionRef, 94 | { 95 | timestamp: serverTimestamp(), 96 | message: inputText, 97 | user: user, 98 | } 99 | ); 100 | console.log(docRef); 101 | 102 | setInputText(""); 103 | }; 104 | 105 | return ( 106 |
107 | 108 | 109 |
110 | {messages.map((message, index) => ( 111 | 117 | ))} 118 |
119 | 120 |
121 | 122 |
123 | ) => 127 | setInputText(e.target.value) 128 | } 129 | value={inputText} 130 | disabled={Boolean(!channelId)} 131 | /> 132 | 140 |
141 | 142 |
143 | 144 | 145 | 146 |
147 |
148 |
149 | ); 150 | }; 151 | 152 | export default Chat; 153 | --------------------------------------------------------------------------------