Skip to content

Commit

Permalink
Merge pull request #2585 from stakwork/feature/sync
Browse files Browse the repository at this point in the history
feat: fixed nodes alignment
  • Loading branch information
Rassl authored Dec 26, 2024
2 parents a3f517f + f1551d7 commit 0feaae1
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions src/components/Universe/Graph/Cubes/SelectionDataNodes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Connections } from './Connections'
import { Node as GraphNode } from './Node'

const radius = 50
const MAX_LENGTH = 5
const MAX_LENGTH = 7

type GraphData<T = string> = {
links: Link<T>[]
Expand Down Expand Up @@ -40,49 +40,41 @@ export const SelectionDataNodes = memo(() => {

const oldNodes = pathGraph?.nodes || []

// Combine `oldNodes` and `selectionData.nodes` for position calculations
const combinedNodes = [...oldNodes, ...selectionData.nodes]
// Filter out nodes that already exist in oldNodes
const newNodes = selectionData.nodes.filter((i) => !oldNodes.some((n) => n.ref_id === i.ref_id))

// Calculate the angular spacing considering both old and new nodes
const totalNodes = combinedNodes.length
const thetaSpan = (Math.PI * 2) / totalNodes // Angle between points

const nodes = selectionData.nodes.map((node, index) => {
// Check if the node exists in oldNodes
const existingNode = oldNodes.find((oldNode) => oldNode.ref_id === node.ref_id)

if (existingNode) {
// Retain the old node's position
return existingNode
}
// Find the start position from oldNodes
const startPositionNode = oldNodes.find((i) => i.x !== 0 || i.y !== 0)
const startPosition = startPositionNode ? { x: startPositionNode.x, y: startPositionNode.y } : { x: 0, y: 0 }

// Calculate new position for nodes not in oldNodes
const theta = thetaSpan * (oldNodes.length + index) // Offset by oldNodes count
const x = node.ref_id === selectedNode?.ref_id ? 0 : Math.cos(theta) * radius
const y = node.ref_id === selectedNode?.ref_id ? 0 : Math.sin(theta) * radius
const z = node.ref_id === selectedNode?.ref_id ? 0 : 0
// Calculate the starting angle (theta) for the start position
const startTheta = Math.atan2(startPosition.y, startPosition.x)

return { ...node, x, y, z }
})
// Total nodes including both old and new ones
const totalNodes = oldNodes.length + newNodes.length - 1
const thetaSpan = (Math.PI * 2) / totalNodes // Angle between points

// Retain old nodes with their original positions
const retainedOldNodes = oldNodes.filter(
(oldNode) => !selectionData.nodes.some((node) => node.ref_id === oldNode.ref_id),
)
const nodes = [
...oldNodes,
...newNodes.map((node, index) => {
// Calculate angular position for the new node
const theta = startTheta + thetaSpan * (index + 1) // Start adding from startTheta
const x = node.ref_id === selectedNode?.ref_id ? 0 : Math.cos(theta) * radius
const y = node.ref_id === selectedNode?.ref_id ? 0 : Math.sin(theta) * radius
const z = node.ref_id === selectedNode?.ref_id ? 0 : 0

// Merge the calculated nodes with retained old nodes
const mergedNodes = [...nodes, ...retainedOldNodes]
return { ...node, x, y, z }
}),
]

return { nodes: mergedNodes, links: selectionData.links }
return { nodes, links: selectionData.links }
}, [selectionData, selectedNode, pathGraph?.nodes])

const graphData: GraphData = useMemo(() => {
if (newData?.nodes?.length) {
return newData
}

console.log(pathGraph)

if (pathGraph) {
return {
nodes: pathGraph.nodes,
Expand Down

0 comments on commit 0feaae1

Please sign in to comment.