|
| 1 | +import { useState, useEffect } from 'react'; |
| 2 | +import { Modal, Select, Switch, message, Alert, Tag, Space } from 'antd'; |
| 3 | +import { Copy } from 'lucide-react'; |
| 4 | + |
| 5 | +const { Option } = Select; |
| 6 | + |
| 7 | +export default function CopyToProjectModal({ |
| 8 | + open, |
| 9 | + onClose, |
| 10 | + sourceProject, |
| 11 | + selectedVarIds, |
| 12 | + allProjects, |
| 13 | + onSuccess |
| 14 | +}) { |
| 15 | + const [targetProjectId, setTargetProjectId] = useState(null); |
| 16 | + const [overwrite, setOverwrite] = useState(false); |
| 17 | + const [loading, setLoading] = useState(false); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (open) { |
| 21 | + setTargetProjectId(null); |
| 22 | + setOverwrite(false); |
| 23 | + } |
| 24 | + }, [open]); |
| 25 | + |
| 26 | + const handleCopy = async () => { |
| 27 | + if (!targetProjectId) { |
| 28 | + message.error('Please select a target project'); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + if (selectedVarIds.length === 0) { |
| 33 | + message.error('No variables selected to copy'); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + setLoading(true); |
| 38 | + try { |
| 39 | + const result = await window.electronAPI.envVars.copyToProject({ |
| 40 | + sourceProjectId: sourceProject.id, |
| 41 | + targetProjectId, |
| 42 | + envVarIds: selectedVarIds, |
| 43 | + overwrite, |
| 44 | + }); |
| 45 | + |
| 46 | + if (result.success) { |
| 47 | + const { copied, updated, skipped, errors } = result.data; |
| 48 | + |
| 49 | + let messageText = `Copy complete: ${copied} copied`; |
| 50 | + if (updated > 0) messageText += `, ${updated} updated`; |
| 51 | + if (skipped > 0) messageText += `, ${skipped} skipped`; |
| 52 | + |
| 53 | + message.success(messageText); |
| 54 | + |
| 55 | + if (errors && errors.length > 0) { |
| 56 | + console.error('Copy errors:', errors); |
| 57 | + message.warning(`${errors.length} variables had errors`); |
| 58 | + } |
| 59 | + |
| 60 | + onSuccess(); |
| 61 | + onClose(); |
| 62 | + } else { |
| 63 | + message.error(result.error || 'Failed to copy variables'); |
| 64 | + } |
| 65 | + } catch (error) { |
| 66 | + message.error('Failed to copy: ' + error.message); |
| 67 | + } finally { |
| 68 | + setLoading(false); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + // Filter out the source project from target options |
| 73 | + const availableProjects = allProjects.filter(p => p.id !== sourceProject.id); |
| 74 | + const targetProject = availableProjects.find(p => p.id === targetProjectId); |
| 75 | + |
| 76 | + return ( |
| 77 | + <Modal |
| 78 | + title={ |
| 79 | + <div className="flex items-center gap-2"> |
| 80 | + <Copy className="w-5 h-5" /> |
| 81 | + <span>Copy Variables to Another Project</span> |
| 82 | + </div> |
| 83 | + } |
| 84 | + open={open} |
| 85 | + onCancel={onClose} |
| 86 | + onOk={handleCopy} |
| 87 | + okText="Copy Variables" |
| 88 | + confirmLoading={loading} |
| 89 | + width={550} |
| 90 | + destroyOnClose |
| 91 | + > |
| 92 | + <div className="space-y-4"> |
| 93 | + <Alert |
| 94 | + message={`Copying ${selectedVarIds.length} variable${selectedVarIds.length !== 1 ? 's' : ''} from ${sourceProject.name}`} |
| 95 | + description="Select the target project where you want to copy these variables." |
| 96 | + type="info" |
| 97 | + showIcon |
| 98 | + /> |
| 99 | + |
| 100 | + <div> |
| 101 | + <label className="block text-sm font-medium mb-2"> |
| 102 | + Source Project |
| 103 | + </label> |
| 104 | + <div className="p-3 bg-gray-800 rounded-lg"> |
| 105 | + <div className="font-medium">{sourceProject.name}</div> |
| 106 | + {sourceProject.description && ( |
| 107 | + <div className="text-sm text-gray-400 mt-1"> |
| 108 | + {sourceProject.description} |
| 109 | + </div> |
| 110 | + )} |
| 111 | + <Tag color="blue" className="mt-2"> |
| 112 | + {selectedVarIds.length} variable{selectedVarIds.length !== 1 ? 's' : ''} selected |
| 113 | + </Tag> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + |
| 117 | + <div> |
| 118 | + <label className="block text-sm font-medium mb-2"> |
| 119 | + Target Project <span className="text-red-500">*</span> |
| 120 | + </label> |
| 121 | + <Select |
| 122 | + value={targetProjectId} |
| 123 | + onChange={setTargetProjectId} |
| 124 | + placeholder="Select target project" |
| 125 | + className="w-full" |
| 126 | + showSearch |
| 127 | + optionFilterProp="children" |
| 128 | + > |
| 129 | + {availableProjects.map(project => ( |
| 130 | + <Option key={project.id} value={project.id}> |
| 131 | + <div> |
| 132 | + <div className="font-medium">{project.name}</div> |
| 133 | + {project.description && ( |
| 134 | + <div className="text-xs text-gray-400"> |
| 135 | + {project.description} |
| 136 | + </div> |
| 137 | + )} |
| 138 | + </div> |
| 139 | + </Option> |
| 140 | + ))} |
| 141 | + </Select> |
| 142 | + {availableProjects.length === 0 && ( |
| 143 | + <div className="text-sm text-gray-400 mt-2"> |
| 144 | + No other projects available. Create a new project first. |
| 145 | + </div> |
| 146 | + )} |
| 147 | + </div> |
| 148 | + |
| 149 | + {targetProject && ( |
| 150 | + <div className="p-3 bg-gray-800 rounded-lg"> |
| 151 | + <div className="text-sm text-gray-400 mb-1">Target Project</div> |
| 152 | + <div className="font-medium">{targetProject.name}</div> |
| 153 | + {targetProject._count && ( |
| 154 | + <Tag color="green" className="mt-2"> |
| 155 | + {targetProject._count.envVars} existing variable{targetProject._count.envVars !== 1 ? 's' : ''} |
| 156 | + </Tag> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + )} |
| 160 | + |
| 161 | + <div className="flex items-center justify-between p-3 bg-gray-800 rounded-lg"> |
| 162 | + <div> |
| 163 | + <div className="font-medium">Overwrite existing variables</div> |
| 164 | + <div className="text-sm text-gray-400"> |
| 165 | + Update values if keys already exist in target |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + <Switch |
| 169 | + checked={overwrite} |
| 170 | + onChange={setOverwrite} |
| 171 | + /> |
| 172 | + </div> |
| 173 | + |
| 174 | + <Alert |
| 175 | + message="Note" |
| 176 | + description={ |
| 177 | + <Space direction="vertical" size="small"> |
| 178 | + <div>• Variables will be encrypted with the same master key</div> |
| 179 | + <div>• Descriptions will be copied along with values</div> |
| 180 | + <div>• All operations will be logged in audit history</div> |
| 181 | + {!overwrite && <div>• Existing keys in target will be skipped</div>} |
| 182 | + </Space> |
| 183 | + } |
| 184 | + type="warning" |
| 185 | + showIcon |
| 186 | + /> |
| 187 | + </div> |
| 188 | + </Modal> |
| 189 | + ); |
| 190 | +} |
0 commit comments