Skip to content

Commit

Permalink
馃悰 Fix event edges display when navigating in linked typebots
Browse files Browse the repository at this point in the history
Closes #1392
  • Loading branch information
baptisteArno committed Mar 26, 2024
1 parent a48a211 commit f646826
Showing 1 changed file with 20 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ import {
useColorModeValue,
useEventListener,
} from '@chakra-ui/react'
import React, {
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from 'react'
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'
import { useEndpoints } from '../../providers/EndpointsProvider'
import { useGraph } from '../../providers/GraphProvider'
import { TEventSource } from '@typebot.io/schemas'
import { isNotDefined } from '@typebot.io/lib'

const endpointHeight = 32

Expand All @@ -35,23 +28,6 @@ export const EventSourceEndpoint = ({
const [eventTransformProp, setEventTransformProp] = useState<string>()
const ref = useRef<HTMLDivElement | null>(null)

const endpointY = useMemo(
() =>
ref.current
? Number(
(
(ref.current?.getBoundingClientRect().y +
(endpointHeight * graphPosition.scale) / 2 -
graphPosition.y) /
graphPosition.scale
).toFixed(2)
)
: undefined,
// We need to force recompute whenever the event node position changes
// eslint-disable-next-line react-hooks/exhaustive-deps
[graphPosition.scale, graphPosition.y, eventTransformProp]
)

useLayoutEffect(() => {
const mutationObserver = new MutationObserver((entries) => {
setEventTransformProp((entries[0].target as HTMLElement).style.transform)
Expand All @@ -68,12 +44,28 @@ export const EventSourceEndpoint = ({
}, [source.eventId])

useEffect(() => {
if (isNotDefined(endpointY)) return
const y = ref.current
? Number(
(
(ref.current?.getBoundingClientRect().y +
(endpointHeight * graphPosition.scale) / 2 -
graphPosition.y) /
graphPosition.scale
).toFixed(2)
)
: undefined
if (y === undefined) return
setSourceEndpointYOffset?.({
id: source.eventId,
y: endpointY,
y,
})
}, [endpointY, setSourceEndpointYOffset, source.eventId])
}, [
graphPosition.scale,
graphPosition.y,
setSourceEndpointYOffset,
source.eventId,
eventTransformProp,
])

useEffect(
() => () => {
Expand Down

0 comments on commit f646826

Please sign in to comment.