-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚸 (condition) Enable multiple condition items in one block
Closes #162
- Loading branch information
1 parent
96eb77d
commit 6725c17
Showing
24 changed files
with
327 additions
and
216 deletions.
There are no files selected for viewing
File renamed without changes.
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
3 changes: 3 additions & 0 deletions
3
apps/builder/src/features/blocks/inputs/buttons/components/index.ts
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,3 @@ | ||
export * from './ButtonsItemNode' | ||
export * from './ButtonsIcon' | ||
export * from './ButtonsOptionsForm' |
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
export { ButtonsOptionsForm } from './components/ButtonsOptionsForm' | ||
export { ButtonNodeContent } from './components/ButtonNodeContent' | ||
export { ButtonsInputIcon } from './components/ButtonsInputIcon' | ||
export * from './components' |
File renamed without changes.
20 changes: 5 additions & 15 deletions
20
...tionSettingsBody/ConditonSettingsBody.tsx → ...s/ConditionItemForm/ConditionItemForm.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
1 change: 1 addition & 0 deletions
1
apps/builder/src/features/blocks/logic/condition/components/ConditionItemForm/index.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 @@ | ||
export * from './ConditionItemForm' |
180 changes: 180 additions & 0 deletions
180
apps/builder/src/features/blocks/logic/condition/components/ConditionItemNode.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,180 @@ | ||
import { | ||
Stack, | ||
Tag, | ||
Text, | ||
Flex, | ||
Wrap, | ||
Fade, | ||
IconButton, | ||
PopoverTrigger, | ||
Popover, | ||
Portal, | ||
PopoverContent, | ||
PopoverArrow, | ||
PopoverBody, | ||
useEventListener, | ||
} from '@chakra-ui/react' | ||
import { useTypebot } from '@/features/editor' | ||
import { | ||
Comparison, | ||
ConditionItem, | ||
ComparisonOperators, | ||
ItemType, | ||
ItemIndices, | ||
} from 'models' | ||
import React, { useRef } from 'react' | ||
import { byId, isNotDefined } from 'utils' | ||
import { PlusIcon } from '@/components/icons' | ||
import { ConditionItemForm } from './ConditionItemForm' | ||
import { useGraph } from '@/features/graph' | ||
import cuid from 'cuid' | ||
|
||
type Props = { | ||
item: ConditionItem | ||
isMouseOver: boolean | ||
indices: ItemIndices | ||
} | ||
|
||
export const ConditionItemNode = ({ item, isMouseOver, indices }: Props) => { | ||
const { typebot, createItem, updateItem } = useTypebot() | ||
const { openedItemId, setOpenedItemId } = useGraph() | ||
const ref = useRef<HTMLDivElement | null>(null) | ||
|
||
const handleMouseDown = (e: React.MouseEvent) => e.stopPropagation() | ||
|
||
const openPopover = () => { | ||
setOpenedItemId(item.id) | ||
} | ||
|
||
const handleItemChange = (updates: Partial<ConditionItem>) => { | ||
updateItem(indices, { ...item, ...updates }) | ||
} | ||
|
||
const handlePlusClick = (event: React.MouseEvent) => { | ||
event.stopPropagation() | ||
const itemIndex = indices.itemIndex + 1 | ||
const newItemId = cuid() | ||
createItem( | ||
{ | ||
blockId: item.blockId, | ||
type: ItemType.CONDITION, | ||
id: newItemId, | ||
}, | ||
{ ...indices, itemIndex } | ||
) | ||
setOpenedItemId(newItemId) | ||
} | ||
|
||
const handleMouseWheel = (e: WheelEvent) => { | ||
e.stopPropagation() | ||
} | ||
useEventListener('wheel', handleMouseWheel, ref.current) | ||
|
||
return ( | ||
<Popover | ||
placement="left" | ||
isLazy | ||
isOpen={openedItemId === item.id} | ||
closeOnBlur={false} | ||
> | ||
<PopoverTrigger> | ||
<Flex p={3} pos="relative" w="full" onClick={openPopover}> | ||
{item.content.comparisons.length === 0 || | ||
comparisonIsEmpty(item.content.comparisons[0]) ? ( | ||
<Text color={'gray.500'}>Configure...</Text> | ||
) : ( | ||
<Stack maxW="170px"> | ||
{item.content.comparisons.map((comparison, idx) => { | ||
const variable = typebot?.variables.find( | ||
byId(comparison.variableId) | ||
) | ||
return ( | ||
<Wrap key={comparison.id} spacing={1} noOfLines={1}> | ||
{idx > 0 && ( | ||
<Text>{item.content.logicalOperator ?? ''}</Text> | ||
)} | ||
{variable?.name && ( | ||
<Tag bgColor="orange.400" color="white"> | ||
{variable.name} | ||
</Tag> | ||
)} | ||
{comparison.comparisonOperator && ( | ||
<Text> | ||
{parseComparisonOperatorSymbol( | ||
comparison.comparisonOperator | ||
)} | ||
</Text> | ||
)} | ||
{comparison?.value && ( | ||
<Tag bgColor={'gray.200'}> | ||
<Text noOfLines={1}>{comparison.value}</Text> | ||
</Tag> | ||
)} | ||
</Wrap> | ||
) | ||
})} | ||
</Stack> | ||
)} | ||
<Fade | ||
in={isMouseOver} | ||
style={{ | ||
position: 'absolute', | ||
bottom: '-15px', | ||
zIndex: 3, | ||
left: '90px', | ||
}} | ||
unmountOnExit | ||
> | ||
<IconButton | ||
aria-label="Add item" | ||
icon={<PlusIcon />} | ||
size="xs" | ||
shadow="md" | ||
colorScheme="gray" | ||
onClick={handlePlusClick} | ||
/> | ||
</Fade> | ||
</Flex> | ||
</PopoverTrigger> | ||
<Portal> | ||
<PopoverContent pos="relative" onMouseDown={handleMouseDown}> | ||
<PopoverArrow /> | ||
<PopoverBody | ||
py="6" | ||
overflowY="scroll" | ||
maxH="400px" | ||
shadow="lg" | ||
ref={ref} | ||
> | ||
<ConditionItemForm | ||
itemContent={item.content} | ||
onItemChange={handleItemChange} | ||
/> | ||
</PopoverBody> | ||
</PopoverContent> | ||
</Portal> | ||
</Popover> | ||
) | ||
} | ||
|
||
const comparisonIsEmpty = (comparison: Comparison) => | ||
isNotDefined(comparison.comparisonOperator) && | ||
isNotDefined(comparison.value) && | ||
isNotDefined(comparison.variableId) | ||
|
||
const parseComparisonOperatorSymbol = (operator: ComparisonOperators) => { | ||
switch (operator) { | ||
case ComparisonOperators.CONTAINS: | ||
return 'contains' | ||
case ComparisonOperators.EQUAL: | ||
return '=' | ||
case ComparisonOperators.GREATER: | ||
return '>' | ||
case ComparisonOperators.IS_SET: | ||
return 'is set' | ||
case ComparisonOperators.LESS: | ||
return '<' | ||
case ComparisonOperators.NOT_EQUAL: | ||
return '!=' | ||
} | ||
} |
73 changes: 0 additions & 73 deletions
73
apps/builder/src/features/blocks/logic/condition/components/ConditionNodeContent.tsx
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
apps/builder/src/features/blocks/logic/condition/components/ConditionSettingsBody/index.tsx
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { ConditionSettingsBody } from './components/ConditionSettingsBody' | ||
export { ConditionNodeContent } from './components/ConditionNodeContent' | ||
export { ConditionIcon } from './components/ConditionIcon' | ||
export * from './components/ConditionItemNode' | ||
export * from './components/ConditionIcon' |
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
Oops, something went wrong.