import { useEffect, useState, useContext } from 'react'; import { ApiContext } from '../../utils/api_context'; import { useChat } from '../../utils/use_chat'; import { Link, useParams } from 'react-router-dom'; import { generateGruvboxFromString } from '../../utils/generate_gruvbox'; /* A lot of this is stolen from my Docker presentation :). https://github.com/USUFSLC/sochat */ export const ChatRoom = () => { const { id } = useParams(); const [chatRoom, setChatRoom] = useState(''); const [active, connections, messages, sendMessage] = useChat(chatRoom); const [message, setMessage] = useState(''); const [color, setColor] = useState(generateGruvboxFromString('placeholder')); const [user, setUser] = useState({}); const api = useContext(ApiContext); const fetchUser = async () => { const res = await api.get('/users/me'); if (res.user) { setUser(res.user); setColor(generateGruvboxFromString(`${res.user.firstName} ${res.user.lastName}`)); } }; const fetchChatRoom = async (id) => { const room = await api.get(`/chat_rooms/${id}`); if (room) { setChatRoom(room); } }; const scrollToBottomOfChat = () => { const objDiv = document.getElementById('chat'); objDiv.scrollTop = objDiv.scrollHeight; }; const sendThisMessage = () => { sendMessage(message); setMessage(''); }; useEffect(() => { fetchUser(); fetchChatRoom(id); }, [id]); useEffect(() => { scrollToBottomOfChat(); }, [messages]); return (

{chatRoom?.name || `Chat Room ${chatRoom?.id}`}

{messages.map((message) => (
{message.userName}: {message.content}
))}

Connected Users ({connections.length})


    {connections.map((user) => (
  • {user.userName}
  • ))}
{active ? ( <>
Send
) : (

This room has been marked inactive and has been deleted.

)}
Back to map
); };