forked from odigos-io/odigos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45ed9f2
commit eb3ad4a
Showing
6 changed files
with
97 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useContainerWidth'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect, useState, useRef } from 'react'; | ||
|
||
export function useContainerWidth() { | ||
const containerRef = useRef<HTMLDivElement | null>(null); | ||
const [containerWidth, setContainerWidth] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
const updateWidth = () => { | ||
if (containerRef.current) { | ||
setContainerWidth( | ||
containerRef.current.getBoundingClientRect().width - 64 | ||
); | ||
} | ||
}; | ||
|
||
updateWidth(); | ||
|
||
window.addEventListener('resize', updateWidth); | ||
return () => window.removeEventListener('resize', updateWidth); | ||
}, []); | ||
|
||
return { containerRef, containerWidth }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useNodeDataFlowHandlers'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// src/hooks/useNodeDataFlowHandlers.ts | ||
import { useCallback } from 'react'; | ||
import { useDrawerStore } from '@/store'; | ||
import { K8sActualSource, ActualDestination } from '@/types'; | ||
|
||
const TYPE_SOURCE = 'source'; | ||
const TYPE_DESTINATION = 'destination'; | ||
|
||
export function useNodeDataFlowHandlers( | ||
sources: K8sActualSource[], | ||
destinations: ActualDestination[] | ||
) { | ||
const setSelectedItem = useDrawerStore( | ||
({ setSelectedItem }) => setSelectedItem | ||
); | ||
|
||
const handleNodeClick = useCallback( | ||
(_, object: any) => { | ||
if (object.data.type === TYPE_SOURCE) { | ||
const { id } = object.data; | ||
const selectedDrawerItem = sources.find( | ||
({ kind, name, namespace }) => | ||
kind === id.kind && name === id.name && namespace === id.namespace | ||
); | ||
if (!selectedDrawerItem) return; | ||
|
||
const { kind, name, namespace } = selectedDrawerItem; | ||
|
||
setSelectedItem({ | ||
id: { kind, name, namespace }, | ||
item: selectedDrawerItem, | ||
type: TYPE_SOURCE, | ||
}); | ||
} | ||
|
||
if (object.data.type === TYPE_DESTINATION) { | ||
const { id } = object.data; | ||
const selectedDrawerItem = destinations.find( | ||
(destination) => destination.id === id | ||
); | ||
|
||
setSelectedItem({ | ||
id, | ||
item: selectedDrawerItem, | ||
type: TYPE_DESTINATION, | ||
}); | ||
} | ||
}, | ||
[sources, destinations, setSelectedItem] | ||
); | ||
|
||
return { | ||
handleNodeClick, | ||
}; | ||
} |