2025-03-26 00:30:00 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import React, { createContext, useContext, useRef, useState } from "react";
|
|
|
|
|
import type { DrawIoEmbedRef } from "react-drawio";
|
2025-12-02 19:11:23 +09:00
|
|
|
import { extractDiagramXML, formatXML } from "../lib/utils";
|
2025-03-26 00:30:00 +00:00
|
|
|
|
|
|
|
|
interface DiagramContextType {
|
|
|
|
|
chartXML: string;
|
|
|
|
|
latestSvg: string;
|
|
|
|
|
diagramHistory: { svg: string; xml: string }[];
|
2025-12-02 19:11:23 +09:00
|
|
|
lastAgentGeneratedXml: string;
|
|
|
|
|
getLastAgentGeneratedXml: () => string;
|
|
|
|
|
setLastAgentGeneratedXml: (xml: string) => void;
|
|
|
|
|
markAgentDiagramPending: () => void;
|
2025-03-26 00:30:00 +00:00
|
|
|
loadDiagram: (chart: string) => void;
|
|
|
|
|
handleExport: () => void;
|
2025-03-26 06:47:44 +00:00
|
|
|
resolverRef: React.Ref<((value: string) => void) | null>;
|
|
|
|
|
drawioRef: React.Ref<DrawIoEmbedRef | null>;
|
2025-03-26 00:30:00 +00:00
|
|
|
handleDiagramExport: (data: any) => void;
|
2025-03-27 08:09:22 +00:00
|
|
|
clearDiagram: () => void;
|
2025-03-26 00:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const DiagramContext = createContext<DiagramContextType | undefined>(undefined);
|
|
|
|
|
|
|
|
|
|
export function DiagramProvider({ children }: { children: React.ReactNode }) {
|
|
|
|
|
const [chartXML, setChartXML] = useState<string>("");
|
|
|
|
|
const [latestSvg, setLatestSvg] = useState<string>("");
|
|
|
|
|
const [diagramHistory, setDiagramHistory] = useState<
|
|
|
|
|
{ svg: string; xml: string }[]
|
|
|
|
|
>([]);
|
2025-12-02 19:11:23 +09:00
|
|
|
const [lastAgentGeneratedXml, setLastAgentGeneratedXmlState] = useState<string>("");
|
|
|
|
|
const lastAgentGeneratedXmlRef = useRef<string>("");
|
|
|
|
|
const agentDiagramPendingRef = useRef<boolean>(false);
|
2025-03-26 06:47:44 +00:00
|
|
|
const drawioRef = useRef<DrawIoEmbedRef | null>(null);
|
2025-03-26 00:30:00 +00:00
|
|
|
const resolverRef = useRef<((value: string) => void) | null>(null);
|
|
|
|
|
|
2025-12-02 19:11:23 +09:00
|
|
|
// Wrapper to keep ref and state in sync
|
|
|
|
|
const setLastAgentGeneratedXml = (xml: string) => {
|
|
|
|
|
lastAgentGeneratedXmlRef.current = xml;
|
|
|
|
|
setLastAgentGeneratedXmlState(xml);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Getter that returns the ref value (always up-to-date, even in async contexts)
|
|
|
|
|
const getLastAgentGeneratedXml = () => lastAgentGeneratedXmlRef.current;
|
|
|
|
|
|
|
|
|
|
const markAgentDiagramPending = () => {
|
|
|
|
|
console.log('[DiagramContext] markAgentDiagramPending called');
|
|
|
|
|
agentDiagramPendingRef.current = true;
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-26 00:30:00 +00:00
|
|
|
const handleExport = () => {
|
|
|
|
|
if (drawioRef.current) {
|
|
|
|
|
drawioRef.current.exportDiagram({
|
|
|
|
|
format: "xmlsvg",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const loadDiagram = (chart: string) => {
|
|
|
|
|
if (drawioRef.current) {
|
|
|
|
|
drawioRef.current.load({
|
|
|
|
|
xml: chart,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDiagramExport = (data: any) => {
|
|
|
|
|
const extractedXML = extractDiagramXML(data.data);
|
|
|
|
|
setChartXML(extractedXML);
|
|
|
|
|
setLatestSvg(data.data);
|
|
|
|
|
setDiagramHistory((prev) => [
|
|
|
|
|
...prev,
|
|
|
|
|
{
|
|
|
|
|
svg: data.data,
|
|
|
|
|
xml: extractedXML,
|
|
|
|
|
},
|
|
|
|
|
]);
|
2025-12-02 19:11:23 +09:00
|
|
|
|
|
|
|
|
// If agent just generated a diagram, update lastAgentGeneratedXml with the exported XML
|
|
|
|
|
// This ensures we compare apples-to-apples (both formatted the same way)
|
|
|
|
|
if (agentDiagramPendingRef.current) {
|
|
|
|
|
const formatted = formatXML(extractedXML);
|
|
|
|
|
console.log('[DiagramContext] Setting lastAgentGeneratedXml from export, length:', formatted.length);
|
|
|
|
|
setLastAgentGeneratedXml(formatted);
|
|
|
|
|
agentDiagramPendingRef.current = false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-26 00:30:00 +00:00
|
|
|
if (resolverRef.current) {
|
|
|
|
|
resolverRef.current(extractedXML);
|
|
|
|
|
resolverRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-27 08:09:22 +00:00
|
|
|
const clearDiagram = () => {
|
|
|
|
|
const emptyDiagram = `<mxfile><diagram name="Page-1" id="page-1"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel></diagram></mxfile>`;
|
|
|
|
|
loadDiagram(emptyDiagram);
|
|
|
|
|
setChartXML(emptyDiagram);
|
|
|
|
|
setLatestSvg("");
|
|
|
|
|
setDiagramHistory([]);
|
2025-12-02 19:11:23 +09:00
|
|
|
setLastAgentGeneratedXml("");
|
2025-03-27 08:09:22 +00:00
|
|
|
};
|
|
|
|
|
|
2025-03-26 00:30:00 +00:00
|
|
|
return (
|
|
|
|
|
<DiagramContext.Provider
|
|
|
|
|
value={{
|
|
|
|
|
chartXML,
|
|
|
|
|
latestSvg,
|
|
|
|
|
diagramHistory,
|
2025-12-02 19:11:23 +09:00
|
|
|
lastAgentGeneratedXml,
|
|
|
|
|
getLastAgentGeneratedXml,
|
|
|
|
|
setLastAgentGeneratedXml,
|
|
|
|
|
markAgentDiagramPending,
|
2025-03-26 00:30:00 +00:00
|
|
|
loadDiagram,
|
|
|
|
|
handleExport,
|
|
|
|
|
resolverRef,
|
|
|
|
|
drawioRef,
|
|
|
|
|
handleDiagramExport,
|
2025-03-27 08:09:22 +00:00
|
|
|
clearDiagram,
|
2025-03-26 00:30:00 +00:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</DiagramContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useDiagram() {
|
|
|
|
|
const context = useContext(DiagramContext);
|
|
|
|
|
if (context === undefined) {
|
|
|
|
|
throw new Error("useDiagram must be used within a DiagramProvider");
|
|
|
|
|
}
|
|
|
|
|
return context;
|
|
|
|
|
}
|