From 6c8b5c48a2a5eb30c1e99a6712c61edd3214f6c0 Mon Sep 17 00:00:00 2001 From: "dayuan.jiang" Date: Thu, 27 Mar 2025 08:09:22 +0000 Subject: [PATCH] refactor: Relocate handleClear Logic: Move the chat and diagram clearing logic to ChatPanel (or a Server Action) and have ChatInput call the onClearChat prop. Introduce a clearDiagram method in DiagramContext. --- components/chat-input.tsx | 10 +--------- components/chat-panel.tsx | 6 +++++- contexts/diagram-context.tsx | 10 ++++++++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/components/chat-input.tsx b/components/chat-input.tsx index 0f51f59..b14a279 100644 --- a/components/chat-input.tsx +++ b/components/chat-input.tsx @@ -40,7 +40,7 @@ export function ChatInput({ showHistory = false, onToggleHistory = () => {}, }: ChatInputProps) { - const { loadDiagram: onDisplayChart, diagramHistory } = useDiagram(); + const { diagramHistory } = useDiagram(); const textareaRef = useRef(null); const fileInputRef = useRef(null); const [isDragging, setIsDragging] = useState(false); @@ -132,14 +132,6 @@ export function ChatInput({ // Handle clearing conversation and diagram const handleClear = () => { onClearChat(); - onDisplayChart(` - - - - - - - `); setShowClearDialog(false); }; diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx index 4d18ffe..3a807c9 100644 --- a/components/chat-panel.tsx +++ b/components/chat-panel.tsx @@ -20,6 +20,7 @@ export default function ChatPanel() { loadDiagram: onDisplayChart, handleExport: onExport, resolverRef, + clearDiagram, } = useDiagram(); const onFetchChart = () => { @@ -123,7 +124,10 @@ export default function ChatPanel() { status={status} onSubmit={onFormSubmit} onChange={handleInputChange} - onClearChat={() => setMessages([])} + onClearChat={() => { + setMessages([]); + clearDiagram(); + }} files={files} onFileChange={handleFileChange} showHistory={showHistory} diff --git a/contexts/diagram-context.tsx b/contexts/diagram-context.tsx index 98c5f45..a47f8ae 100644 --- a/contexts/diagram-context.tsx +++ b/contexts/diagram-context.tsx @@ -13,6 +13,7 @@ interface DiagramContextType { resolverRef: React.Ref<((value: string) => void) | null>; drawioRef: React.Ref; handleDiagramExport: (data: any) => void; + clearDiagram: () => void; } const DiagramContext = createContext(undefined); @@ -59,6 +60,14 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) { } }; + const clearDiagram = () => { + const emptyDiagram = ``; + loadDiagram(emptyDiagram); + setChartXML(emptyDiagram); + setLatestSvg(""); + setDiagramHistory([]); + }; + return ( {children}