PanelHeaderMenuProvider.tsx 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import { FC, ReactElement, useEffect, useState } from 'react';
  2. import { useSelector } from 'react-redux';
  3. import { PanelMenuItem } from '@grafana/data';
  4. import { getPanelStateForModel } from 'app/features/panel/state/selectors';
  5. import { StoreState } from '../../../../types';
  6. import { DashboardModel, PanelModel } from '../../state';
  7. import { getPanelMenu } from '../../utils/getPanelMenu';
  8. interface PanelHeaderMenuProviderApi {
  9. items: PanelMenuItem[];
  10. }
  11. interface Props {
  12. panel: PanelModel;
  13. dashboard: DashboardModel;
  14. children: (props: PanelHeaderMenuProviderApi) => ReactElement;
  15. }
  16. export const PanelHeaderMenuProvider: FC<Props> = ({ panel, dashboard, children }) => {
  17. const [items, setItems] = useState<PanelMenuItem[]>([]);
  18. const angularComponent = useSelector((state: StoreState) => getPanelStateForModel(state, panel)?.angularComponent);
  19. useEffect(() => {
  20. setItems(getPanelMenu(dashboard, panel, angularComponent));
  21. }, [dashboard, panel, angularComponent, setItems]);
  22. return children({ items });
  23. };