File size: 6,002 Bytes
7a79b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Main JavaScript for CodeScribe AI

// Initialize the application
document.addEventListener('DOMContentLoaded', function() {
    initializeApp();
});

function initializeApp() {
    // Set up event listeners
    setupEventListeners();
    
    // Load initial data
    loadDashboardData();
    
    // Initialize real-time updates
    startRealTimeUpdates();
}

function setupEventListeners() {
    // Mobile menu toggle
    const mobileMenuButton = document.querySelector('[data-mobile-menu]');
    if (mobileMenuButton) {
        mobileMenuButton.addEventListener('click', toggleMobileMenu);
    }

    // Search functionality
    const searchInput = document.querySelector('[data-search-input]');
    if (searchInput) {
        searchInput.addEventListener('input', handleSearch);
    }

    // Theme toggle (if needed in future)
    const themeToggle = document.querySelector('[data-theme-toggle]');
    if (themeToggle) {
        themeToggle.addEventListener('click', toggleTheme);
    }
}

function toggleMobileMenu() {
    const sidebar = document.querySelector('nav-sidebar');
    if (sidebar) {
        sidebar.toggleMobile();
    }
}

function handleSearch(event) {
    const searchTerm = event.target.value.toLowerCase();
    
    // Implement search logic here
    console.log('Searching for:', searchTerm);
    
    // You can add API calls or filter content based on search term
}

function toggleTheme() {
    // Implement theme toggle logic
    document.documentElement.classList.toggle('dark');
}

// Load dashboard data from API
async function loadDashboardData() {
    try {
        // Simulate API call - replace with actual API endpoint
        const response = await fetch('/api/dashboard');
        const data = await response.json();
        
        updateDashboardUI(data);
    } catch (error) {
        console.error('Failed to load dashboard data:', error);
        showNotification('Failed to load dashboard data', 'error');
    }
}

function updateDashboardUI(data) {
    // Update dashboard elements with real data
    // This would be implemented based on your API response structure
}

function startRealTimeUpdates() {
    // Set up WebSocket or polling for real-time updates
    setInterval(updateRealTimeData, 30000); // Update every 30 seconds
}

async function updateRealTimeData() {
    try {
        // Fetch latest activity and update UI
        const response = await fetch('/api/activity/latest');
        const activity = await response.json();
        
        updateActivityFeed(activity);
    } catch (error) {
        console.error('Failed to update real-time data:', error);
    }
}

function updateActivityFeed(activities) {
    const activityContainer = document.querySelector('[data-activity-feed]');
    if (!activityContainer) return;
    
    // Update activity feed with new data
    // Implementation depends on your data structure
}

// Notification system
function showNotification(message, type = 'info') {
    // Create notification element
    const notification = document.createElement('div');
    notification.className = `fixed top-4 right-4 p-4 rounded-lg shadow-lg z-50 transition-all duration-300 ${
        type === 'error' ? 'bg-red-500 text-white' :
        type === 'success' ? 'bg-green-500 text-white' :
        'bg-primary text-white'
    }`;
    notification.textContent = message;
    
    document.body.appendChild(notification);
    
    // Remove notification after 5 seconds
    setTimeout(() => {
        notification.remove();
    }, 5000);
}

// Agent control functions
async function startAgent(agentId) {
    try {
        const response = await fetch(`/api/agents/${agentId}/start`, {
            method: 'POST'
        });
        
        if (response.ok) {
            showNotification('Agent started successfully', 'success');
            // Update UI to show agent as active
        } else {
            throw new Error('Failed to start agent');
        }
    } catch (error) {
        console.error('Failed to start agent:', error);
        showNotification('Failed to start agent', 'error');
    }
}

async function stopAgent(agentId) {
    try {
        const response = await fetch(`/api/agents/${agentId}/stop`, {
            method: 'POST'
        });
        
        if (response.ok) {
            showNotification('Agent stopped successfully', 'success');
        } else {
            throw new Error('Failed to stop agent');
        }
    } catch (error) {
        console.error('Failed to stop agent:', error);
        showNotification('Failed to stop agent', 'error');
    }
}

// Repository management
async function addRepository(repoData) {
    try {
        const response = await fetch('/api/repositories', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(repoData)
        });
        
        if (response.ok) {
            showNotification('Repository added successfully', 'success');
            loadDashboardData(); // Refresh data
        } else {
            throw new Error('Failed to add repository');
        }
    } catch (error) {
        console.error('Failed to add repository:', error);
        showNotification('Failed to add repository', 'error');
    }
}

// Utility functions
function formatDate(dateString) {
    const date = new Date(dateString);
    return date.toLocaleDateString('en-US', {
        year: 'numeric',
        month: 'short',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit'
    });
}

function debounce(func, wait) {
    let timeout;
    return function executedFunction(...args) {
        const later = () => {
            clearTimeout(timeout);
            func(...args);
        };
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
    };
}

// Export functions for use in components
window.CodeScribe = {
    showNotification,
    startAgent,
    stopAgent,
    addRepository,
    formatDate
};