grafana/public/app/features/scenes/core/SceneComponentWrapper.tsx
Torkel Ödegaard 849134b5dd
Scene: Small refactorings and name changes (#51866)
* Rename onMount and onUnmount and some other small refactorings

* More refactorings fixing typescript issues
2022-07-07 16:49:05 +02:00

33 lines
910 B
TypeScript

import React, { useEffect } from 'react';
import { SceneComponentEditingWrapper } from '../editor/SceneComponentEditWrapper';
import { SceneComponentProps, SceneObject } from './types';
export function SceneComponentWrapper<T extends SceneObject>({ model, isEditing }: SceneComponentProps<T>) {
const Component = (model as any).constructor['Component'] ?? EmptyRenderer;
const inner = <Component model={model} isEditing={isEditing} />;
// Handle component activation state state
useEffect(() => {
if (!model.isActive) {
model.activate();
}
return () => {
if (model.isActive) {
model.deactivate();
}
};
}, [model]);
if (!isEditing) {
return inner;
}
return <SceneComponentEditingWrapper model={model}>{inner}</SceneComponentEditingWrapper>;
}
function EmptyRenderer<T>(_: SceneComponentProps<T>): React.ReactElement | null {
return null;
}