2023-03-15 08:35:16 +01:00
|
|
|
import { Group, Edge, IdMap, Source, Block, Target } from '@typebot.io/schemas'
|
2021-12-16 10:43:49 +01:00
|
|
|
import {
|
|
|
|
|
createContext,
|
|
|
|
|
Dispatch,
|
|
|
|
|
ReactNode,
|
|
|
|
|
SetStateAction,
|
|
|
|
|
useContext,
|
|
|
|
|
useState,
|
|
|
|
|
} from 'react'
|
|
|
|
|
|
|
|
|
|
export const stubLength = 20
|
|
|
|
|
export const blockWidth = 300
|
|
|
|
|
export const blockAnchorsOffset = {
|
|
|
|
|
left: {
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 20,
|
|
|
|
|
},
|
|
|
|
|
top: {
|
|
|
|
|
x: blockWidth / 2,
|
|
|
|
|
y: 0,
|
|
|
|
|
},
|
|
|
|
|
right: {
|
|
|
|
|
x: blockWidth,
|
|
|
|
|
y: 20,
|
|
|
|
|
},
|
|
|
|
|
}
|
2022-01-12 09:10:59 +01:00
|
|
|
|
2021-12-16 10:43:49 +01:00
|
|
|
export type Coordinates = { x: number; y: number }
|
|
|
|
|
|
|
|
|
|
type Position = Coordinates & { scale: number }
|
|
|
|
|
|
|
|
|
|
export type Anchor = {
|
|
|
|
|
coordinates: Coordinates
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export type Node = Omit<Group, 'blocks'> & {
|
|
|
|
|
blocks: (Block & {
|
2021-12-16 10:43:49 +01:00
|
|
|
sourceAnchorsPosition: { left: Coordinates; right: Coordinates }
|
|
|
|
|
})[]
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 08:47:02 +01:00
|
|
|
export const graphPositionDefaultValue = (
|
|
|
|
|
firstGroupCoordinates: Coordinates
|
|
|
|
|
) => ({
|
|
|
|
|
x: 400 - firstGroupCoordinates.x,
|
|
|
|
|
y: 100 - firstGroupCoordinates.y,
|
|
|
|
|
scale: 1,
|
|
|
|
|
})
|
2021-12-16 10:43:49 +01:00
|
|
|
|
2022-01-12 09:10:59 +01:00
|
|
|
export type ConnectingIds = {
|
2022-01-19 18:54:49 +01:00
|
|
|
source: Source
|
2022-01-06 09:40:56 +01:00
|
|
|
target?: Target
|
2022-01-12 09:10:59 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export type GroupsCoordinates = IdMap<Coordinates>
|
2022-02-02 08:05:02 +01:00
|
|
|
|
2023-02-21 15:25:14 +01:00
|
|
|
type PreviewingBlock = {
|
|
|
|
|
id: string
|
|
|
|
|
groupId: string
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-26 16:12:28 +02:00
|
|
|
const graphContext = createContext<{
|
2021-12-16 10:43:49 +01:00
|
|
|
graphPosition: Position
|
|
|
|
|
setGraphPosition: Dispatch<SetStateAction<Position>>
|
2022-01-12 09:10:59 +01:00
|
|
|
connectingIds: ConnectingIds | null
|
|
|
|
|
setConnectingIds: Dispatch<SetStateAction<ConnectingIds | null>>
|
2023-02-21 15:25:14 +01:00
|
|
|
previewingBlock?: PreviewingBlock
|
|
|
|
|
setPreviewingBlock: Dispatch<SetStateAction<PreviewingBlock | undefined>>
|
2022-02-04 19:00:08 +01:00
|
|
|
previewingEdge?: Edge
|
|
|
|
|
setPreviewingEdge: Dispatch<SetStateAction<Edge | undefined>>
|
2022-06-11 07:27:38 +02:00
|
|
|
openedBlockId?: string
|
|
|
|
|
setOpenedBlockId: Dispatch<SetStateAction<string | undefined>>
|
2022-11-16 14:56:09 +01:00
|
|
|
openedItemId?: string
|
|
|
|
|
setOpenedItemId: Dispatch<SetStateAction<string | undefined>>
|
2022-02-02 08:05:02 +01:00
|
|
|
isReadOnly: boolean
|
2022-06-11 07:27:38 +02:00
|
|
|
focusedGroupId?: string
|
|
|
|
|
setFocusedGroupId: Dispatch<SetStateAction<string | undefined>>
|
2022-12-22 17:02:34 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2021-12-22 14:59:07 +01:00
|
|
|
//@ts-ignore
|
2021-12-16 10:43:49 +01:00
|
|
|
}>({
|
2022-12-16 08:47:02 +01:00
|
|
|
graphPosition: graphPositionDefaultValue({ x: 0, y: 0 }),
|
2021-12-16 10:43:49 +01:00
|
|
|
connectingIds: null,
|
|
|
|
|
})
|
|
|
|
|
|
2022-02-02 08:05:02 +01:00
|
|
|
export const GraphProvider = ({
|
|
|
|
|
children,
|
|
|
|
|
isReadOnly = false,
|
|
|
|
|
}: {
|
|
|
|
|
children: ReactNode
|
|
|
|
|
isReadOnly?: boolean
|
|
|
|
|
}) => {
|
2022-12-16 08:47:02 +01:00
|
|
|
const [graphPosition, setGraphPosition] = useState(
|
|
|
|
|
graphPositionDefaultValue({ x: 0, y: 0 })
|
|
|
|
|
)
|
2022-01-12 09:10:59 +01:00
|
|
|
const [connectingIds, setConnectingIds] = useState<ConnectingIds | null>(null)
|
2022-02-04 19:00:08 +01:00
|
|
|
const [previewingEdge, setPreviewingEdge] = useState<Edge>()
|
2023-02-21 15:25:14 +01:00
|
|
|
const [previewingBlock, setPreviewingBlock] = useState<PreviewingBlock>()
|
2022-06-11 07:27:38 +02:00
|
|
|
const [openedBlockId, setOpenedBlockId] = useState<string>()
|
2022-11-16 14:56:09 +01:00
|
|
|
const [openedItemId, setOpenedItemId] = useState<string>()
|
2022-06-11 07:27:38 +02:00
|
|
|
const [focusedGroupId, setFocusedGroupId] = useState<string>()
|
2022-03-29 10:23:45 +02:00
|
|
|
|
2021-12-16 10:43:49 +01:00
|
|
|
return (
|
|
|
|
|
<graphContext.Provider
|
|
|
|
|
value={{
|
|
|
|
|
graphPosition,
|
|
|
|
|
setGraphPosition,
|
|
|
|
|
connectingIds,
|
|
|
|
|
setConnectingIds,
|
2022-02-04 19:00:08 +01:00
|
|
|
previewingEdge,
|
|
|
|
|
setPreviewingEdge,
|
2022-06-11 07:27:38 +02:00
|
|
|
openedBlockId,
|
|
|
|
|
setOpenedBlockId,
|
2022-11-16 14:56:09 +01:00
|
|
|
openedItemId,
|
|
|
|
|
setOpenedItemId,
|
2022-02-02 08:05:02 +01:00
|
|
|
isReadOnly,
|
2022-06-11 07:27:38 +02:00
|
|
|
focusedGroupId,
|
|
|
|
|
setFocusedGroupId,
|
2023-02-21 15:25:14 +01:00
|
|
|
setPreviewingBlock,
|
|
|
|
|
previewingBlock,
|
2021-12-16 10:43:49 +01:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</graphContext.Provider>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useGraph = () => useContext(graphContext)
|