-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d49e4d9
commit 250fb20
Showing
24 changed files
with
1,129 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use client' | ||
|
||
import React, { useState, useEffect } from 'react'; | ||
import { AsyncApiComponentWP } from '@asyncapi/react-component'; | ||
|
||
import { appState, useDocumentsState, useSettingsState, useOtherState, otherState } from '../../states'; | ||
|
||
import type { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs'; | ||
|
||
interface HTMLWrapperProps {} | ||
|
||
export const HTMLWrapper: React.FunctionComponent<HTMLWrapperProps> = () => { | ||
const [parsedSpec, setParsedSpec] = useState<AsyncAPIDocumentInterface | null>(null); | ||
// const { navigationSvc } = useServices(); | ||
const document = useDocumentsState(state => state.documents['asyncapi']?.document) || null; | ||
const autoRendering = useSettingsState(state => state.templates.autoRendering); | ||
const templateRerender = useOtherState(state => state.templateRerender); | ||
|
||
// useEffect(() => { | ||
// navigationSvc.scrollToHash(); | ||
// }, []); // eslint-disable-line | ||
|
||
useEffect(() => { | ||
if (autoRendering || parsedSpec === null) { | ||
setParsedSpec(document); | ||
} | ||
}, [document]); // eslint-disable-line | ||
|
||
useEffect(() => { | ||
if (templateRerender) { | ||
setParsedSpec(document); | ||
otherState.setState({ templateRerender: false }); | ||
} | ||
}, [templateRerender]); // eslint-disable-line | ||
|
||
if (!document) { | ||
return ( | ||
<div className="flex flex-1 overflow-hidden h-full justify-center items-center text-2xl mx-auto px-6 text-center"> | ||
<p>Empty or invalid document. Please fix errors/define AsyncAPI document.</p> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
parsedSpec && ( | ||
<div className="flex flex-1 flex-col h-full overflow-hidden"> | ||
<div className="overflow-auto"> | ||
{/* <AsyncApiComponentWP | ||
schema={parsedSpec} | ||
config={{ | ||
show: { | ||
errors: false, | ||
sidebar: appState.getState().readOnly, | ||
}, | ||
}} | ||
/> */} | ||
</div> | ||
</div> | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
|
||
import { TemplateSidebar } from './TemplateSidebar'; | ||
import { HTMLWrapper } from './HTMLWrapper'; | ||
|
||
import { appState } from '../../states'; | ||
|
||
interface TemplateProps {} | ||
|
||
export const Template: React.FunctionComponent<TemplateProps> = () => { | ||
return ( | ||
<div className="flex flex-1 flex-col h-full overflow-hidden"> | ||
{!appState.getState().readOnly && <TemplateSidebar />} | ||
<HTMLWrapper /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react'; | ||
import { VscRefresh } from 'react-icons/vsc'; | ||
|
||
import { useSettingsState, otherState } from '../../states'; | ||
|
||
interface TemplateSidebarProps {} | ||
|
||
export const TemplateSidebar: React.FunctionComponent<TemplateSidebarProps> = () => { | ||
const autoRendering = useSettingsState(state => state.templates.autoRendering); | ||
|
||
return ( | ||
<div | ||
className="flex flex-row items justify-between bg-gray-800 border-b border-gray-700 text-sm" | ||
style={{ height: '30px', lineHeight: '30px' }} | ||
> | ||
{autoRendering ? ( | ||
<div /> | ||
) : ( | ||
<div className="ml-2 text-gray-500 text-xs flex" style={{ height: '30px', lineHeight: '30px' }}> | ||
<button type="button" className="text-xs" onClick={() => otherState.setState({ templateRerender: true })}> | ||
<div className="inline-block"> | ||
<VscRefresh className="w-4 h-4 mt-1" /> | ||
</div> | ||
</button> | ||
<span className="ml-2 italic"> | ||
Rerender | ||
</span> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Template'; | ||
export * from './HTMLWrapper'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { useStore, useReactFlow, useNodes, useEdges } from 'reactflow'; | ||
import { VscDebugStart, VscDebugPause, VscRefresh } from 'react-icons/vsc'; | ||
|
||
import { calculateNodesForDynamicLayout } from './utils/node-calculator'; | ||
|
||
import type { FunctionComponent } from 'react'; | ||
|
||
interface ControlsProps {} | ||
|
||
export const Controls: FunctionComponent<ControlsProps> = () => { | ||
const [animateNodes, setAnimateNodes] = useState(false); | ||
|
||
const { fitView } = useReactFlow(); | ||
const nodes = useNodes(); | ||
const edges = useEdges(); | ||
const setNodes = useStore(state => state.setNodes); | ||
const setEdges = useStore(state => state.setEdges); | ||
|
||
useEffect(() => { | ||
if (nodes.length > 0) { | ||
const newNodeEdges = edges.map(edge => ({ ...edge, animated: animateNodes })); | ||
setEdges([...newNodeEdges]); | ||
} | ||
}, [animateNodes]); | ||
|
||
const reloadInterface = () => { | ||
setNodes(calculateNodesForDynamicLayout(nodes)); | ||
fitView(); | ||
}; | ||
|
||
return ( | ||
<div className="absolute top-0 right-0 mr-5 mt-5 rounded-lg bg-white z-20 space-x-10 px-4 pt-1 shadow-lg"> | ||
<button type="button" className="text-xs" onClick={() => setAnimateNodes(!animateNodes)}> | ||
{animateNodes && <VscDebugPause className="w-4 h-4" />} | ||
{!animateNodes && <VscDebugStart className="w-4 h-4" />} | ||
</button> | ||
<button type="button" className="text-xs" onClick={reloadInterface}> | ||
<VscRefresh className="w-4 h-4" /> | ||
</button> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use client'; | ||
|
||
import { useEffect } from 'react'; | ||
import ReactFlow, { Controls as FlowControls, Background, BackgroundVariant, useReactFlow, useStore, useNodesState, useEdgesState, useNodes } from 'reactflow'; | ||
|
||
import NodeTypes from './Nodes'; | ||
import { Controls } from './Controls'; | ||
import { getElementsFromAsyncAPISpec } from './utils/node-factory'; | ||
import { calculateNodesForDynamicLayout } from './utils/node-calculator'; | ||
|
||
import type { AsyncAPIDocumentInterface } from '@asyncapi/parser/cjs'; | ||
import type { FunctionComponent } from 'react'; | ||
|
||
interface FlowDiagramProps { | ||
parsedSpec: AsyncAPIDocumentInterface; | ||
} | ||
|
||
interface AutoLayoutProps {} | ||
|
||
const AutoLayout: FunctionComponent<AutoLayoutProps> = () => { | ||
const { fitView } = useReactFlow(); | ||
const nodes = useNodes(); | ||
const setNodes = useStore(state => state.setNodes); | ||
|
||
useEffect(() => { | ||
if (nodes.length === 0 || !nodes[0].width) { | ||
return; | ||
} | ||
|
||
const nodesWithOrginalPosition = nodes.filter(node => node.position.x === 0 && node.position.y === 0); | ||
if (nodesWithOrginalPosition.length > 1) { | ||
const calculatedNodes = calculateNodesForDynamicLayout(nodes); | ||
setNodes(calculatedNodes); | ||
fitView(); | ||
} | ||
}, [nodes]); | ||
|
||
return null; | ||
}; | ||
|
||
export const FlowDiagram: FunctionComponent<FlowDiagramProps> = ({ parsedSpec }) => { | ||
const [nodes, setNodes, onNodesChange] = useNodesState([]); | ||
const [edges, setEdges, onEdgesChange] = useEdgesState([]); | ||
|
||
useEffect(() => { | ||
const elements = getElementsFromAsyncAPISpec(parsedSpec); | ||
const newNodes = elements.map(el => el.node).filter(Boolean); | ||
const newEdges = elements.map(el => el.edge).filter(Boolean); | ||
|
||
setNodes(newNodes); | ||
setEdges(newEdges); | ||
}, [parsedSpec]); | ||
|
||
return ( | ||
<div className="h-screen bg-gray-800 relative"> | ||
<ReactFlow | ||
nodeTypes={NodeTypes} | ||
nodes={nodes} | ||
edges={edges} | ||
minZoom={0.1} | ||
onNodesChange={onNodesChange} | ||
onEdgesChange={onEdgesChange} | ||
fitView={true} | ||
> | ||
<Background variant={BackgroundVariant.Dots} gap={20} size={1} className="bg-gray-200" /> | ||
<AutoLayout /> | ||
<Controls /> | ||
<div className="-mt-20"> | ||
<FlowControls style={{ bottom: '95px' }} className='bg-white' /> | ||
</div> | ||
</ReactFlow> | ||
<div className="m-4 px-2 text-lg absolute text-gray-800 top-0 left-0 bg-white space-x-2 py-2 border border-gray-100 inline-block"> | ||
<span className="font-bold">Event Visualiser</span> | ||
<span className="text-gray-200">|</span> | ||
<span className="font-light capitalize">{parsedSpec.info().title()}</span> | ||
</div> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.