import React, { useState, useRef, useEffect } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Badge } from '@/components/ui/badge' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' interface QuickCaptureProps { onCreateMemory: (content: string, tags: string[], category?: string, project?: string) => Promise onCreateTask: (title: string, description: string, category: string, priority: string, project?: string, tags?: string[]) => Promise availableProjects: string[] className?: string } export function QuickCapture({ onCreateMemory, onCreateTask, availableProjects, className = '' }: QuickCaptureProps) { const [isExpanded, setIsExpanded] = useState(false) const [showFullDialog, setShowFullDialog] = useState(false) const [captureMode, setCaptureMode] = useState<'memory' | 'task'>('memory') const [quickText, setQuickText] = useState('') const [isCreating, setIsCreating] = useState(false) // Full dialog state const [formData, setFormData] = useState({ title: '', content: '', description: '', category: 'personal' as const, priority: 'medium' as const, project: '', tags: '' }) const quickInputRef = useRef(null) const textareaRef = useRef(null) // Focus input when expanded useEffect(() => { if (isExpanded && quickInputRef.current) { quickInputRef.current.focus() } }, [isExpanded]) // Focus textarea when dialog opens useEffect(() => { if (showFullDialog && textareaRef.current) { textareaRef.current.focus() } }, [showFullDialog]) // Handle click outside to collapse useEffect(() => { const handleClickOutside = (event: MouseEvent) => { const target = event.target as Element if (isExpanded && !target.closest('.quick-capture-container')) { setIsExpanded(false) setQuickText('') } } document.addEventListener('mousedown', handleClickOutside) return () => document.removeEventListener('mousedown', handleClickOutside) }, [isExpanded]) const handleQuickSubmit = async () => { if (!quickText.trim()) return setIsCreating(true) try { if (captureMode === 'memory') { // Extract tags from text (look for #hashtags) const tagMatches = quickText.match(/#\w+/g) || [] const tags = tagMatches.map(tag => tag.substring(1)) const contentWithoutTags = quickText.replace(/#\w+/g, '').trim() await onCreateMemory(contentWithoutTags, tags) } else { // For tasks, treat the text as title, with basic categorization const category = guessTaskCategory(quickText) await onCreateTask(quickText, '', category, 'medium') } setQuickText('') setIsExpanded(false) } catch (error) { console.error('Failed to create:', error) } finally { setIsCreating(false) } } const handleFullSubmit = async () => { if (captureMode === 'memory' && !formData.content.trim()) return if (captureMode === 'task' && !formData.title.trim()) return setIsCreating(true) try { if (captureMode === 'memory') { const tags = formData.tags.split(',').map(t => t.trim()).filter(Boolean) await onCreateMemory( formData.content, tags, formData.category, formData.project || undefined ) } else { const tags = formData.tags.split(',').map(t => t.trim()).filter(Boolean) await onCreateTask( formData.title, formData.description, formData.category, formData.priority, formData.project || undefined, tags ) } setFormData({ title: '', content: '', description: '', category: 'personal', priority: 'medium', project: '', tags: '' }) setShowFullDialog(false) } catch (error) { console.error('Failed to create:', error) } finally { setIsCreating(false) } } const guessTaskCategory = (text: string): string => { const lowerText = text.toLowerCase() if (lowerText.includes('bug') || lowerText.includes('fix') || lowerText.includes('error')) return 'code' if (lowerText.includes('meeting') || lowerText.includes('call') || lowerText.includes('client')) return 'work' if (lowerText.includes('research') || lowerText.includes('study') || lowerText.includes('learn')) return 'research' return 'personal' } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault() handleQuickSubmit() } else if (e.key === 'Escape') { setIsExpanded(false) setQuickText('') } } return ( <>
{!isExpanded ? ( /* Floating Action Button */ ) : ( /* Expanded Quick Capture */
{/* Mode Toggle */}
{/* Quick Input */}
setQuickText(e.target.value)} onKeyDown={handleKeyDown} placeholder={ captureMode === 'memory' ? 'Quick memory... (use #tags)' : 'Quick task title...' } className="bg-gray-700/50 border-gray-600 text-white placeholder-gray-400" disabled={isCreating} />
{/* Quick Tips */}

💡 Tips:

{captureMode === 'memory' ? (

• Use #hashtags for auto-tagging

) : (

• Use keywords: bug, meeting, research for auto-categorization

)}

• Press Enter to create, Escape to close

)}
{/* Full Editor Dialog */} {captureMode === 'memory' ? '🧠' : '📋'} Quick {captureMode === 'memory' ? 'Memory' : 'Task'} - Full Editor
{captureMode === 'task' && (
setFormData({ ...formData, title: e.target.value })} placeholder="Task title..." className="bg-gray-700 border-gray-600 text-white" />
)}