import { animate, motion } from "framer-motion"; import React, { useContext, useMemo, useState } from "react"; import { BsFillFilterCircleFill } from "react-icons/bs"; import { ConfigContext } from "../Controller/ConfigController"; import { setHoveredNodesAndEdges } from "../Controller/graphSlice"; import { useAppDispatch, useAppSelector } from "../hooks"; function FilterBar() { const [visible, setVisible] = useState(false); const { config } = useContext(ConfigContext)!; const nodes = useAppSelector((state) => state.graph.nodes); const dispatch = useAppDispatch(); const types = useMemo(() => { const res: { type: string; count: number }[] = []; nodes .map((node) => node.type) .forEach((type) => { const current = res.find((item) => item.type === type); if (!current) { res.push({ type, count: 1, }); } else { current.count++; } }); return res; }, [nodes]); return ( <> {config.showFilter && ( <> setVisible(!visible)} whileHover={{ opacity: 0.5, }} > {types.map((type) => { return ( { const typeNodes = nodes.filter( (node) => node.type === type.type ); dispatch( setHoveredNodesAndEdges({ nodes: typeNodes, }) ); }} initial={{ fontSize: "10px", marginRight: "8px", borderRadius: "5px", background: config.typeConfig ? config.typeConfig[type.type]?.fill || "#cecece" : "#cecece", padding: "2px 6px", color: "#fff", cursor: "pointer", overflow: "hidden", whiteSpace: "nowrap", minWidth: "max-content", }} > {type.type} {type.count}δΈͺ ); })} )} ); } export default FilterBar;