Skip to content

Commit 211cbd6

Browse files
committed
fix chat
1 parent abaf91c commit 211cbd6

File tree

1 file changed

+68
-18
lines changed
  • NotificationsService/NotificationsService/wwwroot

1 file changed

+68
-18
lines changed

NotificationsService/NotificationsService/wwwroot/index.html

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<body class="bg-light">
1919
<div class="container py-4">
2020
<h1 class="mb-4">Open Pull Requests</h1>
21+
<label>User ID: <input id="userId" value="user123"></label><br>
22+
<label>Session ID: <input id="sessionId" value="session456"></label><br>
2123

2224
<!-- PR List View -->
2325
<div id="pr-list" class="list-group">
@@ -51,7 +53,7 @@ <h5 class="mt-4">Review:</h5>
5153
<!-- Chat Text Area and Submit Button -->
5254
<div class="chat-container">
5355
<textarea id="chat-input" placeholder="Enter your comment..."></textarea>
54-
<button class="chat-submit-btn" onclick="submitChatComment()">
56+
<button class="chat-submit-btn" onclick="sendChatMessage()">
5557
&#10148;
5658
</button>
5759
</div>
@@ -150,6 +152,8 @@ <h6>Action:</h6>
150152

151153
// Set the Review HTML content
152154
document.getElementById('detail-review').innerHTML = html;
155+
console.info('Connecting to chat.');
156+
connectChat();
153157
}
154158

155159
function showListView() {
@@ -212,23 +216,6 @@ <h6>Action:</h6>
212216
alert('Failed to submit your decision.');
213217
}
214218
}
215-
function submitChatComment() {
216-
const comment = document.getElementById("chat-input").value.trim();
217-
if (!comment) {
218-
alert("Please enter a comment before submitting.");
219-
return;
220-
}
221-
222-
// Placeholder logic for testing – replace with actual backend call if needed
223-
const chatBox = document.getElementById("chat-response-box");
224-
const simulatedResponse = `🤖 Response to: "${comment}"\n`;
225-
226-
chatBox.textContent += simulatedResponse;
227-
chatBox.scrollTop = chatBox.scrollHeight;
228-
229-
document.getElementById("chat-input").value = '';
230-
}
231-
232219

233220
function setupWebSocket() {
234221
console.info('setupWebSocket() entered.');
@@ -325,6 +312,69 @@ <h6>Action:</h6>
325312
fetchPrs();
326313
console.info('Setting up WebSocket');
327314
setupWebSocket();
315+
chatSocket = {};
316+
317+
function connectChat() {
318+
if (chatSocket && (chatSocket.readyState === WebSocket.OPEN || chatSocket.readyState === WebSocket.CONNECTING)) {
319+
console.log("Chat WebSocket is already connecting or connected.");
320+
return;
321+
}
322+
const userId = document.getElementById("userId").value;
323+
const sessionId = document.getElementById("sessionId").value;
324+
//const url = `ws://localhost:8080/ws/chat/${userId}/${sessionId}`;
325+
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
326+
const url = `${protocol}//notifications.codeominous.com/ws/chat/${userId}/${sessionId}`;
327+
328+
chatSocket = new WebSocket(url);
329+
330+
chatSocket.onmessage = function (event) {
331+
const msg = event.data;
332+
document.getElementById("chat-response-box").innerHTML += `<div class='bot'><strong>Claude:</strong> ${msg}</div>`;
333+
};
334+
335+
chatSocket.onopen = function () {
336+
document.getElementById("chat-response-box").innerHTML += "<div><em>Connected to chat server.</em></div>";
337+
};
338+
339+
chatSocket.onclose = function () {
340+
document.getElementById("chat-response-box").innerHTML += "<div><em>Disconnected.</em></div>";
341+
};
342+
}
343+
344+
function sendChatMessage() {
345+
if (chatSocket && (chatSocket.readyState != WebSocket.OPEN)) {
346+
console.log("Chat WebSocket is not connected.");
347+
return;
348+
}
349+
const comment = document.getElementById("chat-input").value.trim();
350+
if (!comment) {
351+
alert("Please enter a comment before submitting.");
352+
return;
353+
}
354+
connectChat();
355+
const msg = comment.value;
356+
comment.value = "";
357+
chatSocket.send(msg);
358+
}
359+
360+
function submitChatCommentTest() {
361+
const comment = document.getElementById("chat-input").value.trim();
362+
if (!comment) {
363+
alert("Please enter a comment before submitting.");
364+
return;
365+
}
366+
367+
// Placeholder logic for testing – replace with actual backend call if needed
368+
const chatBox = document.getElementById("chat-response-box");
369+
const simulatedResponse = `🤖 Response to: "${comment}"\n`;
370+
371+
chatBox.textContent += simulatedResponse;
372+
chatBox.scrollTop = chatBox.scrollHeight;
373+
374+
document.getElementById("chat-input").value = '';
375+
}
376+
377+
328378
</script>
329379
</body>
330380
</html>

0 commit comments

Comments
 (0)