"use client"; import React, { createContext, useContext, useRef, useState } from "react"; import type { DrawIoEmbedRef } from "react-drawio"; import { extractDiagramXML } from "../lib/utils"; interface DiagramContextType { chartXML: string; latestSvg: string; diagramHistory: { svg: string; xml: string }[]; loadDiagram: (chart: string) => void; handleExport: () => void; resolverRef: React.Ref<((value: string) => void) | null>; drawioRef: React.Ref; handleDiagramExport: (data: any) => void; clearDiagram: () => void; saveDiagramToFile: (filename: string) => void; } const DiagramContext = createContext(undefined); export function DiagramProvider({ children }: { children: React.ReactNode }) { const [chartXML, setChartXML] = useState(""); const [latestSvg, setLatestSvg] = useState(""); const [diagramHistory, setDiagramHistory] = useState< { svg: string; xml: string }[] >([]); const drawioRef = useRef(null); const resolverRef = useRef<((value: string) => void) | null>(null); // Track if we're expecting an export for history (user-initiated) const expectHistoryExportRef = useRef(false); // Track if we're expecting an export for file save const saveResolverRef = useRef<((xml: string) => void) | null>(null); const handleExport = () => { if (drawioRef.current) { // Mark that this export should be saved to history expectHistoryExportRef.current = true; 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); // Only add to history if this was a user-initiated export if (expectHistoryExportRef.current) { setDiagramHistory((prev) => [ ...prev, { svg: data.data, xml: extractedXML, }, ]); expectHistoryExportRef.current = false; } if (resolverRef.current) { resolverRef.current(extractedXML); resolverRef.current = null; } // Handle save to file if requested if (saveResolverRef.current) { saveResolverRef.current(extractedXML); saveResolverRef.current = null; } }; const clearDiagram = () => { const emptyDiagram = ``; loadDiagram(emptyDiagram); setChartXML(emptyDiagram); setLatestSvg(""); setDiagramHistory([]); }; const saveDiagramToFile = (filename: string) => { if (!drawioRef.current) { console.warn("Draw.io editor not ready"); return; } // Export diagram and save when export completes drawioRef.current.exportDiagram({ format: "xmlsvg" }); saveResolverRef.current = (xml: string) => { // Wrap in proper .drawio format let fileContent = xml; if (!xml.includes("${xml}`; } const blob = new Blob([fileContent], { type: "application/xml" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; // Add .drawio extension if not present a.download = filename.endsWith(".drawio") ? filename : `${filename}.drawio`; document.body.appendChild(a); a.click(); document.body.removeChild(a); // Delay URL revocation to ensure download completes setTimeout(() => URL.revokeObjectURL(url), 100); }; }; return ( {children} ); } export function useDiagram() { const context = useContext(DiagramContext); if (context === undefined) { throw new Error("useDiagram must be used within a DiagramProvider"); } return context; }