github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/ui/dashboard/src/components/dashboards/check/CheckGrouping/index.tsx (about) 1 import CheckPanel from "../CheckPanel"; 2 import sortBy from "lodash/sortBy"; 3 import { 4 CheckGroupNodeStates, 5 CheckGroupingActions, 6 useCheckGrouping, 7 } from "../../../../hooks/useCheckGrouping"; 8 import { CheckNode } from "../common"; 9 import { useCallback, useEffect, useState } from "react"; 10 11 type CheckGroupingProps = { 12 node: CheckNode; 13 }; 14 15 const CheckGrouping = ({ node }: CheckGroupingProps) => { 16 const { dispatch, nodeStates } = useCheckGrouping(); 17 const [restoreNodeStates, setRestoreNodeStates] = 18 useState<CheckGroupNodeStates | null>(null); 19 20 const expand = useCallback(() => { 21 setRestoreNodeStates(nodeStates); 22 dispatch({ type: CheckGroupingActions.EXPAND_ALL_NODES }); 23 }, [dispatch, nodeStates]); 24 25 const restore = useCallback(() => { 26 if (restoreNodeStates) { 27 dispatch({ 28 type: CheckGroupingActions.UPDATE_NODES, 29 nodes: restoreNodeStates, 30 }); 31 } 32 }, [dispatch, restoreNodeStates]); 33 34 useEffect(() => { 35 window.onbeforeprint = expand; 36 window.onafterprint = restore; 37 }, [expand, restore]); 38 39 return ( 40 <div className="space-y-4 md:space-y-6 col-span-12"> 41 {sortBy(node.children, "sort")?.map((child) => ( 42 <CheckPanel key={child.name} depth={1} node={child} /> 43 ))} 44 </div> 45 ); 46 }; 47 48 export default CheckGrouping;