2022-07-07 09:49:05 -05:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
|
|
|
|
import { SceneComponentEditingWrapper } from '../editor/SceneComponentEditWrapper';
|
|
|
|
|
|
|
|
import { SceneComponentProps, SceneObject } from './types';
|
|
|
|
|
2022-11-15 02:49:39 -06:00
|
|
|
export function SceneComponentWrapper<T extends SceneObject>({
|
|
|
|
model,
|
|
|
|
isEditing,
|
|
|
|
...otherProps
|
|
|
|
}: SceneComponentProps<T>) {
|
2022-07-07 09:49:05 -05:00
|
|
|
const Component = (model as any).constructor['Component'] ?? EmptyRenderer;
|
2022-11-15 02:49:39 -06:00
|
|
|
const inner = <Component {...otherProps} model={model} isEditing={isEditing} />;
|
2022-07-07 09:49:05 -05:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|