|
| 1 | +import { useState } from 'react'; |
| 2 | +import { Modal, Input, Select, Switch, message, Alert } from 'antd'; |
| 3 | +import { Upload } from 'lucide-react'; |
| 4 | + |
| 5 | +const { TextArea } = Input; |
| 6 | +const { Option } = Select; |
| 7 | + |
| 8 | +export default function ImportEnvModal({ open, onClose, projectId, onSuccess }) { |
| 9 | + const [content, setContent] = useState(''); |
| 10 | + const [format, setFormat] = useState('env'); |
| 11 | + const [overwrite, setOverwrite] = useState(false); |
| 12 | + const [loading, setLoading] = useState(false); |
| 13 | + |
| 14 | + const handleFileUpload = (e) => { |
| 15 | + const file = e.target.files[0]; |
| 16 | + if (!file) return; |
| 17 | + |
| 18 | + const reader = new FileReader(); |
| 19 | + reader.onload = (event) => { |
| 20 | + setContent(event.target.result); |
| 21 | + |
| 22 | + // Auto-detect format |
| 23 | + if (file.name.endsWith('.json')) { |
| 24 | + setFormat('json'); |
| 25 | + } else { |
| 26 | + setFormat('env'); |
| 27 | + } |
| 28 | + }; |
| 29 | + reader.readAsText(file); |
| 30 | + }; |
| 31 | + |
| 32 | + const handleImport = async () => { |
| 33 | + if (!content.trim()) { |
| 34 | + message.error('Please provide content to import'); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + setLoading(true); |
| 39 | + try { |
| 40 | + const result = await window.electronAPI.envVars.import({ |
| 41 | + projectId, |
| 42 | + content, |
| 43 | + format, |
| 44 | + overwrite, |
| 45 | + }); |
| 46 | + |
| 47 | + if (result.success) { |
| 48 | + const { imported, updated, skipped, total, errors } = result.data; |
| 49 | + |
| 50 | + let messageText = `Import complete: ${imported} imported`; |
| 51 | + if (updated > 0) messageText += `, ${updated} updated`; |
| 52 | + if (skipped > 0) messageText += `, ${skipped} skipped`; |
| 53 | + |
| 54 | + message.success(messageText); |
| 55 | + |
| 56 | + if (errors && errors.length > 0) { |
| 57 | + console.error('Import errors:', errors); |
| 58 | + message.warning(`${errors.length} variables had errors`); |
| 59 | + } |
| 60 | + |
| 61 | + onSuccess(); |
| 62 | + handleClose(); |
| 63 | + } else { |
| 64 | + message.error(result.error || 'Failed to import'); |
| 65 | + } |
| 66 | + } catch (error) { |
| 67 | + message.error('Failed to import: ' + error.message); |
| 68 | + } finally { |
| 69 | + setLoading(false); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + const handleClose = () => { |
| 74 | + setContent(''); |
| 75 | + setFormat('env'); |
| 76 | + setOverwrite(false); |
| 77 | + onClose(); |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <Modal |
| 82 | + title={ |
| 83 | + <div className="flex items-center gap-2"> |
| 84 | + <Upload className="w-5 h-5" /> |
| 85 | + <span>Import Environment Variables</span> |
| 86 | + </div> |
| 87 | + } |
| 88 | + open={open} |
| 89 | + onCancel={handleClose} |
| 90 | + onOk={handleImport} |
| 91 | + okText="Import" |
| 92 | + confirmLoading={loading} |
| 93 | + width={600} |
| 94 | + destroyOnClose |
| 95 | + > |
| 96 | + <div className="space-y-4"> |
| 97 | + <Alert |
| 98 | + message="Import your existing .env or JSON files" |
| 99 | + description="Paste the content below or upload a file. Comments in .env files will be preserved as descriptions." |
| 100 | + type="info" |
| 101 | + showIcon |
| 102 | + /> |
| 103 | + |
| 104 | + <div> |
| 105 | + <label className="block text-sm font-medium mb-2"> |
| 106 | + Upload File (Optional) |
| 107 | + </label> |
| 108 | + <input |
| 109 | + type="file" |
| 110 | + accept=".env,.json,.txt" |
| 111 | + onChange={handleFileUpload} |
| 112 | + className="block w-full text-sm text-gray-400 |
| 113 | + file:mr-4 file:py-2 file:px-4 |
| 114 | + file:rounded-md file:border-0 |
| 115 | + file:text-sm file:font-semibold |
| 116 | + file:bg-blue-600 file:text-white |
| 117 | + hover:file:bg-blue-700 |
| 118 | + file:cursor-pointer cursor-pointer" |
| 119 | + /> |
| 120 | + </div> |
| 121 | + |
| 122 | + <div> |
| 123 | + <label className="block text-sm font-medium mb-2">Format</label> |
| 124 | + <Select |
| 125 | + value={format} |
| 126 | + onChange={setFormat} |
| 127 | + className="w-full" |
| 128 | + > |
| 129 | + <Option value="env">.env format (KEY=value)</Option> |
| 130 | + <Option value="json">JSON format</Option> |
| 131 | + </Select> |
| 132 | + </div> |
| 133 | + |
| 134 | + <div> |
| 135 | + <label className="block text-sm font-medium mb-2">Content</label> |
| 136 | + <TextArea |
| 137 | + value={content} |
| 138 | + onChange={(e) => setContent(e.target.value)} |
| 139 | + placeholder={ |
| 140 | + format === 'env' |
| 141 | + ? '# Database configuration\nDB_HOST=localhost\nDB_PORT=5432\nDB_NAME=myapp' |
| 142 | + : '{\n "DB_HOST": "localhost",\n "DB_PORT": "5432",\n "DB_NAME": "myapp"\n}' |
| 143 | + } |
| 144 | + rows={10} |
| 145 | + className="font-mono text-sm" |
| 146 | + /> |
| 147 | + </div> |
| 148 | + |
| 149 | + <div className="flex items-center justify-between p-3 bg-gray-800 rounded-lg"> |
| 150 | + <div> |
| 151 | + <div className="font-medium">Overwrite existing variables</div> |
| 152 | + <div className="text-sm text-gray-400"> |
| 153 | + Update values if keys already exist |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + <Switch |
| 157 | + checked={overwrite} |
| 158 | + onChange={setOverwrite} |
| 159 | + /> |
| 160 | + </div> |
| 161 | + |
| 162 | + {format === 'env' && ( |
| 163 | + <Alert |
| 164 | + message="Tip" |
| 165 | + description="Comments above variables (lines starting with #) will be imported as descriptions." |
| 166 | + type="success" |
| 167 | + showIcon |
| 168 | + /> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + </Modal> |
| 172 | + ); |
| 173 | +} |
0 commit comments