🖐️ Analytics drop off rates
This commit is contained in:
79
apps/builder/contexts/AnalyticsGraphProvider.tsx
Normal file
79
apps/builder/contexts/AnalyticsGraphProvider.tsx
Normal file
@ -0,0 +1,79 @@
|
||||
import { Block, PublicTypebot } from 'bot-engine'
|
||||
import {
|
||||
createContext,
|
||||
Dispatch,
|
||||
ReactNode,
|
||||
SetStateAction,
|
||||
useContext,
|
||||
useState,
|
||||
} from 'react'
|
||||
import { Coordinates } from './GraphContext'
|
||||
|
||||
type Position = Coordinates & { scale: number }
|
||||
|
||||
const graphPositionDefaultValue = { x: 400, y: 100, scale: 1 }
|
||||
|
||||
const graphContext = createContext<{
|
||||
typebot?: PublicTypebot
|
||||
updateBlockPosition: (blockId: string, newPositon: Coordinates) => void
|
||||
graphPosition: Position
|
||||
setGraphPosition: Dispatch<SetStateAction<Position>>
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
//@ts-ignore
|
||||
}>({
|
||||
graphPosition: graphPositionDefaultValue,
|
||||
})
|
||||
|
||||
export const AnalyticsGraphProvider = ({
|
||||
children,
|
||||
initialTypebot,
|
||||
}: {
|
||||
children: ReactNode
|
||||
initialTypebot: PublicTypebot
|
||||
}) => {
|
||||
const [typebot, setTypebot] = useState(initialTypebot)
|
||||
|
||||
const [graphPosition, setGraphPosition] = useState(graphPositionDefaultValue)
|
||||
|
||||
const updateBlocks = (blocks: Block[]) => {
|
||||
if (!typebot) return
|
||||
setTypebot({
|
||||
...typebot,
|
||||
blocks: [...blocks],
|
||||
})
|
||||
}
|
||||
|
||||
const updateBlockPosition = (blockId: string, newPosition: Coordinates) => {
|
||||
if (!typebot) return
|
||||
blockId === 'start-block'
|
||||
? setTypebot({
|
||||
...typebot,
|
||||
startBlock: {
|
||||
...typebot.startBlock,
|
||||
graphCoordinates: newPosition,
|
||||
},
|
||||
})
|
||||
: updateBlocks(
|
||||
typebot.blocks.map((block) =>
|
||||
block.id === blockId
|
||||
? { ...block, graphCoordinates: newPosition }
|
||||
: block
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<graphContext.Provider
|
||||
value={{
|
||||
typebot,
|
||||
graphPosition,
|
||||
setGraphPosition,
|
||||
updateBlockPosition,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</graphContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const useAnalyticsGraph = () => useContext(graphContext)
|
@ -323,9 +323,15 @@ export const TypebotContext = ({
|
||||
|
||||
const publishTypebot = async () => {
|
||||
if (!localTypebot) return
|
||||
if (!localPublishedTypebot)
|
||||
updatePublicId(parseDefaultPublicId(localTypebot.name, localTypebot.id))
|
||||
if (hasUnsavedChanges) await saveTypebot()
|
||||
if (!localPublishedTypebot) {
|
||||
const newPublicId = parseDefaultPublicId(
|
||||
localTypebot.name,
|
||||
localTypebot.id
|
||||
)
|
||||
updatePublicId(newPublicId)
|
||||
localTypebot.publicId = newPublicId
|
||||
}
|
||||
if (hasUnsavedChanges || !localPublishedTypebot) await saveTypebot()
|
||||
setIsPublishing(true)
|
||||
if (localPublishedTypebot) {
|
||||
const { error } = await updatePublishedTypebot(
|
||||
|
Reference in New Issue
Block a user