-
Notifications
You must be signed in to change notification settings - Fork 3
/
ComponentTree.tsx
66 lines (58 loc) · 2.05 KB
/
ComponentTree.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import classNames from 'classnames'
import { isEqual } from 'lodash'
import { useCallback, useRef } from 'react'
import { ComponentState, PageState } from '../../shared/models'
import findComponentState from '../utils/findComponentState'
import CustomContextMenu from './CustomContextMenu'
import { useStudioContext } from './useStudioContext'
export default function ComponentTree() {
const { pageState, } = useStudioContext()
return (
<div>
{pageState.componentsState.map(c => <ComponentNode c={c} key={c.uuid} />)}
</div>
)
}
function ComponentNode({ c }: { c: ComponentState }) {
const ref = useRef<HTMLDivElement>(null)
const {
activeComponentState,
setActiveComponentState,
pageStateOnFile,
moduleNameToComponentMetadata
} = useStudioContext()
const activeComponentUUID = activeComponentState?.uuid
const updateActiveComponent = useCallback(() => {
if (activeComponentUUID !== c.uuid) {
setActiveComponentState(c)
} else {
setActiveComponentState(undefined)
}
}, [activeComponentUUID, c, setActiveComponentState])
const className = classNames('flex border-solid border-2 ', {
'border-indigo-600': activeComponentUUID === c.uuid,
'border-transparent': activeComponentUUID !== c.uuid
})
const isGlobal = moduleNameToComponentMetadata.localComponents[c.name].global
return (
<div
key={c.uuid}
className='cursor-pointer select-none ml-4'
>
<div
className={className}
ref={ref}
onClick={updateActiveComponent}
>
{!isGlobal && <CustomContextMenu elementRef={ref} componentUUID={c.uuid} />}
{c.name}
{hasUnsavedChanges(c, pageStateOnFile) && <div className='red'>*</div>}
</div>
{c.children?.map(c => <ComponentNode c={c} key={c.uuid}/>)}
</div>
)
}
function hasUnsavedChanges(componentState: ComponentState, pageStateOnFile: PageState) {
const initialComponentState = findComponentState(componentState, pageStateOnFile.componentsState)
return !isEqual(componentState.props, initialComponentState?.props)
}