|
| 1 | +/** |
| 2 | + * BeapImportZone — Inline drop zone for importing BEAP messages or handshake capsules |
| 3 | + * |
| 4 | + * Matches HandshakeView's Import Capsule style. Drop .beap files or click to browse. |
| 5 | + * Uses importFromFile from the ingress pipeline. |
| 6 | + * |
| 7 | + * @version 1.0.0 |
| 8 | + */ |
| 9 | + |
| 10 | +import React, { useState, useCallback, useRef } from 'react' |
| 11 | +import { importFromFile } from '../../ingress/importPipeline' |
| 12 | + |
| 13 | +const ACCEPTED_TYPES = '.beap,.json,.txt,.beap.json,.beap.txt' |
| 14 | + |
| 15 | +interface BeapImportZoneProps { |
| 16 | + theme?: 'default' | 'dark' | 'professional' |
| 17 | + onImported?: () => void |
| 18 | +} |
| 19 | + |
| 20 | +export const BeapImportZone: React.FC<BeapImportZoneProps> = ({ |
| 21 | + theme = 'default', |
| 22 | + onImported, |
| 23 | +}) => { |
| 24 | + const isProfessional = theme === 'professional' |
| 25 | + const textColor = isProfessional ? '#1f2937' : 'white' |
| 26 | + const mutedColor = isProfessional ? '#6b7280' : 'rgba(255,255,255,0.6)' |
| 27 | + const borderColor = isProfessional ? 'rgba(0,0,0,0.1)' : 'rgba(255,255,255,0.15)' |
| 28 | + const [isDragOver, setIsDragOver] = useState(false) |
| 29 | + const [importing, setImporting] = useState(false) |
| 30 | + const [error, setError] = useState<string | null>(null) |
| 31 | + const fileInputRef = useRef<HTMLInputElement>(null) |
| 32 | + |
| 33 | + const handleDrop = useCallback( |
| 34 | + async (e: React.DragEvent) => { |
| 35 | + e.preventDefault() |
| 36 | + e.stopPropagation() |
| 37 | + setIsDragOver(false) |
| 38 | + const file = e.dataTransfer.files?.[0] |
| 39 | + if (!file) return |
| 40 | + setImporting(true) |
| 41 | + setError(null) |
| 42 | + try { |
| 43 | + const result = await importFromFile(file) |
| 44 | + if (result.success) { |
| 45 | + onImported?.() |
| 46 | + } else { |
| 47 | + setError(result.error ?? 'Import failed') |
| 48 | + } |
| 49 | + } catch (err) { |
| 50 | + setError(err instanceof Error ? err.message : 'Import failed') |
| 51 | + } finally { |
| 52 | + setImporting(false) |
| 53 | + } |
| 54 | + }, |
| 55 | + [onImported], |
| 56 | + ) |
| 57 | + |
| 58 | + const handleFileInputChange = useCallback( |
| 59 | + async (e: React.ChangeEvent<HTMLInputElement>) => { |
| 60 | + const file = e.target.files?.[0] |
| 61 | + if (!file) return |
| 62 | + e.target.value = '' |
| 63 | + setImporting(true) |
| 64 | + setError(null) |
| 65 | + try { |
| 66 | + const result = await importFromFile(file) |
| 67 | + if (result.success) { |
| 68 | + onImported?.() |
| 69 | + } else { |
| 70 | + setError(result.error ?? 'Import failed') |
| 71 | + } |
| 72 | + } catch (err) { |
| 73 | + setError(err instanceof Error ? err.message : 'Import failed') |
| 74 | + } finally { |
| 75 | + setImporting(false) |
| 76 | + } |
| 77 | + }, |
| 78 | + [onImported], |
| 79 | + ) |
| 80 | + |
| 81 | + return ( |
| 82 | + <div style={{ marginTop: '12px' }}> |
| 83 | + <input |
| 84 | + ref={fileInputRef} |
| 85 | + type="file" |
| 86 | + accept={ACCEPTED_TYPES} |
| 87 | + onChange={handleFileInputChange} |
| 88 | + style={{ display: 'none' }} |
| 89 | + /> |
| 90 | + <div |
| 91 | + onDragOver={(e) => { |
| 92 | + e.preventDefault() |
| 93 | + e.stopPropagation() |
| 94 | + setIsDragOver(true) |
| 95 | + }} |
| 96 | + onDragLeave={(e) => { |
| 97 | + e.preventDefault() |
| 98 | + e.stopPropagation() |
| 99 | + setIsDragOver(false) |
| 100 | + }} |
| 101 | + onDrop={handleDrop} |
| 102 | + onClick={() => !importing && fileInputRef.current?.click()} |
| 103 | + style={{ |
| 104 | + border: `2px dashed ${isDragOver ? (isProfessional ? '#7c3aed' : '#a78bfa') : borderColor}`, |
| 105 | + borderRadius: '10px', |
| 106 | + padding: '24px 16px', |
| 107 | + textAlign: 'center', |
| 108 | + background: isDragOver |
| 109 | + ? isProfessional |
| 110 | + ? 'rgba(124,58,237,0.06)' |
| 111 | + : 'rgba(167,139,250,0.08)' |
| 112 | + : isProfessional |
| 113 | + ? 'rgba(0,0,0,0.02)' |
| 114 | + : 'rgba(255,255,255,0.04)', |
| 115 | + cursor: importing ? 'not-allowed' : 'pointer', |
| 116 | + transition: 'all 0.2s ease', |
| 117 | + }} |
| 118 | + > |
| 119 | + <div style={{ fontSize: '28px', marginBottom: '8px', opacity: 0.7 }}> |
| 120 | + 📦 |
| 121 | + </div> |
| 122 | + <div |
| 123 | + style={{ |
| 124 | + fontSize: '12px', |
| 125 | + fontWeight: 600, |
| 126 | + color: textColor, |
| 127 | + marginBottom: '4px', |
| 128 | + }} |
| 129 | + > |
| 130 | + {importing ? 'Importing…' : 'Drop a .beap file here or click to browse'} |
| 131 | + </div> |
| 132 | + <div style={{ fontSize: '11px', color: mutedColor }}> |
| 133 | + BEAP messages or handshake requests |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + {error && ( |
| 137 | + <div |
| 138 | + style={{ |
| 139 | + marginTop: '8px', |
| 140 | + fontSize: '11px', |
| 141 | + color: '#ef4444', |
| 142 | + padding: '6px 10px', |
| 143 | + background: 'rgba(239,68,68,0.1)', |
| 144 | + borderRadius: '6px', |
| 145 | + }} |
| 146 | + > |
| 147 | + {error} |
| 148 | + </div> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + ) |
| 152 | +} |
0 commit comments