diff --git a/src/features/toolbar/components/ToolbarContent/ToolbarContent.tsx b/src/features/toolbar/components/ToolbarContent/ToolbarContent.tsx index c16c229b8..a1094c17f 100644 --- a/src/features/toolbar/components/ToolbarContent/ToolbarContent.tsx +++ b/src/features/toolbar/components/ToolbarContent/ToolbarContent.tsx @@ -1,52 +1,66 @@ -import { Fragment } from 'react'; +import { Fragment, useMemo } from 'react'; import { useAtom } from '@reatom/react-v2'; import { ChevronDown16 } from '@konturio/default-icons'; import { toolbar } from '~core/toolbar'; import { ToolbarControl } from '../ToolbarControl/ToolbarControl'; import { ToolbarButton } from '../ToolbarButton/ToolbarButton'; import s from './ToolbarContent.module.css'; +import type { ControlState, ToolbarControlSettings } from '~core/toolbar/types'; +import type { PrimitiveAtom } from '@reatom/core-v2/primitives'; + +type ToolbarControlButton = { + id: string; + settings: ToolbarControlSettings; + stateAtom: PrimitiveAtom; +}; export const ToolbarContent = () => { const [controls] = useAtom(toolbar.controls); + const toolbarContent = useMemo(() => { + return toolbar.toolbarSettings.sections + .map((section) => { + const buttons = section.controls.reduce((acc, id) => { + const settings = controls.get(id); + const stateAtom = toolbar.getControlState(id); + if (settings && stateAtom) acc.push({ id, settings, stateAtom }); + return acc; + }, []); + return { sectionName: section.name, buttons }; + }) + .filter(({ buttons }) => buttons.length > 0); + }, [controls]); + return (
- {toolbar.toolbarSettings.sections.map( - (section, index) => - section.controls.length > 0 && ( - - {index > 0 &&
} - { -
-
- {section.controls.map((id) => { - const settings = controls.get(id); - const stateAtom = toolbar.getControlState(id); - - if (!settings || !stateAtom) return null; - return ( - - ); - })} -
- -
- } -
- ), - )} + {toolbarContent.map(({ sectionName, buttons }, index) => ( + + {index > 0 &&
} + { +
+
+ {buttons.map(({ id, settings, stateAtom }) => { + return ( + + ); + })} +
+ +
+ } + + ))}
); };