forked from accordproject/template-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIButton.tsx
More file actions
82 lines (76 loc) · 2.3 KB
/
AIButton.tsx
File metadata and controls
82 lines (76 loc) · 2.3 KB
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
import { useState } from "react";
import { FaMagic } from "react-icons/fa";
import { message } from "antd";
import { useAI } from "../../hooks/useAI";
import { AIModal } from "./AIModal";
import { EditorType } from "../../services/ai/AIService";
import { DiffModal } from "./DiffModal";
interface AIButtonProps {
editorType: EditorType;
currentContent: string;
onComplete: (completion: string) => void;
}
export function AIButton({ editorType, currentContent, onComplete }: AIButtonProps) {
const [isModalOpen, setIsModalOpen] = useState(false);
const [isDiffModalOpen, setIsDiffModalOpen] = useState(false);
const [diffInfo, setDiffInfo] = useState({ content: "", diff: "", explanation: "" });
const { isProcessing, getCompletion } = useAI();
const handleSubmit = async (prompt: string) => {
try {
const completion = await getCompletion({
prompt,
editorType,
currentContent,
});
if (completion.diff && completion.diff.trim()) {
setDiffInfo({
content: completion.content,
diff: completion.diff,
explanation: completion.explanation || "",
});
setIsModalOpen(false);
setIsDiffModalOpen(true);
} else {
onComplete(completion.content);
setIsModalOpen(false);
if (completion.explanation) {
message.info(completion.explanation);
}
}
} catch (error) {
console.error("AI completion error:", error);
message.error("Failed to get AI completion");
}
};
const handleAcceptChanges = () => {
onComplete(diffInfo.content);
setIsDiffModalOpen(false);
};
return (
<>
<FaMagic
onClick={() => setIsModalOpen(true)}
title="AI Assist"
style={{
cursor: isProcessing ? "wait" : "pointer",
opacity: isProcessing ? 0.5 : 1,
marginRight: "8px",
}}
/>
<AIModal
isOpen={isModalOpen}
onClose={() => setIsModalOpen(false)}
onSubmit={handleSubmit}
isProcessing={isProcessing}
editorType={editorType}
/>
<DiffModal
isOpen={isDiffModalOpen}
onClose={() => setIsDiffModalOpen(false)}
onAccept={handleAcceptChanges}
diff={diffInfo.diff}
explanation={diffInfo.explanation}
/>
</>
);
}