"use client"; import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from "react"; import { IconX, IconClock, IconEdit, IconCheck } from "@tabler/icons-react"; import { Button } from "@kandev/ui"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { cn } from "@/lib/utils"; import { Textarea } from "@kandev/ui/textarea"; /** Strip internal ... blocks from display text. */ function stripSystemTags(text: string): string { return text.replace(/[\s\S]*?<\/kandev-system>/g, "").trim(); } export type QueuedMessageIndicatorHandle = { startEdit: () => void; }; type QueuedMessageIndicatorProps = { content: string; onCancel: () => void; onUpdate: (content: string) => Promise; isVisible: boolean; onEditComplete?: () => void; }; type QueuedEditViewProps = { editValue: string; isSaving: boolean; textareaRef: React.RefObject; onChange: (val: string) => void; onKeyDown: (event: React.KeyboardEvent) => void; onSave: () => void; onCancel: () => void; }; function QueuedEditView({ editValue, isSaving, textareaRef, onChange, onKeyDown, onSave, onCancel, }: QueuedEditViewProps) { return ( onChange(e.target.value)} onKeyDown={onKeyDown} className={cn( "min-h-[60px] max-h-[200px] overflow-y-auto resize-none", "bg-background border-border", )} placeholder="Enter message content..." disabled={isSaving} /> Save Cancel Press Esc to cancel, Cmd+Enter to save ); } type QueuedDisplayViewProps = { displayContent: string; onStartEdit: () => void; onCancel: () => void; }; function QueuedDisplayView({ displayContent, onStartEdit, onCancel }: QueuedDisplayViewProps) { return ( Queued Will run when the agent completes {displayContent} ); } export const QueuedMessageIndicator = forwardRef< QueuedMessageIndicatorHandle, QueuedMessageIndicatorProps >(function QueuedMessageIndicator({ content, onCancel, onUpdate, isVisible, onEditComplete }, ref) { const [isEditing, setIsEditing] = useState(false); const [editValue, setEditValue] = useState(content); const [isSaving, setIsSaving] = useState(false); const textareaRef = useRef(null); useEffect(() => { if (!isEditing) setEditValue(content); }, [content, isEditing]); useEffect(() => { if (isEditing && textareaRef.current) { textareaRef.current.focus(); textareaRef.current.setSelectionRange( textareaRef.current.value.length, textareaRef.current.value.length, ); } }, [isEditing]); const startEdit = useCallback(() => { setEditValue(content); setIsEditing(true); }, [content]); const handleSave = useCallback(async () => { const trimmed = editValue.trim(); if (!trimmed || trimmed === content) { setIsEditing(false); onEditComplete?.(); return; } setIsSaving(true); try { await onUpdate(trimmed); setIsEditing(false); onEditComplete?.(); } catch (error) { console.error("Failed to update queued message:", error); } finally { setIsSaving(false); } }, [editValue, content, onUpdate, onEditComplete]); const handleCancel = useCallback(() => { setEditValue(content); setIsEditing(false); onEditComplete?.(); }, [content, onEditComplete]); const handleKeyDown = useCallback( (event: React.KeyboardEvent) => { if (event.key === "Escape") { event.preventDefault(); handleCancel(); } else if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) { event.preventDefault(); handleSave(); } }, [handleCancel, handleSave], ); useImperativeHandle(ref, () => ({ startEdit }), [startEdit]); if (!isVisible) return null; const visibleContent = stripSystemTags(content); const displayContent = visibleContent.length > 80 ? visibleContent.substring(0, 80) + "..." : visibleContent; return ( {isEditing ? ( ) : ( )} ); });