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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>NeuroCheck — Admin Panel</title>
|
| 7 |
+
<link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville:wght@400;700&family=DM+Sans:wght@300;400;500;600&family=DM+Mono:wght@400;500&display=swap" rel="stylesheet">
|
| 8 |
+
<style>
|
| 9 |
+
:root {
|
| 10 |
+
--navy:#0B1F3A;--teal:#0A7B74;--teal-lt:#E6F5F4;--red:#B53535;--amber:#C97B2A;
|
| 11 |
+
--sand:#F5F1EB;--warm-white:#FDFAF6;--border:#DDD8CF;--text:#1A1510;--muted:#6B6056;
|
| 12 |
+
}
|
| 13 |
+
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
|
| 14 |
+
body{font-family:'DM Sans',sans-serif;background:var(--warm-white);color:var(--text);min-height:100vh;}
|
| 15 |
+
|
| 16 |
+
/* AUTH */
|
| 17 |
+
#authOverlay{position:fixed;inset:0;background:rgba(11,31,58,0.92);backdrop-filter:blur(8px);display:flex;align-items:center;justify-content:center;z-index:100;}
|
| 18 |
+
.auth-card{background:#fff;border-radius:16px;padding:40px 44px;width:400px;max-width:95vw;box-shadow:0 24px 64px rgba(0,0,0,0.35);}
|
| 19 |
+
.auth-logo{display:flex;align-items:center;gap:12px;margin-bottom:28px;}
|
| 20 |
+
.auth-logo-mark{width:42px;height:42px;background:var(--red);border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:18px;font-weight:700;color:#fff;font-family:'Libre Baskerville',serif;}
|
| 21 |
+
.auth-logo h2{font-family:'Libre Baskerville',serif;font-size:18px;color:var(--navy);font-weight:700;}
|
| 22 |
+
.auth-logo p{font-size:12px;color:var(--muted);}
|
| 23 |
+
.auth-field{margin-bottom:16px;}
|
| 24 |
+
.auth-field label{display:block;font-size:11px;font-weight:600;color:var(--muted);margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;}
|
| 25 |
+
.auth-field input{width:100%;padding:11px 14px;border:1.5px solid var(--border);border-radius:9px;font-family:'DM Sans',sans-serif;font-size:14px;color:var(--text);outline:none;transition:border-color 0.2s;}
|
| 26 |
+
.auth-field input:focus{border-color:var(--red);}
|
| 27 |
+
.auth-btn{width:100%;padding:12px;background:var(--navy);color:#fff;border:none;border-radius:9px;font-family:'DM Sans',sans-serif;font-size:14px;font-weight:600;cursor:pointer;transition:background 0.2s;}
|
| 28 |
+
.auth-btn:hover{background:#162F52;}
|
| 29 |
+
.auth-error{background:#FEE2E2;border:1px solid #FECACA;border-radius:8px;padding:9px 14px;font-size:13px;color:#B91C1C;margin-bottom:14px;display:none;}
|
| 30 |
+
|
| 31 |
+
/* SHELL */
|
| 32 |
+
#adminShell{display:none;}
|
| 33 |
+
#adminShell.visible{display:block;}
|
| 34 |
+
header{background:var(--navy);padding:14px 28px;display:flex;align-items:center;gap:14px;border-bottom:3px solid var(--red);}
|
| 35 |
+
.logo-mark{width:34px;height:34px;background:var(--red);border-radius:8px;display:flex;align-items:center;justify-content:center;font-family:'Libre Baskerville',serif;font-size:15px;font-weight:700;color:#fff;}
|
| 36 |
+
.header-text h1{font-family:'Libre Baskerville',serif;font-size:17px;color:#fff;}
|
| 37 |
+
.header-text p{font-size:11px;color:#8FA8BF;}
|
| 38 |
+
.header-right{margin-left:auto;display:flex;align-items:center;gap:12px;}
|
| 39 |
+
.hdr-stat{background:rgba(255,255,255,0.08);border-radius:8px;padding:6px 14px;text-align:center;}
|
| 40 |
+
.hdr-stat .n{font-size:18px;font-weight:700;color:#fff;font-family:'Libre Baskerville',serif;}
|
| 41 |
+
.hdr-stat .l{font-size:10px;color:#8FA8BF;margin-top:1px;}
|
| 42 |
+
.sign-out-btn{background:rgba(181,53,53,0.2);border:1px solid rgba(181,53,53,0.4);color:#F87171;border-radius:8px;padding:6px 14px;font-size:12px;font-weight:500;cursor:pointer;transition:background 0.15s;}
|
| 43 |
+
.sign-out-btn:hover{background:rgba(181,53,53,0.35);}
|
| 44 |
+
|
| 45 |
+
/* LAYOUT */
|
| 46 |
+
.page{display:flex;height:calc(100vh - 59px);overflow:hidden;}
|
| 47 |
+
|
| 48 |
+
/* SIDEBAR */
|
| 49 |
+
.sidebar{width:280px;background:#F8F5F0;border-right:1px solid var(--border);display:flex;flex-direction:column;flex-shrink:0;}
|
| 50 |
+
.sidebar-section{padding:14px 16px 8px;border-bottom:1px solid var(--border);}
|
| 51 |
+
.sidebar-section-title{font-size:11px;font-weight:600;color:var(--muted);text-transform:uppercase;letter-spacing:0.6px;margin-bottom:8px;}
|
| 52 |
+
.search-input{width:100%;padding:8px 12px;border:1.5px solid var(--border);border-radius:8px;font-family:'DM Sans',sans-serif;font-size:13px;outline:none;background:#fff;}
|
| 53 |
+
.search-input:focus{border-color:var(--teal);}
|
| 54 |
+
.user-list{flex:1;overflow-y:auto;padding:8px;}
|
| 55 |
+
.user-list::-webkit-scrollbar{width:4px;}
|
| 56 |
+
.user-list::-webkit-scrollbar-thumb{background:var(--border);border-radius:2px;}
|
| 57 |
+
.user-item{padding:10px 12px;border-radius:9px;cursor:pointer;transition:background 0.12s;margin-bottom:2px;}
|
| 58 |
+
.user-item:hover{background:#EDEBE6;}
|
| 59 |
+
.user-item.active{background:var(--teal-lt);border-left:3px solid var(--teal);}
|
| 60 |
+
.user-item-name{font-size:13px;font-weight:600;color:var(--text);}
|
| 61 |
+
.user-item-meta{font-size:11px;color:var(--muted);margin-top:2px;}
|
| 62 |
+
.user-badge{display:inline-block;background:var(--teal-lt);color:var(--teal);font-size:10px;font-weight:600;padding:1px 7px;border-radius:10px;margin-left:6px;}
|
| 63 |
+
|
| 64 |
+
/* MAIN */
|
| 65 |
+
.main{flex:1;overflow-y:auto;padding:24px 28px;}
|
| 66 |
+
.main::-webkit-scrollbar{width:6px;}
|
| 67 |
+
.main::-webkit-scrollbar-thumb{background:var(--border);border-radius:3px;}
|
| 68 |
+
|
| 69 |
+
/* EMPTY STATE */
|
| 70 |
+
.empty-state{display:flex;flex-direction:column;align-items:center;justify-content:center;height:100%;gap:12px;color:var(--muted);}
|
| 71 |
+
.empty-state .icon{font-size:48px;opacity:0.4;}
|
| 72 |
+
.empty-state p{font-size:14px;}
|
| 73 |
+
|
| 74 |
+
/* USER HEADER */
|
| 75 |
+
.user-header{display:flex;align-items:center;gap:16px;margin-bottom:24px;padding-bottom:20px;border-bottom:1px solid var(--border);}
|
| 76 |
+
.user-avatar-lg{width:52px;height:52px;border-radius:50%;background:var(--teal);display:flex;align-items:center;justify-content:center;font-size:22px;font-weight:700;color:#fff;}
|
| 77 |
+
.user-header-info h2{font-family:'Libre Baskerville',serif;font-size:20px;color:var(--navy);}
|
| 78 |
+
.user-header-info p{font-size:12px;color:var(--muted);margin-top:3px;}
|
| 79 |
+
.user-stats{display:flex;gap:16px;margin-left:auto;}
|
| 80 |
+
.user-stat{text-align:center;}
|
| 81 |
+
.user-stat .n{font-size:20px;font-weight:700;color:var(--navy);font-family:'Libre Baskerville',serif;}
|
| 82 |
+
.user-stat .l{font-size:11px;color:var(--muted);}
|
| 83 |
+
|
| 84 |
+
/* SESSIONS */
|
| 85 |
+
.sessions-grid{display:flex;flex-direction:column;gap:16px;}
|
| 86 |
+
.session-card{background:#fff;border:1px solid var(--border);border-radius:12px;overflow:hidden;}
|
| 87 |
+
.session-card-header{padding:14px 18px;display:flex;align-items:center;gap:12px;cursor:pointer;background:#FAFAF8;border-bottom:1px solid var(--border);transition:background 0.12s;}
|
| 88 |
+
.session-card-header:hover{background:#F2F0EB;}
|
| 89 |
+
.session-card-header.open{background:var(--teal-lt);border-bottom-color:rgba(10,123,116,0.2);}
|
| 90 |
+
.session-icon{font-size:16px;}
|
| 91 |
+
.session-title-wrap{flex:1;min-width:0;}
|
| 92 |
+
.session-title{font-size:13px;font-weight:600;color:var(--text);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
|
| 93 |
+
.session-date{font-size:11px;color:var(--muted);margin-top:2px;}
|
| 94 |
+
.session-toggle{font-size:12px;color:var(--muted);transition:transform 0.2s;}
|
| 95 |
+
.session-toggle.open{transform:rotate(180deg);}
|
| 96 |
+
.session-body{display:none;padding:0;}
|
| 97 |
+
.session-body.open{display:block;}
|
| 98 |
+
|
| 99 |
+
/* MESSAGES */
|
| 100 |
+
.msg-row{display:flex;gap:12px;padding:14px 18px;border-bottom:1px solid #F0EDE8;}
|
| 101 |
+
.msg-row:last-child{border-bottom:none;}
|
| 102 |
+
.msg-row.human{background:#FAFAF8;}
|
| 103 |
+
.msg-row.assistant{background:#fff;}
|
| 104 |
+
.msg-role{width:80px;flex-shrink:0;}
|
| 105 |
+
.msg-role-badge{display:inline-block;font-size:10px;font-weight:600;padding:2px 8px;border-radius:10px;text-transform:uppercase;letter-spacing:0.4px;}
|
| 106 |
+
.msg-role-badge.human{background:#E0EAF4;color:#185FA5;}
|
| 107 |
+
.msg-role-badge.assistant{background:var(--teal-lt);color:var(--teal);}
|
| 108 |
+
.msg-content{flex:1;font-size:13px;color:var(--text);line-height:1.7;white-space:pre-wrap;word-break:break-word;}
|
| 109 |
+
.msg-time{font-size:10px;color:#C0B8AF;flex-shrink:0;padding-top:2px;font-family:'DM Mono',monospace;}
|
| 110 |
+
|
| 111 |
+
/* EXPORT */
|
| 112 |
+
.export-bar{display:flex;gap:10px;margin-bottom:20px;align-items:center;}
|
| 113 |
+
.export-btn{padding:8px 18px;border-radius:8px;font-family:'DM Sans',sans-serif;font-size:13px;font-weight:500;cursor:pointer;border:1.5px solid var(--border);background:#fff;color:var(--text);transition:background 0.15s,border-color 0.15s;}
|
| 114 |
+
.export-btn:hover{background:var(--sand);border-color:var(--teal);}
|
| 115 |
+
.export-btn.primary{background:var(--navy);color:#fff;border-color:var(--navy);}
|
| 116 |
+
.export-btn.primary:hover{background:#162F52;}
|
| 117 |
+
.section-label{font-size:12px;color:var(--muted);margin-left:auto;}
|
| 118 |
+
|
| 119 |
+
/* LOADING */
|
| 120 |
+
.loading{display:flex;align-items:center;gap:8px;padding:20px;font-size:13px;color:var(--muted);}
|
| 121 |
+
.spin{width:16px;height:16px;border:2px solid var(--border);border-top-color:var(--teal);border-radius:50%;animation:spin 0.7s linear infinite;flex-shrink:0;}
|
| 122 |
+
@keyframes spin{to{transform:rotate(360deg);}}
|
| 123 |
+
</style>
|
| 124 |
+
</head>
|
| 125 |
+
<body>
|
| 126 |
+
|
| 127 |
+
<!-- AUTH -->
|
| 128 |
+
<div id="authOverlay">
|
| 129 |
+
<div class="auth-card">
|
| 130 |
+
<div class="auth-logo">
|
| 131 |
+
<div class="auth-logo-mark">⚙</div>
|
| 132 |
+
<div><h2>NeuroCheck Admin</h2><p>Restricted access — authorised personnel only</p></div>
|
| 133 |
+
</div>
|
| 134 |
+
<div id="authError" class="auth-error"></div>
|
| 135 |
+
<div class="auth-field">
|
| 136 |
+
<label>Username</label>
|
| 137 |
+
<input type="text" id="adminUser" placeholder="admin username" autocomplete="username">
|
| 138 |
+
</div>
|
| 139 |
+
<div class="auth-field">
|
| 140 |
+
<label>Password</label>
|
| 141 |
+
<input type="password" id="adminPass" placeholder="••••••••" autocomplete="current-password">
|
| 142 |
+
</div>
|
| 143 |
+
<button class="auth-btn" onclick="adminLogin()">Access Admin Panel</button>
|
| 144 |
+
</div>
|
| 145 |
+
</div>
|
| 146 |
+
|
| 147 |
+
<!-- SHELL -->
|
| 148 |
+
<div id="adminShell">
|
| 149 |
+
<header>
|
| 150 |
+
<div class="logo-mark">⚙</div>
|
| 151 |
+
<div class="header-text">
|
| 152 |
+
<h1>NeuroCheck Admin</h1>
|
| 153 |
+
<p>User & Conversation Management</p>
|
| 154 |
+
</div>
|
| 155 |
+
<div class="header-right">
|
| 156 |
+
<div class="hdr-stat"><div class="n" id="statUsers">—</div><div class="l">Users</div></div>
|
| 157 |
+
<div class="hdr-stat"><div class="n" id="statSessions">—</div><div class="l">Sessions</div></div>
|
| 158 |
+
<div class="hdr-stat"><div class="n" id="statMessages">—</div><div class="l">Messages</div></div>
|
| 159 |
+
<button class="sign-out-btn" onclick="adminSignOut()">Sign Out</button>
|
| 160 |
+
</div>
|
| 161 |
+
</header>
|
| 162 |
+
|
| 163 |
+
<div class="page">
|
| 164 |
+
<div class="sidebar">
|
| 165 |
+
<div class="sidebar-section">
|
| 166 |
+
<div class="sidebar-section-title">Users</div>
|
| 167 |
+
<input type="text" class="search-input" id="userSearch" placeholder="Search users…" oninput="filterUsers()">
|
| 168 |
+
</div>
|
| 169 |
+
<div class="user-list" id="userList">
|
| 170 |
+
<div class="loading"><div class="spin"></div> Loading users…</div>
|
| 171 |
+
</div>
|
| 172 |
+
</div>
|
| 173 |
+
|
| 174 |
+
<div class="main" id="mainPanel">
|
| 175 |
+
<div class="empty-state">
|
| 176 |
+
<div class="icon">👤</div>
|
| 177 |
+
<p>Select a user from the sidebar to view their conversations</p>
|
| 178 |
+
</div>
|
| 179 |
+
</div>
|
| 180 |
+
</div>
|
| 181 |
+
</div>
|
| 182 |
+
|
| 183 |
+
<script>
|
| 184 |
+
const API = window.location.origin;
|
| 185 |
+
let adminToken = sessionStorage.getItem('nc_admin_token') || null;
|
| 186 |
+
let allUsers = [];
|
| 187 |
+
let selectedUserId = null;
|
| 188 |
+
|
| 189 |
+
window.addEventListener('DOMContentLoaded', () => {
|
| 190 |
+
document.getElementById('adminPass').addEventListener('keydown', e => { if(e.key==='Enter') adminLogin(); });
|
| 191 |
+
if (adminToken) showAdmin();
|
| 192 |
+
});
|
| 193 |
+
|
| 194 |
+
// ── Auth ──
|
| 195 |
+
async function adminLogin() {
|
| 196 |
+
const user = document.getElementById('adminUser').value.trim();
|
| 197 |
+
const pass = document.getElementById('adminPass').value;
|
| 198 |
+
const err = document.getElementById('authError');
|
| 199 |
+
err.style.display = 'none';
|
| 200 |
+
if (!user || !pass) { err.textContent='Fill in all fields'; err.style.display='block'; return; }
|
| 201 |
+
|
| 202 |
+
const form = new URLSearchParams();
|
| 203 |
+
form.append('username', user);
|
| 204 |
+
form.append('password', pass);
|
| 205 |
+
|
| 206 |
+
try {
|
| 207 |
+
const res = await fetch(`${API}/auth/login`, {
|
| 208 |
+
method:'POST', headers:{'Content-Type':'application/x-www-form-urlencoded'}, body:form
|
| 209 |
+
});
|
| 210 |
+
const data = await res.json();
|
| 211 |
+
if (!res.ok) throw new Error(data.detail || 'Login failed');
|
| 212 |
+
|
| 213 |
+
// Check admin flag
|
| 214 |
+
const meRes = await fetch(`${API}/auth/me`, { headers:{'Authorization':`Bearer ${data.access_token}`} });
|
| 215 |
+
const me = await meRes.json();
|
| 216 |
+
if (!me.is_admin) throw new Error('Access denied — not an admin account');
|
| 217 |
+
|
| 218 |
+
adminToken = data.access_token;
|
| 219 |
+
sessionStorage.setItem('nc_admin_token', adminToken);
|
| 220 |
+
showAdmin();
|
| 221 |
+
} catch(e) {
|
| 222 |
+
err.textContent = e.message;
|
| 223 |
+
err.style.display = 'block';
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
function adminSignOut() {
|
| 228 |
+
sessionStorage.removeItem('nc_admin_token');
|
| 229 |
+
adminToken = null;
|
| 230 |
+
document.getElementById('adminShell').classList.remove('visible');
|
| 231 |
+
document.getElementById('authOverlay').style.display = 'flex';
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
function showAdmin() {
|
| 235 |
+
document.getElementById('authOverlay').style.display = 'none';
|
| 236 |
+
document.getElementById('adminShell').classList.add('visible');
|
| 237 |
+
loadDashboard();
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
function authFetch(path, opts={}) {
|
| 241 |
+
return fetch(`${API}${path}`, { ...opts, headers:{...(opts.headers||{}), 'Authorization':`Bearer ${adminToken}`} });
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
// ── Dashboard ──
|
| 245 |
+
async function loadDashboard() {
|
| 246 |
+
try {
|
| 247 |
+
const res = await authFetch('/admin/users');
|
| 248 |
+
const data = await res.json();
|
| 249 |
+
allUsers = data.users || [];
|
| 250 |
+
document.getElementById('statUsers').textContent = allUsers.length;
|
| 251 |
+
document.getElementById('statSessions').textContent = data.total_sessions || 0;
|
| 252 |
+
document.getElementById('statMessages').textContent = data.total_messages || 0;
|
| 253 |
+
renderUserList(allUsers);
|
| 254 |
+
} catch(e) { console.error(e); }
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
function renderUserList(users) {
|
| 258 |
+
const list = document.getElementById('userList');
|
| 259 |
+
if (!users.length) { list.innerHTML = '<div style="padding:16px;font-size:13px;color:var(--muted)">No users found</div>'; return; }
|
| 260 |
+
list.innerHTML = users.map(u => `
|
| 261 |
+
<div class="user-item ${u.id===selectedUserId?'active':''}" onclick="selectUser(${u.id},'${escHtml(u.username)}','${escHtml(u.email)}','${u.created_at}',${u.session_count},${u.message_count})">
|
| 262 |
+
<div class="user-item-name">${escHtml(u.username)}<span class="user-badge">${u.session_count}</span></div>
|
| 263 |
+
<div class="user-item-meta">${escHtml(u.email)} · ${fmtDate(u.created_at)}</div>
|
| 264 |
+
</div>
|
| 265 |
+
`).join('');
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
function filterUsers() {
|
| 269 |
+
const q = document.getElementById('userSearch').value.toLowerCase();
|
| 270 |
+
renderUserList(allUsers.filter(u => u.username.toLowerCase().includes(q) || u.email.toLowerCase().includes(q)));
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
// ── User Detail ──
|
| 274 |
+
async function selectUser(id, username, email, createdAt, sessionCount, messageCount) {
|
| 275 |
+
selectedUserId = id;
|
| 276 |
+
document.querySelectorAll('.user-item').forEach(el => el.classList.remove('active'));
|
| 277 |
+
document.querySelectorAll('.user-item').forEach(el => {
|
| 278 |
+
if (el.querySelector('.user-item-name').textContent.startsWith(username)) el.classList.add('active');
|
| 279 |
+
});
|
| 280 |
+
|
| 281 |
+
const main = document.getElementById('mainPanel');
|
| 282 |
+
main.innerHTML = `<div class="loading"><div class="spin"></div> Loading conversations…</div>`;
|
| 283 |
+
|
| 284 |
+
try {
|
| 285 |
+
const res = await authFetch(`/admin/user/${id}/sessions`);
|
| 286 |
+
const data = await res.json();
|
| 287 |
+
const sessions = data.sessions || [];
|
| 288 |
+
|
| 289 |
+
main.innerHTML = `
|
| 290 |
+
<div class="user-header">
|
| 291 |
+
<div class="user-avatar-lg">${username[0].toUpperCase()}</div>
|
| 292 |
+
<div class="user-header-info">
|
| 293 |
+
<h2>${escHtml(username)}</h2>
|
| 294 |
+
<p>${escHtml(email)} · Joined ${fmtDate(createdAt)}</p>
|
| 295 |
+
</div>
|
| 296 |
+
<div class="user-stats">
|
| 297 |
+
<div class="user-stat"><div class="n">${sessionCount}</div><div class="l">Sessions</div></div>
|
| 298 |
+
<div class="user-stat"><div class="n">${messageCount}</div><div class="l">Messages</div></div>
|
| 299 |
+
</div>
|
| 300 |
+
</div>
|
| 301 |
+
<div class="export-bar">
|
| 302 |
+
<button class="export-btn primary" onclick="exportUserCSV(${id},'${username}')">⬇ Export CSV</button>
|
| 303 |
+
<button class="export-btn" onclick="exportUserJSON(${id},'${username}')">⬇ Export JSON</button>
|
| 304 |
+
<span class="section-label">${sessions.length} conversation${sessions.length!==1?'s':''}</span>
|
| 305 |
+
</div>
|
| 306 |
+
<div class="sessions-grid" id="sessionsGrid">
|
| 307 |
+
${sessions.length === 0
|
| 308 |
+
? '<div style="color:var(--muted);font-size:14px;padding:20px 0">No conversations yet.</div>'
|
| 309 |
+
: sessions.map((s,i) => renderSessionCard(s,i)).join('')}
|
| 310 |
+
</div>
|
| 311 |
+
`;
|
| 312 |
+
} catch(e) { main.innerHTML = `<div style="color:var(--red);padding:20px">Error loading sessions: ${e.message}</div>`; }
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
function renderSessionCard(s, idx) {
|
| 316 |
+
const msgs = s.messages || [];
|
| 317 |
+
const humanCount = msgs.filter(m=>m.role==='human').length;
|
| 318 |
+
return `
|
| 319 |
+
<div class="session-card">
|
| 320 |
+
<div class="session-card-header" onclick="toggleSession('sess_${idx}')">
|
| 321 |
+
<div class="session-icon">💬</div>
|
| 322 |
+
<div class="session-title-wrap">
|
| 323 |
+
<div class="session-title">${escHtml(s.title||'Untitled Chat')}</div>
|
| 324 |
+
<div class="session-date">${fmtDateFull(s.created_at)} · ${humanCount} message${humanCount!==1?'s':''} from user</div>
|
| 325 |
+
</div>
|
| 326 |
+
<div class="session-toggle" id="toggle_sess_${idx}">▼</div>
|
| 327 |
+
</div>
|
| 328 |
+
<div class="session-body" id="sess_${idx}">
|
| 329 |
+
${msgs.map(m => `
|
| 330 |
+
<div class="msg-row ${m.role}">
|
| 331 |
+
<div class="msg-role"><span class="msg-role-badge ${m.role}">${m.role==='human'?'Patient':'NeuroCheck'}</span></div>
|
| 332 |
+
<div class="msg-content">${escHtml(m.content)}</div>
|
| 333 |
+
<div class="msg-time">${fmtTime(m.created_at)}</div>
|
| 334 |
+
</div>
|
| 335 |
+
`).join('')}
|
| 336 |
+
</div>
|
| 337 |
+
</div>
|
| 338 |
+
`;
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
+
function toggleSession(id) {
|
| 342 |
+
const body = document.getElementById(id);
|
| 343 |
+
const toggle = document.getElementById('toggle_'+id);
|
| 344 |
+
const header = body.previousElementSibling;
|
| 345 |
+
const isOpen = body.classList.contains('open');
|
| 346 |
+
body.classList.toggle('open', !isOpen);
|
| 347 |
+
toggle.classList.toggle('open', !isOpen);
|
| 348 |
+
header.classList.toggle('open', !isOpen);
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
// ── Export ──
|
| 352 |
+
async function exportUserCSV(userId, username) {
|
| 353 |
+
const res = await authFetch(`/admin/user/${userId}/sessions`);
|
| 354 |
+
const data = await res.json();
|
| 355 |
+
const rows = [['username','session_title','role','content','timestamp']];
|
| 356 |
+
for (const s of (data.sessions||[])) {
|
| 357 |
+
for (const m of (s.messages||[])) {
|
| 358 |
+
rows.push([username, s.title||'', m.role, m.content.replace(/"/g,'""'), m.created_at]);
|
| 359 |
+
}
|
| 360 |
+
}
|
| 361 |
+
const csv = rows.map(r => r.map(c=>`"${c}"`).join(',')).join('\n');
|
| 362 |
+
download(`neurocheck_${username}.csv`, csv, 'text/csv');
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
async function exportUserJSON(userId, username) {
|
| 366 |
+
const res = await authFetch(`/admin/user/${userId}/sessions`);
|
| 367 |
+
const data = await res.json();
|
| 368 |
+
download(`neurocheck_${username}.json`, JSON.stringify(data, null, 2), 'application/json');
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
function download(filename, content, type) {
|
| 372 |
+
const a = document.createElement('a');
|
| 373 |
+
a.href = URL.createObjectURL(new Blob([content], {type}));
|
| 374 |
+
a.download = filename;
|
| 375 |
+
a.click();
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
// ── Utils ──
|
| 379 |
+
function escHtml(t) { return String(t).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"'); }
|
| 380 |
+
function fmtDate(iso) {
|
| 381 |
+
try { return new Date(iso).toLocaleDateString(undefined,{month:'short',day:'numeric',year:'numeric'}); } catch{return iso;}
|
| 382 |
+
}
|
| 383 |
+
function fmtDateFull(iso) {
|
| 384 |
+
try { return new Date(iso).toLocaleDateString(undefined,{month:'short',day:'numeric',year:'numeric',hour:'2-digit',minute:'2-digit'}); } catch{return iso;}
|
| 385 |
+
}
|
| 386 |
+
function fmtTime(iso) {
|
| 387 |
+
try {
|
| 388 |
+
const d=new Date(iso), now=new Date();
|
| 389 |
+
return d.toDateString()===now.toDateString()
|
| 390 |
+
? d.toLocaleTimeString([],{hour:'2-digit',minute:'2-digit'})
|
| 391 |
+
: d.toLocaleDateString([],{month:'short',day:'numeric'})+' '+d.toLocaleTimeString([],{hour:'2-digit',minute:'2-digit'});
|
| 392 |
+
} catch{return iso;}
|
| 393 |
+
}
|
| 394 |
+
</script>
|
| 395 |
+
</body>
|
| 396 |
+
</html>
|