Skip to content

Commit

Permalink
♻️ Use at function instead of dangerous direct lookup array index
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 4, 2024
1 parent a043c3e commit 4ca613e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export const PictureChoiceItemNode = ({
}
useEventListener('wheel', handleMouseWheel, ref.current)

const blockId = typebot
? typebot.groups.at(indices.groupIndex)?.blocks?.at(indices.blockIndex)?.id
: undefined

return (
<Popover
placement="right"
Expand Down Expand Up @@ -132,15 +136,12 @@ export const PictureChoiceItemNode = ({
shadow="lg"
ref={ref}
>
{typebot && (
{typebot && blockId && (
<PictureChoiceItemSettings
workspaceId={typebot.workspaceId}
typebotId={typebot.id}
item={item}
blockId={
typebot.groups[indices.groupIndex].blocks[indices.blockIndex]
.id
}
blockId={blockId}
onItemChange={handleItemChange}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const BlockNode = ({
previewingEdge?.to.blockId === block.id ||
previewingBlock?.id === block.id

const groupId = typebot?.groups[indices.groupIndex].id
const groupId = typebot?.groups.at(indices.groupIndex)?.id

const isDraggingGraph = useGroupsStore((state) => state.isDraggingGraph)

Expand Down Expand Up @@ -240,11 +240,13 @@ export const BlockNode = ({
transition="border-color 0.2s"
>
<BlockIcon type={block.type} mt=".25rem" />
{typebot?.groups[indices.groupIndex].id && (
{typebot?.groups.at(indices.groupIndex)?.id && (
<BlockNodeContent
block={block}
indices={indices}
groupId={typebot.groups[indices.groupIndex].id}
groupId={
typebot.groups.at(indices.groupIndex)?.id as string
}
/>
)}
{(hasIcomingEdge || isDefined(connectingIds)) && (
Expand Down
32 changes: 18 additions & 14 deletions apps/builder/src/features/graph/components/nodes/item/ItemNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export const ItemNode = ({
const handleMouseEnter = () => setIsMouseOver(true)
const handleMouseLeave = () => setIsMouseOver(false)

const groupId = typebot?.groups.at(indices.groupIndex)?.id

return (
<ContextMenu<HTMLDivElement>
renderMenu={() => <ItemNodeContextMenu indices={indices} />}
Expand Down Expand Up @@ -114,20 +116,22 @@ export const ItemNode = ({
isMouseOver={isMouseOver}
indices={indices}
/>
{typebot && (isConnectable || pathname.endsWith('analytics')) && (
<BlockSourceEndpoint
source={{
blockId: block.id,
itemId: item.id,
}}
groupId={typebot.groups[indices.groupIndex].id}
pos="absolute"
right="-49px"
bottom="9px"
pointerEvents="all"
isHidden={!isConnectable}
/>
)}
{typebot &&
(isConnectable || pathname.endsWith('analytics')) &&
groupId && (
<BlockSourceEndpoint
source={{
blockId: block.id,
itemId: item.id,
}}
groupId={groupId}
pos="absolute"
right="-49px"
bottom="9px"
pointerEvents="all"
isHidden={!isConnectable}
/>
)}
</Flex>
</Stack>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const ItemNodesList = ({

const isLastBlock =
isDefined(typebot) &&
typebot.groups[groupIndex]?.blocks?.[blockIndex + 1] === undefined
typebot.groups.at(groupIndex)?.blocks?.at(blockIndex + 1) === undefined

const someChoiceItemsAreNotConnected =
block.type === InputBlockType.CHOICE ||
Expand Down Expand Up @@ -131,6 +131,8 @@ export const ItemNodesList = ({
elem && (placeholderRefs.current[idx] = elem)
}

const groupId = typebot?.groups.at(groupIndex)?.id

return (
<Stack flex={1} spacing={1} maxW="full" onClick={stopPropagating}>
<PlaceholderNode
Expand All @@ -153,11 +155,8 @@ export const ItemNodesList = ({
/>
</Stack>
))}
{isLastBlock && someChoiceItemsAreNotConnected && (
<DefaultItemNode
block={block}
groupId={typebot.groups[groupIndex].id}
/>
{isLastBlock && someChoiceItemsAreNotConnected && groupId && (
<DefaultItemNode block={block} groupId={groupId} />
)}

{draggedItem && draggedItem.blockId === block.id && (
Expand Down
2 changes: 1 addition & 1 deletion packages/bot-engine/getFirstEdgeId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export const getFirstEdgeId = ({
return event.outgoingEdgeId
}
if (typebot.version === '6') return typebot.events[0].outgoingEdgeId
return typebot.groups[0].blocks[0].outgoingEdgeId
return typebot.groups.at(0)?.blocks.at(0)?.outgoingEdgeId
}
4 changes: 2 additions & 2 deletions packages/lib/getBlockById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export const getBlockById = (
for (let groupIndex = 0; groupIndex < groups.length; groupIndex++) {
for (
let blockIndex = 0;
blockIndex < groups[groupIndex].blocks.length;
blockIndex < (groups.at(groupIndex)?.blocks?.length ?? 0);
blockIndex++
) {
if (groups[groupIndex].blocks[blockIndex].id === blockId) {
if (groups.at(groupIndex)?.blocks?.at(blockIndex)?.id === blockId) {
return {
block: groups[groupIndex].blocks[blockIndex],
group: groups[groupIndex],
Expand Down

0 comments on commit 4ca613e

Please sign in to comment.