From b3a68e76949b8ff1e048122a487fd5326b39ec40 Mon Sep 17 00:00:00 2001
From: Kirill Rogovoy <kirill@rogovoy.me>
Date: Mon, 26 Jul 2021 20:39:26 +0300
Subject: Refactoring

---
 pages/index.tsx | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)

(limited to 'pages/index.tsx')

diff --git a/pages/index.tsx b/pages/index.tsx
index 00d8473..98ab795 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -9,14 +9,11 @@ import type {
 import { OrgRoamGraphReponse, OrgRoamLink, OrgRoamNode } from '../api'
 import { GraphData, NodeObject } from 'force-graph'
 
-import Head from 'next/head'
-
 import { useWindowSize } from '@react-hook/window-size'
 import { useAnimation } from '@lilib/hooks'
 
-import { Button, Box, IconButton, useTheme, useDisclosure, Flex } from '@chakra-ui/react'
+import { Box, useTheme } from '@chakra-ui/react'
 
-import { ChevronLeftIcon, SettingsIcon } from '@chakra-ui/icons'
 import { initialPhysics, initialFilter } from '../components/config'
 import { Tweaks } from '../components/tweaks'
 
@@ -247,16 +244,9 @@ export const Graph = function (props: GraphProps) {
   }, [filter, graphData.nodes, linksByNodeId])
 
   const filteredLinks = useMemo(() => {
-    return graphData.links.filter((link) => {
-      // we need to cover both because force-graph modifies the original data
-      // but if we supply the original data on each render, the graph will re-render sporadically
-      //const sourceId = typeof link.source === 'object' ? link.source.id! : (link.source as string)
-      //const targetId = typeof link.target === 'object' ? link.target.id! : (link.target as string)
-      let showNode = true
-      if (!filter.parents && link.type === 'parent') {
-        showNode = false
-      }
-      return link.type !== 'cite' && showNode
+    return graphData.links.filter((linkArg) => {
+      const link = linkArg as OrgRoamLink
+      return link.type !== 'cite' && (filter.parents || link.type !== 'parent')
     })
   }, [filter, JSON.stringify(graphData.links)])
 
@@ -331,6 +321,7 @@ export const Graph = function (props: GraphProps) {
         fg.d3Force('y', null)
         threeDim ? fg.d3Force('z', null) : null
       }
+
       physics.linkStrength && fg.d3Force('link').strength(physics.linkStrength)
       physics.linkIts && fg.d3Force('link').iterations(physics.linkIts)
       physics.charge && fg.d3Force('charge').strength(physics.charge)
@@ -357,7 +348,7 @@ export const Graph = function (props: GraphProps) {
     }
     setScope((currentScope) => ({
       ...currentScope,
-      nodeIds: [...currentScope.nodeIds, nodeId as string],
+      nodeIds: [...currentScope.nodeIds, node.id as string],
     }))
   }
 
@@ -371,7 +362,7 @@ export const Graph = function (props: GraphProps) {
     algorithm: physics.algorithms[physics.algorithmName],
   })
 
-  const lastHoverNode = useRef()
+  const lastHoverNode = useRef<NodeObject | null>(null)
   useEffect(() => {
     if (hoverNode) {
       lastHoverNode.current = hoverNode
-- 
cgit v1.2.3

