-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved perfomance, decreased rerenders, added color support f…
…or links
- Loading branch information
Showing
6 changed files
with
119 additions
and
104 deletions.
There are no files selected for viewing
64 changes: 33 additions & 31 deletions
64
src/components/Universe/Graph/Connections/LineComponent.tsx
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 |
---|---|---|
@@ -1,42 +1,44 @@ | ||
import { Line } from '@react-three/drei' | ||
import gsap from 'gsap' | ||
import { forwardRef, useEffect } from 'react' | ||
import { memo, useEffect, useRef } from 'react' | ||
import { Color, Vector3 } from 'three' | ||
import { Line2 } from 'three-stdlib' | ||
|
||
type LineComponentProps = { | ||
source: Vector3 | ||
target: Vector3 | ||
isSelected: boolean | ||
lineWidth: number | ||
visible: boolean | ||
} | ||
|
||
const LineComponent = forwardRef<Line2, LineComponentProps>( | ||
({ source, target, isSelected, lineWidth, visible }, ref) => { | ||
useEffect(() => { | ||
const line = (ref as React.MutableRefObject<Line2 | null>).current | ||
|
||
if (line) { | ||
gsap.fromTo( | ||
line.material, | ||
{ linewidth: 5 }, | ||
{ | ||
linewidth: isSelected ? 2 : lineWidth, | ||
duration: 1, | ||
}, | ||
) | ||
} | ||
}, [isSelected, lineWidth, ref]) | ||
|
||
const color = new Color(0xff0000) | ||
|
||
return ( | ||
<Line ref={ref} color={color} isLine2 lineWidth={2} opacity={0.5} points={[source, target]} visible={visible} /> | ||
) | ||
}, | ||
) | ||
|
||
LineComponent.displayName = 'LineComponent' | ||
|
||
export default LineComponent | ||
const VECTOR = new Vector3(0, 0, 0) | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
export const _LineComponent = (props: LineComponentProps) => { | ||
const { isSelected, lineWidth, visible } = props | ||
const ref = useRef<Line2>(null) | ||
|
||
useEffect(() => { | ||
const line = (ref as React.MutableRefObject<Line2 | null>).current | ||
|
||
if (line) { | ||
gsap.fromTo( | ||
line.material, | ||
{ linewidth: 5 }, | ||
{ | ||
linewidth: isSelected ? 2 : lineWidth, | ||
duration: 1, | ||
}, | ||
) | ||
} | ||
}, [isSelected, lineWidth, ref]) | ||
|
||
const color = new Color(0xff0000) | ||
|
||
return ( | ||
<Line ref={ref} color={color} isLine2 lineWidth={2} opacity={0.5} points={[VECTOR, VECTOR]} visible={visible} /> | ||
) | ||
} | ||
|
||
_LineComponent.displayName = 'LineComponent' | ||
|
||
export const LineComponent = memo(_LineComponent) |
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
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,24 @@ | ||
// @ts-nocheck | ||
// @ts-ignore | ||
|
||
import { useEffect, useRef } from 'react' | ||
|
||
export function useTraceUpdate(props: { [s: string]: unknown } | ArrayLike<unknown>) { | ||
const prev = useRef(props) | ||
|
||
useEffect(() => { | ||
const changedProps = Object.entries(props).reduce((ps, [k, v]) => { | ||
if (prev.current[k] !== v) { | ||
ps[k] = [prev.current[k], v] | ||
} | ||
|
||
return ps | ||
}, {}) | ||
|
||
if (Object.keys(changedProps).length > 0) { | ||
console.log('Changed props:', changedProps) | ||
} | ||
|
||
prev.current = props | ||
}) | ||
} |