-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Canvas): Extract onAddStep hook
- Loading branch information
Showing
3 changed files
with
47 additions
and
48 deletions.
There are no files selected for viewing
44 changes: 5 additions & 39 deletions
44
packages/ui/src/components/Visualization/Custom/ContextMenu/ItemAddStep.tsx
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
38 changes: 38 additions & 0 deletions
38
packages/ui/src/components/Visualization/Custom/hooks/add-step.hook.tsx
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,38 @@ | ||
import { useCallback, useContext, useMemo } from 'react'; | ||
import { AddStepMode, IVisualizationNode } from '../../../../models/visualization/base-visual-entity'; | ||
import { CatalogModalContext } from '../../../../providers'; | ||
import { EntitiesContext } from '../../../../providers/entities.provider'; | ||
|
||
export const useAddStep = ( | ||
vizNode: IVisualizationNode, | ||
mode: AddStepMode.PrependStep | AddStepMode.AppendStep = AddStepMode.AppendStep, | ||
) => { | ||
const entitiesContext = useContext(EntitiesContext); | ||
const catalogModalContext = useContext(CatalogModalContext); | ||
|
||
const onAddStep = useCallback(async () => { | ||
if (!vizNode || !entitiesContext) return; | ||
|
||
/** Get compatible nodes and the location where can be introduced */ | ||
const compatibleNodes = entitiesContext.camelResource.getCompatibleComponents(mode, vizNode.data); | ||
|
||
/** Open Catalog modal, filtering the compatible nodes */ | ||
const definedComponent = await catalogModalContext?.getNewComponent(compatibleNodes); | ||
if (!definedComponent) return; | ||
|
||
/** Add new node to the entities */ | ||
vizNode.addBaseEntityStep(definedComponent, mode); | ||
|
||
/** Update entity */ | ||
entitiesContext.updateEntitiesFromCamelResource(); | ||
}, [catalogModalContext, entitiesContext, mode, vizNode]); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
onAddStep, | ||
}), | ||
[onAddStep], | ||
); | ||
|
||
return value; | ||
}; |