From 0cf9f6d07f8b5922eca2176031d52725f2b6ee65 Mon Sep 17 00:00:00 2001 From: Lncvrt Date: Sun, 1 Feb 2026 19:04:40 -0700 Subject: [PATCH] Add sending messages, and add support for the server sending edit/delete instructions --- src/app/game/berry-dash/chatroom/page.tsx | 56 +++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/app/game/berry-dash/chatroom/page.tsx b/src/app/game/berry-dash/chatroom/page.tsx index fea7290..fbbed6e 100644 --- a/src/app/game/berry-dash/chatroom/page.tsx +++ b/src/app/game/berry-dash/chatroom/page.tsx @@ -3,6 +3,7 @@ import { BackButton } from '@/app/components/BackButton' import { DiscordButton } from '@/app/components/DiscordButton' import { GetIconForUser } from '@/util/bd' +import { getCookie } from '@/util/cookie' import axios from 'axios' import { useEffect, useRef, useState } from 'react' @@ -34,14 +35,24 @@ interface CustomIconEntry { export default function BerryDashChatroom () { const [connected, setConnected] = useState(false) + const [loggedIn, setLoggedIn] = useState(false) + const [token, setToken] = useState(null) const [messages, setMessages] = useState([]) const [customIconCache, setCustomIconCache] = useState([]) const [ws, setWs] = useState(null) + const messagesDiv = useRef(null) + const [input, setInput] = useState('') useEffect(() => { document.title = 'Lncvrt Games - Chatroom' + const token = getCookie('accountToken', '-1') + if (token !== '-1') { + setLoggedIn(true) + setToken(token) + } + const socket = new WebSocket('/api/ws') socket.onopen = () => { @@ -63,6 +74,17 @@ export default function BerryDashChatroom () { } else if (message.for == 'upload:chatroom_message') { const msg = message.data as Message setMessages(prev => [...prev.slice(1), msg]) + } else if (message.for == 'delete:chatroom_message') { + const msg = message.data.fillerMessage as Message + setMessages(prev => [msg, ...prev.slice(0, -1)]) + } else if (message.for == 'edit:chatroom_message') { + setMessages(prev => + prev.map(msg => + msg.id === message.data.id + ? { ...msg, content: btoa(message.data.newContent) } + : msg + ) + ) } } @@ -164,6 +186,40 @@ export default function BerryDashChatroom () { ) })} + {loggedIn ? ( +
{ + e.preventDefault() + setInput('') + ws?.send( + JSON.stringify({ + type: 'upload', + kind: 'chatroom_message', + data: { + content: input, + auth: token + }, + timestamp: Date.now() + }) + ) + }} + className='flex flex-row gap-2 -mb-4 mt-6' + > + setInput(e.target.value)} + > + +
+ ) : ( +

+ You must be logged in to send messages +

+ )} )} {!connected && (