2022-07-07 01:53:02 -05:00
|
|
|
import { css, cx } from '@emotion/css';
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import { GrafanaTheme2 } from '@grafana/data';
|
2023-01-10 05:30:53 -06:00
|
|
|
import { sceneGraph, SceneObject, isSceneObject, SceneLayoutChild } from '@grafana/scenes';
|
2022-07-07 01:53:02 -05:00
|
|
|
import { Icon, useStyles2 } from '@grafana/ui';
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
node: SceneObject;
|
|
|
|
selectedObject?: SceneObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function SceneObjectTree({ node, selectedObject }: Props) {
|
|
|
|
const styles = useStyles2(getStyles);
|
|
|
|
const state = node.useState();
|
2022-07-19 10:46:49 -05:00
|
|
|
let children: SceneLayoutChild[] = [];
|
2022-07-07 01:53:02 -05:00
|
|
|
|
|
|
|
for (const propKey of Object.keys(state)) {
|
|
|
|
const propValue = (state as any)[propKey];
|
|
|
|
if (isSceneObject(propValue)) {
|
|
|
|
children.push(propValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 10:46:49 -05:00
|
|
|
if ('children' in state) {
|
|
|
|
for (const child of state.children) {
|
2022-07-07 01:53:02 -05:00
|
|
|
children.push(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = node.constructor.name;
|
|
|
|
const isSelected = selectedObject === node;
|
2022-11-16 04:36:30 -06:00
|
|
|
const onSelectNode = () => sceneGraph.getSceneEditor(node).onSelectObject(node);
|
2022-07-07 01:53:02 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.node}>
|
|
|
|
<div className={styles.header} onClick={onSelectNode}>
|
|
|
|
<div className={styles.icon}>{children.length > 0 && <Icon name="angle-down" size="sm" />}</div>
|
|
|
|
<div className={cx(styles.name, isSelected && styles.selected)}>{name}</div>
|
|
|
|
</div>
|
|
|
|
{children.length > 0 && (
|
|
|
|
<div className={styles.children}>
|
|
|
|
{children.map((child) => (
|
|
|
|
<SceneObjectTree node={child} selectedObject={selectedObject} key={child.state.key} />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const getStyles = (theme: GrafanaTheme2) => {
|
|
|
|
return {
|
|
|
|
node: css({
|
|
|
|
display: 'flex',
|
|
|
|
flexGrow: 0,
|
|
|
|
cursor: 'pointer',
|
|
|
|
flexDirection: 'column',
|
|
|
|
padding: '2px 4px',
|
|
|
|
}),
|
|
|
|
header: css({
|
|
|
|
display: 'flex',
|
|
|
|
fontWeight: 500,
|
|
|
|
}),
|
|
|
|
name: css({}),
|
|
|
|
selected: css({
|
|
|
|
color: theme.colors.error.text,
|
|
|
|
}),
|
|
|
|
icon: css({
|
|
|
|
width: theme.spacing(3),
|
|
|
|
color: theme.colors.text.secondary,
|
|
|
|
}),
|
|
|
|
children: css({
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
paddingLeft: 8,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
};
|