remove reactEmbeddableRegistryHasKey usage from observability_solution plugins (#203249)

Part of https://github.com/elastic/kibana/issues/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 <elasticmachine@users.noreply.github.com>
This commit is contained in:
Nathan Reese 2024-12-10 11:19:09 -07:00 committed by GitHub
parent 1e29e6caff
commit 8ee4b49728
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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';
@ -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>;
@ -197,7 +98,7 @@ export function registerEmbeddableItem({
}
`}
>
<EmbeddableWidget {...parameters} />
<ReactEmbeddable {...parameters} />
</EuiFlexItem>
);
},