import React from 'react'; import { useUIStore } from '@/stores/useUIStore'; import { useRuntimeAPIs } from '@/hooks/useRuntimeAPIs'; import { useI18n } from '@/lib/i18n'; import { DiagramEditor, type DiagramEditorHandle } from '@/components/diagram'; import { Icon } from '@/components/icon/Icon'; export function DiagramView() { const { t } = useI18n(); const { files } = useRuntimeAPIs(); const [filePath, setFilePath] = React.useState(null); const [xml, setXml] = React.useState(''); const [loading, setLoading] = React.useState(true); const editorRef = React.useRef(null); const pendingDiagramFile = useUIStore((state) => state.pendingDiagramFile); const loadFile = React.useCallback(async (path: string) => { setLoading(true); setFilePath(path); try { const result = await files?.readFile?.(path); if (result) { setXml(result.content); } } catch { setXml(''); } finally { setLoading(false); } }, [files]); React.useEffect(() => { if (!pendingDiagramFile) { return; } const pending = useUIStore.getState().consumePendingDiagramFile(); if (pending) { void loadFile(pending); } }, [loadFile, pendingDiagramFile]); const saveDiagram = React.useCallback(async () => { const newXml = editorRef.current?.getXml(); if (filePath && files?.writeFile && newXml && newXml !== xml) { await files.writeFile(filePath, newXml); setXml(newXml); } }, [filePath, files, xml]); const fileName = filePath ? filePath.split('/').pop() || filePath : ''; if (!filePath) { return (
{t('filesView.editor.pickFileFromTree')}
); } if (loading) { return (
); } return (
{fileName}
); }