mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 22:32:27 +08:00
fix: prevent duplicate diagram history entries on message send (#48)
When sending a message, the history was being added twice because: 1. handleExport() triggers exportDiagram() which adds to history 2. AI responds and loadDiagram() is called, which internally triggers another export event in DrawIO, adding a duplicate entry Added expectHistoryExportRef flag to track user-initiated exports and only add to history when the export was explicitly requested.
This commit is contained in:
@@ -26,9 +26,13 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
|
|||||||
>([]);
|
>([]);
|
||||||
const drawioRef = useRef<DrawIoEmbedRef | null>(null);
|
const drawioRef = useRef<DrawIoEmbedRef | null>(null);
|
||||||
const resolverRef = useRef<((value: string) => void) | null>(null);
|
const resolverRef = useRef<((value: string) => void) | null>(null);
|
||||||
|
// Track if we're expecting an export for history (user-initiated)
|
||||||
|
const expectHistoryExportRef = useRef<boolean>(false);
|
||||||
|
|
||||||
const handleExport = () => {
|
const handleExport = () => {
|
||||||
if (drawioRef.current) {
|
if (drawioRef.current) {
|
||||||
|
// Mark that this export should be saved to history
|
||||||
|
expectHistoryExportRef.current = true;
|
||||||
drawioRef.current.exportDiagram({
|
drawioRef.current.exportDiagram({
|
||||||
format: "xmlsvg",
|
format: "xmlsvg",
|
||||||
});
|
});
|
||||||
@@ -47,13 +51,19 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
|
|||||||
const extractedXML = extractDiagramXML(data.data);
|
const extractedXML = extractDiagramXML(data.data);
|
||||||
setChartXML(extractedXML);
|
setChartXML(extractedXML);
|
||||||
setLatestSvg(data.data);
|
setLatestSvg(data.data);
|
||||||
setDiagramHistory((prev) => [
|
|
||||||
...prev,
|
// Only add to history if this was a user-initiated export
|
||||||
{
|
if (expectHistoryExportRef.current) {
|
||||||
svg: data.data,
|
setDiagramHistory((prev) => [
|
||||||
xml: extractedXML,
|
...prev,
|
||||||
},
|
{
|
||||||
]);
|
svg: data.data,
|
||||||
|
xml: extractedXML,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expectHistoryExportRef.current = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (resolverRef.current) {
|
if (resolverRef.current) {
|
||||||
resolverRef.current(extractedXML);
|
resolverRef.current(extractedXML);
|
||||||
resolverRef.current = null;
|
resolverRef.current = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user