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