11---
22import { authors } from " ../ts/vars" ;
3+ import ChatController from " ../controllers/chat" ;
34
45// math equation to generate the accurate number of the day
56const x = new Date ().getDate ();
@@ -20,6 +21,8 @@ if (Math.random() < 0.1) {
2021
2122let accuracyStyle = ` color: hsl(${accuracyPercentage }, 100%, 50%); font-weight: bold; ` ;
2223const author = authors [Math .floor (Math .random () * authors .length )];
24+
25+ const messages = ChatController .getInstance ().getMessages ();
2326---
2427
2528<script >
@@ -72,6 +75,56 @@ const author = authors[Math.floor(Math.random() * authors.length)];
7275 }
7376</script >
7477
78+ <script is:inline >
79+ document.addEventListener("DOMContentLoaded", () => {
80+ const form = document.getElementById("chat");
81+ const input = document.getElementById("message");
82+ let lastSubmittedMessage = "";
83+ form.addEventListener("submit", (e) => {
84+ e.preventDefault();
85+
86+ // Save the message BEFORE clearing the input
87+ lastSubmittedMessage = input.value;
88+
89+ fetch("/api/chat", {
90+ method: "POST",
91+ body: JSON.stringify({ message: input.value }),
92+ headers: {
93+ "Content-Type": "application/json",
94+ },
95+ });
96+ input.value = "";
97+ input.focus();
98+ });
99+
100+ const eventSource = new EventSource("/api/stream");
101+ eventSource.onmessage = (event) => {
102+ const messages = document.getElementById("messages");
103+
104+ // Create the new message element first
105+ const li = document.createElement("li");
106+ li.className = "text-sm text-white list-none text-left";
107+
108+ // Compare with the saved message
109+ if (lastSubmittedMessage === JSON.parse(event.data)) {
110+ li.innerText = "YOU: " + JSON.parse(event.data);
111+ // Clear lastSubmittedMessage after it's been matched
112+ lastSubmittedMessage = "";
113+ } else {
114+ li.innerText = JSON.parse(event.data);
115+ }
116+
117+ // Add the new message
118+ messages.appendChild(li);
119+
120+ // Then check if we need to remove old messages
121+ while (messages.children.length > 5) {
122+ messages.removeChild(messages.firstChild);
123+ }
124+ };
125+ });
126+ </script >
127+
75128<header class =" text-center text-white py-8" >
76129 <h1 class =" text-4xl font-bold text-grain-text hover:animate-vibrate2x" >
77130 <a href =" /fizzbuzz" class =" hover:underline" >accuratelinuxgraphs.com</a >
@@ -141,21 +194,41 @@ const author = authors[Math.floor(Math.random() * authors.length)];
141194 title =" Ad"
142195 /></a
143196 >
144- </div >
145- <audio loop id =" accuRotateAudio" class =" hidden" >
146- <source
147- src =" /assets/audio/extremebinarydrumandbass.mp3"
148- type =" audio/mp3"
149- id =" accuRotateAudio2"
150- />
151- Your browser does not support the audio element. Seek help.
152- </audio >
153- <p class =" invisible" id =" credits" >
154- AccuRotate Audio Credit: <a
155- href =" https://opengameart.org/content/extreme-binary-dnb"
156- >Gobusto - Extreme Binary DnB</a
157- >
158- </p >
159197
160- <hr class =" animate-ping" />
198+ <div class =" bg-grain-panel rounded-lg p-4 mt-4" >
199+ <h2 class =" text-xl font-bold mb-2" >Chat</h2 >
200+ <ul id =" messages" class =" list-disc pl-5 font-bold bg-grain-bg" >
201+ {
202+ messages .map ((message ) => (
203+ <li class = " text-sm text-white list-none text-left" >{ message } </li >
204+ ))
205+ }
206+ </ul >
207+ <form id =" chat" >
208+ <input
209+ type =" text"
210+ id =" message"
211+ class =" border border-gray-300 rounded px-2 py-1 w-full mt-2"
212+ placeholder =" Type your message here..."
213+ />
214+ <input type =" submit" hidden />
215+ </form >
216+ </div >
217+ <audio loop id =" accuRotateAudio" class =" hidden" >
218+ <source
219+ src =" /assets/audio/extremebinarydrumandbass.mp3"
220+ type =" audio/mp3"
221+ id =" accuRotateAudio2"
222+ />
223+ Your browser does not support the audio element. Seek help.
224+ </audio >
225+ <p class =" invisible" id =" credits" >
226+ AccuRotate Audio Credit: <a
227+ href =" https://opengameart.org/content/extreme-binary-dnb"
228+ >Gobusto - Extreme Binary DnB</a
229+ >
230+ </p >
231+
232+ <hr class =" animate-ping" />
233+ </div >
161234</header >
0 commit comments