|
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
| 2 | +import styled from 'styled-components'; |
| 3 | +import { sortBy, takeRight } from 'lodash-es'; |
| 4 | + |
| 5 | +import AutosizeTextArea from '@codesandbox/common/lib/components/AutosizeTextArea'; |
| 6 | +import { ENTER } from '@codesandbox/common/lib/utils/keycodes'; |
| 7 | +import { useOvermind } from 'app/overmind'; |
| 8 | + |
| 9 | +const Container = styled.div` |
| 10 | + min-height: 200px; |
| 11 | + max-height: 300px; |
| 12 | + padding: 0 1rem; |
| 13 | + color: white; |
| 14 | + font-size: 0.875rem; |
| 15 | + display: flex; |
| 16 | + flex-direction: column; |
| 17 | + overflow-y: auto; |
| 18 | +`; |
| 19 | + |
| 20 | +const Messages = styled.div` |
| 21 | + height: 100%; |
| 22 | + flex: 1; |
| 23 | +`; |
| 24 | + |
| 25 | +export const Chat: React.FC = () => { |
| 26 | + const [value, setValue] = useState(''); |
| 27 | + const [height, setHeight] = useState(''); |
| 28 | + const { state, actions } = useOvermind(); |
| 29 | + const messagesRef = useRef(null); |
| 30 | + const scrollDown = () => { |
| 31 | + if (messagesRef.current) { |
| 32 | + messagesRef.current.scrollTop = messagesRef.current.scrollHeight; |
| 33 | + } |
| 34 | + }; |
| 35 | + useEffect(scrollDown); |
| 36 | + |
| 37 | + const handleKeyDown = (e: KeyboardEvent) => { |
| 38 | + if (e.keyCode === ENTER && !e.shiftKey) { |
| 39 | + e.preventDefault(); |
| 40 | + e.stopPropagation(); |
| 41 | + // Enter |
| 42 | + actions.live.onSendChat({ |
| 43 | + message: value, |
| 44 | + }); |
| 45 | + setValue(''); |
| 46 | + scrollDown(); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 51 | + setValue(e.target.value); |
| 52 | + }; |
| 53 | + |
| 54 | + const { messages, users } = state.live.roomInfo.chat; |
| 55 | + const currentUserId = state.live.liveUserId; |
| 56 | + const roomInfoUsers = state.live.roomInfo.users; |
| 57 | + |
| 58 | + return ( |
| 59 | + <Container ref={messagesRef}> |
| 60 | + <Messages> |
| 61 | + {messages.length > 0 ? ( |
| 62 | + sortBy(takeRight(messages, 100), 'date').map((message, i) => { |
| 63 | + const metadata = roomInfoUsers.find(u => u.id === message.userId); |
| 64 | + const color = metadata |
| 65 | + ? `rgb(${metadata.color[0]}, ${metadata.color[1]}, ${ |
| 66 | + metadata.color[2] |
| 67 | + })` |
| 68 | + : '#636363'; |
| 69 | + const name = users[message.userId]; |
| 70 | + return ( |
| 71 | + <div key={message.date}> |
| 72 | + {(i === 0 || messages[i - 1].userId !== message.userId) && ( |
| 73 | + <div |
| 74 | + style={{ |
| 75 | + color, |
| 76 | + fontWeight: 600, |
| 77 | + marginBottom: '0.25rem', |
| 78 | + marginTop: '0.5rem', |
| 79 | + }} |
| 80 | + > |
| 81 | + {name} |
| 82 | + {currentUserId === message.userId && ' (you)'} |
| 83 | + {!metadata && ' (left)'} |
| 84 | + </div> |
| 85 | + )} |
| 86 | + <div |
| 87 | + style={{ |
| 88 | + color: 'rgba(255, 255, 255, 0.7)', |
| 89 | + fontWeight: 400, |
| 90 | + marginBottom: '.25rem', |
| 91 | + }} |
| 92 | + > |
| 93 | + {message.message.split('\n').map(m => ( |
| 94 | + <span key={m}> |
| 95 | + {m} |
| 96 | + <br /> |
| 97 | + </span> |
| 98 | + ))} |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + ); |
| 102 | + }) |
| 103 | + ) : ( |
| 104 | + <div |
| 105 | + style={{ |
| 106 | + fontStyle: 'italic', |
| 107 | + color: 'rgba(255, 255, 255, 0.5)', |
| 108 | + }} |
| 109 | + > |
| 110 | + No messages, start sending some! |
| 111 | + </div> |
| 112 | + )} |
| 113 | + </Messages> |
| 114 | + <AutosizeTextArea |
| 115 | + useCacheForDOMMeasurements |
| 116 | + value={value} |
| 117 | + onChange={handleChange} |
| 118 | + placeholder="Send a message..." |
| 119 | + style={{ |
| 120 | + width: '100%', |
| 121 | + minHeight: height, |
| 122 | + marginTop: '0.5rem', |
| 123 | + }} |
| 124 | + onKeyDown={handleKeyDown} |
| 125 | + onHeightChange={setHeight} |
| 126 | + /> |
| 127 | + </Container> |
| 128 | + ); |
| 129 | +}; |
0 commit comments