MCPcopy
hub / github.com/premieroctet/openchakra / useDropComponent

Function useDropComponent

src/hooks/useDropComponent.ts:6–42  ·  view source on GitHub ↗
(
  componentId: string,
  accept: (ComponentType | MetaComponentType)[] = rootComponents,
  canDrop: boolean = true,
)

Source from the content-addressed store, hash-verified

4import builder from '~core/models/composer/builder'
5
6export const useDropComponent = (
7 componentId: string,
8 accept: (ComponentType | MetaComponentType)[] = rootComponents,
9 canDrop: boolean = true,
10) => {
11 const dispatch = useDispatch()
12
13 const [{ isOver }, drop] = useDrop({
14 accept,
15 collect: monitor => ({
16 isOver: monitor.isOver({ shallow: true }) && monitor.canDrop(),
17 }),
18 drop: (item: ComponentItemProps, monitor: DropTargetMonitor) => {
19 if (!monitor.isOver()) {
20 return
21 }
22
23 if (item.isMoved) {
24 dispatch.components.moveComponent({
25 parentId: componentId,
26 componentId: item.id,
27 })
28 } else if (item.isMeta) {
29 dispatch.components.addMetaComponent(builder[item.type](componentId))
30 } else {
31 dispatch.components.addComponent({
32 parentName: componentId,
33 type: item.type,
34 rootParentType: item.rootParentType,
35 })
36 }
37 },
38 canDrop: () => canDrop,
39 })
40
41 return { drop, isOver }
42}

Callers 15

EditorFunction · 0.90
InputRightElementPreviewFunction · 0.90
ButtonPreviewFunction · 0.90
BreadcrumbPreviewFunction · 0.90
IconButtonPreviewFunction · 0.90
AlertPreviewFunction · 0.90
BoxPreviewFunction · 0.90
AvatarPreviewFunction · 0.90
AvatarGroupPreviewFunction · 0.90
BreadcrumbItemPreviewFunction · 0.90
InputLeftElementPreviewFunction · 0.90

Calls 1

useDispatchFunction · 0.85

Tested by

no test coverage detected