2022-01-12 09:32:16 +01:00
|
|
|
import {
|
|
|
|
IconButton,
|
|
|
|
Menu,
|
|
|
|
MenuButton,
|
|
|
|
MenuButtonProps,
|
|
|
|
MenuItem,
|
|
|
|
MenuList,
|
2022-04-08 14:30:46 -05:00
|
|
|
useDisclosure,
|
2022-01-12 09:32:16 +01:00
|
|
|
} from '@chakra-ui/react'
|
2022-02-04 19:00:08 +01:00
|
|
|
import assert from 'assert'
|
2022-04-08 14:30:46 -05:00
|
|
|
import { DownloadIcon, MoreVerticalIcon, SettingsIcon } from 'assets/icons'
|
2022-01-12 09:32:16 +01:00
|
|
|
import { useTypebot } from 'contexts/TypebotContext'
|
2022-04-08 15:10:21 -05:00
|
|
|
import { useUser } from 'contexts/UserContext'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-01-12 09:32:16 +01:00
|
|
|
import { parseDefaultPublicId } from 'services/typebots'
|
2022-04-08 15:10:21 -05:00
|
|
|
import { isNotDefined } from 'utils'
|
2022-04-08 14:30:46 -05:00
|
|
|
import { EditorSettingsModal } from './EditorSettingsModal'
|
2022-01-12 09:32:16 +01:00
|
|
|
|
|
|
|
export const BoardMenuButton = (props: MenuButtonProps) => {
|
2022-04-08 15:10:21 -05:00
|
|
|
const { query } = useRouter()
|
2022-01-12 09:32:16 +01:00
|
|
|
const { typebot } = useTypebot()
|
2022-04-08 15:10:21 -05:00
|
|
|
const { user } = useUser()
|
2022-01-12 09:32:16 +01:00
|
|
|
const [isDownloading, setIsDownloading] = useState(false)
|
2022-04-08 14:30:46 -05:00
|
|
|
const { isOpen, onOpen, onClose } = useDisclosure()
|
2022-01-12 09:32:16 +01:00
|
|
|
|
2022-04-08 15:10:21 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
user &&
|
|
|
|
isNotDefined(user.graphNavigation) &&
|
|
|
|
isNotDefined(query.isFirstBot)
|
|
|
|
)
|
|
|
|
onOpen()
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [])
|
|
|
|
|
2022-01-12 09:32:16 +01:00
|
|
|
const downloadFlow = () => {
|
2022-02-04 19:00:08 +01:00
|
|
|
assert(typebot)
|
2022-01-12 09:32:16 +01:00
|
|
|
setIsDownloading(true)
|
|
|
|
const data =
|
|
|
|
'data:application/json;charset=utf-8,' +
|
|
|
|
encodeURIComponent(JSON.stringify(typebot))
|
|
|
|
const fileName = `typebot-export-${parseDefaultPublicId(
|
|
|
|
typebot.name,
|
|
|
|
typebot.id
|
|
|
|
)}.json`
|
|
|
|
const linkElement = document.createElement('a')
|
|
|
|
linkElement.setAttribute('href', data)
|
|
|
|
linkElement.setAttribute('download', fileName)
|
|
|
|
linkElement.click()
|
|
|
|
setIsDownloading(false)
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Menu>
|
|
|
|
<MenuButton
|
|
|
|
as={IconButton}
|
2022-04-08 14:30:46 -05:00
|
|
|
bgColor="white"
|
2022-01-12 09:32:16 +01:00
|
|
|
icon={<MoreVerticalIcon transform={'rotate(90deg)'} />}
|
|
|
|
isLoading={isDownloading}
|
2022-02-04 19:00:08 +01:00
|
|
|
size="sm"
|
2022-04-08 14:30:46 -05:00
|
|
|
shadow="lg"
|
2022-01-12 09:32:16 +01:00
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
<MenuList>
|
2022-04-08 14:30:46 -05:00
|
|
|
<MenuItem icon={<SettingsIcon />} onClick={onOpen}>
|
|
|
|
Editor settings
|
|
|
|
</MenuItem>
|
2022-01-12 09:32:16 +01:00
|
|
|
<MenuItem icon={<DownloadIcon />} onClick={downloadFlow}>
|
|
|
|
Export flow
|
|
|
|
</MenuItem>
|
|
|
|
</MenuList>
|
2022-04-08 14:30:46 -05:00
|
|
|
<EditorSettingsModal isOpen={isOpen} onClose={onClose} />
|
2022-01-12 09:32:16 +01:00
|
|
|
</Menu>
|
|
|
|
)
|
|
|
|
}
|