// 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 };