Skip to content

Commit

Permalink
Added await for isEditable in top_nav EditorMenu at Canvas.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznietsov committed Jul 19, 2022
1 parent 545b32a commit 2457e74
Showing 1 changed file with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
* 2.0.
*/

import React, { FC, useCallback } from 'react';
import React, { FC, useCallback, useEffect, useMemo, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { BaseVisType, VisGroups, VisTypeAlias } from '@kbn/visualizations-plugin/public';
import { EmbeddableFactoryDefinition, EmbeddableInput } from '@kbn/embeddable-plugin/public';
import {
EmbeddableFactory,
EmbeddableFactoryDefinition,
EmbeddableInput,
} from '@kbn/embeddable-plugin/public';
import { trackCanvasUiMetric, METRIC_TYPE } from '../../../lib/ui_metric';
import {
useEmbeddablesService,
Expand All @@ -27,6 +31,11 @@ interface Props {
addElement: (element: Partial<ElementSpec>) => void;
}

interface UnwrappedEmbeddableFactory {
factory: EmbeddableFactory;
isEditable: boolean;
}

export const EditorMenu: FC<Props> = ({ addElement }) => {
const embeddablesService = useEmbeddablesService();
const { pathname, search, hash } = useLocation();
Expand All @@ -35,6 +44,26 @@ export const EditorMenu: FC<Props> = ({ addElement }) => {
const visualizationsService = useVisualizationsService();
const IS_DARK_THEME = platformService.getUISetting('theme:darkMode');

const embeddableFactories = useMemo(
() => (embeddablesService ? Array.from(embeddablesService.getEmbeddableFactories()) : []),
[embeddablesService]
);

const [unwrappedEmbeddableFactories, setUnwrappedEmbeddableFactories] = useState<
UnwrappedEmbeddableFactory[]
>([]);

useEffect(() => {
Promise.all(
embeddableFactories.map<Promise<UnwrappedEmbeddableFactory>>(async (factory) => ({
factory,
isEditable: await factory.isEditable(),
}))
).then((factories) => {
setUnwrappedEmbeddableFactories(factories);
});
}, [embeddableFactories]);

const createNewVisType = useCallback(
(visType?: BaseVisType | VisTypeAlias) => () => {
let path = '';
Expand Down Expand Up @@ -113,18 +142,17 @@ export const EditorMenu: FC<Props> = ({ addElement }) => {
a === b ? 0 : a ? -1 : 1
);

const factories = embeddablesService
? Array.from(embeddablesService.getEmbeddableFactories()).filter(
({ type, isEditable, canCreateNew, isContainerType }) =>
// @ts-expect-error ts 4.5 upgrade
isEditable() &&
!isContainerType &&
canCreateNew() &&
!['visualization', 'ml'].some((factoryType) => {
return type.includes(factoryType);
})
)
: [];
const factories = unwrappedEmbeddableFactories
.filter(
({ isEditable, factory: { type, canCreateNew, isContainerType } }) =>
isEditable &&
!isContainerType &&
canCreateNew() &&
!['visualization', 'ml'].some((factoryType) => {
return type.includes(factoryType);
})
)
.map(({ factory }) => factory);

const promotedVisTypes = getVisTypesByGroup(VisGroups.PROMOTED);

Expand Down

0 comments on commit 2457e74

Please sign in to comment.