File size: 5,263 Bytes
5da4770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'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 (
      <div className="space-y-3" onClick={(e) => e.stopPropagation()}>
        <div className="flex items-center gap-2">
          <Input
            value={editedName}
            onChange={(e) => setEditedName(e.target.value)}
            onKeyDown={handleKeyDown}
            placeholder="Version name"
            className="flex-1"
            autoFocus
          />
          <div className="flex items-center gap-1">
            <Button
              size="sm"
              variant="ghost"
              onClick={(e) => {
                e.stopPropagation();
                handleSave();
              }}
              disabled={!hasChanges || updateVersionMutation.isPending}
              className="h-8 w-8 p-0"
            >
              {updateVersionMutation.isPending ? (
                <Loader2 className="h-4 w-4 animate-spin" />
              ) : (
                <Check className="h-4 w-4" />
              )}
            </Button>
            <Button
              size="sm"
              variant="ghost"
              onClick={(e) => {
                e.stopPropagation();
                handleCancel();
              }}
              disabled={updateVersionMutation.isPending}
              className="h-8 w-8 p-0"
            >
              <X className="h-4 w-4" />
            </Button>
          </div>
        </div>
        <Textarea
          value={editedDescription}
          onChange={(e) => setEditedDescription(e.target.value)}
          onKeyDown={handleKeyDown}
          placeholder="Change description (optional)"
          className="min-h-[60px] resize-none"
        />
        <div className="text-xs text-muted-foreground">
          Press Escape to cancel, Cmd+Enter to save
        </div>
      </div>
    );
  }

  return (
    <div className="group space-y-1">
      <div className="flex items-center gap-2">
        <span className={cn(
          "font-medium",
          isActive && "text-primary"
        )}>
          {versionName}
        </span>
        <Button
          size="sm"
          variant="ghost"
          onClick={(e) => {
            e.stopPropagation();
            handleStartEdit();
          }}
          className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100 transition-opacity"
        >
          <Edit2 className="h-3 w-3" />
        </Button>
      </div>
      {changeDescription && (
        <p className="text-sm text-muted-foreground line-clamp-2">
          {changeDescription}
        </p>
      )}
    </div>
  );
}