|
| 1 | +import { useState, useRef, useEffect } from 'react' |
| 2 | +import { Lock, Eye, EyeOff, Download, Upload } from 'lucide-react' |
| 3 | +import { cn } from '../../lib/utils' |
| 4 | + |
| 5 | +interface Props { |
| 6 | + mode: 'export' | 'import' |
| 7 | + hasEncryptedCredentials?: boolean |
| 8 | + onConfirm: (password: string | undefined) => void |
| 9 | + onCancel: () => void |
| 10 | +} |
| 11 | + |
| 12 | +export function ExportImportDialog({ mode, hasEncryptedCredentials, onConfirm, onCancel }: Props): JSX.Element { |
| 13 | + const [includeCredentials, setIncludeCredentials] = useState(mode === 'import' ? !!hasEncryptedCredentials : false) |
| 14 | + const [password, setPassword] = useState('') |
| 15 | + const [confirm, setConfirm] = useState('') |
| 16 | + const [showPw, setShowPw] = useState(false) |
| 17 | + const [error, setError] = useState('') |
| 18 | + const inputRef = useRef<HTMLInputElement>(null) |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + setTimeout(() => inputRef.current?.focus(), 50) |
| 22 | + }, []) |
| 23 | + |
| 24 | + const handleSubmit = () => { |
| 25 | + if (!includeCredentials) { onConfirm(undefined); return } |
| 26 | + if (!password) { setError('Enter a password.'); return } |
| 27 | + if (mode === 'export' && password !== confirm) { setError('Passwords do not match.'); return } |
| 28 | + onConfirm(password) |
| 29 | + } |
| 30 | + |
| 31 | + const Icon = mode === 'export' ? Download : Upload |
| 32 | + const isImport = mode === 'import' |
| 33 | + |
| 34 | + return ( |
| 35 | + <div |
| 36 | + className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm" |
| 37 | + onClick={onCancel} |
| 38 | + > |
| 39 | + <div |
| 40 | + className="bg-popover border border-border rounded-xl shadow-2xl w-full max-w-sm mx-4 overflow-hidden" |
| 41 | + onClick={(e) => e.stopPropagation()} |
| 42 | + > |
| 43 | + {/* Header */} |
| 44 | + <div className="flex items-center gap-3 px-5 py-4 border-b border-border"> |
| 45 | + <div className="w-8 h-8 rounded-lg bg-primary/10 flex items-center justify-center shrink-0"> |
| 46 | + <Icon className="w-4 h-4 text-primary" /> |
| 47 | + </div> |
| 48 | + <div> |
| 49 | + <p className="text-sm font-semibold text-foreground"> |
| 50 | + {mode === 'export' ? 'Export Connections' : 'Import Connections'} |
| 51 | + </p> |
| 52 | + <p className="text-xs text-muted-foreground mt-0.5"> |
| 53 | + {mode === 'export' |
| 54 | + ? 'Optionally include encrypted credentials' |
| 55 | + : 'Provide the password if the file includes credentials'} |
| 56 | + </p> |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + |
| 60 | + {/* Body */} |
| 61 | + <div className="px-5 py-4 space-y-4"> |
| 62 | + {/* Toggle */} |
| 63 | + {!isImport && ( |
| 64 | + <label className="flex items-center gap-3 cursor-pointer"> |
| 65 | + <div |
| 66 | + onClick={() => { setIncludeCredentials(!includeCredentials); setError('') }} |
| 67 | + className={cn( |
| 68 | + 'w-9 h-5 rounded-full transition-colors relative', |
| 69 | + includeCredentials ? 'bg-primary' : 'bg-muted' |
| 70 | + )} |
| 71 | + > |
| 72 | + <span className={cn( |
| 73 | + 'absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full shadow transition-transform', |
| 74 | + includeCredentials && 'translate-x-4' |
| 75 | + )} /> |
| 76 | + </div> |
| 77 | + <span className="text-sm text-foreground/80">Include passwords (encrypted)</span> |
| 78 | + </label> |
| 79 | + )} |
| 80 | + |
| 81 | + {isImport && hasEncryptedCredentials && ( |
| 82 | + <div className="flex items-center gap-2 text-xs text-amber-400 bg-amber-400/10 rounded-lg px-3 py-2"> |
| 83 | + <Lock className="w-3.5 h-3.5 shrink-0" /> |
| 84 | + This file contains encrypted credentials. Enter the export password to restore them. |
| 85 | + </div> |
| 86 | + )} |
| 87 | + |
| 88 | + {/* Password fields */} |
| 89 | + {(includeCredentials || (isImport && hasEncryptedCredentials)) && ( |
| 90 | + <div className="space-y-3"> |
| 91 | + <div className="relative"> |
| 92 | + <input |
| 93 | + ref={inputRef} |
| 94 | + type={showPw ? 'text' : 'password'} |
| 95 | + value={password} |
| 96 | + onChange={(e) => { setPassword(e.target.value); setError('') }} |
| 97 | + onKeyDown={(e) => { if (e.key === 'Enter') handleSubmit() }} |
| 98 | + placeholder={isImport ? 'Export password' : 'Encryption password'} |
| 99 | + className="w-full bg-secondary text-sm text-foreground rounded-lg px-3 py-2 pr-9 focus:outline-none focus:ring-1 focus:ring-primary border border-transparent focus:border-primary transition" |
| 100 | + /> |
| 101 | + <button |
| 102 | + type="button" |
| 103 | + onClick={() => setShowPw(!showPw)} |
| 104 | + className="absolute right-2.5 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground transition-colors" |
| 105 | + > |
| 106 | + {showPw ? <EyeOff className="w-3.5 h-3.5" /> : <Eye className="w-3.5 h-3.5" />} |
| 107 | + </button> |
| 108 | + </div> |
| 109 | + |
| 110 | + {!isImport && ( |
| 111 | + <input |
| 112 | + type={showPw ? 'text' : 'password'} |
| 113 | + value={confirm} |
| 114 | + onChange={(e) => { setConfirm(e.target.value); setError('') }} |
| 115 | + onKeyDown={(e) => { if (e.key === 'Enter') handleSubmit() }} |
| 116 | + placeholder="Confirm password" |
| 117 | + className="w-full bg-secondary text-sm text-foreground rounded-lg px-3 py-2 focus:outline-none focus:ring-1 focus:ring-primary border border-transparent focus:border-primary transition" |
| 118 | + /> |
| 119 | + )} |
| 120 | + |
| 121 | + {error && <p className="text-xs text-red-400">{error}</p>} |
| 122 | + </div> |
| 123 | + )} |
| 124 | + </div> |
| 125 | + |
| 126 | + {/* Footer */} |
| 127 | + <div className="flex items-center justify-end gap-2 px-5 py-3 border-t border-border"> |
| 128 | + <button |
| 129 | + onClick={onCancel} |
| 130 | + className="px-4 py-1.5 text-sm text-muted-foreground hover:text-foreground transition-colors" |
| 131 | + > |
| 132 | + Cancel |
| 133 | + </button> |
| 134 | + <button |
| 135 | + onClick={handleSubmit} |
| 136 | + className="px-4 py-1.5 text-sm font-medium bg-primary text-primary-foreground hover:bg-primary/90 rounded-lg transition-colors" |
| 137 | + > |
| 138 | + {mode === 'export' ? 'Export' : 'Import'} |
| 139 | + </button> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + ) |
| 144 | +} |
0 commit comments