mirror of https://github.com/grafana/grafana
Scene: Small refactorings and name changes (#51866)
* Rename onMount and onUnmount and some other small refactorings * More refactorings fixing typescript issuespull/52052/head
parent
6447e08809
commit
849134b5dd
@ -0,0 +1,32 @@ |
||||
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; |
||||
} |
Loading…
Reference in new issue