2023-03-15 11:51:30 +01:00
|
|
|
import { Coordinates } from '@dnd-kit/utilities'
|
|
|
|
|
import { Edge } from '@typebot.io/schemas'
|
2021-12-16 10:43:49 +01:00
|
|
|
import {
|
|
|
|
|
createContext,
|
|
|
|
|
Dispatch,
|
|
|
|
|
ReactNode,
|
|
|
|
|
SetStateAction,
|
|
|
|
|
useContext,
|
|
|
|
|
useState,
|
|
|
|
|
} from 'react'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { graphPositionDefaultValue } from '../constants'
|
|
|
|
|
import { ConnectingIds } from '../types'
|
2021-12-16 10:43:49 +01:00
|
|
|
|
|
|
|
|
type Position = Coordinates & { scale: number }
|
|
|
|
|
|
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
|
2024-01-26 10:38:44 +01:00
|
|
|
isAnalytics: 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,
|
2024-01-26 10:38:44 +01:00
|
|
|
isAnalytics = false,
|
2022-02-02 08:05:02 +01:00
|
|
|
}: {
|
|
|
|
|
children: ReactNode
|
|
|
|
|
isReadOnly?: boolean
|
2024-01-26 10:38:44 +01:00
|
|
|
isAnalytics?: boolean
|
2022-02-02 08:05:02 +01:00
|
|
|
}) => {
|
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,
|
2024-01-26 10:38:44 +01:00
|
|
|
isAnalytics,
|
2021-12-16 10:43:49 +01:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</graphContext.Provider>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useGraph = () => useContext(graphContext)
|