'use client'; import React, { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Edit2, Check, X, Loader2 } from 'lucide-react'; import { useUpdateVersionDetails } from '@/lib/versioning/hooks/use-versions'; import { cn } from '@/lib/utils'; interface VersionInlineEditorProps { agentId: string; versionId: string; versionName: string; changeDescription?: string; isActive?: boolean; onUpdate?: (updatedVersion: { versionName: string; changeDescription?: string }) => void; } export function VersionInlineEditor({ agentId, versionId, versionName, changeDescription, isActive = false, onUpdate }: VersionInlineEditorProps) { const [isEditing, setIsEditing] = useState(false); const [editedName, setEditedName] = useState(versionName); const [editedDescription, setEditedDescription] = useState(changeDescription || ''); const [hasChanges, setHasChanges] = useState(false); const updateVersionMutation = useUpdateVersionDetails(); useEffect(() => { const nameChanged = editedName !== versionName; const descriptionChanged = editedDescription !== (changeDescription || ''); setHasChanges(nameChanged || descriptionChanged); }, [editedName, editedDescription, versionName, changeDescription]); const handleStartEdit = () => { setIsEditing(true); setEditedName(versionName); setEditedDescription(changeDescription || ''); }; const handleCancel = () => { setIsEditing(false); setEditedName(versionName); setEditedDescription(changeDescription || ''); setHasChanges(false); }; const handleSave = async () => { if (!hasChanges) { setIsEditing(false); return; } try { const updateData: { version_name?: string; change_description?: string } = {}; if (editedName !== versionName) { updateData.version_name = editedName; } if (editedDescription !== (changeDescription || '')) { updateData.change_description = editedDescription; } await updateVersionMutation.mutateAsync({ agentId, versionId, data: updateData }); setIsEditing(false); onUpdate?.({ versionName: editedName, changeDescription: editedDescription }); } catch (error) { // Error is handled by the mutation hook console.error('Failed to update version:', error); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Escape') { handleCancel(); } else if (e.key === 'Enter' && e.metaKey) { handleSave(); } }; if (isEditing) { return (
e.stopPropagation()}>
setEditedName(e.target.value)} onKeyDown={handleKeyDown} placeholder="Version name" className="flex-1" autoFocus />