Files
user-to-supporter-chat-rust/templates/user.html
T
2026-06-18 22:46:33 +01:00

464 lines
13 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, viewport-fit=cover" />
<title>Support Chat</title>
<script src="https://unpkg.com/htmx.org@1.9.12"></script>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg: #111318;
--surface: #191c22;
--surface-soft: #20242c;
--border: #2b303a;
--accent: #8f9cff;
--accent-dim: #737fd3;
--danger: #d93d3d;
--danger-soft: rgba(217, 61, 61, 0.16);
--danger-text: #ffd1d1;
--text: #eceff4;
--muted: #9aa3b2;
--user: #8f9cff;
--supporter: #2b303a;
--radius: 12px;
}
html, body {
width: 100%;
height: 100%;
height: var(--app-height, 100dvh);
overflow: hidden;
-webkit-text-size-adjust: 100%;
}
body {
background: var(--bg);
display: flex;
align-items: center;
justify-content: center;
font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
padding: 4.4rem 1rem max(1rem, env(safe-area-inset-bottom));
}
button, input { font: inherit; }
.page-timer {
display: inline-flex;
align-items: center;
justify-content: center;
position: fixed;
top: 1rem;
left: 50%;
transform: translateX(-50%);
color: var(--danger-text);
border: 1px solid var(--danger);
border-radius: 999px;
padding: 0.38rem 0.8rem;
font-size: 0.82rem;
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;
white-space: nowrap;
}
#start-form {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 20px;
padding: 2rem;
width: min(360px, 100%);
text-align: left;
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.22);
}
#start-form h1 {
font-size: 1.35rem;
font-weight: 700;
margin-bottom: 0.45rem;
letter-spacing: -0.02em;
}
#start-form p {
font-size: 0.9rem;
color: var(--muted);
margin-bottom: 1.35rem;
line-height: 1.55;
}
#start-form input[type="text"] {
width: 100%;
padding: 0.72rem 0.95rem;
background: var(--bg);
border: 1px solid var(--border);
border-radius: var(--radius);
color: var(--text);
font-size: 16px;
line-height: 1.25;
margin-bottom: 1rem;
outline: none;
transition: border-color 0.2s;
}
#start-form input[type="text"]:focus { border-color: var(--accent); }
#start-form button {
width: 100%;
padding: 0.72rem;
background: var(--accent);
color: #111318;
border: none;
border-radius: var(--radius);
font-size: 0.95rem;
font-weight: 700;
cursor: pointer;
transition: background 0.2s;
}
#start-form button:hover { background: var(--accent-dim); }
#chat-container {
display: none;
flex-direction: column;
width: min(390px, 100%);
height: min(620px, calc(var(--app-height, 100dvh) - 5.4rem));
background: var(--surface);
border: 1px solid var(--border);
border-radius: 20px;
overflow: hidden;
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.22);
}
#chat-header {
display: flex;
align-items: center;
gap: 0.6rem;
padding: 0.9rem 1rem;
border-bottom: 1px solid var(--border);
font-weight: 700;
font-size: 0.95rem;
min-height: 52px;
}
.chat-session-id {
margin-left: auto;
font-size: 0.72rem;
color: var(--muted);
font-weight: 400;
white-space: nowrap;
}
#messages {
flex: 1;
min-height: 0;
overflow-y: auto;
padding: 0.9rem;
display: flex;
flex-direction: column;
gap: 0.5rem;
scroll-behavior: smooth;
}
#messages::-webkit-scrollbar { width: 4px; }
#messages::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.msg-wrap { display: flex; }
.msg-user-wrap { justify-content: flex-end; }
.msg-supporter-wrap { justify-content: flex-start; }
.msg-user, .msg-supporter {
max-width: 78%;
padding: 0.58rem 0.85rem;
border-radius: 16px;
font-size: 0.88rem;
line-height: 1.48;
overflow-wrap: anywhere;
}
.msg-user {
background: var(--user);
color: #111318;
border-bottom-right-radius: 4px;
}
.msg-supporter {
background: var(--supporter);
color: var(--text);
border-bottom-left-radius: 4px;
}
.msg-label {
display: block;
font-size: 0.68rem;
font-weight: 600;
opacity: 0.7;
margin-bottom: 2px;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.msg-text { margin: 0; }
.msg-time {
display: block;
font-size: 0.66rem;
opacity: 0.55;
margin-top: 4px;
text-align: right;
}
.msg-system-wrap {
display: flex;
justify-content: center;
margin: 0.25rem 0;
}
.msg-system-text {
font-size: 0.75rem;
color: var(--muted);
background: var(--bg);
padding: 0.25rem 0.75rem;
border-radius: 20px;
text-align: center;
}
.typing-bubble {
display: none;
padding: 0 0.9rem 0.55rem;
justify-content: flex-start;
}
.typing-bubble.visible { display: flex; }
.typing-bubble-inner {
position: relative;
display: inline-flex;
align-items: center;
gap: 4px;
min-width: 48px;
padding: 0.58rem 0.78rem;
background: var(--supporter);
border-radius: 16px;
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 {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--muted);
animation: typing-bounce 1.1s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: 0.16s; }
.typing-dot:nth-child(3) { animation-delay: 0.32s; }
@keyframes typing-bounce {
0%, 75%, 100% { transform: translateY(0); opacity: 0.45; }
35% { transform: translateY(-5px); opacity: 1; }
}
#chat-form {
display: flex;
gap: 0.45rem;
padding: 0.65rem 0.85rem max(0.8rem, env(safe-area-inset-bottom));
border-top: 1px solid var(--border);
flex-shrink: 0;
}
#chat-form input[type="text"] {
flex: 1;
min-width: 0;
padding: 0.58rem 0.85rem;
background: var(--bg);
border: 1px solid var(--border);
border-radius: var(--radius);
color: var(--text);
font-size: 16px;
line-height: 1.25;
outline: none;
transition: border-color 0.2s;
}
#chat-form input[type="text"]:focus { border-color: var(--accent); }
#chat-form button {
padding: 0.58rem 1rem;
background: var(--accent);
color: #111318;
border: none;
border-radius: var(--radius);
font-weight: 700;
font-size: 0.88rem;
cursor: pointer;
transition: background 0.2s;
white-space: nowrap;
}
#chat-form button:hover { background: var(--accent-dim); }
@media (max-width: 520px) {
body { align-items: stretch; padding: 4.2rem 0.75rem max(0.75rem, env(safe-area-inset-bottom)); }
.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 { width: 100%; height: calc(var(--app-height, 100dvh) - 5rem); max-height: none; border-radius: 18px; }
.chat-session-id { display: none; }
.msg-user, .msg-supporter { max-width: 86%; }
#messages { padding: 0.8rem; }
#chat-form { gap: 0.4rem; }
#chat-form button { padding-inline: 0.85rem; }
}
</style>
</head>
<body>
<span class="page-timer" hx-get="/reset-countdown" hx-trigger="load, every 1s" hx-swap="innerHTML">Reset in --:--</span>
<div id="start-form">
<h1>Talk to support</h1>
<p>Start a conversation and a supporter can reply from the dashboard.</p>
<form hx-post="/sessions/new"
hx-target="body"
hx-swap="beforeend">
<input type="text" name="name" placeholder="Your name (optional)" maxlength="40" />
<button type="submit">Start chat</button>
</form>
</div>
<div id="chat-container"></div>
<script>
let ws = null;
let userTypingActive = false;
function setAppHeight() {
const height = window.visualViewport ? window.visualViewport.height : window.innerHeight;
document.documentElement.style.setProperty('--app-height', `${height}px`);
}
setAppHeight();
window.addEventListener('resize', setAppHeight);
if (window.visualViewport) {
window.visualViewport.addEventListener('resize', setAppHeight);
window.visualViewport.addEventListener('scroll', setAppHeight);
}
function scrollMessages() {
const el = document.getElementById('messages');
if (el) el.scrollTop = el.scrollHeight;
}
function showSupporterTyping() {
const indicator = document.getElementById('peer-typing');
if (!indicator) return;
indicator.classList.add('visible');
scrollMessages();
}
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;
ws.send(`typing:user:${state}:${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) => {
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() {
stopUserTyping();
const oldWs = ws;
ws = null;
window.__sessionId = null;
userTypingActive = false;
if (oldWs) oldWs.close();
const startForm = document.getElementById('start-form');
const chatContainer = document.getElementById('chat-container');
startForm.style.display = 'block';
chatContainer.style.display = 'none';
chatContainer.innerHTML = '';
alert('Chat history was cleared. Please start a new chat.');
}
document.addEventListener('htmx:afterRequest', (e) => {
if (e.detail.requestConfig?.path === '/sessions/new' && !ws) {
const container = document.getElementById('chat-container');
if (!container) return;
const match = container.innerHTML.match(/\/messages\/([a-f0-9-]+)/);
if (!match) return;
const sessionId = match[1];
window.__sessionId = sessionId;
const wsProtocol = location.protocol === 'https:' ? 'wss' : 'ws';
ws = new WebSocket(`${wsProtocol}://${location.host}/ws/user/${sessionId}`);
ws.onmessage = (message) => {
if (message.data === '__reset__') {
resetChat();
return;
}
if (message.data.startsWith('typing:supporter:start:')) {
showSupporterTyping();
return;
}
if (message.data.startsWith('typing:supporter:stop:')) {
hideSupporterTyping();
return;
}
if (message.data.startsWith('typing:user:')) return;
hideSupporterTyping();
htmx.trigger('#messages', 'refresh');
};
const chatInput = document.getElementById('chat-input');
if (chatInput) {
chatInput.addEventListener('focus', () => setTimeout(setAppHeight, 80));
chatInput.addEventListener('blur', () => setTimeout(setAppHeight, 80));
}
setAppHeight();
ws.onerror = () => console.warn('WebSocket error; polling will continue.');
}
});
</script>
</body>
</html>