claude-code-templates
Version:
CLI tool to setup Claude Code configurations with framework-specific commands, automation hooks and MCP Servers for your projects
1,476 lines (1,268 loc) • 108 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Claude Code Analytics - Chats</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
/* Terminal theme - black and orange */
--bg-primary: #0d1117;
--bg-secondary: #161b22;
--bg-tertiary: #21262d;
--text-primary: #ffffff;
--text-secondary: #8b949e;
--text-accent: #ff6b35;
--text-success: #3fb950;
--text-warning: #d29922;
--text-error: #da3633;
--border-primary: #30363d;
--border-secondary: #40464e;
--shadow: rgba(0, 0, 0, 0.5);
/* Terminal specific colors */
--terminal-orange: #ff6b35;
--terminal-orange-hover: #ff8659;
--terminal-dark: #0d1117;
--terminal-gray: #21262d;
/* Message bubble colors */
--message-received: #cc5500; /* Warm orange for received messages */
--message-sent: #1e7e34; /* Darker green for sent messages */
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
height: 100vh;
overflow: hidden;
}
/* Mobile-first chat app layout */
.chat-app {
display: flex;
height: 100vh;
position: relative;
}
/* Mobile sidebar (conversations list) */
.chat-sidebar {
width: 100%;
height: 100%;
background: var(--bg-primary);
display: flex;
flex-direction: column;
position: relative;
z-index: 100;
}
/* Chat sidebar header */
.chat-header {
background: var(--bg-secondary);
padding: 16px 20px;
border-bottom: 1px solid var(--border-primary);
display: flex;
align-items: center;
justify-content: between;
min-height: 64px;
}
.chat-header-content {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
}
.chat-title {
font-size: 1.2rem;
font-weight: 700;
color: var(--text-primary);
}
.header-actions {
display: flex;
gap: 12px;
align-items: center;
}
.header-btn {
background: rgba(255, 107, 53, 0.1);
border: 1px solid rgba(255, 107, 53, 0.2);
color: var(--terminal-orange);
font-size: 0.8rem;
font-weight: 600;
cursor: pointer;
padding: 6px 12px;
border-radius: 8px;
transition: all 0.2s ease;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.header-btn:hover {
background: rgba(255, 107, 53, 0.15);
border-color: rgba(255, 107, 53, 0.3);
color: var(--terminal-orange);
transform: translateY(-1px);
box-shadow: 0 2px 4px rgba(255, 107, 53, 0.2);
}
.header-btn:active {
transform: translateY(0);
box-shadow: 0 1px 2px rgba(255, 107, 53, 0.2);
}
.back-btn {
background: rgba(255, 107, 53, 0.15) !important;
border: 1px solid rgba(255, 107, 53, 0.3) !important;
color: var(--terminal-orange) !important;
font-size: 1.2rem !important;
font-weight: 700 !important;
padding: 8px 12px !important;
min-width: 40px !important;
text-align: center !important;
}
.back-btn:hover {
background: rgba(255, 107, 53, 0.2) !important;
border-color: rgba(255, 107, 53, 0.4) !important;
}
/* Search bar */
.chat-search {
padding: 16px 20px;
background: var(--bg-primary);
border-bottom: 1px solid var(--border-primary);
}
.search-input {
width: 100%;
padding: 12px 16px;
background: var(--bg-tertiary);
border: 1px solid var(--border-primary);
border-radius: 24px;
color: var(--text-primary);
font-size: 1rem;
outline: none;
transition: border-color 0.2s ease;
}
.search-input:focus {
border-color: var(--terminal-orange);
}
.search-input::placeholder {
color: var(--text-secondary);
}
/* Conversations list */
.conversations-list {
flex: 1;
overflow-y: auto;
background: var(--bg-primary);
}
.conversation-item {
display: flex;
align-items: center;
padding: 16px 20px;
border-bottom: 1px solid var(--border-primary);
cursor: pointer;
transition: background-color 0.2s ease;
position: relative;
}
.conversation-item:hover {
background: var(--bg-secondary);
}
.conversation-item.active {
background: var(--bg-secondary);
border-left: 4px solid var(--terminal-orange);
}
.conversation-avatar {
width: 48px;
height: 48px;
border-radius: 50%;
background: var(--terminal-orange);
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
margin-right: 16px;
flex-shrink: 0;
color: var(--bg-primary);
font-weight: bold;
}
.conversation-content {
flex: 1;
min-width: 0;
}
.conversation-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 4px;
}
.conversation-name {
font-weight: 600;
color: var(--text-primary);
font-size: 1rem;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
.conversation-time {
color: var(--text-secondary);
font-size: 0.8rem;
flex-shrink: 0;
margin-left: 8px;
}
.conversation-preview {
color: var(--text-secondary);
font-size: 0.9rem;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
line-height: 1.3;
}
.conversation-meta {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 4px;
}
.conversation-state {
font-size: 0.7rem;
padding: 3px 8px;
border-radius: 12px;
text-transform: uppercase;
font-weight: 600;
letter-spacing: 0.5px;
display: inline-flex;
align-items: center;
gap: 4px;
transition: all 0.2s ease;
}
/* Working states - Green (Claude is actively working) */
.state-working {
background: rgba(40, 167, 69, 0.15);
color: #28a745;
border: 1px solid rgba(40, 167, 69, 0.3);
animation: pulse-working 2s infinite;
}
@keyframes pulse-working {
0%, 100% { opacity: 1; }
50% { opacity: 0.7; }
}
/* Active states - Blue/Cyan (Recent activity, ready) */
.state-active {
background: rgba(23, 162, 184, 0.15);
color: #17a2b8;
border: 1px solid rgba(23, 162, 184, 0.3);
}
/* Waiting/Responding states - Orange (Waiting for input/response) */
.state-waiting {
background: rgba(255, 193, 7, 0.15);
color: #ffc107;
border: 1px solid rgba(255, 193, 7, 0.3);
}
.state-responding {
background: rgba(255, 152, 0, 0.15);
color: #ff9800;
border: 1px solid rgba(255, 152, 0, 0.3);
}
/* Typing states - Purple (User interaction) */
.state-typing {
background: rgba(156, 39, 176, 0.15);
color: #9c27b0;
border: 1px solid rgba(156, 39, 176, 0.3);
}
/* Idle states - Gray (Inactive but not old) */
.state-idle {
background: rgba(108, 117, 125, 0.15);
color: #6c757d;
border: 1px solid rgba(108, 117, 125, 0.3);
}
/* Inactive states - Red/Gray (Old conversations) */
.state-inactive {
background: rgba(134, 142, 150, 0.1);
color: #868e96;
border: 1px solid rgba(134, 142, 150, 0.2);
}
/* Special states */
.state-recent {
background: rgba(0, 123, 255, 0.15);
color: #007bff;
border: 1px solid rgba(0, 123, 255, 0.3);
}
.state-finishing {
background: rgba(40, 167, 69, 0.2);
color: #28a745;
border: 1px solid rgba(40, 167, 69, 0.4);
}
.state-empty {
background: rgba(184, 188, 200, 0.1);
color: var(--text-secondary);
border: 1px solid rgba(184, 188, 200, 0.2);
}
/* Hover effects */
.conversation-state:hover {
transform: translateY(-1px);
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.message-count {
background: rgba(255, 107, 53, 0.2);
color: var(--terminal-orange);
border: 1px solid rgba(255, 107, 53, 0.3);
border-radius: 10px;
font-size: 0.7rem;
font-weight: 600;
padding: 2px 6px;
min-width: 18px;
text-align: center;
}
/* Message styles - matching AgentsPage.js format */
.messages-list {
padding: 16px;
display: flex;
flex-direction: column;
gap: 16px;
}
.message {
display: flex;
flex-direction: column;
max-width: 85%;
}
.message-user {
align-self: flex-end;
}
.message-assistant {
align-self: flex-start;
}
.message-bubble {
position: relative;
}
.message-user .message-bubble .message-content {
background: var(--message-sent);
color: white;
padding: 12px 16px;
border-radius: 18px 18px 4px 18px;
font-size: 1rem;
line-height: 1.4;
word-wrap: break-word;
}
.message-assistant .message-bubble .message-content {
background: var(--terminal-dark);
color: var(--text-primary);
padding: 12px 16px;
border: 2px solid var(--message-received);
border-radius: 8px;
font-size: 1rem;
line-height: 1.4;
word-wrap: break-word;
}
.message-meta {
display: flex;
align-items: center;
gap: 8px;
margin-top: 4px;
font-size: 0.7rem;
color: var(--text-secondary);
}
.message-user .message-meta {
justify-content: flex-end;
}
.message-assistant .message-meta {
justify-content: flex-start;
}
.message-time {
font-size: 0.7rem;
color: var(--text-secondary);
}
.tool-indicator {
color: var(--terminal-orange);
padding: 2px 6px;
border-radius: 10px;
font-size: 0.6rem;
font-weight: bold;
}
.token-indicator {
background: var(--bg-tertiary);
color: var(--text-secondary);
padding: 2px 6px;
border-radius: 8px;
font-size: 0.6rem;
}
/* Code formatting in messages */
.message-content pre {
background: var(--terminal-dark);
color: #ffab70;
padding: 12px;
border-radius: 8px;
overflow-x: auto;
margin: 8px 0;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 0.9rem;
border: 1px solid var(--border-primary);
}
.message-content code {
background: var(--terminal-gray);
color: #ffab70;
padding: 2px 6px;
border-radius: 4px;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 0.9rem;
}
.message-content pre code {
background: none;
padding: 0;
}
.message-content strong {
font-weight: 600;
color: #ffab70;
}
.message-content em {
font-style: italic;
color: var(--text-secondary);
}
/* Expandable message styles */
.expandable-message {
width: 100%;
}
.message-expand-link {
color: var(--terminal-orange);
cursor: pointer;
font-size: 0.9rem;
margin-top: 8px;
padding: 4px 0;
border-top: 1px solid rgba(255, 107, 53, 0.3);
text-align: center;
transition: all 0.2s ease;
}
.message-expand-link:hover {
color: var(--terminal-orange-hover);
background: rgba(255, 107, 53, 0.1);
}
.message-collapse-link {
color: var(--text-secondary);
cursor: pointer;
font-size: 0.8rem;
margin-top: 8px;
padding: 4px 0;
border-top: 1px solid var(--border-primary);
text-align: center;
transition: all 0.2s ease;
}
.message-collapse-link:hover {
color: var(--text-primary);
background: rgba(255, 255, 255, 0.05);
}
/* Tool call and result styles */
.tool-call {
margin: 8px 0;
padding: 8px 0;
background: none;
}
.tool-summary {
display: flex;
align-items: center;
gap: 6px;
font-size: 0.9rem;
color: var(--text-primary);
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
cursor: pointer;
transition: all 0.2s ease;
word-wrap: break-word;
word-break: break-all;
overflow-wrap: break-word;
max-width: 100%;
flex-wrap: wrap;
}
.tool-summary:hover {
background: rgba(255, 107, 53, 0.1);
padding: 2px 4px;
border-radius: 4px;
}
.tool-bullet {
color: var(--terminal-orange);
font-weight: bold;
}
.tool-name {
font-weight: bold;
color: var(--terminal-orange);
word-wrap: break-word;
word-break: break-all;
overflow-wrap: break-word;
}
.tool-summary-text {
word-wrap: break-word;
word-break: break-all;
overflow-wrap: break-word;
flex-shrink: 1;
}
.tool-expand-note {
display: flex;
align-items: center;
gap: 6px;
margin-top: 4px;
font-size: 0.8rem;
color: var(--text-secondary);
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
cursor: pointer;
transition: all 0.2s ease;
}
.tool-expand-note:hover {
color: var(--terminal-orange);
}
.tool-branch {
color: var(--text-secondary);
}
.tool-note {
font-style: italic;
color: var(--text-secondary);
}
.tool-parameters {
margin-top: 8px;
border-top: 1px solid var(--border-primary);
padding-top: 8px;
}
.tool-parameters-header {
display: flex;
align-items: center;
gap: 6px;
font-size: 0.8rem;
color: var(--text-secondary);
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
margin-bottom: 8px;
}
.tool-param-title {
font-weight: bold;
}
.tool-parameters-content {
background: var(--terminal-dark);
color: var(--terminal-orange);
padding: 8px;
border-radius: 4px;
font-size: 0.8rem;
margin: 0;
border: 1px solid var(--border-primary);
word-wrap: break-word;
word-break: break-all;
overflow-wrap: break-word;
max-width: 100%;
overflow-x: auto;
white-space: pre-wrap;
}
.tool-result {
background: var(--bg-tertiary);
border: 1px solid var(--border-primary);
border-radius: 8px;
margin: 8px 0;
overflow: hidden;
}
.tool-result-header {
background: transparent;
color: var(--text-primary);
padding: 8px 12px;
font-weight: bold;
font-size: 0.9rem;
display: flex;
align-items: center;
gap: 8px;
}
.tool-result-content {
padding: 12px;
}
.tool-result-content pre {
background: var(--terminal-dark);
color: var(--text-primary);
padding: 8px;
border-radius: 4px;
font-size: 0.8rem;
margin: 0;
white-space: pre-wrap;
}
/* Expandable tool result styles */
.expandable-tool-result {
width: 100%;
}
.tool-result-expand-link {
color: var(--terminal-orange);
cursor: pointer;
font-size: 0.8rem;
margin-top: 8px;
padding: 6px 12px;
border-top: 1px solid rgba(255, 107, 53, 0.3);
text-align: center;
transition: all 0.2s ease;
background: rgba(255, 107, 53, 0.05);
border-radius: 0 0 4px 4px;
}
.tool-result-expand-link:hover {
color: var(--terminal-orange-hover);
background: rgba(255, 107, 53, 0.1);
}
.tool-result-collapse-link {
color: var(--text-secondary);
cursor: pointer;
font-size: 0.7rem;
margin-top: 8px;
padding: 4px 8px;
border-top: 1px solid var(--border-primary);
text-align: center;
transition: all 0.2s ease;
background: rgba(255, 255, 255, 0.02);
}
.tool-result-collapse-link:hover {
color: var(--text-primary);
background: rgba(255, 255, 255, 0.05);
}
/* Loading and error states */
.messages-loading {
display: flex;
flex-direction: column;
align-items: center;
padding: 40px 20px;
color: var(--text-secondary);
}
.messages-loading .loading-spinner {
margin-bottom: 12px;
}
.no-messages-found {
text-align: center;
padding: 40px 20px;
color: var(--text-secondary);
}
.no-messages-icon {
font-size: 3rem;
margin-bottom: 16px;
}
.error-loading-messages {
text-align: center;
padding: 40px 20px;
color: var(--text-error);
}
.error-icon {
font-size: 3rem;
margin-bottom: 16px;
}
.retry-messages {
background: var(--terminal-orange);
color: white;
border: none;
padding: 10px 20px;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
margin-top: 12px;
transition: background-color 0.2s ease;
}
.retry-messages:hover {
background: var(--terminal-orange-hover);
}
/* Chat view (initially hidden) */
.chat-view {
width: 100%;
height: 100%;
background: var(--bg-primary);
position: absolute;
top: 0;
left: 100%;
z-index: 200;
display: flex;
flex-direction: column;
transition: left 0.3s ease;
}
.chat-view.active {
left: 0;
}
/* Chat view header */
.chat-view-header {
background: var(--bg-secondary);
padding: 16px 20px;
border-bottom: 1px solid var(--border-primary);
display: flex;
align-items: center;
min-height: 64px;
}
.chat-view-back {
margin-right: 16px;
}
.chat-view-info {
flex: 1;
}
.chat-view-title {
font-size: 1rem;
font-weight: 600;
color: var(--text-primary);
margin: 0;
}
.chat-view-subtitle {
font-size: 0.8rem;
color: var(--text-secondary);
margin: 0;
}
/* Tools toggle switch */
.tools-toggle {
margin-left: auto;
display: flex;
align-items: center;
gap: 8px;
opacity: 0;
transition: opacity 0.3s ease;
}
.chat-view.conversation-selected .tools-toggle {
opacity: 1;
}
.tools-toggle-label {
font-size: 13px;
color: var(--text-secondary);
user-select: none;
cursor: pointer;
}
.toggle-switch {
position: relative;
display: inline-block;
width: 44px;
height: 24px;
}
.toggle-switch input {
opacity: 0;
width: 0;
height: 0;
}
.toggle-slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #555;
transition: 0.3s;
border-radius: 12px;
}
.toggle-slider:before {
position: absolute;
content: "";
height: 18px;
width: 18px;
left: 3px;
bottom: 3px;
background-color: white;
transition: 0.3s;
border-radius: 50%;
}
.toggle-switch input:checked + .toggle-slider {
background-color: #cc5500;
}
.toggle-switch input:checked + .toggle-slider:before {
transform: translateX(20px);
}
.toggle-switch input:hover + .toggle-slider {
background-color: #666;
}
.toggle-switch input:checked:hover + .toggle-slider {
background-color: #e66600;
}
/* Hide complete assistant messages with tools when toggle is off */
.chat-view:not(.show-tools) .message-assistant.has-tools {
display: none !important;
}
/* Messages area */
.chat-messages {
flex: 1;
overflow-y: auto;
padding: 20px;
padding-bottom: 80px; /* Space for status footer */
background: var(--bg-primary);
}
/* Loading states */
.loading-spinner {
border: 2px solid var(--border-primary);
border-top: 2px solid var(--terminal-orange);
border-radius: 50%;
width: 24px;
height: 24px;
animation: spin 1s linear infinite;
margin: 0 auto;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.no-conversations {
text-align: center;
padding: 40px 20px;
color: var(--text-secondary);
}
/* Status Footer */
.status-footer {
background: var(--bg-primary);
border-top: 1px solid var(--border-primary);
padding: 12px 20px;
display: flex;
align-items: center;
justify-content: space-between;
min-height: 50px;
position: sticky;
bottom: 0;
backdrop-filter: blur(10px);
z-index: 10;
}
.status-indicator {
display: flex;
align-items: center;
gap: 8px;
}
.status-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--text-secondary);
transition: all 0.3s ease;
}
.status-dot.ready {
background: #28a745;
box-shadow: 0 0 6px rgba(40, 167, 69, 0.4);
}
.status-dot.working {
background: var(--terminal-orange);
box-shadow: 0 0 6px rgba(255, 107, 53, 0.4);
animation: pulse 2s infinite;
}
.status-dot.waiting {
background: #ffc107;
box-shadow: 0 0 6px rgba(255, 193, 7, 0.4);
}
.status-dot.error {
background: #dc3545;
box-shadow: 0 0 6px rgba(220, 53, 69, 0.4);
}
.status-dot.idle {
background: var(--text-secondary);
}
@keyframes pulse {
0%, 100% {
opacity: 1;
transform: scale(1);
}
50% {
opacity: 0.7;
transform: scale(1.1);
}
}
.status-text {
color: var(--text-primary);
font-size: 0.9rem;
font-weight: 500;
}
.status-details {
color: var(--text-secondary);
font-size: 0.8rem;
text-align: right;
}
/* Hide status footer when no conversation is selected */
.chat-view:not(.conversation-selected) .status-footer {
display: none;
}
.no-conversations-icon {
font-size: 3rem;
margin-bottom: 16px;
}
/* Responsive adjustments */
@media (min-width: 769px) {
.chat-app {
/* On larger screens, show side-by-side if needed */
}
}
</style>
</head>
<body>
<div class="chat-app" id="chatApp">
<!-- Conversations List (Mobile Sidebar) -->
<div class="chat-sidebar" id="chatSidebar">
<!-- Header -->
<div class="chat-header">
<div class="chat-header-content">
<h1 class="chat-title">Claude Code Chats</h1>
<div class="header-actions">
<button class="header-btn" id="refreshBtn" title="Refresh">
Refresh
</button>
</div>
</div>
</div>
<!-- Search -->
<div class="chat-search">
<input
type="text"
class="search-input"
placeholder="Search conversations..."
id="searchInput"
/>
</div>
<!-- Conversations List -->
<div class="conversations-list" id="conversationsList">
<div class="loading-spinner" style="margin: 40px auto;"></div>
</div>
</div>
<!-- Chat View -->
<div class="chat-view" id="chatView">
<div class="chat-view-header">
<button class="header-btn back-btn chat-view-back" id="backToList">
←
</button>
<div class="chat-view-info">
<h2 class="chat-view-title" id="chatViewTitle">Select a conversation</h2>
<p class="chat-view-subtitle" id="chatViewSubtitle"></p>
</div>
<div class="tools-toggle" id="toolsToggle">
<span class="tools-toggle-label" onclick="document.getElementById('showToolsSwitch').click()">Show Tools</span>
<label class="toggle-switch">
<input type="checkbox" id="showToolsSwitch" checked>
<span class="toggle-slider"></span>
</label>
</div>
</div>
<div class="chat-messages" id="chatMessages">
<div class="no-conversations">
<div class="no-conversations-icon">💬</div>
<h3>No conversation selected</h3>
<p>Choose a conversation from the list to view messages</p>
</div>
</div>
<!-- Status Footer -->
<div class="status-footer" id="statusFooter">
<div class="status-indicator">
<div class="status-dot" id="statusDot"></div>
<span class="status-text" id="statusText">Ready</span>
</div>
<div class="status-details" id="statusDetails"></div>
</div>
</div>
</div>
<!-- Import WebSocket and Data Services -->
<script src="services/WebSocketService.js"></script>
<script src="services/DataService.js"></script>
<script src="services/StateService.js"></script>
<script>
class ChatsMobileApp {
constructor() {
this.conversations = [];
this.selectedConversationId = null;
this.loadedMessages = new Map(); // Cache messages by conversation ID (stores paginated data)
// Pagination state for messages
this.messagesPagination = {
currentPage: 0,
limit: 15, // Load 15 messages per page
hasMore: true,
isLoading: false,
conversationId: null
};
// Message scroll listener reference
this.messagesScrollListener = null;
// Tools visibility state
this.showTools = true;
// Auto-scroll state tracking
this.isUserScrolling = false;
this.autoScrollEnabled = true;
this.scrollThreshold = 100; // pixels from bottom to consider "at bottom"
this.userScrollTimeout = null;
// Initialize services for real-time updates
this.webSocketService = new WebSocketService();
this.stateService = new StateService();
this.dataService = new DataService(this.webSocketService);
this.init();
}
async init() {
this.bindEvents();
this.setupRealTimeUpdates();
this.loadToolsPreference();
await this.loadConversations();
}
bindEvents() {
// Refresh conversations
document.getElementById('refreshBtn').addEventListener('click', () => {
this.loadConversations();
});
// Back to conversations list
document.getElementById('backToList').addEventListener('click', () => {
this.showConversationsList();
});
// Search functionality
const searchInput = document.getElementById('searchInput');
searchInput.addEventListener('input', (e) => {
this.filterConversations(e.target.value);
});
// Show Tools toggle functionality
const showToolsSwitch = document.getElementById('showToolsSwitch');
showToolsSwitch.addEventListener('change', (e) => {
this.toggleTools(e.target.checked);
});
}
setupRealTimeUpdates() {
console.log('🔧 Setting up real-time updates...');
// Subscribe to DataService events for real-time updates
this.dataService.addEventListener((type, data) => {
console.log('📡 DataService event:', type, data);
if (type === 'new_message') {
console.log('🔄 WebSocket: New message received', { conversationId: data.conversationId });
this.handleNewMessage(data.conversationId, data.message, data.metadata);
}
});
// Try to connect WebSocket
try {
console.log('🌐 Attempting WebSocket connection...');
this.webSocketService.connect();
// Log WebSocket connection status
setTimeout(() => {
const status = this.webSocketService.getStatus();
console.log('🔗 WebSocket status:', status);
}, 2000);
} catch (error) {
console.warn('WebSocket connection failed, using fallback polling:', error);
}
}
async loadConversations() {
const conversationsList = document.getElementById('conversationsList');
try {
conversationsList.innerHTML = '<div class="loading-spinner" style="margin: 40px auto;"></div>';
// Fetch conversations and states simultaneously (like AgentsPage.js)
const [conversationsResponse, statesResponse] = await Promise.all([
fetch('/api/conversations'),
fetch('/api/conversation-state') // Use singular like AgentsPage.js
]);
if (!conversationsResponse.ok) {
throw new Error(`HTTP error! status: ${conversationsResponse.status}`);
}
const conversationsData = await conversationsResponse.json();
this.conversations = conversationsData.conversations || [];
// Get conversation states (like AgentsPage.js)
let states = {};
if (statesResponse.ok) {
const statesData = await statesResponse.json();
states = statesData.activeStates || {};
console.log('📊 Loaded conversation states:', Object.keys(states).length, 'conversations');
} else {
console.warn('Could not load conversation states:', statesResponse.status);
}
this.renderConversations(this.conversations, states);
} catch (error) {
console.error('Error loading conversations:', error);
conversationsList.innerHTML = `
<div class="no-conversations">
<div class="no-conversations-icon">⚠️</div>
<h3>Error loading conversations</h3>
<p>${error.message}</p>
<button onclick="location.reload()" style="margin-top: 12px; padding: 8px 16px; background: var(--text-accent); color: white; border: none; border-radius: 4px; cursor: pointer;">Retry</button>
</div>
`;
}
}
renderConversations(conversations, states = {}) {
const conversationsList = document.getElementById('conversationsList');
if (conversations.length === 0) {
conversationsList.innerHTML = `
<div class="no-conversations">
<div class="no-conversations-icon">💬</div>
<h3>No conversations found</h3>
<p>Start a conversation with Claude Code to see it here</p>
</div>
`;
return;
}
conversationsList.innerHTML = conversations.map(conv => {
const state = states[conv.id] || 'inactive';
const stateClass = this.getStateClass(state);
const stateLabel = this.getStateLabel(state);
// Debug logging for first few conversations
console.log(`🔍 Conversation ${conv.id.slice(-8)}: State="${state}" -> Label="${stateLabel}" Class="${stateClass}"`);
const lastActivity = this.formatRelativeTime(new Date(conv.lastModified));
const messageCount = conv.messageCount || 0;
const projectName = conv.project || 'Unknown Project';
const conversationId = conv.id.slice(-8);
// Get first letter of project name for avatar
const firstLetter = projectName.charAt(0).toUpperCase();
return `
<div class="conversation-item" data-conversation-id="${conv.id}">
<div class="conversation-avatar">
${firstLetter}
</div>
<div class="conversation-content">
<div class="conversation-header">
<div class="conversation-name">${projectName}</div>
<div class="conversation-time">${lastActivity}</div>
</div>
<div class="conversation-preview">
Conversation ${conversationId}
</div>
<div class="conversation-meta">
<span class="conversation-state ${stateClass}">${stateLabel}</span>
${messageCount > 0 ? `<span class="message-count">${messageCount}</span>` : ''}
</div>
</div>
</div>
`;
}).join('');
// Bind conversation click events
conversationsList.querySelectorAll('.conversation-item').forEach(item => {
item.addEventListener('click', () => {
const conversationId = item.dataset.conversationId;
this.selectConversation(conversationId);
});
});
}
selectConversation(conversationId) {
this.selectedConversationId = conversationId;
// Update active state
document.querySelectorAll('.conversation-item').forEach(item => {
item.classList.toggle('active', item.dataset.conversationId === conversationId);
});
// Show chat view
this.showChatView(conversationId);
}
showChatView(conversationId) {
const conversation = this.conversations.find(conv => conv.id === conversationId);
if (!conversation) return;
const chatView = document.getElementById('chatView');
const chatViewTitle = document.getElementById('chatViewTitle');
const chatViewSubtitle = document.getElementById('chatViewSubtitle');
// Update chat view header
const projectName = conversation.project || 'Unknown Project';
const convId = conversation.id.slice(-8);
chatViewTitle.textContent = projectName;
chatViewSubtitle.textContent = `Conversation ${convId}`;
// Show chat view with animation
chatView.classList.add('active');
// Apply tools visibility state
if (this.showTools) {
chatView.classList.add('show-tools');
} else {
chatView.classList.remove('show-tools');
}
// Load messages (placeholder for now)
this.loadChatMessages(conversationId);
}
showConversationsList() {
const chatView = document.getElementById('chatView');
chatView.classList.remove('active');
chatView.classList.remove('conversation-selected'); // Hide status footer
this.selectedConversationId = null;
// Clean up scroll tracking when leaving conversation
this.removeScrollTracking();
// Remove active state from conversations
document.querySelectorAll('.conversation-item').forEach(item => {
item.classList.remove('active');
});
// Reset status
this.updateStatus('idle', 'Ready', '');
}
async loadChatMessages(conversationId) {
const chatView = document.getElementById('chatView');
// Mark conversation as selected and show status footer
chatView.classList.add('conversation-selected');
// Reset pagination for new conversation
this.messagesPagination = {
currentPage: 0,
limit: 15,
hasMore: true,
isLoading: false,
conversationId: conversationId
};
// Clear cached messages for this conversation
this.loadedMessages.delete(conversationId);
// Load first page of messages
await this.loadMoreMessages(conversationId, true);
}
/**
* Load more messages (for infinite scroll)
* @param {string} conversationId - Conversation ID
* @param {boolean} isInitialLoad - Whether this is the initial load
*/
async loadMoreMessages(conversationId, isInitialLoad = false) {
const chatMessages = document.getElementById('chatMessages');
if (!chatMessages) return;
// Prevent concurrent loading
if (this.messagesPagination.isLoading || !this.messagesPagination.hasMore) {
return;
}
// Ensure we're loading for the correct conversation
if (this.messagesPagination.conversationId !== conversationId) {
return;
}
try {
this.messagesPagination.isLoading = true;
if (isInitialLoad) {
// Show loading state for initial load
chatMessages.innerHTML = `
<div class="messages-loading">
<div class="loading-spinner"></div>
<span>Loading messages...</span>
</div>
`;
// Update status while loading
this.updateStatus('working', 'Loading conversation...', 'Fetching messages from server');
} else {
// Show loading indicator at top for infinite scroll
this.showMessagesLoadingIndicator(true);
}
// Fetch paginated messages from the server
const response = await fetch(`/api/conversations/${conversationId}/messages?page=${this.messagesPagination.currentPage}&limit=${this.messagesPagination.limit}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const messagesData = await response.json();
if (messagesData && messagesData.messages) {
// Update pagination state - handle both paginated and non-paginated responses
if (messagesData.pagination) {
// Paginated response
this.messagesPagination.hasMore = messagesData.pagination.hasMore;
this.messagesPagination.currentPage = messagesData.pagination.page + 1;
} else {
// Non-paginated response (fallback) - treat as complete data
this.messagesPagination.hasMore = false;
this.messagesPagination.currentPage = 1;
}
// Get existing messages or initialize
let existingMessages = this.loadedMessages.get(conversationId) || [];
if (isInitialLoad) {
// For initial load, replace all messages (newest messages first)
existingMessages = messagesData.messages;
} else {
// For infinite scroll, prepend older messages to the beginning
existingMessages = [...messagesData.messages, ...existingMessages];
}
// Cache the combined messages
this.loadedMessages.set(conversationId, existingMessages);
// Render messages
this.renderCachedMessages(existingMessages, !isInitialLoad);
// Setup scroll listener for infinite scroll (only on initial load)
if (isInitialLoad) {
this.setupMessagesScrollListener(conversationId);
// Setup intelligent scroll tracking for chat behavior
this.setupScrollTracking();
// Enable auto-scroll for initial load
this.autoScrollEnabled = true;
// Scroll to bottom for initial load
this.scrollToBottom();
// Update status based on conversation state
this.analyzeConversationStatus(existingMessages);
}
} else if (isInitialLoad) {
chatMessages.innerHTML = `
<div class="no-messages-found">
<div class="no-messages-icon">💭</div>
<h4>No messages found</h4>
<p>This conversation has no messages or they could not be loaded.</p>
</div>
`;
}
} catch (error) {
console.error('Error loading messages:', error);
if (isInitialLoad) {
this.updateStatus('error', 'Error loading conversation', error.message);
chatMessages.innerHTML = `
<div class="error-loading-messages">
<div class="error-icon">⚠️</div>
<h4>Error loading messages</h4>
<p>${error.message}</p>
<button class="retry-messages">Retry</button>
</div>
`;
// Add retry functionality
const retryBtn = chatMessages.querySelector('.retry-messages');
if (retryBtn) {
retryBtn.addEventListener('click', () => {
this.loadChatMessages(conversationId);
});
}
}
} finally {
this.mess