start frontend
This commit is contained in:
215
static/css/chat.css
Normal file
215
static/css/chat.css
Normal file
@@ -0,0 +1,215 @@
|
||||
:root {
|
||||
color-scheme: light;
|
||||
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
html,
|
||||
body {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: #f7f8fa;
|
||||
color: #1f2937;
|
||||
}
|
||||
|
||||
.app {
|
||||
display: flex;
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.chat {
|
||||
flex: 1;
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
background: #ffffff;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.chat__header {
|
||||
padding: 16px 20px;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
font-weight: 600;
|
||||
background: #f9fafb;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.chat__settings {
|
||||
padding: 6px 12px;
|
||||
border-radius: 999px;
|
||||
border: 1px solid #d1d5db;
|
||||
background: #ffffff;
|
||||
color: #1f2937;
|
||||
font-weight: 600;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.chat__settings:hover {
|
||||
background: #f3f4f6;
|
||||
}
|
||||
|
||||
.chat__messages {
|
||||
flex: 1;
|
||||
padding: 16px 20px;
|
||||
overflow-y: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.message {
|
||||
max-width: 70%;
|
||||
padding: 10px 12px;
|
||||
border-radius: 12px;
|
||||
line-height: 1.4;
|
||||
font-size: 14px;
|
||||
word-wrap: break-word;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.message--out {
|
||||
align-self: flex-end;
|
||||
background: #2563eb;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.message--in {
|
||||
align-self: flex-start;
|
||||
background: #f3f4f6;
|
||||
color: #111827;
|
||||
}
|
||||
|
||||
.chat__footer {
|
||||
padding: 16px 20px;
|
||||
border-top: 1px solid #e5e7eb;
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
background: #f9fafb;
|
||||
}
|
||||
|
||||
.chat__input {
|
||||
flex: 1;
|
||||
padding: 10px 12px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #d1d5db;
|
||||
background: #ffffff;
|
||||
color: #111827;
|
||||
}
|
||||
|
||||
.chat__input::placeholder {
|
||||
color: #9ca3af;
|
||||
}
|
||||
|
||||
.chat__button {
|
||||
padding: 10px 16px;
|
||||
border-radius: 8px;
|
||||
border: none;
|
||||
background: #2563eb;
|
||||
color: #ffffff;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.chat__status {
|
||||
padding: 8px 20px 0;
|
||||
font-size: 12px;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
.drawer {
|
||||
width: 320px;
|
||||
max-width: 100%;
|
||||
height: 100%;
|
||||
border-left: 1px solid #e5e7eb;
|
||||
background: #ffffff;
|
||||
padding: 20px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
transform: translateX(100%);
|
||||
transition: transform 0.25s ease;
|
||||
}
|
||||
|
||||
.drawer--open {
|
||||
transform: translateX(0%);
|
||||
}
|
||||
|
||||
.drawer__header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.drawer__title {
|
||||
margin: 0;
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.drawer__close {
|
||||
border: 1px solid #d1d5db;
|
||||
background: #ffffff;
|
||||
border-radius: 8px;
|
||||
padding: 4px 8px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.drawer__form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.drawer__field {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr auto;
|
||||
grid-template-rows: auto auto;
|
||||
gap: 6px 12px;
|
||||
align-items: center;
|
||||
font-size: 13px;
|
||||
color: #1f2937;
|
||||
}
|
||||
|
||||
.drawer__label {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.drawer__range {
|
||||
grid-column: 1 / -1;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.drawer__value {
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.drawer__hint {
|
||||
margin: 0;
|
||||
font-size: 12px;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
@media (max-width: 900px) {
|
||||
.drawer {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
top: 0;
|
||||
height: 100vh;
|
||||
box-shadow: -12px 0 24px rgba(15, 23, 42, 0.08);
|
||||
}
|
||||
}
|
||||
123
static/js/chat.js
Normal file
123
static/js/chat.js
Normal file
@@ -0,0 +1,123 @@
|
||||
const statusEl = document.getElementById("status");
|
||||
const messagesEl = document.getElementById("messages");
|
||||
const formEl = document.getElementById("chat-form");
|
||||
const inputEl = document.getElementById("chat-input");
|
||||
const settingsToggle = document.getElementById("settings-toggle");
|
||||
const settingsDrawer = document.getElementById("settings-drawer");
|
||||
const settingsClose = document.getElementById("settings-close");
|
||||
const rangeInputs = settingsDrawer
|
||||
? Array.from(settingsDrawer.querySelectorAll("input[type='range']"))
|
||||
: [];
|
||||
|
||||
const scheme = window.location.protocol === "https:" ? "wss" : "ws";
|
||||
const socketUrl = `${scheme}://${window.location.host}/ws`;
|
||||
let socket;
|
||||
let streamingBubble = null;
|
||||
|
||||
const addMessage = (text, direction) => {
|
||||
const bubble = document.createElement("div");
|
||||
bubble.className = `message message--${direction}`;
|
||||
bubble.textContent = text;
|
||||
messagesEl.appendChild(bubble);
|
||||
messagesEl.scrollTop = messagesEl.scrollHeight;
|
||||
};
|
||||
|
||||
const connect = () => {
|
||||
socket = new WebSocket(socketUrl);
|
||||
|
||||
socket.addEventListener("open", () => {
|
||||
statusEl.textContent = "Connected";
|
||||
});
|
||||
|
||||
socket.addEventListener("message", (event) => {
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(event.data);
|
||||
} catch (error) {
|
||||
addMessage(event.data, "in");
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.type === "start") {
|
||||
streamingBubble = document.createElement("div");
|
||||
streamingBubble.className = "message message--in";
|
||||
streamingBubble.textContent = "";
|
||||
messagesEl.appendChild(streamingBubble);
|
||||
messagesEl.scrollTop = messagesEl.scrollHeight;
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.type === "chunk") {
|
||||
if (!streamingBubble) {
|
||||
streamingBubble = document.createElement("div");
|
||||
streamingBubble.className = "message message--in";
|
||||
messagesEl.appendChild(streamingBubble);
|
||||
}
|
||||
streamingBubble.textContent += payload.data ?? "";
|
||||
messagesEl.scrollTop = messagesEl.scrollHeight;
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.type === "end") {
|
||||
streamingBubble = null;
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
socket.addEventListener("close", () => {
|
||||
statusEl.textContent = "Disconnected";
|
||||
});
|
||||
|
||||
socket.addEventListener("error", () => {
|
||||
statusEl.textContent = "Connection error";
|
||||
});
|
||||
};
|
||||
|
||||
const updateDrawerValue = (input) => {
|
||||
const output = input.parentElement?.querySelector(".drawer__value");
|
||||
if (!output) return;
|
||||
output.textContent = input.value;
|
||||
};
|
||||
|
||||
rangeInputs.forEach((input) => {
|
||||
updateDrawerValue(input);
|
||||
input.addEventListener("input", () => updateDrawerValue(input));
|
||||
});
|
||||
|
||||
const closeDrawer = () => {
|
||||
settingsDrawer?.classList.remove("drawer--open");
|
||||
if (settingsDrawer) settingsDrawer.setAttribute("aria-hidden", "true");
|
||||
if (settingsToggle) settingsToggle.setAttribute("aria-expanded", "false");
|
||||
};
|
||||
|
||||
const openDrawer = () => {
|
||||
settingsDrawer?.classList.add("drawer--open");
|
||||
if (settingsDrawer) settingsDrawer.setAttribute("aria-hidden", "false");
|
||||
if (settingsToggle) settingsToggle.setAttribute("aria-expanded", "true");
|
||||
};
|
||||
|
||||
settingsToggle?.addEventListener("click", () => {
|
||||
if (settingsDrawer?.classList.contains("drawer--open")) {
|
||||
closeDrawer();
|
||||
} else {
|
||||
openDrawer();
|
||||
}
|
||||
});
|
||||
|
||||
settingsClose?.addEventListener("click", closeDrawer);
|
||||
|
||||
formEl.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
const text = inputEl.value.trim();
|
||||
if (!text) return;
|
||||
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
||||
addMessage("Not connected.", "in");
|
||||
return;
|
||||
}
|
||||
addMessage(text, "out");
|
||||
socket.send(text);
|
||||
inputEl.value = "";
|
||||
inputEl.focus();
|
||||
});
|
||||
|
||||
connect();
|
||||
Reference in New Issue
Block a user