2
0
Files
bot/apps/builder/contexts/BoardContext.tsx
Baptiste Arnaud 9dbad1dbab 🚀 Init bot-engine
2021-12-08 09:33:25 +01:00

60 lines
1.5 KiB
TypeScript

import { BrowserJsPlumbInstance } from '@jsplumb/browser-ui'
import {
createContext,
Dispatch,
ReactNode,
SetStateAction,
useContext,
useState,
} from 'react'
import { Step } from 'bot-engine'
type Position = { x: number; y: number; scale: number }
const graphPositionDefaultValue = { x: 400, y: 100, scale: 1 }
const graphContext = createContext<{
position: Position
setGraphPosition: Dispatch<SetStateAction<Position>>
plumbInstance?: BrowserJsPlumbInstance
setPlumbInstance: Dispatch<SetStateAction<BrowserJsPlumbInstance | undefined>>
draggedStep?: Step
setDraggedStep: Dispatch<SetStateAction<Step | undefined>>
}>({
position: graphPositionDefaultValue,
setGraphPosition: () => {
console.log("I'm not instantiated")
},
setPlumbInstance: () => {
console.log("I'm not instantiated")
},
setDraggedStep: () => {
console.log("I'm not instantiated")
},
})
export const GraphProvider = ({ children }: { children: ReactNode }) => {
const [graphPosition, setGraphPosition] = useState(graphPositionDefaultValue)
const [plumbInstance, setPlumbInstance] = useState<
BrowserJsPlumbInstance | undefined
>()
const [draggedStep, setDraggedStep] = useState<Step | undefined>()
return (
<graphContext.Provider
value={{
position: graphPosition,
setGraphPosition,
plumbInstance,
setPlumbInstance,
draggedStep,
setDraggedStep,
}}
>
{children}
</graphContext.Provider>
)
}
export const useGraph = () => useContext(graphContext)