2
0

🐛 Fix event edges display when navigating in linked typebots

Closes #1392
This commit is contained in:
Baptiste Arnaud
2024-03-26 19:27:58 +01:00
parent a48a211509
commit f6468261f5

View File

@ -4,17 +4,10 @@ import {
useColorModeValue, useColorModeValue,
useEventListener, useEventListener,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import React, { import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from 'react'
import { useEndpoints } from '../../providers/EndpointsProvider' import { useEndpoints } from '../../providers/EndpointsProvider'
import { useGraph } from '../../providers/GraphProvider' import { useGraph } from '../../providers/GraphProvider'
import { TEventSource } from '@typebot.io/schemas' import { TEventSource } from '@typebot.io/schemas'
import { isNotDefined } from '@typebot.io/lib'
const endpointHeight = 32 const endpointHeight = 32
@ -35,23 +28,6 @@ export const EventSourceEndpoint = ({
const [eventTransformProp, setEventTransformProp] = useState<string>() const [eventTransformProp, setEventTransformProp] = useState<string>()
const ref = useRef<HTMLDivElement | null>(null) 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(() => { useLayoutEffect(() => {
const mutationObserver = new MutationObserver((entries) => { const mutationObserver = new MutationObserver((entries) => {
setEventTransformProp((entries[0].target as HTMLElement).style.transform) setEventTransformProp((entries[0].target as HTMLElement).style.transform)
@ -68,12 +44,28 @@ export const EventSourceEndpoint = ({
}, [source.eventId]) }, [source.eventId])
useEffect(() => { 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?.({ setSourceEndpointYOffset?.({
id: source.eventId, id: source.eventId,
y: endpointY, y,
}) })
}, [endpointY, setSourceEndpointYOffset, source.eventId]) }, [
graphPosition.scale,
graphPosition.y,
setSourceEndpointYOffset,
source.eventId,
eventTransformProp,
])
useEffect( useEffect(
() => () => { () => () => {