import React, { useState, useEffect } from 'react'; import { Box, VStack, HStack, Text, Heading, FormControl, FormLabel, Input, Button, useToast, Alert, AlertIcon, AlertTitle, AlertDescription, Card, CardBody, CardHeader, Spinner, Divider, Badge, Icon, Switch, FormHelperText, } from '@chakra-ui/react'; import { FiCheck, FiX, FiSettings, FiZap, FiSave } from 'react-icons/fi'; interface S3Config { endpoint: string; region: string; accessKeyId: string; secretAccessKey: string; bucketName: string; useSSL: boolean; } interface TestResult { success: boolean; message: string; details?: any; } export const S3Configuration: React.FC = () => { const [config, setConfig] = useState({ endpoint: '', region: 'us-east-1', accessKeyId: '', secretAccessKey: '', bucketName: '', useSSL: true, }); const [isLoading, setIsLoading] = useState(false); const [isTesting, setIsTesting] = useState(false); const [isSaving, setIsSaving] = useState(false); const [testResult, setTestResult] = useState(null); const [currentConfig, setCurrentConfig] = useState(null); const toast = useToast(); // Load current configuration on component mount useEffect(() => { loadCurrentConfig(); }, []); const loadCurrentConfig = async () => { setIsLoading(true); try { const response = await fetch('/api/config/s3'); if (response.ok) { const data = await response.json(); setCurrentConfig(data.config); setConfig(data.config); } } catch (error) { console.error('Error loading S3 config:', error); } finally { setIsLoading(false); } }; const handleInputChange = (field: keyof S3Config, value: string | boolean) => { setConfig(prev => ({ ...prev, [field]: value, })); }; const testConnection = async () => { setIsTesting(true); setTestResult(null); try { const response = await fetch('/api/config/s3/test', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(config), }); const result = await response.json(); if (response.ok) { setTestResult({ success: true, message: 'Connection successful!', details: result, }); toast({ title: 'Connection Test Successful', description: 'S3 bucket connection is working properly', status: 'success', duration: 5000, isClosable: true, }); } else { setTestResult({ success: false, message: result.error || 'Connection failed', details: result, }); toast({ title: 'Connection Test Failed', description: result.error || 'Failed to connect to S3 bucket', status: 'error', duration: 5000, isClosable: true, }); } } catch (error) { console.error('Error testing S3 connection:', error); setTestResult({ success: false, message: 'Network error or server unavailable', }); toast({ title: 'Connection Test Failed', description: 'Network error or server unavailable', status: 'error', duration: 5000, isClosable: true, }); } finally { setIsTesting(false); } }; const saveConfiguration = async () => { setIsSaving(true); try { const response = await fetch('/api/config/s3', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(config), }); if (response.ok) { setCurrentConfig(config); toast({ title: 'Configuration Saved', description: 'S3 configuration has been saved successfully', status: 'success', duration: 3000, isClosable: true, }); } else { const error = await response.json(); throw new Error(error.error || 'Failed to save configuration'); } } catch (error) { console.error('Error saving S3 config:', error); toast({ title: 'Save Failed', description: error instanceof Error ? error.message : 'Failed to save configuration', status: 'error', duration: 5000, isClosable: true, }); } finally { setIsSaving(false); } }; const hasChanges = () => { if (!currentConfig) return true; return JSON.stringify(config) !== JSON.stringify(currentConfig); }; if (isLoading) { return ( Loading S3 configuration... ); } return ( {/* Header */} S3 Configuration Configure your S3-compatible storage connection for music file storage and playback. {/* Configuration Form */} Connection Settings {/* Endpoint */} S3 Endpoint handleInputChange('endpoint', e.target.value)} placeholder="https://s3.amazonaws.com or http://localhost:9000 for MinIO" bg="gray.700" borderColor="gray.600" color="white" _placeholder={{ color: 'gray.400' }} _focus={{ borderColor: 'blue.400', boxShadow: 'none' }} /> For AWS S3, use: https://s3.amazonaws.com. For MinIO: http://localhost:9000 {/* Region */} Region handleInputChange('region', e.target.value)} placeholder="us-east-1" bg="gray.700" borderColor="gray.600" color="white" _placeholder={{ color: 'gray.400' }} _focus={{ borderColor: 'blue.400', boxShadow: 'none' }} /> AWS region (e.g., us-east-1, eu-west-1) or 'us-east-1' for MinIO {/* Access Key */} Access Key ID handleInputChange('accessKeyId', e.target.value)} placeholder="Your S3 access key" bg="gray.700" borderColor="gray.600" color="white" _placeholder={{ color: 'gray.400' }} _focus={{ borderColor: 'blue.400', boxShadow: 'none' }} /> {/* Secret Key */} Secret Access Key handleInputChange('secretAccessKey', e.target.value)} placeholder="Your S3 secret key" bg="gray.700" borderColor="gray.600" color="white" _placeholder={{ color: 'gray.400' }} _focus={{ borderColor: 'blue.400', boxShadow: 'none' }} /> {/* Bucket Name */} Bucket Name handleInputChange('bucketName', e.target.value)} placeholder="music-files" bg="gray.700" borderColor="gray.600" color="white" _placeholder={{ color: 'gray.400' }} _focus={{ borderColor: 'blue.400', boxShadow: 'none' }} /> The S3 bucket where music files will be stored {/* Use SSL */} Use SSL/TLS handleInputChange('useSSL', e.target.checked)} colorScheme="blue" /> Enable for HTTPS connections (recommended for production) {/* Test Connection */} Test Connection Test your S3 configuration to ensure it's working properly before saving. {testResult && ( {testResult.success ? 'Connection Successful' : 'Connection Failed'} {testResult.message} {testResult.details && ( Details: {JSON.stringify(testResult.details, null, 2)} )} )} {/* Save Configuration */} Save Configuration Save your S3 configuration to use it for music file storage and playback. {currentConfig && ( Configuration Loaded )} {!hasChanges() && currentConfig && ( No changes to save )} {/* Help Section */} Configuration Help For AWS S3: • Endpoint: https://s3.amazonaws.com
• Region: Your AWS region (e.g., us-east-1)
• Access Key: Your AWS access key
• Secret Key: Your AWS secret key
• Bucket: Your S3 bucket name
For MinIO (Local Development): • Endpoint: http://localhost:9000
• Region: us-east-1
• Access Key: minioadmin
• Secret Key: minioadmin
• Bucket: Create a bucket named 'music-files'
For Other S3-Compatible Services: • Endpoint: Your service's endpoint URL
• Region: Your service's region
• Access Key: Your service access key
• Secret Key: Your service secret key
• Bucket: Your bucket name
); };