// API Key management functionality // Load API keys async function loadApiKeys() { if (!config.isConfigured()) { showAlert('Please configure your API settings first.', 'warning'); return; } const container = document.getElementById('apiKeysContainer'); container.innerHTML = '
Loading API keys...
'; try { const response = await apiClient.getApiKeys(); // Handle structured response - extract api_keys array from response object const apiKeys = response.api_keys || response; displayApiKeys(apiKeys); } catch (error) { handleApiError(error, 'loading API keys'); container.innerHTML = '
Failed to load API keys
'; } } // Display API keys function displayApiKeys(apiKeys) { const container = document.getElementById('apiKeysContainer'); if (!apiKeys || apiKeys.length === 0) { container.innerHTML = `

No API keys found

Create your first API key to get started!

`; return; } const apiKeysHtml = `
${apiKeys.map(key => ` `).join('')}
Name Key Status Created Last Used Actions
${escapeHtml(key.name)}
${key.key ? key.key.substring(0, 8) + '...' : 'Hidden'}
${key.is_active ? 'Active' : 'Inactive'} ${formatDate(key.created_at)} ${key.last_used_at ? formatDate(key.last_used_at) : 'Never'}
`; container.innerHTML = apiKeysHtml; } // Show create API key modal function showCreateApiKeyModal() { const modalBody = `
Choose a descriptive name to identify this API key
Important: The API key will only be shown once after creation. Make sure to copy and store it securely.
`; const modalFooter = ` `; const modal = createModal('createApiKeyModal', 'Create New API Key', modalBody, modalFooter); // Handle form submission document.getElementById('createApiKeyForm').addEventListener('submit', (e) => { e.preventDefault(); createApiKey(); }); modal.show(); // Focus on name field setTimeout(() => document.getElementById('apiKeyName').focus(), 100); } // Create API key async function createApiKey() { const name = document.getElementById('apiKeyName').value.trim(); const description = document.getElementById('apiKeyDescription').value.trim(); if (!name) { showAlert('API key name is required', 'danger'); return; } const createButton = document.querySelector('#createApiKeyModal .btn-primary'); setLoadingState(createButton); try { const result = await apiClient.createApiKey({ name, description }); // Close the create modal bootstrap.Modal.getInstance(document.getElementById('createApiKeyModal')).hide(); removeModal('createApiKeyModal'); // Show the new API key showNewApiKeyModal(result); // Refresh the list loadApiKeys(); } catch (error) { handleApiError(error, 'creating API key'); } finally { setLoadingState(createButton, false); } } // Show new API key modal function showNewApiKeyModal(apiKeyData) { const modalBody = `
API Key Created Successfully!

${escapeHtml(apiKeyData.name)}

Important: This is the only time you'll see this API key. Make sure to copy and store it securely before closing this dialog.
`; const modalFooter = ` `; const modal = createModal('newApiKeyModal', 'New API Key Created', modalBody, modalFooter); modal.show(); // Auto-select the API key text setTimeout(() => { const input = document.getElementById('newApiKeyValue'); input.select(); input.focus(); }, 100); } // Copy API key to clipboard async function copyApiKey(key) { if (!key) { showAlert('No API key to copy', 'warning'); return; } try { await copyToClipboard(key); } catch (error) { // Fallback for older browsers const textArea = document.createElement('textarea'); textArea.value = key; document.body.appendChild(textArea); textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); showAlert('API key copied to clipboard!', 'success'); } } // View API key details async function viewApiKey(keyId) { try { const response = await apiClient.getApiKeys(); // Handle structured response - extract api_keys array from response object const apiKeys = response.api_keys || response; const apiKey = apiKeys.find(k => k.id === keyId); if (!apiKey) { showAlert('API key not found', 'danger'); return; } const modalBody = `
${escapeHtml(apiKey.name)}

${escapeHtml(apiKey.description || 'No description')}


Key Information

Created: ${formatDate(apiKey.created_at)}

Last Used: ${apiKey.last_used_at ? formatDate(apiKey.last_used_at) : 'Never'}

ID: ${apiKey.id}

Status & Security

Status: ${apiKey.is_active ? 'Active' : 'Inactive'}

Key Preview: ${apiKey.key ? apiKey.key.substring(0, 8) + '...' : 'Hidden'}

${apiKey.last_used_at ? `
Usage Statistics
This API key was last used on ${formatDate(apiKey.last_used_at)}
` : `
This API key has never been used
`}
`; const modalFooter = ` `; const modal = createModal('viewApiKeyModal', 'API Key Details', modalBody, modalFooter); modal.show(); } catch (error) { handleApiError(error, 'loading API key details'); } } // Delete API key function deleteApiKey(keyId) { confirmAction('Are you sure you want to delete this API key? This action cannot be undone and will immediately revoke access for any applications using this key.', async () => { try { await apiClient.deleteApiKey(keyId); showAlert('API key deleted successfully!', 'success'); loadApiKeys(); // Close any open modals const modals = ['viewApiKeyModal', 'newApiKeyModal']; modals.forEach(modalId => { const modalElement = document.getElementById(modalId); if (modalElement) { bootstrap.Modal.getInstance(modalElement)?.hide(); removeModal(modalId); } }); } catch (error) { handleApiError(error, 'deleting API key'); } }); } // Generate API key usage report function generateUsageReport() { showAlert('Usage report functionality would be implemented here', 'info'); } // Bulk operations for API keys function bulkDeleteApiKeys() { showAlert('Bulk operations functionality would be implemented here', 'info'); }