add chat function
#
This commit is contained in:
@@ -0,0 +1,321 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<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: #0f1117;
|
||||
--surface: #1a1d27;
|
||||
--border: #2a2d3a;
|
||||
--accent: #6c63ff;
|
||||
--accent-dim:#4a44b5;
|
||||
--text: #e8e8f0;
|
||||
--muted: #6b6e80;
|
||||
--customer: #6c63ff;
|
||||
--agent: #22c55e;
|
||||
--radius: 12px;
|
||||
}
|
||||
|
||||
body {
|
||||
background: var(--bg);
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-family: 'Inter', system-ui, sans-serif;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
/* --- Start form --- */
|
||||
#start-form {
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 20px;
|
||||
padding: 2.5rem;
|
||||
width: 360px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#start-form .icon {
|
||||
font-size: 2.5rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
#start-form h1 {
|
||||
font-size: 1.4rem;
|
||||
font-weight: 700;
|
||||
margin-bottom: 0.4rem;
|
||||
letter-spacing: -0.02em;
|
||||
}
|
||||
|
||||
#start-form p {
|
||||
font-size: 0.9rem;
|
||||
color: var(--muted);
|
||||
margin-bottom: 1.8rem;
|
||||
}
|
||||
|
||||
#start-form input[type="text"] {
|
||||
width: 100%;
|
||||
padding: 0.75rem 1rem;
|
||||
background: var(--bg);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
color: var(--text);
|
||||
font-size: 0.95rem;
|
||||
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.75rem;
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
font-size: 0.95rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
#start-form button:hover { background: var(--accent-dim); }
|
||||
|
||||
/* --- Chat container --- */
|
||||
#chat-container {
|
||||
display: none;
|
||||
flex-direction: column;
|
||||
width: 380px;
|
||||
height: 580px;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 20px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#chat-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.6rem;
|
||||
padding: 1rem 1.2rem;
|
||||
border-bottom: 1px solid var(--border);
|
||||
font-weight: 600;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.chat-status-dot {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: 50%;
|
||||
background: var(--muted);
|
||||
}
|
||||
|
||||
.chat-status-dot.active { background: var(--agent); }
|
||||
|
||||
.chat-session-id {
|
||||
margin-left: auto;
|
||||
font-size: 0.75rem;
|
||||
color: var(--muted);
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
#messages {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 1rem;
|
||||
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; }
|
||||
|
||||
/* --- Message bubbles --- */
|
||||
.msg-wrap { display: flex; }
|
||||
|
||||
.msg-customer-wrap { justify-content: flex-end; }
|
||||
.msg-agent-wrap { justify-content: flex-start; }
|
||||
|
||||
.msg-customer, .msg-agent {
|
||||
max-width: 75%;
|
||||
padding: 0.6rem 0.9rem;
|
||||
border-radius: 16px;
|
||||
font-size: 0.88rem;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.msg-customer {
|
||||
background: var(--customer);
|
||||
color: #fff;
|
||||
border-bottom-right-radius: 4px;
|
||||
}
|
||||
|
||||
.msg-agent {
|
||||
background: var(--border);
|
||||
color: var(--text);
|
||||
border-bottom-left-radius: 4px;
|
||||
}
|
||||
|
||||
.msg-label {
|
||||
display: block;
|
||||
font-size: 0.7rem;
|
||||
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.68rem;
|
||||
opacity: 0.5;
|
||||
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;
|
||||
}
|
||||
|
||||
/* --- Typing indicator --- */
|
||||
#typing-indicator {
|
||||
display: none;
|
||||
padding: 0 1rem 0.5rem;
|
||||
gap: 4px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
#typing-indicator.htmx-request { display: flex; }
|
||||
|
||||
.typing-dot {
|
||||
width: 6px; height: 6px;
|
||||
border-radius: 50%;
|
||||
background: var(--muted);
|
||||
animation: bounce 1.2s infinite;
|
||||
}
|
||||
|
||||
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
|
||||
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
|
||||
|
||||
@keyframes bounce {
|
||||
0%, 80%, 100% { transform: translateY(0); }
|
||||
40% { transform: translateY(-6px); }
|
||||
}
|
||||
|
||||
/* --- Input form --- */
|
||||
#chat-form {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
padding: 0.75rem 1rem;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
#chat-form input[type="text"] {
|
||||
flex: 1;
|
||||
padding: 0.6rem 0.9rem;
|
||||
background: var(--bg);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
color: var(--text);
|
||||
font-size: 0.9rem;
|
||||
outline: none;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
#chat-form input[type="text"]:focus { border-color: var(--accent); }
|
||||
|
||||
#chat-form button {
|
||||
padding: 0.6rem 1.1rem;
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
font-weight: 600;
|
||||
font-size: 0.88rem;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
#chat-form button:hover { background: var(--accent-dim); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<!-- Step 1: Start form -->
|
||||
<div id="start-form">
|
||||
<div class="icon">💬</div>
|
||||
<h1>Talk to Support</h1>
|
||||
<p>We're here to help. Start a conversation and we'll get back to you right away.</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>
|
||||
|
||||
<!-- Step 2: Chat UI (injected by server after session creation) -->
|
||||
<div id="chat-container"></div>
|
||||
|
||||
<script>
|
||||
function scrollMessages() {
|
||||
const el = document.getElementById('messages');
|
||||
if (el) el.scrollTop = el.scrollHeight;
|
||||
}
|
||||
|
||||
// Auto-scroll when HTMX adds new messages
|
||||
document.addEventListener('htmx:afterSwap', (e) => {
|
||||
if (e.detail.target.id === 'messages') scrollMessages();
|
||||
});
|
||||
|
||||
// WebSocket for instant push (agent replies trigger a refresh)
|
||||
// Session ID is injected after session creation
|
||||
let ws = null;
|
||||
|
||||
document.addEventListener('htmx:afterRequest', (e) => {
|
||||
if (e.detail.requestConfig?.path === '/sessions/new' && !ws) {
|
||||
// Extract session id from the returned HTML
|
||||
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];
|
||||
|
||||
ws = new WebSocket(`ws://${location.host}/ws/customer/${sessionId}`);
|
||||
ws.onmessage = () => {
|
||||
// Trigger an out-of-band refresh of the messages div
|
||||
htmx.trigger('#messages', 'refresh');
|
||||
};
|
||||
ws.onerror = () => console.warn('WS error, falling back to polling');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user