ui cleanup mobile view also

This commit is contained in:
Bartal Læarsson
2026-06-18 22:25:52 +01:00
parent f24da78970
commit 9d9b04d01b
5 changed files with 342 additions and 105 deletions
+1 -1
View File
@@ -284,7 +284,7 @@ pub async fn new_session(
hx-target=\"#messages\" \ hx-target=\"#messages\" \
hx-swap=\"beforeend\" \ hx-swap=\"beforeend\" \
hx-on=\"htmx:afterRequest: this.reset(); scrollMessages()\">\ hx-on=\"htmx:afterRequest: this.reset(); scrollMessages()\">\
<input id=\"chat-input\" type=\"text\" name=\"content\" placeholder=\"Type your message...\" autocomplete=\"off\" maxlength=\"1000\" oninput=\"sendUserTyping()\" required />\ <input id=\"chat-input\" type=\"text\" name=\"content\" placeholder=\"Type your message...\" autocomplete=\"off\" maxlength=\"1000\" oninput=\"handleUserTypingInput()\" required />\
<button type=\"submit\">Send</button>\ <button type=\"submit\">Send</button>\
</form>\ </form>\
</div>\ </div>\
+25 -10
View File
@@ -9,8 +9,10 @@ use std::sync::Arc;
use crate::state::{AppState, RESET_EVENT}; use crate::state::{AppState, RESET_EVENT};
const USER_TYPING_PREFIX: &str = "typing:user:"; const USER_TYPING_START_PREFIX: &str = "typing:user:start:";
const SUPPORTER_TYPING_PREFIX: &str = "typing:supporter:"; const USER_TYPING_STOP_PREFIX: &str = "typing:user:stop:";
const SUPPORTER_TYPING_START_PREFIX: &str = "typing:supporter:start:";
const SUPPORTER_TYPING_STOP_PREFIX: &str = "typing:supporter:stop:";
/// WebSocket for user — pushes notifications when supporter replies /// WebSocket for user — pushes notifications when supporter replies
pub async fn user_ws( pub async fn user_ws(
@@ -21,6 +23,13 @@ pub async fn user_ws(
ws.on_upgrade(move |socket| handle_user_socket(socket, session_id, state)) ws.on_upgrade(move |socket| handle_user_socket(socket, session_id, state))
} }
fn is_typing_event_for_session(text: &str, session_id: &str) -> bool {
text.strip_prefix(USER_TYPING_START_PREFIX) == Some(session_id)
|| text.strip_prefix(USER_TYPING_STOP_PREFIX) == Some(session_id)
|| text.strip_prefix(SUPPORTER_TYPING_START_PREFIX) == Some(session_id)
|| text.strip_prefix(SUPPORTER_TYPING_STOP_PREFIX) == Some(session_id)
}
async fn handle_user_socket(mut socket: WebSocket, session_id: String, state: Arc<AppState>) { async fn handle_user_socket(mut socket: WebSocket, session_id: String, state: Arc<AppState>) {
let tx = state.get_or_create_notifier(&session_id); let tx = state.get_or_create_notifier(&session_id);
let mut rx = tx.subscribe(); let mut rx = tx.subscribe();
@@ -28,9 +37,9 @@ async fn handle_user_socket(mut socket: WebSocket, session_id: String, state: Ar
loop { loop {
tokio::select! { tokio::select! {
// Forward broadcast events to the user browser. // Forward broadcast events to the user browser.
Ok(sid) = rx.recv() => { Ok(event) = rx.recv() => {
if sid == session_id || sid == RESET_EVENT || sid.starts_with(SUPPORTER_TYPING_PREFIX) || sid.starts_with(USER_TYPING_PREFIX) { if event == session_id || event == RESET_EVENT || is_typing_event_for_session(&event, &session_id) {
if socket.send(Message::Text(sid)).await.is_err() { if socket.send(Message::Text(event)).await.is_err() {
break; break;
} }
} }
@@ -39,8 +48,9 @@ async fn handle_user_socket(mut socket: WebSocket, session_id: String, state: Ar
msg = socket.recv() => { msg = socket.recv() => {
match msg { match msg {
Some(Ok(Message::Text(text))) => { Some(Ok(Message::Text(text))) => {
let expected = format!("{USER_TYPING_PREFIX}{session_id}"); if is_typing_event_for_session(&text, &session_id)
if text == expected { && (text.starts_with(USER_TYPING_START_PREFIX) || text.starts_with(USER_TYPING_STOP_PREFIX))
{
let _ = tx.send(text.clone()); let _ = tx.send(text.clone());
let _ = state.supporter_notifier.send(text); let _ = state.supporter_notifier.send(text);
} }
@@ -62,20 +72,25 @@ pub async fn supporter_ws(
ws.on_upgrade(move |socket| handle_supporter_socket(socket, state)) ws.on_upgrade(move |socket| handle_supporter_socket(socket, state))
} }
fn supporter_typing_session_id(text: &str) -> Option<&str> {
text.strip_prefix(SUPPORTER_TYPING_START_PREFIX)
.or_else(|| text.strip_prefix(SUPPORTER_TYPING_STOP_PREFIX))
}
async fn handle_supporter_socket(mut socket: WebSocket, state: Arc<AppState>) { async fn handle_supporter_socket(mut socket: WebSocket, state: Arc<AppState>) {
let mut rx = state.supporter_notifier.subscribe(); let mut rx = state.supporter_notifier.subscribe();
loop { loop {
tokio::select! { tokio::select! {
Ok(session_id) = rx.recv() => { Ok(event) = rx.recv() => {
if socket.send(Message::Text(session_id)).await.is_err() { if socket.send(Message::Text(event)).await.is_err() {
break; break;
} }
} }
msg = socket.recv() => { msg = socket.recv() => {
match msg { match msg {
Some(Ok(Message::Text(text))) => { Some(Ok(Message::Text(text))) => {
if let Some(session_id) = text.strip_prefix(SUPPORTER_TYPING_PREFIX) { if let Some(session_id) = supporter_typing_session_id(&text) {
if !session_id.is_empty() && state.sessions.contains_key(session_id) { if !session_id.is_empty() && state.sessions.contains_key(session_id) {
let tx = state.get_or_create_notifier(session_id); let tx = state.get_or_create_notifier(session_id);
let _ = tx.send(text); let _ = tx.send(text);
+24 -15
View File
@@ -14,6 +14,9 @@
--surface-soft: #20242c; --surface-soft: #20242c;
--border: #2b303a; --border: #2b303a;
--accent: #8f9cff; --accent: #8f9cff;
--danger: #d93d3d;
--danger-soft: rgba(217, 61, 61, 0.16);
--danger-text: #ffd1d1;
--text: #eceff4; --text: #eceff4;
--muted: #9aa3b2; --muted: #9aa3b2;
--radius: 14px; --radius: 14px;
@@ -21,13 +24,14 @@
body { body {
min-height: 100vh; min-height: 100vh;
min-height: 100dvh;
background: var(--bg); background: var(--bg);
color: var(--text); color: var(--text);
font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
padding: 1.5rem; padding: 1.25rem;
} }
.card { .card {
@@ -42,24 +46,27 @@
.timer { .timer {
display: inline-flex; display: inline-flex;
margin-bottom: 1rem; margin-bottom: 1rem;
color: var(--muted); color: var(--danger-text);
border: 1px solid var(--border); border: 1px solid var(--danger);
border-radius: 999px; border-radius: 999px;
padding: 0.35rem 0.75rem; padding: 0.38rem 0.8rem;
font-size: 0.78rem; font-size: 0.82rem;
background: var(--bg); font-weight: 800;
background: var(--danger-soft);
box-shadow: 0 0 0 1px rgba(217, 61, 61, 0.08), 0 8px 24px rgba(217, 61, 61, 0.08);
} }
h1 { h1 {
font-size: clamp(1.6rem, 4vw, 2.15rem); font-size: clamp(1.55rem, 4vw, 2.1rem);
letter-spacing: -0.035em; letter-spacing: -0.035em;
margin-bottom: 0.5rem; margin-bottom: 0.55rem;
} }
p { p {
color: var(--muted); color: var(--muted);
line-height: 1.6; line-height: 1.55;
margin-bottom: 1.5rem; margin-bottom: 1.25rem;
font-size: 0.95rem;
} }
.choices { .choices {
@@ -77,7 +84,7 @@
background: var(--surface-soft); background: var(--surface-soft);
border: 1px solid var(--border); border: 1px solid var(--border);
border-radius: var(--radius); border-radius: var(--radius);
padding: 1.1rem; padding: 1.05rem;
transition: transform 0.15s, border-color 0.15s, background 0.15s; transition: transform 0.15s, border-color 0.15s, background 0.15s;
} }
@@ -88,11 +95,13 @@
} }
.role-title { font-weight: 700; font-size: 1rem; } .role-title { font-weight: 700; font-size: 1rem; }
.role-detail { color: var(--muted); font-size: 0.88rem; } .role-detail { color: var(--muted); font-size: 0.88rem; line-height: 1.45; }
@media (max-width: 560px) { @media (max-width: 560px) {
body { align-items: stretch; padding: 1rem; }
.choices { grid-template-columns: 1fr; } .choices { grid-template-columns: 1fr; }
.card { padding: 1.5rem; } .card { padding: 1.35rem; border-radius: 18px; align-self: center; }
.timer { width: 100%; justify-content: center; }
} }
</style> </style>
</head> </head>
@@ -100,12 +109,12 @@
<main class="card"> <main class="card">
<span class="timer" hx-get="/reset-countdown" hx-trigger="load, every 1s" hx-swap="innerHTML">Reset in --:--</span> <span class="timer" hx-get="/reset-countdown" hx-trigger="load, every 1s" hx-swap="innerHTML">Reset in --:--</span>
<h1>Support chat</h1> <h1>Support chat</h1>
<p>Open the side you want to test. Each option opens in a new tab.</p> <p>A small in-memory chat demo with separate user and supporter views. Conversations, messages, and rate limits reset every 3 minutes.</p>
<div class="choices"> <div class="choices">
<a href="/user" target="_blank" rel="noopener noreferrer"> <a href="/user" target="_blank" rel="noopener noreferrer">
<span class="role-title">User</span> <span class="role-title">User</span>
<span class="role-detail">Start a new conversation</span> <span class="role-detail">Start a conversation</span>
</a> </a>
<a href="/supporter" target="_blank" rel="noopener noreferrer"> <a href="/supporter" target="_blank" rel="noopener noreferrer">
<span class="role-title">Supporter</span> <span class="role-title">Supporter</span>
+189 -44
View File
@@ -15,6 +15,9 @@
--border: #2b303a; --border: #2b303a;
--accent: #8f9cff; --accent: #8f9cff;
--accent-dim: #737fd3; --accent-dim: #737fd3;
--danger: #d93d3d;
--danger-soft: rgba(217, 61, 61, 0.16);
--danger-text: #ffd1d1;
--text: #eceff4; --text: #eceff4;
--muted: #9aa3b2; --muted: #9aa3b2;
--user: #8f9cff; --user: #8f9cff;
@@ -32,9 +35,10 @@
.layout { .layout {
display: grid; display: grid;
grid-template-columns: 280px 1fr; grid-template-columns: 280px minmax(0, 1fr);
grid-template-rows: 56px 1fr; grid-template-rows: 56px minmax(0, 1fr);
height: 100vh; height: 100vh;
height: 100dvh;
} }
.topbar { .topbar {
@@ -42,7 +46,7 @@
display: flex; display: flex;
align-items: center; align-items: center;
gap: 0.75rem; gap: 0.75rem;
padding: 0 1.5rem; padding: 0 1.25rem;
border-bottom: 1px solid var(--border); border-bottom: 1px solid var(--border);
background: var(--surface); background: var(--surface);
} }
@@ -54,13 +58,20 @@
} }
.topbar-timer { .topbar-timer {
display: inline-flex;
align-items: center;
justify-content: center;
margin-left: auto; margin-left: auto;
font-size: 0.78rem; font-size: 0.82rem;
color: var(--muted); color: var(--danger-text);
background: var(--bg); background: var(--danger-soft);
padding: 0.25rem 0.65rem; padding: 0.34rem 0.75rem;
border-radius: 20px; border-radius: 20px;
border: 1px solid var(--border); border: 1px solid var(--danger);
font-weight: 800;
letter-spacing: 0.01em;
box-shadow: 0 0 0 1px rgba(217, 61, 61, 0.08), 0 8px 24px rgba(217, 61, 61, 0.08);
white-space: nowrap;
} }
.sidebar { .sidebar {
@@ -68,6 +79,7 @@
display: flex; display: flex;
flex-direction: column; flex-direction: column;
overflow: hidden; overflow: hidden;
min-width: 0;
} }
.sidebar-header { .sidebar-header {
@@ -89,8 +101,10 @@
gap: 0.3rem; gap: 0.3rem;
} }
#session-list::-webkit-scrollbar { width: 4px; } #session-list::-webkit-scrollbar,
#session-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; } #supporter-messages::-webkit-scrollbar { width: 4px; height: 4px; }
#session-list::-webkit-scrollbar-thumb,
#supporter-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.session-item { .session-item {
width: 100%; width: 100%;
@@ -117,6 +131,7 @@
display: flex; display: flex;
align-items: center; align-items: center;
gap: 0.5rem; gap: 0.5rem;
flex-wrap: wrap;
} }
.session-badge { .session-badge {
@@ -145,41 +160,55 @@
.main-panel { .main-panel {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center;
overflow: hidden; overflow: hidden;
min-width: 0;
background: var(--bg);
}
.main-header,
#supporter-messages,
.typing-bubble,
#reply-area {
width: min(760px, 100%);
} }
.main-header { .main-header {
padding: 0.9rem 1.5rem; padding: 0.85rem 1rem;
border-bottom: 1px solid var(--border); border-bottom: 1px solid var(--border);
display: flex; display: flex;
align-items: center; align-items: center;
gap: 0.5rem; gap: 0.5rem;
min-height: 56px; min-height: 54px;
background: var(--bg);
} }
#active-session-name { #active-session-name {
font-weight: 700; font-weight: 700;
font-size: 1rem; font-size: 1rem;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
} }
#active-session-meta { #active-session-meta {
font-size: 0.8rem; font-size: 0.78rem;
color: var(--muted); color: var(--muted);
white-space: nowrap;
} }
#supporter-messages { #supporter-messages {
flex: 1; flex: 1;
min-height: 0;
overflow-y: auto; overflow-y: auto;
padding: 1.2rem 1.5rem; padding: 1rem;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 0.5rem; gap: 0.5rem;
scroll-behavior: smooth; scroll-behavior: smooth;
} }
#supporter-messages::-webkit-scrollbar { width: 4px; }
#supporter-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.empty-state { .empty-state {
flex: 1; flex: 1;
display: flex; display: flex;
@@ -189,6 +218,8 @@
color: var(--muted); color: var(--muted);
gap: 0.5rem; gap: 0.5rem;
font-size: 0.9rem; font-size: 0.9rem;
text-align: center;
padding: 1rem;
} }
.msg-wrap { display: flex; } .msg-wrap { display: flex; }
@@ -196,11 +227,12 @@
.msg-supporter-wrap { justify-content: flex-start; } .msg-supporter-wrap { justify-content: flex-start; }
.msg-user, .msg-supporter { .msg-user, .msg-supporter {
max-width: 65%; max-width: min(72%, 520px);
padding: 0.6rem 0.9rem; padding: 0.58rem 0.85rem;
border-radius: 16px; border-radius: 16px;
font-size: 0.88rem; font-size: 0.88rem;
line-height: 1.5; line-height: 1.48;
overflow-wrap: anywhere;
} }
.msg-user { .msg-user {
@@ -217,7 +249,7 @@
.msg-label { .msg-label {
display: block; display: block;
font-size: 0.7rem; font-size: 0.68rem;
font-weight: 600; font-weight: 600;
opacity: 0.7; opacity: 0.7;
margin-bottom: 2px; margin-bottom: 2px;
@@ -229,7 +261,7 @@
.msg-time { .msg-time {
display: block; display: block;
font-size: 0.68rem; font-size: 0.66rem;
opacity: 0.55; opacity: 0.55;
margin-top: 4px; margin-top: 4px;
text-align: right; text-align: right;
@@ -239,30 +271,44 @@
.msg-system-text { .msg-system-text {
font-size: 0.75rem; font-size: 0.75rem;
color: var(--muted); color: var(--muted);
background: var(--bg); background: var(--surface);
padding: 0.25rem 0.75rem; padding: 0.25rem 0.75rem;
border-radius: 20px; border-radius: 20px;
text-align: center;
} }
.typing-bubble { .typing-bubble {
display: none; display: none;
padding: 0 1.5rem 0.55rem; padding: 0 1rem 0.55rem;
justify-content: flex-start; justify-content: flex-start;
} }
.typing-bubble.visible { display: flex; } .typing-bubble.visible { display: flex; }
.typing-bubble-inner { .typing-bubble-inner {
position: relative;
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
gap: 4px; gap: 4px;
min-width: 48px; min-width: 48px;
padding: 0.65rem 0.85rem; padding: 0.58rem 0.78rem;
background: var(--supporter); background: var(--supporter);
border-radius: 16px; border-radius: 16px;
border-bottom-left-radius: 4px; border-bottom-left-radius: 4px;
} }
.typing-bubble-inner::after {
content: "";
position: absolute;
left: 7px;
bottom: -4px;
width: 9px;
height: 9px;
background: var(--supporter);
transform: rotate(45deg);
border-radius: 2px;
}
.typing-dot { .typing-dot {
width: 6px; width: 6px;
height: 6px; height: 6px;
@@ -281,15 +327,17 @@
#reply-area { #reply-area {
border-top: 1px solid var(--border); border-top: 1px solid var(--border);
padding: 0.75rem 1.5rem; padding: 0.65rem 1rem 0.8rem;
display: none; display: none;
background: var(--bg);
} }
#reply-area.visible { display: flex; gap: 0.6rem; } #reply-area.visible { display: flex; gap: 0.5rem; }
#reply-area input[type="text"] { #reply-area input[type="text"] {
flex: 1; flex: 1;
padding: 0.65rem 1rem; min-width: 0;
padding: 0.58rem 0.85rem;
background: var(--surface); background: var(--surface);
border: 1px solid var(--border); border: 1px solid var(--border);
border-radius: var(--radius); border-radius: var(--radius);
@@ -302,7 +350,7 @@
#reply-area input[type="text"]:focus { border-color: var(--accent); } #reply-area input[type="text"]:focus { border-color: var(--accent); }
#reply-area button { #reply-area button {
padding: 0.65rem 1.2rem; padding: 0.58rem 1rem;
background: var(--accent); background: var(--accent);
color: #111318; color: #111318;
border: none; border: none;
@@ -311,9 +359,70 @@
font-size: 0.88rem; font-size: 0.88rem;
cursor: pointer; cursor: pointer;
transition: background 0.2s; transition: background 0.2s;
white-space: nowrap;
} }
#reply-area button:hover { background: var(--accent-dim); } #reply-area button:hover { background: var(--accent-dim); }
@media (max-width: 820px) {
html, body { overflow: hidden; }
.layout {
grid-template-columns: minmax(0, 1fr);
grid-template-rows: auto auto minmax(0, 1fr);
}
.topbar {
min-height: 56px;
padding: 0.65rem 0.85rem;
flex-wrap: wrap;
}
.topbar-timer {
margin-left: 0;
font-size: 0.78rem;
padding: 0.3rem 0.65rem;
}
.sidebar {
border-right: 0;
border-bottom: 1px solid var(--border);
max-height: 30vh;
}
.sidebar-header { padding: 0.7rem 0.85rem; }
#session-list {
flex-direction: row;
overflow-x: auto;
overflow-y: hidden;
padding: 0.5rem 0.75rem 0.65rem;
}
.session-item {
min-width: 190px;
max-width: 230px;
}
.main-header,
#supporter-messages,
.typing-bubble,
#reply-area { width: 100%; }
.main-header { padding: 0.75rem 0.85rem; min-height: 48px; }
#supporter-messages { padding: 0.85rem; }
.msg-user, .msg-supporter { max-width: 86%; }
#reply-area { padding: 0.6rem 0.75rem 0.75rem; }
}
@media (max-width: 460px) {
.topbar { align-items: flex-start; flex-direction: column; gap: 0.45rem; }
.topbar-timer { width: 100%; justify-content: center; text-align: center; }
.sidebar { max-height: 28vh; }
.session-item { min-width: 165px; }
#reply-area.visible { gap: 0.4rem; }
#reply-area button { padding-inline: 0.85rem; }
}
</style> </style>
</head> </head>
<body> <body>
@@ -362,8 +471,8 @@
<script> <script>
let activeSessionId = null; let activeSessionId = null;
let supporterWs = null; let supporterWs = null;
let typingHideTimer = null; let lastTypingSessionId = null;
let lastTypingSent = 0; let supporterTypingActive = false;
function selectSession(button) { function selectSession(button) {
document.querySelectorAll('.session-item').forEach(function(b) { b.classList.remove('selected'); }); document.querySelectorAll('.session-item').forEach(function(b) { b.classList.remove('selected'); });
@@ -372,17 +481,22 @@
} }
function setActiveSession(sessionId, name) { function setActiveSession(sessionId, name) {
stopSupporterTyping(lastTypingSessionId);
activeSessionId = sessionId; activeSessionId = sessionId;
document.getElementById('active-session-name').textContent = name; document.getElementById('active-session-name').textContent = name;
document.getElementById('active-session-name').style.color = '';
document.getElementById('active-session-meta').textContent = '#' + sessionId.slice(0, 8); document.getElementById('active-session-meta').textContent = '#' + sessionId.slice(0, 8);
document.getElementById('reply-area').classList.add('visible'); document.getElementById('reply-area').classList.add('visible');
document.getElementById('reply-input').value = '';
hideUserTyping(); hideUserTyping();
scrollSupporterMessages(); scrollSupporterMessages();
} }
function resetSupporterDashboard() { function resetSupporterDashboard() {
stopSupporterTyping(lastTypingSessionId);
activeSessionId = null; activeSessionId = null;
document.getElementById('active-session-name').textContent = 'Select a conversation'; document.getElementById('active-session-name').textContent = 'Select a conversation';
document.getElementById('active-session-name').style.color = 'var(--muted)';
document.getElementById('active-session-meta').textContent = ''; document.getElementById('active-session-meta').textContent = '';
document.getElementById('reply-area').classList.remove('visible'); document.getElementById('reply-area').classList.remove('visible');
hideUserTyping(); hideUserTyping();
@@ -399,23 +513,43 @@
const indicator = document.getElementById('supporter-peer-typing'); const indicator = document.getElementById('supporter-peer-typing');
if (!indicator) return; if (!indicator) return;
indicator.classList.add('visible'); indicator.classList.add('visible');
clearTimeout(typingHideTimer);
typingHideTimer = setTimeout(hideUserTyping, 1400);
scrollSupporterMessages(); scrollSupporterMessages();
} }
function hideUserTyping() { function hideUserTyping() {
const indicator = document.getElementById('supporter-peer-typing'); const indicator = document.getElementById('supporter-peer-typing');
if (indicator) indicator.classList.remove('visible'); if (indicator) indicator.classList.remove('visible');
clearTimeout(typingHideTimer);
} }
function sendSupporterTyping() { function sendSupporterTypingState(state, sessionId = activeSessionId) {
if (!activeSessionId || !supporterWs || supporterWs.readyState !== WebSocket.OPEN) return; if (!sessionId || !supporterWs || supporterWs.readyState !== WebSocket.OPEN) return;
const now = Date.now(); supporterWs.send(`typing:supporter:${state}:${sessionId}`);
if (now - lastTypingSent < 500) return; }
lastTypingSent = now;
supporterWs.send(`typing:supporter:${activeSessionId}`); function startSupporterTyping() {
const input = document.getElementById('reply-input');
if (!activeSessionId || !input || !input.value.trim()) {
stopSupporterTyping(lastTypingSessionId || activeSessionId);
return;
}
if (supporterTypingActive && lastTypingSessionId === activeSessionId) return;
supporterTypingActive = true;
lastTypingSessionId = activeSessionId;
sendSupporterTypingState('start', activeSessionId);
}
function stopSupporterTyping(sessionId = activeSessionId) {
if (!supporterTypingActive && !sessionId) return;
const targetSession = sessionId || lastTypingSessionId;
if (targetSession) sendSupporterTypingState('stop', targetSession);
supporterTypingActive = false;
lastTypingSessionId = null;
}
function handleReplyInput() {
const input = document.getElementById('reply-input');
if (input && input.value.trim()) startSupporterTyping();
else stopSupporterTyping(lastTypingSessionId || activeSessionId);
} }
document.addEventListener('htmx:afterSwap', (e) => { document.addEventListener('htmx:afterSwap', (e) => {
@@ -428,6 +562,8 @@
const content = input.value.trim(); const content = input.value.trim();
if (!content) return; if (!content) return;
stopSupporterTyping(activeSessionId);
fetch(`/supporter/send/${activeSessionId}`, { fetch(`/supporter/send/${activeSessionId}`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
@@ -437,8 +573,8 @@
.then(html => { .then(html => {
const msgs = document.getElementById('supporter-messages'); const msgs = document.getElementById('supporter-messages');
msgs.insertAdjacentHTML('beforeend', html); msgs.insertAdjacentHTML('beforeend', html);
scrollSupporterMessages();
input.value = ''; input.value = '';
scrollSupporterMessages();
}); });
}); });
@@ -446,7 +582,7 @@
if (e.key === 'Enter') document.getElementById('reply-btn').click(); if (e.key === 'Enter') document.getElementById('reply-btn').click();
}); });
document.getElementById('reply-input').addEventListener('input', sendSupporterTyping); document.getElementById('reply-input').addEventListener('input', handleReplyInput);
function connectSupporterWS() { function connectSupporterWS() {
const wsProtocol = location.protocol === 'https:' ? 'wss' : 'ws'; const wsProtocol = location.protocol === 'https:' ? 'wss' : 'ws';
@@ -458,16 +594,22 @@
resetSupporterDashboard(); resetSupporterDashboard();
return; return;
} }
if (data.startsWith('typing:user:')) { if (data.startsWith('typing:user:start:')) {
const sessionId = data.replace('typing:user:', ''); const sessionId = data.replace('typing:user:start:', '');
if (sessionId === activeSessionId) showUserTyping(); if (sessionId === activeSessionId) showUserTyping();
return; return;
} }
if (data.startsWith('typing:user:stop:')) {
const sessionId = data.replace('typing:user:stop:', '');
if (sessionId === activeSessionId) hideUserTyping();
return;
}
if (data.startsWith('typing:supporter:')) return; if (data.startsWith('typing:supporter:')) return;
htmx.trigger('#session-list', 'refresh'); htmx.trigger('#session-list', 'refresh');
if (data === activeSessionId) { if (data === activeSessionId) {
hideUserTyping();
fetch(`/messages/${activeSessionId}`) fetch(`/messages/${activeSessionId}`)
.then(r => r.text()) .then(r => r.text())
.then(html => { .then(html => {
@@ -477,6 +619,9 @@
} }
}; };
supporterWs.onopen = () => {
if (supporterTypingActive && lastTypingSessionId) sendSupporterTypingState('start', lastTypingSessionId);
};
supporterWs.onclose = () => setTimeout(connectSupporterWS, 2000); supporterWs.onclose = () => setTimeout(connectSupporterWS, 2000);
} }
+103 -35
View File
@@ -15,6 +15,9 @@
--border: #2b303a; --border: #2b303a;
--accent: #8f9cff; --accent: #8f9cff;
--accent-dim: #737fd3; --accent-dim: #737fd3;
--danger: #d93d3d;
--danger-soft: rgba(217, 61, 61, 0.16);
--danger-text: #ffd1d1;
--text: #eceff4; --text: #eceff4;
--muted: #9aa3b2; --muted: #9aa3b2;
--user: #8f9cff; --user: #8f9cff;
@@ -25,33 +28,40 @@
body { body {
background: var(--bg); background: var(--bg);
min-height: 100vh; min-height: 100vh;
min-height: 100dvh;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text); color: var(--text);
padding: 1rem; padding: 4.4rem 1rem 1rem;
} }
.page-timer { .page-timer {
display: inline-flex;
align-items: center;
justify-content: center;
position: fixed; position: fixed;
top: 1rem; top: 1rem;
left: 50%; left: 50%;
transform: translateX(-50%); transform: translateX(-50%);
color: var(--muted); color: var(--danger-text);
border: 1px solid var(--border); border: 1px solid var(--danger);
border-radius: 999px; border-radius: 999px;
padding: 0.35rem 0.75rem; padding: 0.38rem 0.8rem;
font-size: 0.78rem; font-size: 0.82rem;
background: var(--surface); font-weight: 800;
background: var(--danger-soft);
box-shadow: 0 0 0 1px rgba(217, 61, 61, 0.08), 0 8px 24px rgba(217, 61, 61, 0.08);
z-index: 10; z-index: 10;
white-space: nowrap;
} }
#start-form { #start-form {
background: var(--surface); background: var(--surface);
border: 1px solid var(--border); border: 1px solid var(--border);
border-radius: 20px; border-radius: 20px;
padding: 2.2rem; padding: 2rem;
width: min(360px, 100%); width: min(360px, 100%);
text-align: left; text-align: left;
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.22); box-shadow: 0 18px 55px rgba(0, 0, 0, 0.22);
@@ -67,13 +77,13 @@
#start-form p { #start-form p {
font-size: 0.9rem; font-size: 0.9rem;
color: var(--muted); color: var(--muted);
margin-bottom: 1.5rem; margin-bottom: 1.35rem;
line-height: 1.55; line-height: 1.55;
} }
#start-form input[type="text"] { #start-form input[type="text"] {
width: 100%; width: 100%;
padding: 0.75rem 1rem; padding: 0.72rem 0.95rem;
background: var(--bg); background: var(--bg);
border: 1px solid var(--border); border: 1px solid var(--border);
border-radius: var(--radius); border-radius: var(--radius);
@@ -88,7 +98,7 @@
#start-form button { #start-form button {
width: 100%; width: 100%;
padding: 0.75rem; padding: 0.72rem;
background: var(--accent); background: var(--accent);
color: #111318; color: #111318;
border: none; border: none;
@@ -105,7 +115,8 @@
display: none; display: none;
flex-direction: column; flex-direction: column;
width: min(390px, 100%); width: min(390px, 100%);
height: min(600px, calc(100vh - 4.5rem)); height: min(620px, calc(100vh - 5.4rem));
height: min(620px, calc(100dvh - 5.4rem));
background: var(--surface); background: var(--surface);
border: 1px solid var(--border); border: 1px solid var(--border);
border-radius: 20px; border-radius: 20px;
@@ -117,23 +128,26 @@
display: flex; display: flex;
align-items: center; align-items: center;
gap: 0.6rem; gap: 0.6rem;
padding: 1rem 1.2rem; padding: 0.9rem 1rem;
border-bottom: 1px solid var(--border); border-bottom: 1px solid var(--border);
font-weight: 700; font-weight: 700;
font-size: 0.95rem; font-size: 0.95rem;
min-height: 52px;
} }
.chat-session-id { .chat-session-id {
margin-left: auto; margin-left: auto;
font-size: 0.75rem; font-size: 0.72rem;
color: var(--muted); color: var(--muted);
font-weight: 400; font-weight: 400;
white-space: nowrap;
} }
#messages { #messages {
flex: 1; flex: 1;
min-height: 0;
overflow-y: auto; overflow-y: auto;
padding: 1rem; padding: 0.9rem;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 0.5rem; gap: 0.5rem;
@@ -149,11 +163,12 @@
.msg-supporter-wrap { justify-content: flex-start; } .msg-supporter-wrap { justify-content: flex-start; }
.msg-user, .msg-supporter { .msg-user, .msg-supporter {
max-width: 75%; max-width: 78%;
padding: 0.6rem 0.9rem; padding: 0.58rem 0.85rem;
border-radius: 16px; border-radius: 16px;
font-size: 0.88rem; font-size: 0.88rem;
line-height: 1.5; line-height: 1.48;
overflow-wrap: anywhere;
} }
.msg-user { .msg-user {
@@ -170,7 +185,7 @@
.msg-label { .msg-label {
display: block; display: block;
font-size: 0.7rem; font-size: 0.68rem;
font-weight: 600; font-weight: 600;
opacity: 0.7; opacity: 0.7;
margin-bottom: 2px; margin-bottom: 2px;
@@ -182,7 +197,7 @@
.msg-time { .msg-time {
display: block; display: block;
font-size: 0.68rem; font-size: 0.66rem;
opacity: 0.55; opacity: 0.55;
margin-top: 4px; margin-top: 4px;
text-align: right; text-align: right;
@@ -200,27 +215,41 @@
background: var(--bg); background: var(--bg);
padding: 0.25rem 0.75rem; padding: 0.25rem 0.75rem;
border-radius: 20px; border-radius: 20px;
text-align: center;
} }
.typing-bubble { .typing-bubble {
display: none; display: none;
padding: 0 1rem 0.55rem; padding: 0 0.9rem 0.55rem;
justify-content: flex-start; justify-content: flex-start;
} }
.typing-bubble.visible { display: flex; } .typing-bubble.visible { display: flex; }
.typing-bubble-inner { .typing-bubble-inner {
position: relative;
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
gap: 4px; gap: 4px;
min-width: 48px; min-width: 48px;
padding: 0.65rem 0.85rem; padding: 0.58rem 0.78rem;
background: var(--supporter); background: var(--supporter);
border-radius: 16px; border-radius: 16px;
border-bottom-left-radius: 4px; border-bottom-left-radius: 4px;
} }
.typing-bubble-inner::after {
content: "";
position: absolute;
left: 7px;
bottom: -4px;
width: 9px;
height: 9px;
background: var(--supporter);
transform: rotate(45deg);
border-radius: 2px;
}
.typing-dot { .typing-dot {
width: 6px; width: 6px;
height: 6px; height: 6px;
@@ -239,14 +268,15 @@
#chat-form { #chat-form {
display: flex; display: flex;
gap: 0.5rem; gap: 0.45rem;
padding: 0.75rem 1rem; padding: 0.65rem 0.85rem 0.8rem;
border-top: 1px solid var(--border); border-top: 1px solid var(--border);
} }
#chat-form input[type="text"] { #chat-form input[type="text"] {
flex: 1; flex: 1;
padding: 0.6rem 0.9rem; min-width: 0;
padding: 0.58rem 0.85rem;
background: var(--bg); background: var(--bg);
border: 1px solid var(--border); border: 1px solid var(--border);
border-radius: var(--radius); border-radius: var(--radius);
@@ -259,7 +289,7 @@
#chat-form input[type="text"]:focus { border-color: var(--accent); } #chat-form input[type="text"]:focus { border-color: var(--accent); }
#chat-form button { #chat-form button {
padding: 0.6rem 1.1rem; padding: 0.58rem 1rem;
background: var(--accent); background: var(--accent);
color: #111318; color: #111318;
border: none; border: none;
@@ -268,9 +298,19 @@
font-size: 0.88rem; font-size: 0.88rem;
cursor: pointer; cursor: pointer;
transition: background 0.2s; transition: background 0.2s;
white-space: nowrap;
} }
#chat-form button:hover { background: var(--accent-dim); } #chat-form button:hover { background: var(--accent-dim); }
@media (max-width: 520px) {
body { align-items: stretch; padding: 4.2rem 0.75rem 0.75rem; }
.page-timer { top: 0.75rem; width: calc(100% - 1.5rem); text-align: center; justify-content: center; }
#start-form { align-self: center; padding: 1.45rem; border-radius: 18px; }
#chat-container { height: calc(100vh - 5rem); height: calc(100dvh - 5rem); border-radius: 18px; }
.chat-session-id { display: none; }
.msg-user, .msg-supporter { max-width: 86%; }
}
</style> </style>
</head> </head>
<body> <body>
@@ -291,8 +331,7 @@
<script> <script>
let ws = null; let ws = null;
let typingHideTimer = null; let userTypingActive = false;
let lastTypingSent = 0;
function scrollMessages() { function scrollMessages() {
const el = document.getElementById('messages'); const el = document.getElementById('messages');
@@ -303,27 +342,51 @@
const indicator = document.getElementById('peer-typing'); const indicator = document.getElementById('peer-typing');
if (!indicator) return; if (!indicator) return;
indicator.classList.add('visible'); indicator.classList.add('visible');
clearTimeout(typingHideTimer);
typingHideTimer = setTimeout(() => indicator.classList.remove('visible'), 1400);
scrollMessages(); scrollMessages();
} }
function sendUserTyping() { function hideSupporterTyping() {
const indicator = document.getElementById('peer-typing');
if (indicator) indicator.classList.remove('visible');
}
function sendUserTypingState(state) {
if (!ws || ws.readyState !== WebSocket.OPEN || !window.__sessionId) return; if (!ws || ws.readyState !== WebSocket.OPEN || !window.__sessionId) return;
const now = Date.now(); ws.send(`typing:user:${state}:${window.__sessionId}`);
if (now - lastTypingSent < 500) return; }
lastTypingSent = now;
ws.send(`typing:user:${window.__sessionId}`); function startUserTyping() {
if (userTypingActive) return;
userTypingActive = true;
sendUserTypingState('start');
}
function stopUserTyping() {
if (!userTypingActive) return;
sendUserTypingState('stop');
userTypingActive = false;
}
function handleUserTypingInput() {
const input = document.getElementById('chat-input');
if (input && input.value.trim()) startUserTyping();
else stopUserTyping();
} }
document.addEventListener('htmx:afterSwap', (e) => { document.addEventListener('htmx:afterSwap', (e) => {
if (e.detail.target.id === 'messages') scrollMessages(); if (e.detail.target.id === 'messages') scrollMessages();
}); });
document.addEventListener('htmx:beforeRequest', (e) => {
if (e.detail.elt && e.detail.elt.id === 'chat-form') stopUserTyping();
});
function resetChat() { function resetChat() {
stopUserTyping();
const oldWs = ws; const oldWs = ws;
ws = null; ws = null;
window.__sessionId = null; window.__sessionId = null;
userTypingActive = false;
if (oldWs) oldWs.close(); if (oldWs) oldWs.close();
const startForm = document.getElementById('start-form'); const startForm = document.getElementById('start-form');
const chatContainer = document.getElementById('chat-container'); const chatContainer = document.getElementById('chat-container');
@@ -349,11 +412,16 @@
resetChat(); resetChat();
return; return;
} }
if (message.data.startsWith('typing:supporter:')) { if (message.data.startsWith('typing:supporter:start:')) {
showSupporterTyping(); showSupporterTyping();
return; return;
} }
if (message.data.startsWith('typing:supporter:stop:')) {
hideSupporterTyping();
return;
}
if (message.data.startsWith('typing:user:')) return; if (message.data.startsWith('typing:user:')) return;
hideSupporterTyping();
htmx.trigger('#messages', 'refresh'); htmx.trigger('#messages', 'refresh');
}; };
ws.onerror = () => console.warn('WebSocket error; polling will continue.'); ws.onerror = () => console.warn('WebSocket error; polling will continue.');