From 151531b6edf2af6fbdafaf6bba324c8d2b3c7468 Mon Sep 17 00:00:00 2001 From: Kirill Rogovoy Date: Thu, 29 Jul 2021 17:03:35 +0300 Subject: Minor refactoring --- pages/index.tsx | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) (limited to 'pages') diff --git a/pages/index.tsx b/pages/index.tsx index c4cc011..06f24ea 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -66,16 +66,7 @@ export function GraphPage() { const nodeByIdRef = useRef({}) const linksByNodeIdRef = useRef({}) - /* const fetchGraphData = () => { - * return fetch('http://localhost:35901/graph') - * .then((res) => res.json()) - * .then((orgRoamGraphData) => { - * console.log('fetching graphdata') - * parseGraphData(orgRoamGraphData) - * }) - * } - */ - const parseGraphData = (orgRoamGraphData: OrgRoamGraphReponse) => { + const updateGraphData = (orgRoamGraphData: OrgRoamGraphReponse) => { const nodesByFile = orgRoamGraphData.nodes.reduce((acc, node) => { return { ...acc, @@ -125,15 +116,14 @@ export function GraphPage() { const { setEmacsTheme } = useContext(ThemeContext) useEffect(() => { const socket = new WebSocket('ws://localhost:35903') - socket.addEventListener('open', (event) => { + socket.addEventListener('open', () => { console.log('Connection with Emacs established') }) socket.addEventListener('message', (event) => { const message = JSON.parse(event.data) - console.log(typeof message.type) switch (message.type) { case 'graphdata': - parseGraphData(message.data) + updateGraphData(message.data) break case 'theme': console.log('Received theme data') @@ -450,7 +440,6 @@ export const Graph = function (props: GraphProps) { visuals.nodeHighlight, visuals.linkColorScheme, ]) - const highlightedLinks = useMemo(() => linksByNodeId[hoverNode?.id!] ?? [], [hoverNode]) const previouslyHighlightedLinks = useMemo( () => linksByNodeId[lastHoverNode.current?.id!] ?? [], @@ -493,14 +482,12 @@ export const Graph = function (props: GraphProps) { const nodeColor = getLinkNodeColor(sourceId, targetId) return theme.colors[nodeColor][500] } + if (!needsHighlighting && !visuals.linkColorScheme) { const nodeColor = getLinkNodeColor(sourceId, targetId) return theme.colors[nodeColor][500] } - if (!needsHighlighting && !visuals.linkColorScheme) { - const nodeColor = getLinkNodeColor(sourceId, targetId) - return theme.colors[nodeColor][500] - } + if (!needsHighlighting) { return theme.colors.gray[visuals.linkColorScheme] } @@ -509,12 +496,15 @@ export const Graph = function (props: GraphProps) { const nodeColor = getLinkNodeColor(sourceId, targetId) return theme.colors[nodeColor][500] } + if (!visuals.linkHighlight) { return theme.colors.gray[visuals.linkColorScheme] } + if (!visuals.linkColorScheme) { return highlightColors[getLinkNodeColor(sourceId, targetId)][visuals.linkHighlight](opacity) } + return highlightColors[visuals.linkColorScheme][visuals.linkHighlight](opacity) } @@ -617,11 +607,6 @@ export const Graph = function (props: GraphProps) { linkDirectionalParticles: physics.particles ? physics.particlesNumber : undefined, linkColor: (link) => { - /* if (!physics.highlight || !visuals.linkHighlight) { - * return visuals.linkColorScheme - * ? theme.colors.gray[visuals.linkColorScheme] - * : theme.colors[getLinkNodeColor(link)][500] - * } */ 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) const linkIsHighlighted = isLinkRelatedToNode(link, centralHighlightedNode) -- cgit v1.2.3 From 70b5c59233dbdae0b780746c25f7cef156f77fc9 Mon Sep 17 00:00:00 2001 From: Kirill Rogovoy Date: Thu, 29 Jul 2021 17:06:04 +0300 Subject: Minor refactoring --- pages/index.tsx | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) (limited to 'pages') diff --git a/pages/index.tsx b/pages/index.tsx index 06f24ea..51b21fc 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -123,18 +123,13 @@ export function GraphPage() { const message = JSON.parse(event.data) switch (message.type) { case 'graphdata': - updateGraphData(message.data) - break + return updateGraphData(message.data) case 'theme': - console.log('Received theme data') - setEmacsTheme(message.data) - break + return setEmacsTheme(message.data) case 'command': - console.log('command') switch (message.data.commandName) { case 'follow': - setEmacsNodeId(message.data.id) - break + return setEmacsNodeId(message.data.id) case 'zoom': { const links = linksByNodeIdRef.current[message.data.id!] ?? [] const nodes = Object.fromEntries( @@ -146,20 +141,12 @@ export function GraphPage() { /* zoomToFit(500, 200, (node: OrgRoamNode)=>nodes[node.id!]) */ } default: - console.log('oopsie whoopsie') + return console.error('unknown message type', message.type) } } }) - // fetchGraphData() }, []) - useEffect(() => { - if (!emacsNodeId) { - return - } - //fetchGraphData() - }, [emacsNodeId]) - const [threeDim, setThreeDim] = useState(false) if (!graphData) { -- cgit v1.2.3 From 4e3d884c402b7dc7d12f0cae88a9a312b10f166f Mon Sep 17 00:00:00 2001 From: Kirill Rogovoy Date: Thu, 29 Jul 2021 17:18:52 +0300 Subject: Minor refactoring --- pages/index.tsx | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'pages') diff --git a/pages/index.tsx b/pages/index.tsx index 51b21fc..e0e8b6e 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -246,25 +246,19 @@ export const Graph = function (props: GraphProps) { const filteredNodes = useMemo(() => { return graphData.nodes.filter((node) => { const links = linksByNodeId[node.id as string] ?? [] - let showNode = true - if (filter.orphans) { - if (filter.parents) { - showNode = links.length !== 0 - } else { - if (links.length === 0) { - showNode = false - } else { - if ( - links.length - - links.filter((link) => link.type === 'parent' || link.type === 'cite').length === - 0 - ) { - showNode = false - } - } - } + if (!filter.orphans) { + return true } - return showNode + + if (filter.parents) { + return links.length !== 0 + } + + if (links.length === 0) { + return false + } + + return !links.some(link => !['parent', 'cite'].includes(link.type)) }) }, [filter, graphData.nodes, linksByNodeId]) @@ -491,7 +485,7 @@ export const Graph = function (props: GraphProps) { if (!visuals.linkColorScheme) { return highlightColors[getLinkNodeColor(sourceId, targetId)][visuals.linkHighlight](opacity) } - + return highlightColors[visuals.linkColorScheme][visuals.linkHighlight](opacity) } -- cgit v1.2.3