import React, { useState, useEffect } from 'react'; import { Memory, MemoryCategory } from '@/types'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Badge } from '@/components/ui/badge'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { X, Save, Eye, EyeOff, Plus, AlertCircle, Loader2 } from 'lucide-react'; interface MemoryEditModalProps { memory: Memory | null; isOpen: boolean; onClose: () => void; onSave: (updatedMemory: Memory) => void; isLoading?: boolean; } const MEMORY_CATEGORIES: MemoryCategory[] = [ 'personal', 'work', 'code', 'research', 'conversations', 'preferences' ]; export function MemoryEditModal({ memory, isOpen, onClose, onSave, isLoading = false }: MemoryEditModalProps) { const [editedMemory, setEditedMemory] = useState(null); const [newTag, setNewTag] = useState(''); const [showFullContent, setShowFullContent] = useState(false); const [hasChanges, setHasChanges] = useState(false); // Initialize edited memory when modal opens useEffect(() => { if (memory && isOpen) { setEditedMemory({ ...memory }); setHasChanges(false); setShowFullContent(false); } }, [memory, isOpen]); // Track changes useEffect(() => { if (memory && editedMemory) { const hasChanged = ( memory.content !== editedMemory.content || memory.category !== editedMemory.category || memory.priority !== editedMemory.priority || JSON.stringify(memory.tags) !== JSON.stringify(editedMemory.tags) ); setHasChanges(hasChanged); } }, [memory, editedMemory]); const handleSave = () => { if (editedMemory && hasChanges) { onSave(editedMemory); } }; const handleCancel = () => { if (hasChanges) { const confirmed = window.confirm('You have unsaved changes. Are you sure you want to close?'); if (!confirmed) return; } onClose(); }; const addTag = () => { if (newTag.trim() && editedMemory) { const currentTags = editedMemory.tags || []; if (!currentTags.includes(newTag.trim())) { setEditedMemory({ ...editedMemory, tags: [...currentTags, newTag.trim()] }); } setNewTag(''); } }; const removeTag = (tagToRemove: string) => { if (editedMemory) { setEditedMemory({ ...editedMemory, tags: (editedMemory.tags || []).filter(tag => tag !== tagToRemove) }); } }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); addTag(); } }; if (!editedMemory) return null; const previewContent = editedMemory.content.length > 300 ? editedMemory.content.substring(0, 300) + '...' : editedMemory.content; return ( Edit Memory {hasChanges && ( Unsaved Changes )} Edit memory content, category, priority, and tags.
{/* Category and Priority */}
{/* Tags */}
setNewTag(e.target.value)} onKeyPress={handleKeyPress} placeholder="Add a tag..." className="flex-1" />
{editedMemory.tags && editedMemory.tags.length > 0 && (
{editedMemory.tags.map((tag, index) => ( {tag} ))}
)}
{/* Content */}
{showFullContent ? (