Skip to content

Commit

Permalink
remove reactEmbeddableRegistryHasKey usage from observability_solutio…
Browse files Browse the repository at this point in the history
…n plugins (elastic#203249)

Part of elastic#203250

All embeddables have been converted to new embeddable system. Now its
time to clean-up the legacy embeddable system. Part of that clean up
involves removing usage of `reactEmbeddableRegistryHasKey`.

This PR removes `reactEmbeddableRegistryHasKey` from
observability_solution plugins

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
nreese and elasticmachine authored Dec 10, 2024
1 parent 1e29e6c commit 8ee4b49
Showing 1 changed file with 3 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { EuiLoadingSpinner, EuiFlexItem } from '@elastic/eui';
import { EuiFlexItem } from '@elastic/eui';
import { css } from '@emotion/css';
import { ReactEmbeddableRenderer } from '@kbn/embeddable-plugin/public';
import type { GlobalWidgetParameters } from '@kbn/investigate-plugin/public';
import { useAbortableAsync } from '@kbn/observability-utils-browser/hooks/use_abortable_async';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { v4 } from 'uuid';
import { ErrorMessage } from '../../components/error_message';
import { useKibana } from '../../hooks/use_kibana';
import React, { useMemo, useRef } from 'react';
import { Options } from '../register_items';

export const EMBEDDABLE_ITEM_TYPE = 'embeddable';
Expand Down Expand Up @@ -62,101 +58,6 @@ function ReactEmbeddable({ type, config, timeRange: { from, to }, savedObjectId
);
}

function LegacyEmbeddable({ type, config, timeRange: { from, to }, savedObjectId }: Props) {
const {
dependencies: {
start: { embeddable },
},
} = useKibana();

const [targetElement, setTargetElement] = useState<HTMLElement | null>(null);

const embeddableInstanceAsync = useAbortableAsync(async () => {
const factory = embeddable.getEmbeddableFactory(type);

if (!factory) {
throw new Error(`Cannot find embeddable factory for ${type}`);
}

const configWithId = {
id: savedObjectId ?? v4(),
...config,
};

const configWithOverrides = {
...configWithId,
timeRange: {
from,
to,
},
overrides: {
axisX: { hide: true },
axisLeft: { style: { axisTitle: { visible: false } } },
settings: { showLegend: false },
},
};

if (savedObjectId) {
return factory.createFromSavedObject(configWithOverrides.id, configWithOverrides);
}

const instance = await factory.create(configWithOverrides);

return instance;
}, [type, savedObjectId, config, from, to, embeddable]);

const embeddableInstance = embeddableInstanceAsync.value;

useEffect(() => {
if (!targetElement || !embeddableInstance) {
return;
}

embeddableInstance.render(targetElement);

return () => {};
}, [embeddableInstance, targetElement]);

useEffect(() => {
return () => {
if (embeddableInstance) {
embeddableInstance.destroy();
}
};
}, [embeddableInstance]);

if (embeddableInstanceAsync.error) {
return <ErrorMessage error={embeddableInstanceAsync.error} />;
}

if (!embeddableInstance) {
return <EuiLoadingSpinner />;
}

return (
<div
className={embeddableClassName}
ref={(element) => {
setTargetElement(element);
}}
/>
);
}

function EmbeddableWidget(props: Props) {
const {
dependencies: {
start: { embeddable },
},
} = useKibana();

if (embeddable.reactEmbeddableRegistryHasKey(props.type)) {
return <ReactEmbeddable {...props} />;
}

return <LegacyEmbeddable {...props} />;
}

interface EmbeddableItemParams {
type: string;
config: Record<string, any>;
Expand Down Expand Up @@ -197,7 +98,7 @@ export function registerEmbeddableItem({
}
`}
>
<EmbeddableWidget {...parameters} />
<ReactEmbeddable {...parameters} />
</EuiFlexItem>
);
},
Expand Down

0 comments on commit 8ee4b49

Please sign in to comment.