import { createSignal, createEffect, Show, For } from 'solid-js'; import { Card } from '../components/ui/Card'; import { Button } from '../components/ui/Button'; import { Input } from '../components/ui/Input'; import { toast } from '../components/ui/Toast'; import { CheckCircle, AlertCircle, Shield, Key, Globe, Clock, Users, Settings } from 'lucide-solid'; import { getApiV1BaseUrl } from '@/lib/api-url'; interface APIKey { id: number; name: string; permissions: string[]; is_active: boolean; last_used?: string; expires_at?: string; created_at: string; updated_at: string; } interface BrowserExtension { id: number; extension_id: string; name: string; is_active: boolean; last_seen?: string; created_at: string; updated_at: string; } interface QuickStartGuide { title: string; description: string; icon: any; steps: string[]; } interface Example { title: string; description: string; code: string; language: string; } const BrowserExtensionSettings = () => { const apiBaseUrl = getApiV1BaseUrl(); const [apiKeys, setApiKeys] = createSignal([]); const [extensions, setExtensions] = createSignal([]); const [loading, setLoading] = createSignal(false); const [showCreateKey, setShowCreateKey] = createSignal(false); const [newKeyName, setNewKeyName] = createSignal(''); const [newKeyPermissions, setNewKeyPermissions] = createSignal([ 'bookmarks:read', 'bookmarks:write', 'files:read', 'files:write' ]); const [activeTab, setActiveTab] = createSignal<'overview' | 'api-keys' | 'extensions' | 'examples'>('api-keys'); const quickStartGuides: QuickStartGuide[] = [ { title: 'Generate API Key', description: 'Create a secure API key for your browser extension', icon: , steps: [ 'Go to Settings → Browser Extension', 'Click "Generate New Key"', 'Choose permissions and name', 'Copy the generated key' ] }, { title: 'Configure Extension', description: 'Set up your browser extension with the API key', icon: , steps: [ 'Install the Trackeep browser extension', 'Open extension options', 'Paste your API key', 'Test connection', 'Start saving bookmarks!' ] }, { title: 'Security Best Practices', description: 'Keep your API keys secure and monitor usage', icon: , steps: [ 'Use unique names for each key', 'Set expiration dates for temporary access', 'Revoke unused keys immediately', 'Monitor key usage regularly', 'Never share keys publicly' ] } ]; const codeExamples: Example[] = [ { title: 'JavaScript Extension', description: 'Basic API key validation and bookmark creation', code: `// Validate API key const response = await fetch('/api/v1/browser-extension/validate', { headers: { 'Authorization': 'Bearer tk_your_api_key_here' } }); // Create bookmark const bookmarkData = { title: 'My Awesome Bookmark', url: 'https://example.com', description: 'A useful website', tags: ['development', 'tools'] }; const createResponse = await fetch('/api/v1/bookmarks', { method: 'POST', headers: { 'Authorization': 'Bearer tk_your_api_key_here', 'Content-Type': 'application/json' }, body: JSON.stringify(bookmarkData) });`, language: 'javascript' }, { title: 'Python Integration', description: 'Server-side API integration example', code: `import requests # Validate API key response = requests.get( 'https://your-trackeep.com/api/v1/browser-extension/validate', headers={'Authorization': 'Bearer tk_your_api_key_here'} ) # Create bookmark bookmark_data = { 'title': 'My Awesome Bookmark', 'url': 'https://example.com', 'description': 'A useful website', 'tags': ['development', 'tools'] } response = requests.post( 'https://your-trackeep.com/api/v1/bookmarks', json=bookmark_data, headers={ 'Authorization': 'Bearer tk_your_api_key_here', 'Content-Type': 'application/json' } )`, language: 'python' }, { title: 'cURL Command', description: 'Command line testing for API endpoints', code: `# Validate API key curl -X GET \\\n -H "Authorization: Bearer tk_your_api_key_here" \\\n https://your-trackeep.com/api/v1/browser-extension/validate # Create bookmark curl -X POST \\\n -H "Authorization: Bearer tk_your_api_key_here" \\\n -H "Content-Type: application/json" \\\n -d '{"title":"My Bookmark","url":"https://example.com"}' \\\n https://your-trackeep.com/api/v1/bookmarks`, language: 'bash' } ]; const availablePermissions = [ { id: 'bookmarks:read', label: 'Read Bookmarks', description: 'View and read your bookmarks' }, { id: 'bookmarks:write', label: 'Write Bookmarks', description: 'Create, edit, and delete bookmarks' }, { id: 'files:read', label: 'Read Files', description: 'View and download your files' }, { id: 'files:write', label: 'Write Files', description: 'Upload, edit, and delete files' }, { id: 'notes:read', label: 'Read Notes', description: 'View your notes' }, { id: 'notes:write', label: 'Write Notes', description: 'Create, edit, and delete notes' }, { id: 'tasks:read', label: 'Read Tasks', description: 'View your tasks' }, { id: 'tasks:write', label: 'Write Tasks', description: 'Create, edit, and delete tasks' } ]; const loadApiKeys = async () => { try { const response = await fetch(`${apiBaseUrl}/browser-extension/api-keys`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); if (response.ok) { const data = await response.json(); setApiKeys(data); } else { toast.error('Failed to load API keys'); } } catch (error) { toast.error('Error loading API keys'); } }; const loadExtensions = async () => { try { const response = await fetch(`${apiBaseUrl}/browser-extension/extensions`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); if (response.ok) { const data = await response.json(); setExtensions(data); } else { toast.error('Failed to load extensions'); } } catch (error) { toast.error('Error loading extensions'); } }; const createAPIKey = async () => { if (!newKeyName().trim()) { toast.error('Please enter a name for the API key'); return; } if (newKeyPermissions().length === 0) { toast.error('Please select at least one permission'); return; } setLoading(true); try { const response = await fetch(`${apiBaseUrl}/browser-extension/api-keys/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` }, body: JSON.stringify({ name: newKeyName(), permissions: newKeyPermissions(), expires_in: 365 // 1 year }) }); if (response.ok) { const data = await response.json(); toast.success(`API key "${data.name}" created successfully!`); setNewKeyName(''); setNewKeyPermissions(['bookmarks:read', 'bookmarks:write', 'files:read', 'files:write']); setShowCreateKey(false); await loadApiKeys(); } else { const error = await response.json(); toast.error(error.error || 'Failed to create API key'); } } catch (error) { toast.error('Error creating API key'); } finally { setLoading(false); } }; const revokeAPIKey = async (keyId: number) => { if (!confirm('Are you sure you want to revoke this API key? This action cannot be undone.')) { return; } try { const response = await fetch(`${apiBaseUrl}/browser-extension/api-keys/${keyId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); if (response.ok) { toast.success('API key revoked successfully'); await loadApiKeys(); } else { const error = await response.json(); toast.error(error.error || 'Failed to revoke API key'); } } catch (error) { toast.error('Error revoking API key'); } }; const revokeExtension = async (extensionId: string) => { if (!confirm('Are you sure you want to revoke this extension? This action cannot be undone.')) { return; } try { const response = await fetch(`${apiBaseUrl}/browser-extension/extensions/${extensionId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` } }); if (response.ok) { toast.success('Extension revoked successfully'); await loadExtensions(); } else { const error = await response.json(); toast.error(error.error || 'Failed to revoke extension'); } } catch (error) { toast.error('Error revoking extension'); } }; const togglePermission = (permissionId: string) => { const current = newKeyPermissions(); if (current.includes(permissionId)) { setNewKeyPermissions(current.filter(p => p !== permissionId)); } else { setNewKeyPermissions([...current, permissionId]); } }; createEffect(() => { loadApiKeys(); loadExtensions(); }, []); return (

Browser Extension Settings

Manage API keys and browser extensions for secure access to your Trackeep account.

{/* Tab Navigation */}
{/* Overview Tab */}
{guide => (
{guide.icon}

{guide.title}

{guide.description}

{step => (
{step}
)}
)}

Security Status

API Keys Secure
All keys using secure API key authentication
Extensions Registered
{extensions().length} active extensions
Quick Setup Available
Get started in under 5 minutes

Recent Activity

Last API key created:
{apiKeys().length > 0 ? new Date(apiKeys()[0].created_at).toLocaleDateString() : 'No keys created yet'}
Total API keys:
{apiKeys().length} active, {apiKeys().filter(k => !k.is_active).length} revoked
Extensions active:
{extensions().length} connected
{/* API Keys Section */}

API Keys

Create New API Key

setNewKeyName((e.target as HTMLInputElement).value)} placeholder="e.g., Chrome Extension, Laptop Backup" class="w-full" />
{permission => ( )}
{key => (

{key.name}

Created: {new Date(key.created_at).toLocaleDateString()}
{permission => ( {permission} )}
{key.expires_at && (
Expires: {new Date(key.expires_at).toLocaleDateString()}
)}
{key.is_active ? 'Active' : 'Revoked'}
)}
{/* Browser Extensions Section */}

Registered Extensions

Manage browser extensions that have access to your account.

{extension => (

{extension.name}

Extension ID: {extension.extension_id}
Registered: {new Date(extension.created_at).toLocaleDateString()}
{extension.last_seen && (
Last seen: {new Date(extension.last_seen).toLocaleDateString()}
)}
{extension.is_active ? 'Active' : 'Revoked'}
)}
{/* Examples Tab */}
{example => (

{example.language.toUpperCase()}
{example.title}

{example.description}

                    {example.code}
                  
)}
); }; export default BrowserExtensionSettings;