Skip to content

Commit

Permalink
Merge branch 'feature/node-renderer-cleanup' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Sep 29, 2023
2 parents b729296 + 62a3bee commit 7a66081
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useWallet, WalletProvider } from '@xyo-network/react-wallet'
import { MemorySentinel, SentinelConfigSchema } from '@xyo-network/sentinel'
import { useState } from 'react'

import { CytoscapeInstanceProvider } from '../contexts'
import { useCytoscapeElements, useCytoscapeOptions } from '../hooks'
import { ProvidedNodeRenderer } from './ProvidedNodeRenderer'
import { NodeRelationalGraphFlexBox } from './RelationalGraph'
Expand Down Expand Up @@ -62,7 +63,9 @@ const MemoryNodeDecorator: Decorator = (Story, args) => {
return (
<WalletProvider rootWallet={wallet}>
<NodeProvider node={node}>
<Story {...args} />
<CytoscapeInstanceProvider>
<Story {...args} />
</CytoscapeInstanceProvider>
</NodeProvider>
</WalletProvider>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ export const NodeRelationalGraphFlexBox = forwardRef<HTMLDivElement, NodeRelatio
return (
<FlexCol {...props}>
<ActionsContainer>
<ButtonGroup>
{actions}
<Button size={'small'} variant={'contained'} onClick={handleReset}>
Reset
</Button>
</ButtonGroup>
<>
{actions ? (
<ButtonGroup>
{actions}
<Button size={'small'} variant={'contained'} onClick={handleReset}>
Reset
</Button>
</ButtonGroup>
) : (
<Button size={'small'} variant={'contained'} onClick={handleReset}>
Reset
</Button>
)}
</>
</ActionsContainer>
{/* Cytoscape Element */}
<FlexCol alignItems="stretch" height="100%" position="absolute" ref={sharedRef} width="100%"></FlexCol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { useWallet, WalletProvider } from '@xyo-network/react-wallet'
import { MemorySentinel, SentinelConfigSchema } from '@xyo-network/sentinel'
import { useState } from 'react'

import { CytoscapeInstanceProvider } from '../../../contexts'
import { ModuleGraphFlexBox } from './GraphFlexBox'
import { ModuleGraphFlexBoxWithProvider } from './GraphFlexBox'

const nodeUrl = 'http://localhost:8080/node'

Expand Down Expand Up @@ -58,25 +57,23 @@ const MemoryNodeDecorator: Decorator = (Story, args) => {
return (
<WalletProvider rootWallet={wallet}>
<NodeProvider node={node}>
<CytoscapeInstanceProvider>
<Story {...args} />
</CytoscapeInstanceProvider>
<Story {...args} />
</NodeProvider>
</WalletProvider>
)
}

// eslint-disable-next-line import/no-default-export
export default {
component: ModuleGraphFlexBox,
title: 'node/renderer/ModuleGraphFlexBox',
component: ModuleGraphFlexBoxWithProvider,
title: 'node/renderer/ModuleGraphFlexBoxWithProvider',
} as Meta

const Template: StoryFn<typeof ModuleGraphFlexBox> = (props) => <ModuleGraphFlexBox {...props} />
const Template: StoryFn<typeof ModuleGraphFlexBoxWithProvider> = (props) => <ModuleGraphFlexBoxWithProvider {...props} />

const TemplateWithProvidedModule: StoryFn<typeof ModuleGraphFlexBox> = (props) => {
const TemplateWithProvidedModule: StoryFn<typeof ModuleGraphFlexBoxWithProvider> = (props) => {
const [node] = useProvidedNode()
return <ModuleGraphFlexBox rootModule={node} {...props} />
return <ModuleGraphFlexBoxWithProvider rootModule={node} {...props} />
}

const defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Button } from '@mui/material'
import { FlexBoxProps } from '@xylabs/react-flexbox'
import { ModuleInstance } from '@xyo-network/module'

import { CytoscapeInstanceProvider } from '../../../contexts'
import { useHoveredNode, useNewElements, useRelationalGraphOptions, useRenderNewElements, useSelectedElement } from '../../../hooks'
import { WithExtensions } from '../../cytoscape-extensions'
import { NodeRelationalGraphFlexBox } from '../../RelationalGraph'
Expand Down Expand Up @@ -34,3 +35,11 @@ export const ModuleGraphFlexBox: React.FC<ModuleGraphFlexBoxProps> = ({ rootModu
</WithExtensions>
)
}

export const ModuleGraphFlexBoxWithProvider: React.FC<ModuleGraphFlexBoxProps> = (props) => {
return (
<CytoscapeInstanceProvider>
<ModuleGraphFlexBox {...props} />
</CytoscapeInstanceProvider>
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PopperProps } from '@mui/material'
import { FlexCol } from '@xylabs/react-flexbox'
import { NodeSingular } from 'cytoscape'
import { useLayoutEffect, useRef, useState } from 'react'
import { useEffect, useLayoutEffect, useRef, useState } from 'react'

import { ModuleHoverPopper } from './Popper'

Expand All @@ -11,12 +13,16 @@ export const ModuleGraphNodeHover: React.FC<ModuleHoverProps> = ({ node }) => {
const { address, name } = node?.data() ?? {}
const [boundingBox, setBoundingBox] = useState(node?.renderedBoundingBox())
const ref = useRef<HTMLDivElement>(null)
const [currentEl, setCurrentEl] = useState<HTMLDivElement | null>(null)
const [currentEl, setCurrentEl] = useState<PopperProps['anchorEl'] | null>(null)

useLayoutEffect(() => {
if (node && ref.current) {
// Ensure first render clears the previous element when node changes to avoid flicker
useEffect(() => {
setCurrentEl(null)
}, [node])

useEffect(() => {
if (node) {
setBoundingBox(node.renderedBoundingBox())
setCurrentEl(ref.current)
}

const listener = () => {
Expand All @@ -30,24 +36,30 @@ export const ModuleGraphNodeHover: React.FC<ModuleHoverProps> = ({ node }) => {
}
}, [node])

// Once boundingBox state is set and applied to the layout, update the ref
useLayoutEffect(() => {
setCurrentEl(ref.current)
}, [boundingBox])

return (
<>
<FlexCol
ref={ref}
sx={{
// For easier debugging of the 'ghost' element that matches the hovered cytoscape node
// backgroundColor: '#fff',
// opacity: 0.25,
cursor: 'pointer',
height: boundingBox?.h,
left: boundingBox?.x1,
pointerEvents: 'none',
position: 'absolute',
top: boundingBox?.y1,
width: boundingBox?.w,
}}
/>
{node ? (
<>
<div
ref={ref}
style={{
// backgroundColor: '#fff',
// opacity: 0.25,
cursor: 'pointer',
height: boundingBox?.h,
left: boundingBox?.x1,
pointerEvents: 'none',
position: 'absolute',
top: boundingBox?.y1,
width: boundingBox?.w,
}}
></div>
<ModuleHoverPopper address={address} element={currentEl} name={name} placement={'top'} open />
</>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import { Identicon } from '@xylabs/react-identicon'

export interface ModuleHoverPopperProps extends PopperProps {
address?: string
element?: HTMLElement | null
element?: PopperProps['anchorEl']
name?: string
}

export const ModuleHoverPopper: React.FC<ModuleHoverPopperProps> = ({ address, element, name, ...props }) => {
return (
<Popper anchorEl={element} {...props}>
<FlexCol gap={2} p={2} paper sx={{ backgroundColor: (theme: Theme) => alpha(theme.palette.background.paper, 0.95) }}>
<FlexRow gap={2}>
<Identicon value={address} size={24} />
<Typography>{name}</Typography>
</FlexRow>
<Chip label={address} color={'primary'} />
</FlexCol>
</Popper>
<>
{element ? (
<Popper anchorEl={element} {...props}>
<FlexCol gap={2} p={2} paper sx={{ backgroundColor: (theme: Theme) => alpha(theme.palette.background.paper, 0.95) }}>
<FlexRow gap={2}>
<Identicon value={address} size={24} />
<Typography>{name}</Typography>
</FlexRow>
<Chip label={address} color={'primary'} />
</FlexCol>
</Popper>
) : null}
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ export const useCytoscapeOptions = (
) => {
const defaultStyle = useCytoscapeStyle()

const resolvedLayout = layout ?? ConcentricLayout
const resolvedStyle = style ?? defaultStyle

const options = useMemo<CytoscapeOptions | undefined>(() => {
if (elements && layout && style) {
if (elements && resolvedLayout && resolvedStyle) {
return {
elements,
layout: layout ?? ConcentricLayout,
style: style ?? defaultStyle,
layout: resolvedLayout,
style: resolvedStyle,
}
}
}, [elements, layout, style])
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/packages/node-renderer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './components'
export * from './contexts'
export * from './Cytoscape'
export * from './hooks'

0 comments on commit 7a66081

Please sign in to comment.