-
Notifications
You must be signed in to change notification settings - Fork 691
Expand file tree
/
Copy pathstorage-manager.html
More file actions
547 lines (476 loc) · 20.5 KB
/
storage-manager.html
File metadata and controls
547 lines (476 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Local Storage Manager - NLWeb</title>
<link rel="stylesheet" href="chat-page-styles.css">
<style>
.storage-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.storage-section {
background: var(--bg-secondary);
border: 1px solid var(--border-color);
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
}
.storage-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.storage-title {
font-size: 1.2em;
font-weight: 600;
color: var(--text-primary);
}
.storage-info {
color: var(--text-secondary);
font-size: 0.9em;
margin-bottom: 10px;
}
.storage-size {
color: var(--primary-color);
font-weight: 600;
}
.data-preview {
background: var(--bg-primary);
border: 1px solid var(--border-color);
border-radius: 4px;
padding: 10px;
margin: 10px 0;
font-family: monospace;
font-size: 0.85em;
max-height: 200px;
overflow-y: auto;
white-space: pre-wrap;
word-break: break-all;
}
.button-group {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.action-button {
padding: 8px 16px;
border: 1px solid var(--border-color);
border-radius: 6px;
background: var(--bg-primary);
color: var(--text-primary);
cursor: pointer;
transition: all 0.2s ease;
}
.action-button:hover {
background: var(--bg-hover);
border-color: var(--primary-color);
}
.action-button.danger {
border-color: #dc3545;
color: #dc3545;
}
.action-button.danger:hover {
background: #dc3545;
color: white;
}
.back-link {
display: inline-block;
margin-bottom: 20px;
color: var(--primary-color);
text-decoration: none;
}
.back-link:hover {
text-decoration: underline;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-top: 15px;
}
.stat-card {
background: var(--bg-primary);
border: 1px solid var(--border-color);
border-radius: 6px;
padding: 15px;
text-align: center;
}
.stat-value {
font-size: 2em;
font-weight: bold;
color: var(--primary-color);
}
.stat-label {
color: var(--text-secondary);
font-size: 0.9em;
margin-top: 5px;
}
.success-message {
background: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
padding: 10px 15px;
border-radius: 6px;
margin: 10px 0;
display: none;
}
.empty-state {
text-align: center;
color: var(--text-secondary);
padding: 40px;
}
</style>
</head>
<body>
<div class="storage-container">
<a href="multi-chat-index.html" class="back-link">← Back to Chat</a>
<h1>Local Storage Manager</h1>
<div id="successMessage" class="success-message"></div>
<!-- Conversations Section -->
<div class="storage-section">
<div class="storage-header">
<h2 class="storage-title">Conversations</h2>
<div class="button-group">
<button class="action-button" onclick="exportConversations()">Export JSON</button>
<button class="action-button danger" onclick="clearConversations()">Clear All</button>
</div>
</div>
<div class="storage-info">
Key: <code>nlweb-modern-conversations</code> |
Size: <span id="conversationsSize" class="storage-size">Calculating...</span>
</div>
<div id="conversationsStats" class="stats-grid"></div>
<div id="conversationsPreview" class="data-preview">Loading...</div>
</div>
<!-- User Info Section -->
<div class="storage-section">
<div class="storage-header">
<h2 class="storage-title">User Information</h2>
<div class="button-group">
<button class="action-button danger" onclick="clearUserInfo()">Clear</button>
</div>
</div>
<div class="storage-info">
Key: <code>userInfo</code> |
Size: <span id="userInfoSize" class="storage-size">Calculating...</span>
</div>
<div id="userInfoPreview" class="data-preview">Loading...</div>
</div>
<!-- Auth Token Section -->
<div class="storage-section">
<div class="storage-header">
<h2 class="storage-title">Authentication Token</h2>
<div class="button-group">
<button class="action-button danger" onclick="clearAuthToken()">Clear</button>
</div>
</div>
<div class="storage-info">
Key: <code>authToken</code> |
Status: <span id="authStatus" class="storage-size">Checking...</span>
</div>
<div id="authTokenPreview" class="data-preview">Loading...</div>
</div>
<!-- All Storage Section -->
<div class="storage-section">
<div class="storage-header">
<h2 class="storage-title">All Local Storage</h2>
<div class="button-group">
<button class="action-button" onclick="exportAllData()">Export All</button>
<button class="action-button danger" onclick="clearAllStorage()">Clear Everything</button>
</div>
</div>
<div class="storage-info">
Total items: <span id="totalItems" class="storage-size">Calculating...</span> |
Total size: <span id="totalSize" class="storage-size">Calculating...</span>
</div>
<div id="allStoragePreview" class="data-preview">Loading...</div>
</div>
</div>
<script>
// Helper function to calculate size of data
function getDataSize(data) {
const str = typeof data === 'string' ? data : JSON.stringify(data);
const bytes = new Blob([str]).size;
if (bytes < 1024) return bytes + ' bytes';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB';
return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
}
// Helper function to format JSON for display
function formatJSON(data) {
try {
const parsed = typeof data === 'string' ? JSON.parse(data) : data;
return JSON.stringify(parsed, null, 2);
} catch (e) {
return data;
}
}
// Show success message
function showSuccess(message) {
const successEl = document.getElementById('successMessage');
successEl.textContent = message;
successEl.style.display = 'block';
setTimeout(() => {
successEl.style.display = 'none';
}, 3000);
}
// Load and display conversations
function loadConversations() {
const data = localStorage.getItem('nlweb-modern-conversations');
const sizeEl = document.getElementById('conversationsSize');
const previewEl = document.getElementById('conversationsPreview');
const statsEl = document.getElementById('conversationsStats');
if (!data) {
sizeEl.textContent = '0 bytes';
previewEl.innerHTML = '<div class="empty-state">No conversations stored</div>';
statsEl.innerHTML = '';
return;
}
sizeEl.textContent = getDataSize(data);
try {
const conversations = JSON.parse(data);
// Calculate stats
const totalConversations = conversations.length;
const totalMessages = conversations.reduce((sum, conv) => sum + (conv.messages?.length || 0), 0);
const sites = [...new Set(conversations.map(conv => conv.site || 'all'))];
// Display stats
statsEl.innerHTML = `
<div class="stat-card">
<div class="stat-value">${totalConversations}</div>
<div class="stat-label">Conversations</div>
</div>
<div class="stat-card">
<div class="stat-value">${totalMessages}</div>
<div class="stat-label">Messages</div>
</div>
<div class="stat-card">
<div class="stat-value">${sites.length}</div>
<div class="stat-label">Sites</div>
</div>
`;
// Show preview (first 3 conversations)
const preview = conversations.slice(0, 3).map(conv => ({
id: conv.id,
title: conv.title,
created_at: conv.created_at,
messages: conv.messages?.length || 0,
site: conv.site
}));
previewEl.textContent = formatJSON(preview);
if (conversations.length > 3) {
previewEl.textContent += `\n\n... and ${conversations.length - 3} more conversations`;
}
} catch (e) {
previewEl.textContent = 'Error parsing conversations: ' + e.message;
}
}
// Load and display user info
function loadUserInfo() {
const data = localStorage.getItem('userInfo');
const sizeEl = document.getElementById('userInfoSize');
const previewEl = document.getElementById('userInfoPreview');
if (!data) {
sizeEl.textContent = '0 bytes';
previewEl.innerHTML = '<div class="empty-state">No user information stored</div>';
return;
}
sizeEl.textContent = getDataSize(data);
previewEl.textContent = formatJSON(data);
}
// Load and display auth token
function loadAuthToken() {
const token = localStorage.getItem('authToken');
const statusEl = document.getElementById('authStatus');
const previewEl = document.getElementById('authTokenPreview');
if (!token) {
statusEl.textContent = 'Not authenticated';
previewEl.innerHTML = '<div class="empty-state">No authentication token stored</div>';
return;
}
statusEl.textContent = 'Authenticated';
// Decode JWT to show expiry (without exposing sensitive data)
try {
const parts = token.split('.');
if (parts.length === 3) {
const payload = JSON.parse(atob(parts[1]));
const exp = payload.exp ? new Date(payload.exp * 1000) : null;
const provider = payload.provider || 'Unknown';
previewEl.textContent = formatJSON({
provider: provider,
expires: exp ? exp.toLocaleString() : 'No expiry',
expired: exp ? exp < new Date() : false,
tokenLength: token.length + ' characters'
});
} else {
previewEl.textContent = 'Token stored (length: ' + token.length + ' characters)';
}
} catch (e) {
previewEl.textContent = 'Token stored (length: ' + token.length + ' characters)';
}
}
// Load all storage data
function loadAllStorage() {
const itemsEl = document.getElementById('totalItems');
const sizeEl = document.getElementById('totalSize');
const previewEl = document.getElementById('allStoragePreview');
const allData = {};
let totalSize = 0;
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
const value = localStorage.getItem(key);
allData[key] = value;
totalSize += new Blob([key + value]).size;
}
itemsEl.textContent = localStorage.length;
sizeEl.textContent = getDataSize(totalSize);
// Show keys and sizes
const preview = Object.keys(allData).map(key => ({
key: key,
size: getDataSize(allData[key])
}));
previewEl.textContent = formatJSON(preview);
}
// Export functions
function exportConversations() {
const data = localStorage.getItem('nlweb-modern-conversations');
if (!data) {
alert('No conversations to export');
return;
}
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'nlweb-conversations-' + new Date().toISOString().split('T')[0] + '.json';
a.click();
URL.revokeObjectURL(url);
showSuccess('Conversations exported successfully');
}
function exportAllData() {
const allData = {};
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
allData[key] = localStorage.getItem(key);
}
const blob = new Blob([JSON.stringify(allData, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'nlweb-all-data-' + new Date().toISOString().split('T')[0] + '.json';
a.click();
URL.revokeObjectURL(url);
showSuccess('All data exported successfully');
}
// Clear functions
async function clearConversations() {
if (confirm('Are you sure you want to delete all conversations? This cannot be undone.')) {
// Get conversations before clearing
const data = localStorage.getItem('nlweb-modern-conversations');
let serverConversations = [];
if (data) {
try {
const conversations = JSON.parse(data);
// Filter for server conversations (those with conv_ prefix)
serverConversations = conversations.filter(conv =>
conv.id && conv.id.startsWith('conv_')
);
} catch (e) {
console.error('Error parsing conversations:', e);
}
}
// Clear from localStorage first
localStorage.removeItem('nlweb-modern-conversations');
loadConversations();
// Delete server conversations
if (serverConversations.length > 0) {
let deletedCount = 0;
let failedCount = 0;
// Get user ID if available
const userInfo = localStorage.getItem('userInfo');
let userId = null;
if (userInfo) {
try {
const parsed = JSON.parse(userInfo);
userId = parsed.id || parsed.user_id;
} catch (e) {
console.error('Error parsing userInfo:', e);
}
}
// Delete each server conversation
for (const conv of serverConversations) {
try {
const params = new URLSearchParams({
conversation_id: conv.id
});
if (userId) {
params.append('user_id', userId);
}
const response = await fetch(`/api/conversation/delete?${params}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
});
if (response.ok) {
deletedCount++;
} else {
failedCount++;
const errorData = await response.json();
console.error(`Failed to delete ${conv.id} from server:`, errorData);
}
} catch (error) {
failedCount++;
console.error(`Error deleting ${conv.id} from server:`, error);
}
}
// Show detailed success message
if (failedCount === 0) {
showSuccess(`All conversations cleared successfully (${deletedCount} from server)`);
} else {
showSuccess(`Conversations cleared from local storage. ${deletedCount} deleted from server, ${failedCount} failed.`);
}
} else {
showSuccess('Local conversations cleared successfully');
}
}
}
function clearUserInfo() {
if (confirm('Are you sure you want to clear user information? You will need to log in again.')) {
localStorage.removeItem('userInfo');
loadUserInfo();
showSuccess('User information cleared successfully');
}
}
function clearAuthToken() {
if (confirm('Are you sure you want to clear the authentication token? You will need to log in again.')) {
localStorage.removeItem('authToken');
loadAuthToken();
showSuccess('Authentication token cleared successfully');
}
}
function clearAllStorage() {
if (confirm('Are you sure you want to clear ALL local storage? This will delete all conversations, settings, and login information. This cannot be undone.')) {
if (confirm('This is your last chance. Really delete everything?')) {
localStorage.clear();
loadAll();
showSuccess('All local storage cleared successfully');
}
}
}
// Load all data on page load
function loadAll() {
loadConversations();
loadUserInfo();
loadAuthToken();
loadAllStorage();
}
// Initialize
document.addEventListener('DOMContentLoaded', loadAll);
// Refresh data every 5 seconds
setInterval(loadAll, 5000);
</script>
</body>
</html>