diff options
author | Thomas F. K. Jorna <[email protected]> | 2021-08-02 19:04:10 +0200 |
---|---|---|
committer | Thomas F. K. Jorna <[email protected]> | 2021-08-02 19:04:10 +0200 |
commit | c2b219c06a6bf37d35f6eac7879e04d04bd5d023 (patch) | |
tree | 1e24637887855f49f3b241ccdf5770f08c62d350 /pages | |
parent | b9e491d6ab0ab7a8d900d127ef1d3a0fdaa2ffe1 (diff) |
feature: rudimentary tag filtering in place
Diffstat (limited to 'pages')
-rw-r--r-- | pages/index.tsx | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/pages/index.tsx b/pages/index.tsx index ee826ce..7d34338 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -402,9 +402,14 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { ) }, [centralHighlightedNode.current, linksByNodeId]) - const scopedGraphData = useMemo(() => { - const filteredNodes = graphData.nodes.filter((node) => { + const filteredGraphData = useMemo(() => { + const filteredNodes = graphData.nodes.filter((nodeArg) => { + const node = nodeArg as OrgRoamNode const links = linksByNodeId[node.id as string] ?? [] + if (filter.tags.length && node.tags.length) { + return !filter.tags.some((tag) => node.tags.indexOf(tag) > -1) + } + if (!filter.orphans) { return true } @@ -421,12 +426,23 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { }) const filteredNodeIds = filteredNodes.map((node) => node.id as string) - const filteredLinks = graphData.links.filter((linkArg) => { - const link = linkArg as OrgRoamLink - return filter.parents || link.type !== 'parent' + const filteredLinks = graphData.links.filter((link) => { + if (filter.tags.length) { + 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) + return ( + filteredNodeIds.includes(sourceId as string) && + filteredNodeIds.includes(targetId as string) + ) + } + const linkRoam = link as OrgRoamLink + return filter.parents || linkRoam.type !== 'parent' }) + return { filteredNodes, filteredLinks } + }, [filter, graphData]) - const scopedNodes = filteredNodes.filter((node) => { + const scopedGraphData = useMemo(() => { + const scopedNodes = filteredGraphData.filteredNodes.filter((node) => { const links = linksByNodeId[node.id as string] ?? [] return ( scope.nodeIds.includes(node.id as string) || @@ -438,7 +454,7 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { const scopedNodeIds = scopedNodes.map((node) => node.id as string) - const scopedLinks = filteredLinks.filter((link) => { + const scopedLinks = filteredGraphData.filteredLinks.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) @@ -449,7 +465,7 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { }) return scope.nodeIds.length === 0 - ? { nodes: filteredNodes, links: filteredLinks } + ? { nodes: filteredGraphData.filteredNodes, links: filteredGraphData.filteredLinks } : { nodes: scopedNodes, links: scopedLinks, |