-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07eca32
commit 9aff2ef
Showing
5 changed files
with
510 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
.live-chat { | ||
width: 300px; | ||
height: 400px; | ||
border: 2px solid #000; | ||
background-color: #fff; | ||
display: flex; | ||
flex-direction: column; | ||
font-family: "Comic Sans MS", cursive, sans-serif; | ||
margin-right: 10px; | ||
transition: height 0.3s ease; | ||
|
||
&.minimized { | ||
height: 40px; | ||
} | ||
|
||
.chat-header { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
background-color: #007700; | ||
color: #fff; | ||
padding: 5px; | ||
cursor: pointer; | ||
|
||
h2 { | ||
margin: 0; | ||
font-size: 1em; | ||
} | ||
|
||
button { | ||
background: none; | ||
border: none; | ||
color: #fff; | ||
cursor: pointer; | ||
} | ||
} | ||
|
||
.chat-window { | ||
flex: 1; | ||
padding: 10px; | ||
overflow-y: auto; | ||
border-bottom: 2px solid #000; | ||
background-color: #f0f0f0; | ||
|
||
.chat-message { | ||
margin-bottom: 10px; | ||
padding: 5px; | ||
border-radius: 5px; | ||
background-color: #e0e0e0; | ||
|
||
strong { | ||
color: #007700; | ||
} | ||
} | ||
} | ||
|
||
.chat-input { | ||
display: flex; | ||
padding: 10px; | ||
background-color: #d0d0d0; | ||
|
||
input { | ||
flex: 1; | ||
padding: 5px; | ||
border: 1px solid #000; | ||
border-radius: 3px; | ||
margin-right: 5px; | ||
} | ||
|
||
button { | ||
padding: 5px 10px; | ||
border: 1px solid #000; | ||
border-radius: 3px; | ||
background-color: #007700; | ||
color: #fff; | ||
cursor: pointer; | ||
|
||
&:hover { | ||
background-color: #005500; | ||
} | ||
} | ||
} | ||
} | ||
|
||
.live-chat-controller { | ||
position: fixed; | ||
bottom: 0; | ||
left: 0; | ||
width: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
|
||
.chat-creation { | ||
display: flex; | ||
background-color: #007700; | ||
color: #fff; | ||
padding: 10px; | ||
display: flex; | ||
|
||
input { | ||
flex: 1; | ||
padding: 5px; | ||
border: 1px solid #000; | ||
border-radius: 3px; | ||
margin-right: 5px; | ||
} | ||
|
||
button { | ||
padding: 5px 10px; | ||
border: 1px solid #000; | ||
border-radius: 3px; | ||
background-color: #fff; | ||
color: #007700; | ||
cursor: pointer; | ||
|
||
&:hover { | ||
background-color: #005500; | ||
color: #fff; | ||
} | ||
} | ||
} | ||
|
||
.chat-list { | ||
display: flex; | ||
flex-wrap: nowrap; | ||
align-items: flex-end; | ||
overflow-x: auto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import React, { useState } from "react"; | ||
import { withSharedState } from "@playhtml/react"; | ||
import { PlayProvider } from "@playhtml/react"; | ||
import "./LiveChat.scss"; | ||
|
||
interface ChatMessage { | ||
id: string; | ||
name: string; | ||
text: string; | ||
color?: string; | ||
} | ||
|
||
interface LiveChatProps { | ||
name: string; | ||
} | ||
|
||
const LiveChat = withSharedState( | ||
{ | ||
defaultData: { messages: [] as ChatMessage[] }, | ||
}, | ||
({ data, setData }, { name }) => { | ||
const [newMessage, setNewMessage] = useState(""); | ||
const [isMinimized, setIsMinimized] = useState(true); | ||
const userId = localStorage.getItem("userId") || "unknown"; | ||
const userName = Boolean(localStorage.getItem("username")) | ||
? JSON.parse(localStorage.getItem("username")) || "Anonymous" | ||
: "Anonymous"; | ||
|
||
const handleSend = () => { | ||
if (newMessage.trim()) { | ||
const message = { | ||
id: userId, | ||
name: userName, | ||
text: newMessage, | ||
// this comes from cursor party, if not defined it just defaults to black | ||
// @ts-ignore | ||
color: window.cursors.color, | ||
}; | ||
setData({ messages: [...data.messages, message] }); | ||
setNewMessage(""); | ||
} | ||
}; | ||
|
||
// TODO: add read in local store, handle notifications | ||
return ( | ||
<div className={`live-chat ${isMinimized ? "minimized" : ""}`}> | ||
<div | ||
className="chat-header" | ||
onClick={() => setIsMinimized(!isMinimized)} | ||
> | ||
<h2>{name}</h2> | ||
<button>{isMinimized ? "⬆︎" : "–"}</button> | ||
</div> | ||
{!isMinimized && ( | ||
<> | ||
<div className="chat-window"> | ||
{data.messages.map((msg, index) => ( | ||
<div key={index} className="chat-message"> | ||
<strong | ||
style={{ | ||
color: msg.color || "black", | ||
}} | ||
> | ||
{msg.name} | ||
</strong> | ||
: {msg.text} | ||
</div> | ||
))} | ||
</div> | ||
<div className="chat-input"> | ||
<input | ||
type="text" | ||
value={newMessage} | ||
onChange={(e) => setNewMessage(e.target.value)} | ||
placeholder="Type your message..." | ||
/> | ||
<button onClick={handleSend}>Send</button> | ||
</div> | ||
</> | ||
)} | ||
</div> | ||
); | ||
} | ||
); | ||
|
||
export const LiveChatController = withSharedState( | ||
{ | ||
defaultData: { chatNames: [] as string[] }, | ||
}, | ||
({ data, setData }) => { | ||
const [newChatName, setNewChatName] = useState(""); | ||
|
||
const handleCreateChat = () => { | ||
if (newChatName.trim() && !data.chatNames.includes(newChatName)) { | ||
setData({ chatNames: [...data.chatNames, newChatName] }); | ||
setNewChatName(""); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="live-chat-controller" id="live-chats"> | ||
<div className="chat-list"> | ||
{data.chatNames.map((name, index) => ( | ||
<LiveChat key={index} name={name} /> | ||
))} | ||
</div> | ||
<div className="chat-creation"> | ||
<div> | ||
<input | ||
type="text" | ||
value={newChatName} | ||
onChange={(e) => setNewChatName(e.target.value)} | ||
placeholder="Enter chat name..." | ||
/> | ||
<button onClick={handleCreateChat}>Create Chat</button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.