-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): added ability to turn channels on/off (#986)
* feat(core): added ability to turn channels on/off * Create rotten-numbers-admire.md * feat(core): added ability to turn channels on/off * feat(core): added ability to turn channels on/off
- Loading branch information
Showing
7 changed files
with
189 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@eventcatalog/core": patch | ||
--- | ||
|
||
feat(core): added ability to turn channels on/off |
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,89 @@ | ||
import { useCallback, useMemo, useState, useEffect } from 'react'; | ||
import { type Edge, type Node } from 'reactflow'; | ||
import { | ||
createEdge, | ||
generatedIdForEdge, | ||
generateIdForNode, | ||
getEdgeLabelForMessageAsSource, | ||
getEdgeLabelForServiceAsTarget, | ||
getNodesAndEdgesFromDagre, | ||
} from '@utils/node-graphs/utils/utils'; | ||
|
||
interface EventCatalogVisualizerProps { | ||
nodes: Node[]; | ||
edges: Edge[]; | ||
setNodes: (nodes: Node[]) => void; | ||
setEdges: (edges: Edge[]) => void; | ||
} | ||
|
||
export const useEventCatalogVisualiser = ({ nodes, edges, setNodes, setEdges }: EventCatalogVisualizerProps) => { | ||
const [hideChannels, setHideChannels] = useState(false); | ||
const [initialNodes] = useState(nodes); | ||
const [initialEdges] = useState(edges); | ||
|
||
// Initialize hideChannels from localStorage | ||
useEffect(() => { | ||
const storedHideChannels = localStorage.getItem('EventCatalog:hideChannels'); | ||
if (storedHideChannels !== null) { | ||
setHideChannels(storedHideChannels === 'true'); | ||
} | ||
}, []); | ||
|
||
const toggleChannelsVisibility = useCallback(() => { | ||
setHideChannels((prev) => { | ||
const newValue = !prev; | ||
localStorage.setItem('EventCatalog:hideChannels', JSON.stringify(newValue)); | ||
return newValue; | ||
}); | ||
}, []); | ||
|
||
const channels = useMemo(() => nodes.filter((node) => node.type === 'channels'), [nodes]); | ||
const updatedNodes = useMemo(() => nodes.filter((node) => node.type !== 'channels'), [nodes]); | ||
|
||
const updatedEdges = useMemo(() => { | ||
return edges.reduce<Edge[]>((acc, edge) => { | ||
const { source, target, data } = edge; | ||
const targetIsChannel = channels.some((channel) => channel.id === target); | ||
const sourceIsChannel = channels.some((channel) => channel.id === source); | ||
|
||
if (!sourceIsChannel && !targetIsChannel) { | ||
return [...acc, edge]; | ||
} | ||
|
||
if (sourceIsChannel) { | ||
const edgeLabel = | ||
data.target.collection === 'services' | ||
? getEdgeLabelForMessageAsSource(data.source) | ||
: getEdgeLabelForServiceAsTarget(data.target); | ||
|
||
return [ | ||
...acc, | ||
createEdge({ | ||
id: generatedIdForEdge(data.source, data.target), | ||
source: generateIdForNode(data.source), | ||
target: generateIdForNode(data.target), | ||
label: edgeLabel, | ||
}), | ||
]; | ||
} | ||
|
||
return [...acc, edge]; | ||
}, []); | ||
}, [edges, channels]); | ||
|
||
useEffect(() => { | ||
if (hideChannels) { | ||
const { nodes: newNodes, edges: newEdges } = getNodesAndEdgesFromDagre({ nodes: updatedNodes, edges: updatedEdges }); | ||
setNodes(newNodes); | ||
setEdges(newEdges); | ||
} else { | ||
setNodes(initialNodes); | ||
setEdges(initialEdges); | ||
} | ||
}, [hideChannels]); | ||
|
||
return { | ||
hideChannels, | ||
toggleChannelsVisibility, | ||
}; | ||
}; |
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
Oops, something went wrong.