diff --git a/src/components/Universe/Graph/Connections/LineComponent.tsx b/src/components/Universe/Graph/Connections/LineComponent.tsx new file mode 100644 index 000000000..027ff1da7 --- /dev/null +++ b/src/components/Universe/Graph/Connections/LineComponent.tsx @@ -0,0 +1,49 @@ +import { Line } from '@react-three/drei' +import gsap from 'gsap' +import { forwardRef, useEffect } from 'react' +import { Vector3 } from 'three' +import { Line2 } from 'three-stdlib' + +type LineComponentProps = { + source: Vector3 + target: Vector3 + isSelected: boolean + lineWidth: number + visible: boolean +} + +const LineComponent = forwardRef( + ({ source, target, isSelected, lineWidth, visible }, ref) => { + useEffect(() => { + const line = (ref as React.MutableRefObject).current + + if (line) { + gsap.fromTo( + line.material, + { linewidth: 5 }, + { + linewidth: isSelected ? 2 : lineWidth, + duration: 1, + }, + ) + } + }, [isSelected, lineWidth, ref]) + + return ( + + ) + }, +) + +LineComponent.displayName = 'LineComponent' + +export default LineComponent diff --git a/src/components/Universe/Graph/Connections/index.tsx b/src/components/Universe/Graph/Connections/index.tsx index f4091f123..5d2b06930 100644 --- a/src/components/Universe/Graph/Connections/index.tsx +++ b/src/components/Universe/Graph/Connections/index.tsx @@ -1,12 +1,11 @@ -import { Line } from '@react-three/drei' -import gsap from 'gsap' -import { memo, useEffect, useRef } from 'react' +import { memo, useRef } from 'react' import { Vector3 } from 'three' import { Line2 } from 'three-stdlib' import { useDataStore } from '~/stores/useDataStore' import { useGraphStore, useSelectedNode } from '~/stores/useGraphStore' import { Link } from '~/types' import { LinkPosition } from '..' +import LineComponent from './LineComponent' type Props = { linksPositions: LinkPosition[] @@ -19,26 +18,6 @@ export const Connections = memo(({ linksPositions }: Props) => { const lineRefs = useRef([]) - useEffect(() => { - lineRefs.current.forEach((line, index) => { - if (line) { - const lineWidth = selectedNode ? 0 : 0.5 - - const isSelected = - selectedNode?.ref_id === data?.links[index].source || selectedNode?.ref_id === data?.links[index].target - - gsap.fromTo( - line.material, - { linewidth: 5 }, - { - linewidth: isSelected ? 2 : lineWidth, - duration: 1, - }, - ) - } - }) - }, [data, selectedNode]) - return ( {data?.links.map((l: Link, index) => { @@ -54,18 +33,20 @@ export const Connections = memo(({ linksPositions }: Props) => { linksPositions[index]?.tz || 0, ) + const isSelected = selectedNode?.ref_id === l.source || selectedNode?.ref_id === l.target + + const lineWidth = selectedNode ? 0 : 0.5 + return ( - { lineRefs.current[index] = el as Line2 }} - color="rgba(136, 136, 136, 1)" - isLine2 - lineWidth={1} - opacity={1} - points={[source, target]} - transparent + isSelected={isSelected} + lineWidth={lineWidth} + source={source} + target={target} visible={!showSelectionGraph} /> )