Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | import { useState, useRef } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { X, Send, Bug, Lightbulb, MessageSquare, AlertCircle, Upload, Trash2 } from 'lucide-react'; import { useFeedbackStore, type FeedbackType, type FeedbackPriority, type FeedbackAttachment } from './feedbackStore'; import { Button } from '../ui'; import { useToast } from '../ui/Toast'; import { silentErrorHandler } from '../../lib/error-utils'; interface FeedbackModalProps { onClose: () => void; } const typeOptions: { value: FeedbackType; label: string; icon: React.ReactNode }[] = [ { value: 'bug', label: 'Bug Report', icon: <Bug className="w-4 h-4" /> }, { value: 'feature', label: 'Feature Request', icon: <Lightbulb className="w-4 h-4" /> }, { value: 'general', label: 'General Feedback', icon: <MessageSquare className="w-4 h-4" /> }, ]; const priorityOptions: { value: FeedbackPriority; label: string; color: string }[] = [ { value: 'low', label: 'Low', color: 'text-gray-500' }, { value: 'medium', label: 'Medium', color: 'text-yellow-600' }, { value: 'high', label: 'High', color: 'text-red-500' }, ]; export function FeedbackModal({ onClose }: FeedbackModalProps) { const { submitFeedback, isLoading, error } = useFeedbackStore(); const { toast } = useToast(); const fileInputRef = useRef<HTMLInputElement>(null); const [type, setType] = useState<FeedbackType>('bug'); const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [priority, setPriority] = useState<FeedbackPriority>('medium'); const [attachments, setAttachments] = useState<File[]>([]); const handleSubmit = async () => { if (!title.trim() || !description.trim()) { toast('Please fill in title and description', 'warning'); return; } // Convert files to base64 for storage const processedAttachments: FeedbackAttachment[] = await Promise.all( attachments.map(async (file) => { return new Promise<FeedbackAttachment>((resolve) => { const reader = new FileReader(); reader.onload = () => { resolve({ name: file.name, type: file.type, size: file.size, data: reader.result as string, }); }; reader.readAsDataURL(file); }); }) ); try { await submitFeedback({ type, title: title.trim(), description: description.trim(), priority, attachments: processedAttachments, metadata: { appVersion: '0.0.0', os: navigator.platform, timestamp: Date.now(), }, }); toast('Feedback submitted successfully!', 'success'); // Reset form setTitle(''); setDescription(''); setAttachments([]); setType('bug'); setPriority('medium'); onClose(); } catch (err) { toast('Failed to submit feedback. Please try again.', 'error'); } }; const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => { const files = Array.from(e.target.files || []); // Limit to 5 attachments const newFiles = [...attachments, ...files].slice(0, 5); setAttachments(newFiles); }; const removeAttachment = (index: number) => { setAttachments(attachments.filter((_, i) => i !== index)); }; const formatFileSize = (bytes: number): string => { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; }; return ( <AnimatePresence> <motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/50" onClick={(e) => { if (e.target === e.currentTarget) onClose(); }} > <motion.div initial={{ scale: 0.95, opacity: 0 }} animate={{ scale: 1, opacity: 1 }} exit={{ scale: 0.95, opacity: 0 }} className="w-full max-w-lg bg-white dark:bg-gray-800 rounded-xl shadow-2xl overflow-hidden" role="dialog" aria-modal="true" aria-labelledby="feedback-title" > {/* Header */} <div className="flex items-center justify-between px-6 py-4 border-b border-gray-200 dark:border-gray-700"> <h2 id="feedback-title" className="text-lg font-semibold text-gray-900 dark:text-gray-100"> Submit Feedback </h2> <button onClick={onClose} className="p-1 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors" aria-label="Close" > <X className="w-5 h-5" /> </button> </div> {/* Content */} <div className="px-6 py-4 space-y-4"> {/* Type Selection */} <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Feedback Type </label> <div className="flex gap-2"> {typeOptions.map((opt) => ( <button key={opt.value} onClick={() => setType(opt.value)} className={`flex-1 flex items-center justify-center gap-2 px-3 py-2 rounded-lg border text-sm transition-all ${ type === opt.value ? 'border-orange-400 bg-orange-50 dark:bg-orange-900/20 text-orange-600 dark:text-orange-400' : 'border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700' }`} > {opt.icon} {opt.label} </button> ))} </div> </div> {/* Title */} <div> <label htmlFor="feedback-title-input" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Title </label> <input id="feedback-title-input" type="text" value={title} onChange={(e) => setTitle(e.target.value)} placeholder="Brief summary of your feedback" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-orange-400 dark:bg-gray-700 dark:text-gray-100" maxLength={100} /> </div> {/* Description */} <div> <label htmlFor="feedback-desc-input" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Description </label> <textarea id="feedback-desc-input" value={description} onChange={(e) => setDescription(e.target.value)} placeholder="Please describe your feedback in detail. For bugs, include steps to reproduce." className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-orange-400 dark:bg-gray-700 dark:text-gray-100 resize-none" rows={4} maxLength={2000} /> </div> {/* Priority */} <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Priority </label> <div className="flex gap-2"> {priorityOptions.map((opt) => ( <button key={opt.value} onClick={() => setPriority(opt.value)} className={`flex-1 px-3 py-2 rounded-lg border text-sm transition-all ${ priority === opt.value ? 'border-orange-400 bg-orange-50 dark:bg-orange-900/20 font-medium' : 'border-gray-200 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700' } ${opt.color}`} > {opt.label} </button> ))} </div> </div> {/* Attachments */} <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Attachments (optional, max 5) </label> <input ref={fileInputRef} type="file" multiple accept="image/*" onChange={handleFileSelect} className="hidden" /> <button onClick={() => fileInputRef.current?.click()} className="flex items-center gap-2 px-3 py-2 border border-dashed border-gray-300 dark:border-gray-600 rounded-lg text-sm text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors" > <Upload className="w-4 h-4" /> Add Screenshots </button> {attachments.length > 0 && ( <div className="mt-2 space-y-1"> {attachments.map((file, index) => ( <div key={index} className="flex items-center justify-between px-2 py-1 bg-gray-50 dark:bg-gray-700 rounded text-xs" > <span className="truncate text-gray-600 dark:text-gray-300"> {file.name} ({formatFileSize(file.size)}) </span> <button onClick={() => removeAttachment(index)} className="text-gray-400 hover:text-red-500" > <Trash2 className="w-3.5 h-3.5" /> </button> </div> ))} </div> )} </div> {/* Error Display */} {error && ( <div className="flex items-center gap-2 text-sm text-red-500 bg-red-50 dark:bg-red-900/20 px-3 py-2 rounded-lg"> <AlertCircle className="w-4 h-4" /> {error} </div> )} </div> {/* Footer */} <div className="flex justify-end gap-3 px-6 py-4 bg-gray-50 dark:bg-gray-700/50 border-t border-gray-200 dark:border-gray-700"> <Button variant="outline" onClick={onClose} disabled={isLoading} > Cancel </Button> <Button variant="primary" onClick={() => { handleSubmit().catch(silentErrorHandler('FeedbackModal')); }} loading={isLoading} disabled={!title.trim() || !description.trim()} > <Send className="w-4 h-4 mr-2" /> Submit </Button> </div> </motion.div> </motion.div> </AnimatePresence> ); } |