Files

583 lines
17 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 — FLÓ</title>
<script>
(function () {
var stored = localStorage.getItem('poc-theme');
var theme = stored || (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'tokyo' : 'light');
document.documentElement.setAttribute('data-theme', theme);
})();
</script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500;700&display=swap" rel="stylesheet">
<script src="https://unpkg.com/htmx.org@1.9.12"></script>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root,
[data-theme="light"] {
--bg: #f4f1ec;
--bg-panel: rgba(255, 253, 250, 0.72);
--nav-bg: rgba(244, 241, 236, 0.6);
--ink: #1a1d22;
--ink-muted: #595f68;
--ink-dim: #9299a2;
--accent: #2c5a7a;
--border: rgba(26, 29, 34, 0.1);
--danger: #a03030;
--danger-soft: rgba(160, 48, 48, 0.08);
--danger-text: #a03030;
--user-bubble: #2c5a7a;
--user-bubble-ink: #f4f1ec;
--peer-bubble: rgba(26, 29, 34, 0.06);
--peer-bubble-ink: #1a1d22;
--input-bg: rgba(26, 29, 34, 0.04);
}
[data-theme="tokyo"] {
--bg: #07070d;
--bg-panel: rgba(15, 16, 28, 0.72);
--nav-bg: rgba(7, 7, 13, 0.65);
--ink: #e8f4ff;
--ink-muted: #a3b3cc;
--ink-dim: #7886a0;
--accent: #64dcff;
--border: rgba(100, 220, 255, 0.12);
--danger: #d93d3d;
--danger-soft: rgba(217, 61, 61, 0.16);
--danger-text: #ffd1d1;
--user-bubble: #64dcff;
--user-bubble-ink: #07070d;
--peer-bubble: rgba(100, 220, 255, 0.08);
--peer-bubble-ink: #e8f4ff;
--input-bg: rgba(100, 220, 255, 0.04);
}
html, body {
width: 100%;
height: 100%;
height: var(--app-height, 100dvh);
overflow: hidden;
-webkit-text-size-adjust: 100%;
}
body {
background: var(--bg);
color: var(--ink);
font-family: 'JetBrains Mono', monospace;
font-size: 0.92rem;
font-weight: 300;
line-height: 1.65;
display: flex;
align-items: center;
justify-content: center;
padding: 4.4rem 1rem max(1rem, env(safe-area-inset-bottom));
transition: background 0.3s ease, color 0.3s ease;
}
button, input { font: inherit; }
a {
color: var(--accent);
text-decoration: none;
border-bottom: 1px solid transparent;
transition: all 0.2s ease;
}
a:hover { border-bottom-color: var(--accent); }
/* ---- Timer ---- */
.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: 2px;
padding: 0.3rem 0.7rem;
font-family: 'JetBrains Mono', monospace;
font-size: 0.72rem;
font-weight: 500;
letter-spacing: 0.08em;
background: var(--danger-soft);
z-index: 10;
white-space: nowrap;
}
/* ---- Start form ---- */
#start-form {
background: var(--bg-panel);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
border: 1px solid var(--border);
border-radius: 3px;
padding: 3rem 3.5rem;
width: min(400px, 100%);
text-align: left;
box-shadow: 0 8px 30px rgba(26, 29, 34, 0.06);
animation: panel-in 0.7s cubic-bezier(0.16, 1, 0.3, 1) forwards;
opacity: 0;
transform: translateY(10px);
transition: background 0.3s ease, border-color 0.3s ease;
}
#start-form h1 {
font-family: 'JetBrains Mono', monospace;
font-weight: 700;
font-size: 1.5rem;
letter-spacing: -0.02em;
margin-bottom: 0.6rem;
color: var(--ink);
}
[data-theme="tokyo"] #start-form h1 {
text-shadow: 0 0 10px rgba(100, 220, 255, 0.3);
}
#start-form p {
font-size: 0.85rem;
color: var(--ink-muted);
margin-bottom: 2rem;
line-height: 1.75;
}
#start-form input[type="text"] {
width: 100%;
padding: 0.65rem 0.85rem;
background: var(--input-bg);
border: 1px solid var(--border);
border-radius: 3px;
color: var(--ink);
font-size: 0.88rem;
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 input[type="text"]::placeholder { color: var(--ink-dim); }
#start-form button {
width: 100%;
padding: 0.65rem;
background: var(--accent);
color: var(--bg);
border: none;
border-radius: 3px;
font-family: 'JetBrains Mono', monospace;
font-size: 0.82rem;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: opacity 0.2s;
}
#start-form button:hover { opacity: 0.85; }
/* ---- Chat container ---- */
#chat-container {
display: none;
flex-direction: column;
width: min(420px, 100%);
height: min(620px, calc(var(--app-height, 100dvh) - 5.4rem));
background: var(--bg-panel);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
border: 1px solid var(--border);
border-radius: 3px;
overflow: hidden;
box-shadow: 0 8px 30px rgba(26, 29, 34, 0.06);
transition: background 0.3s ease, border-color 0.3s ease;
}
#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.88rem;
min-height: 52px;
}
.chat-session-id {
margin-left: auto;
font-size: 0.68rem;
color: var(--ink-dim);
font-weight: 400;
white-space: nowrap;
letter-spacing: 0.04em;
}
/* ---- Messages ---- */
#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: 3px; }
#messages::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.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.55rem 0.8rem;
border-radius: 12px;
font-size: 0.85rem;
line-height: 1.55;
overflow-wrap: anywhere;
}
.msg-user {
background: var(--user-bubble);
color: var(--user-bubble-ink);
border-bottom-right-radius: 3px;
}
.msg-supporter {
background: var(--peer-bubble);
color: var(--peer-bubble-ink);
border-bottom-left-radius: 3px;
}
.msg-label {
display: block;
font-size: 0.62rem;
font-weight: 500;
opacity: 0.7;
margin-bottom: 2px;
text-transform: uppercase;
letter-spacing: 0.12em;
}
.msg-text { margin: 0; }
.msg-time {
display: block;
font-size: 0.6rem;
opacity: 0.5;
margin-top: 4px;
text-align: right;
letter-spacing: 0.04em;
}
.msg-system-wrap {
display: flex;
justify-content: center;
margin: 0.25rem 0;
}
.msg-system-text {
font-size: 0.7rem;
color: var(--ink-dim);
background: var(--input-bg);
padding: 0.25rem 0.75rem;
border-radius: 2px;
text-align: center;
letter-spacing: 0.04em;
}
/* ---- Typing bubble ---- */
.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.55rem 0.75rem;
background: var(--peer-bubble);
border-radius: 12px;
border-bottom-left-radius: 3px;
}
.typing-dot {
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--ink-dim);
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(-4px); opacity: 1; }
}
/* ---- Chat input ---- */
#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.55rem 0.8rem;
background: var(--input-bg);
border: 1px solid var(--border);
border-radius: 3px;
color: var(--ink);
font-size: 0.85rem;
line-height: 1.25;
outline: none;
transition: border-color 0.2s;
}
#chat-form input[type="text"]:focus { border-color: var(--accent); }
#chat-form input[type="text"]::placeholder { color: var(--ink-dim); }
#chat-form button {
padding: 0.55rem 0.9rem;
background: var(--accent);
color: var(--bg);
border: none;
border-radius: 3px;
font-family: 'JetBrains Mono', monospace;
font-weight: 700;
font-size: 0.78rem;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition: opacity 0.2s;
white-space: nowrap;
}
#chat-form button:hover { opacity: 0.85; }
/* ---- Theme toggle ---- */
.theme-toggle {
position: fixed;
bottom: 1.5rem;
right: 1.5rem;
width: 28px;
height: 28px;
border-radius: 50%;
border: 1px solid var(--border);
cursor: pointer;
padding: 0;
z-index: 30;
transition: all 0.3s ease;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}
.theme-toggle:hover { transform: scale(1.08); }
[data-theme="light"] .theme-toggle {
background: linear-gradient(135deg, #07070d 0%, #64dcff 100%);
border-color: rgba(26, 29, 34, 0.2);
}
[data-theme="tokyo"] .theme-toggle {
background: #f4f1ec;
border-color: rgba(100, 220, 255, 0.4);
box-shadow:
0 0 10px rgba(100, 220, 255, 0.5),
0 0 25px rgba(100, 220, 255, 0.3);
}
@keyframes panel-in {
0% { opacity: 0; transform: translateY(10px); }
100% { opacity: 1; transform: translateY(0); }
}
@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: 2rem 1.5rem; }
#chat-container { width: 100%; height: calc(var(--app-height, 100dvh) - 5rem); max-height: none; }
.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.75rem; }
.theme-toggle { width: 20px; height: 20px; bottom: 1rem; right: 0.9rem; }
}
</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>
<button class="theme-toggle" id="theme-toggle" aria-label="Toggle theme"></button>
<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.');
}
});
// Theme toggle
(() => {
function currentSystemTheme() {
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'tokyo' : 'light';
}
const btn = document.getElementById('theme-toggle');
if (btn) {
btn.addEventListener('click', () => {
const current = document.documentElement.getAttribute('data-theme') || 'light';
const next = current === 'light' ? 'tokyo' : 'light';
document.documentElement.setAttribute('data-theme', next);
localStorage.setItem('poc-theme', next);
});
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
if (!localStorage.getItem('poc-theme')) {
document.documentElement.setAttribute('data-theme', currentSystemTheme());
}
});
})();
</script>
</body>
</html>