From 5c4ff3d788b9f039aabcb7d7ad43d6ca478ff7e0 Mon Sep 17 00:00:00 2001 From: Kirill Rogovoy Date: Thu, 29 Jul 2021 18:30:44 +0300 Subject: Refactoring: simplify another useMemo --- pages/index.tsx | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) (limited to 'pages') diff --git a/pages/index.tsx b/pages/index.tsx index 18cce7c..d1c9cf2 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -258,9 +258,11 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { if (behavior.followLocalOrZoom) { setScope({ nodeIds: [emacsNodeId] }) setTimeout(() => { - fg?.zoomToFit(1000, numberWithinRange(20, 200, windowWidth / 8), (node: NodeObject) => - getNeighborNodes(emacsNodeId)[node.id!], - ) + fg?.zoomToFit( + 1000, + numberWithinRange(20, 200, windowWidth / 8), + (node: NodeObject) => getNeighborNodes(emacsNodeId)[node.id!], + ) }, 1) } else { fg?.zoomToFit(1000, 200, (node: NodeObject) => getNeighborNodes(emacsNodeId)[node.id!]) @@ -426,7 +428,7 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { ) const getColor = (c: any) => (isNaN(c) ? theme.colors[c][500] : theme.colors.gray[c]) - const highlightColors = Object.fromEntries( + return Object.fromEntries( allColors.map((color) => { const color1 = getColor(color) const crisscross = allColors.map((color2) => [ @@ -436,8 +438,6 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { return [color, Object.fromEntries(crisscross)] }), ) - console.log(highlightColors) - return highlightColors }, [ visuals.nodeColorScheme, visuals.linkHighlight, @@ -445,21 +445,15 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { visuals.linkColorScheme, ]) - const previouslyHighlightedLinks = useMemo( - () => linksByNodeId[lastHoverNode.current?.id!] ?? [], - [JSON.stringify(hoverNode), lastHoverNode.current], - ) - - const previouslyHighlightedNodes = useMemo( - () => - Object.fromEntries( - [ - lastHoverNode.current?.id! as string, - ...previouslyHighlightedLinks.flatMap((link) => [link.source, link.target]), - ].map((nodeId) => [nodeId, {}]), - ), - [JSON.stringify(hoverNode), previouslyHighlightedLinks, lastHoverNode.current], - ) + const previouslyHighlightedNodes = useMemo(() => { + const previouslyHighlightedLinks = linksByNodeId[lastHoverNode.current?.id!] ?? [] + return Object.fromEntries( + [ + lastHoverNode.current?.id! as string, + ...previouslyHighlightedLinks.flatMap((link) => [link.source, link.target]), + ].map((nodeId) => [nodeId, {}]), + ) + }, [JSON.stringify(hoverNode), lastHoverNode.current]) const getNodeColorById = (id: string) => { const linklen = linksByNodeId[id!]?.length ?? 0 -- cgit v1.2.3