import { useState } from "react";
import { Button } from "@/components/ui/button";
import { cn } from "../lib/utils";
import { chatApi } from "../api/chat";
interface SpecContent {
what: string;
why: string;
constraints: string;
success: string;
}
interface ChatSpecCardProps {
content: string;
messageId?: string;
conversationId?: string;
onHandoff?: (spec: SpecContent) => void;
}
export function ChatSpecCard({ content, messageId, conversationId, onHandoff }: ChatSpecCardProps) {
let parsedSpec: SpecContent | null = null;
try {
parsedSpec = JSON.parse(content) as SpecContent;
} catch {
return (
Could not render spec.
);
}
return ;
}
function ChatSpecCardInner({
spec: initialSpec,
messageId,
conversationId,
onHandoff,
}: {
spec: SpecContent;
messageId?: string;
conversationId?: string;
onHandoff?: (spec: SpecContent) => void;
}) {
const [isEditing, setIsEditing] = useState(false);
const [isDraft, setIsDraft] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [spec, setSpec] = useState(initialSpec);
const [editedSpec, setEditedSpec] = useState(initialSpec);
const allFieldsEmpty =
!editedSpec.what.trim() &&
!editedSpec.why.trim() &&
!editedSpec.constraints.trim() &&
!editedSpec.success.trim();
function handleDiscard() {
setEditedSpec(spec);
setIsEditing(false);
}
async function handleSaveChanges() {
setSpec(editedSpec);
setIsEditing(false);
if (conversationId && messageId) {
await chatApi.editMessage(conversationId, messageId, JSON.stringify(editedSpec));
}
}
async function handleSendToPM() {
setIsSubmitting(true);
try {
onHandoff?.(spec);
} finally {
setIsSubmitting(false);
}
}
if (isEditing) {
return (
);
}
return (
{isDraft && (
[Draft]
)}
Constraints
{spec.constraints}
);
}