├── LICENSE ├── client ├── .gitignore ├── index.html ├── package-lock.json ├── package.json ├── postcss.config.cjs ├── public │ └── vite.svg ├── src │ ├── components │ │ ├── Button.tsx │ │ ├── FullScreenCard.tsx │ │ ├── Input.tsx │ │ └── Link.tsx │ ├── context │ │ └── AuthContext.tsx │ ├── hooks │ │ └── useLocalStorage.ts │ ├── index.css │ ├── main.tsx │ ├── pages │ │ ├── Home.tsx │ │ ├── Login.tsx │ │ ├── Signup.tsx │ │ ├── channel │ │ │ └── new.tsx │ │ └── layouts │ │ │ ├── AuthLayout.tsx │ │ │ └── RootLayout.tsx │ ├── router.tsx │ └── vite-env.d.ts ├── tailwind.config.cjs ├── tsconfig.json ├── tsconfig.node.json └── vite.config.ts └── server ├── .gitignore ├── package-lock.json ├── package.json ├── routes └── users.ts └── server.ts /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 WebDevSimplified 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /client/.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | 10 | node_modules 11 | dist 12 | dist-ssr 13 | *.local 14 | 15 | # Editor directories and files 16 | .vscode/* 17 | !.vscode/extensions.json 18 | .idea 19 | .DS_Store 20 | *.suo 21 | *.ntvs* 22 | *.njsproj 23 | *.sln 24 | *.sw? 25 | 26 | .env 27 | -------------------------------------------------------------------------------- /client/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Vite + React + TS 8 | 9 | 10 |
11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "private": true, 4 | "version": "0.0.0", 5 | "type": "module", 6 | "scripts": { 7 | "dev": "vite", 8 | "build": "tsc && vite build", 9 | "preview": "vite preview" 10 | }, 11 | "dependencies": { 12 | "@tanstack/react-query": "^4.22.0", 13 | "axios": "^1.2.2", 14 | "react": "^18.2.0", 15 | "react-dom": "^18.2.0", 16 | "react-router-dom": "^6.6.2", 17 | "react-select": "^5.7.0", 18 | "stream-chat": "^8.2.1", 19 | "stream-chat-react": "^10.5.0" 20 | }, 21 | "devDependencies": { 22 | "@types/react": "^18.0.26", 23 | "@types/react-dom": "^18.0.9", 24 | "@vitejs/plugin-react-swc": "^3.0.0", 25 | "autoprefixer": "^10.4.13", 26 | "postcss": "^8.4.21", 27 | "tailwindcss": "^3.2.4", 28 | "typescript": "^4.9.3", 29 | "vite": "^4.0.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /client/postcss.config.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /client/public/vite.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /client/src/components/Button.tsx: -------------------------------------------------------------------------------- 1 | import { DetailedHTMLProps, forwardRef, ButtonHTMLAttributes } from "react" 2 | 3 | export const Button = forwardRef< 4 | HTMLButtonElement, 5 | DetailedHTMLProps, HTMLButtonElement> 6 | >(({ className, children, ...rest }, ref) => { 7 | return ( 8 | 15 | ) 16 | }) 17 | -------------------------------------------------------------------------------- /client/src/components/FullScreenCard.tsx: -------------------------------------------------------------------------------- 1 | import { ReactNode } from "react" 2 | 3 | type FullScreenCardProps = { 4 | children: ReactNode 5 | } 6 | 7 | export function FullScreenCard({ children }: FullScreenCardProps) { 8 | return ( 9 |
10 |
{children}
11 |
12 | ) 13 | } 14 | 15 | FullScreenCard.Body = function ({ children }: FullScreenCardProps) { 16 | return
{children}
17 | } 18 | 19 | FullScreenCard.BelowCard = function ({ children }: FullScreenCardProps) { 20 | return
{children}
21 | } 22 | -------------------------------------------------------------------------------- /client/src/components/Input.tsx: -------------------------------------------------------------------------------- 1 | import { DetailedHTMLProps, forwardRef, InputHTMLAttributes } from "react" 2 | 3 | export const Input = forwardRef< 4 | HTMLInputElement, 5 | DetailedHTMLProps, HTMLInputElement> 6 | >(({ className, ...rest }, ref) => { 7 | return ( 8 | 13 | ) 14 | }) 15 | -------------------------------------------------------------------------------- /client/src/components/Link.tsx: -------------------------------------------------------------------------------- 1 | import { LinkProps, Link as RouterLink } from "react-router-dom" 2 | 3 | export function Link({ children, className, ...rest }: LinkProps) { 4 | return ( 5 | 9 | {children} 10 | 11 | ) 12 | } 13 | -------------------------------------------------------------------------------- /client/src/context/AuthContext.tsx: -------------------------------------------------------------------------------- 1 | import { useMutation } from "@tanstack/react-query" 2 | import { 3 | UseMutationOptions, 4 | UseMutationResult, 5 | } from "@tanstack/react-query/build/lib/types" 6 | import axios, { AxiosResponse } from "axios" 7 | import { 8 | createContext, 9 | ReactNode, 10 | useContext, 11 | useEffect, 12 | useState, 13 | } from "react" 14 | import { useNavigate } from "react-router-dom" 15 | import { StreamChat } from "stream-chat" 16 | import { useLocalStorage } from "../hooks/useLocalStorage" 17 | 18 | type AuthContext = { 19 | user?: User 20 | streamChat?: StreamChat 21 | signup: UseMutationResult 22 | login: UseMutationResult<{ token: string; user: User }, unknown, string> 23 | logout: UseMutationResult 24 | } 25 | 26 | type User = { 27 | id: string 28 | name: string 29 | image?: string 30 | } 31 | 32 | const Context = createContext(null) 33 | 34 | export function useAuth() { 35 | return useContext(Context) as AuthContext 36 | } 37 | 38 | export function useLoggedInAuth() { 39 | return useContext(Context) as AuthContext & 40 | Required> 41 | } 42 | 43 | type AuthProviderProps = { 44 | children: ReactNode 45 | } 46 | 47 | export function AuthProvider({ children }: AuthProviderProps) { 48 | const navigate = useNavigate() 49 | const [user, setUser] = useLocalStorage("user") 50 | const [token, setToken] = useLocalStorage("token") 51 | const [streamChat, setStreamChat] = useState() 52 | 53 | const signup = useMutation({ 54 | mutationFn: (user: User) => { 55 | return axios.post(`${import.meta.env.VITE_SERVER_URL}/signup`, user) 56 | }, 57 | onSuccess() { 58 | navigate("/login") 59 | }, 60 | }) 61 | 62 | const login = useMutation({ 63 | mutationFn: (id: string) => { 64 | return axios 65 | .post(`${import.meta.env.VITE_SERVER_URL}/login`, { id }) 66 | .then(res => { 67 | return res.data as { token: string; user: User } 68 | }) 69 | }, 70 | onSuccess(data) { 71 | setUser(data.user) 72 | setToken(data.token) 73 | }, 74 | }) 75 | 76 | const logout = useMutation({ 77 | mutationFn: () => { 78 | return axios.post(`${import.meta.env.VITE_SERVER_URL}/logout`, { token }) 79 | }, 80 | onSuccess() { 81 | setUser(undefined) 82 | setToken(undefined) 83 | setStreamChat(undefined) 84 | }, 85 | }) 86 | 87 | useEffect(() => { 88 | if (token == null || user == null) return 89 | const chat = new StreamChat(import.meta.env.VITE_STREAM_API_KEY!) 90 | 91 | if (chat.tokenManager.token === token && chat.userID === user.id) return 92 | 93 | let isInterrupted = false 94 | const connectPromise = chat.connectUser(user, token).then(() => { 95 | if (isInterrupted) return 96 | setStreamChat(chat) 97 | }) 98 | 99 | return () => { 100 | isInterrupted = true 101 | setStreamChat(undefined) 102 | 103 | connectPromise.then(() => { 104 | chat.disconnectUser() 105 | }) 106 | } 107 | }, [token, user]) 108 | 109 | return ( 110 | 111 | {children} 112 | 113 | ) 114 | } 115 | -------------------------------------------------------------------------------- /client/src/hooks/useLocalStorage.ts: -------------------------------------------------------------------------------- 1 | import { useEffect, useState } from "react" 2 | 3 | export function useLocalStorage( 4 | key: string, 5 | initialValue?: T | undefined | (() => T | undefined) 6 | ) { 7 | const [value, setValue] = useState(() => { 8 | const jsonValue = localStorage.getItem(key) 9 | if (jsonValue == null) { 10 | if (typeof initialValue === "function") { 11 | return (initialValue as () => T | undefined)() 12 | } else { 13 | return initialValue 14 | } 15 | } else { 16 | return JSON.parse(jsonValue) 17 | } 18 | }) 19 | 20 | useEffect(() => { 21 | if (value === undefined) { 22 | localStorage.removeItem(key) 23 | return 24 | } 25 | 26 | localStorage.setItem(key, JSON.stringify(value)) 27 | }, [value, key]) 28 | 29 | return [value, setValue] as [T | undefined, typeof setValue] 30 | } 31 | -------------------------------------------------------------------------------- /client/src/index.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /client/src/main.tsx: -------------------------------------------------------------------------------- 1 | import React from "react" 2 | import ReactDOM from "react-dom/client" 3 | import "./index.css" 4 | import { RouterProvider } from "react-router-dom" 5 | import { router } from "./router" 6 | import { QueryClient, QueryClientProvider } from "@tanstack/react-query" 7 | import "stream-chat-react/dist/css/index.css" 8 | 9 | const queryClient = new QueryClient() 10 | 11 | ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( 12 | 13 | 14 | 15 | 16 | 17 | ) 18 | -------------------------------------------------------------------------------- /client/src/pages/Home.tsx: -------------------------------------------------------------------------------- 1 | import { useNavigate } from "react-router-dom" 2 | import { 3 | LoadingIndicator, 4 | Chat, 5 | ChannelList, 6 | Channel, 7 | Window, 8 | MessageInput, 9 | MessageList, 10 | ChannelHeader, 11 | } from "stream-chat-react" 12 | import { ChannelListMessengerProps } from "stream-chat-react/dist/components" 13 | import { useChatContext } from "stream-chat-react/dist/context" 14 | import { Button } from "../components/Button" 15 | import { useLoggedInAuth } from "../context/AuthContext" 16 | 17 | export function Home() { 18 | const { user, streamChat } = useLoggedInAuth() 19 | 20 | if (streamChat == null) return 21 | 22 | return ( 23 | 24 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | ) 38 | } 39 | 40 | function Channels({ loadedChannels }: ChannelListMessengerProps) { 41 | const navigate = useNavigate() 42 | const { logout } = useLoggedInAuth() 43 | const { setActiveChannel, channel: activeChannel } = useChatContext() 44 | 45 | return ( 46 |
47 | 48 |
49 | {loadedChannels != null && loadedChannels.length > 0 50 | ? loadedChannels.map(channel => { 51 | const isActive = channel === activeChannel 52 | const extraClasses = isActive 53 | ? "bg-blue-500 text-white" 54 | : "hover:bg-blue-100 bg-gray-100" 55 | return ( 56 | 72 | ) 73 | }) 74 | : "No Conversations"} 75 |
76 | 79 |
80 | ) 81 | } 82 | -------------------------------------------------------------------------------- /client/src/pages/Login.tsx: -------------------------------------------------------------------------------- 1 | import { FormEvent, useRef } from "react" 2 | import { Navigate } from "react-router-dom" 3 | import { Button } from "../components/Button" 4 | import { Input } from "../components/Input" 5 | import { useAuth } from "../context/AuthContext" 6 | 7 | export function Login() { 8 | const { login, user } = useAuth() 9 | const usernameRef = useRef(null) 10 | 11 | if (user != null) return 12 | 13 | function handleSubmit(e: FormEvent) { 14 | e.preventDefault() 15 | if (login.isLoading) return 16 | 17 | const username = usernameRef.current?.value 18 | if (username == null || username === "") { 19 | return 20 | } 21 | 22 | login.mutate(username) 23 | } 24 | 25 | return ( 26 | <> 27 |

Login

28 |
32 | 33 | 34 | 41 |
42 | 43 | ) 44 | } 45 | -------------------------------------------------------------------------------- /client/src/pages/Signup.tsx: -------------------------------------------------------------------------------- 1 | import { FormEvent, useRef } from "react" 2 | import { Button } from "../components/Button" 3 | import { Input } from "../components/Input" 4 | import { useAuth } from "../context/AuthContext" 5 | 6 | export function Signup() { 7 | const { signup } = useAuth() 8 | const usernameRef = useRef(null) 9 | const nameRef = useRef(null) 10 | const imageUrlRef = useRef(null) 11 | 12 | function handleSubmit(e: FormEvent) { 13 | e.preventDefault() 14 | if (signup.isLoading) return 15 | 16 | const username = usernameRef.current?.value 17 | const name = nameRef.current?.value 18 | const imageUrl = imageUrlRef.current?.value 19 | if (username == null || username === "" || name == null || name === "") { 20 | return 21 | } 22 | 23 | signup.mutate({ id: username, name, image: imageUrl }) 24 | } 25 | 26 | return ( 27 | <> 28 |

Sign Up

29 |
33 | 34 | 35 | 36 | 37 | 38 | 39 | 46 |
47 | 48 | ) 49 | } 50 | -------------------------------------------------------------------------------- /client/src/pages/channel/new.tsx: -------------------------------------------------------------------------------- 1 | import { useMutation, useQuery } from "@tanstack/react-query" 2 | import { FormEvent, useRef } from "react" 3 | import { Button } from "../../components/Button" 4 | import { FullScreenCard } from "../../components/FullScreenCard" 5 | import { Input } from "../../components/Input" 6 | import { Link } from "../../components/Link" 7 | import Select, { SelectInstance } from "react-select" 8 | import { useLoggedInAuth } from "../../context/AuthContext" 9 | import { useNavigate } from "react-router-dom" 10 | 11 | export function NewChannel() { 12 | const { streamChat, user } = useLoggedInAuth() 13 | const navigate = useNavigate() 14 | const createChannel = useMutation({ 15 | mutationFn: ({ 16 | name, 17 | memberIds, 18 | imageUrl, 19 | }: { 20 | name: string 21 | memberIds: string[] 22 | imageUrl?: string 23 | }) => { 24 | if (streamChat == null) throw Error("Not connected") 25 | 26 | return streamChat 27 | .channel("messaging", crypto.randomUUID(), { 28 | name, 29 | image: imageUrl, 30 | members: [user.id, ...memberIds], 31 | }) 32 | .create() 33 | }, 34 | onSuccess() { 35 | navigate("/") 36 | }, 37 | }) 38 | const nameRef = useRef(null) 39 | const imageUrlRef = useRef(null) 40 | const memberIdsRef = 41 | useRef>(null) 42 | 43 | const users = useQuery({ 44 | queryKey: ["stream", "users"], 45 | queryFn: () => 46 | streamChat!.queryUsers({ id: { $ne: user.id } }, { name: 1 }), 47 | enabled: streamChat != null, 48 | }) 49 | 50 | function handleSubmit(e: FormEvent) { 51 | e.preventDefault() 52 | 53 | const name = nameRef.current?.value 54 | const imageUrl = imageUrlRef.current?.value 55 | const selectOptions = memberIdsRef.current?.getValue() 56 | if ( 57 | name == null || 58 | name === "" || 59 | selectOptions == null || 60 | selectOptions.length === 0 61 | ) { 62 | return 63 | } 64 | 65 | createChannel.mutate({ 66 | name, 67 | imageUrl, 68 | memberIds: selectOptions.map(option => option.value), 69 | }) 70 | } 71 | 72 | return ( 73 | 74 | 75 |

76 | New Conversation 77 |

78 |
82 | 83 | 84 | 85 | 86 | 87 |