2022-03-01 07:13:09 +01:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
HStack,
|
|
|
|
IconButton,
|
|
|
|
Stack,
|
|
|
|
Tooltip,
|
|
|
|
Text,
|
|
|
|
Menu,
|
|
|
|
MenuButton,
|
|
|
|
MenuList,
|
|
|
|
MenuItem,
|
2022-06-12 19:16:35 +02:00
|
|
|
useDisclosure,
|
2022-06-18 08:45:51 +02:00
|
|
|
ButtonProps,
|
2023-02-21 15:25:14 +01:00
|
|
|
useColorModeValue,
|
2022-03-01 07:13:09 +01:00
|
|
|
} from '@chakra-ui/react'
|
2022-10-06 08:33:46 +02:00
|
|
|
import {
|
|
|
|
ChevronLeftIcon,
|
|
|
|
CloudOffIcon,
|
|
|
|
LockedIcon,
|
|
|
|
UnlockedIcon,
|
2022-11-15 09:35:48 +01:00
|
|
|
} from '@/components/icons'
|
|
|
|
import { useTypebot } from '@/features/editor'
|
|
|
|
import { useWorkspace } from '@/features/workspace'
|
2022-06-12 19:16:35 +02:00
|
|
|
import { InputBlockType } from 'models'
|
2022-03-23 09:56:39 +01:00
|
|
|
import { useRouter } from 'next/router'
|
2022-03-01 07:13:09 +01:00
|
|
|
import { isNotDefined } from 'utils'
|
2022-11-15 09:35:48 +01:00
|
|
|
import { ChangePlanModal, isFreePlan, LimitReached } from '@/features/billing'
|
|
|
|
import { timeSince } from '@/utils/helpers'
|
2021-12-22 14:59:07 +01:00
|
|
|
|
2022-06-18 08:45:51 +02:00
|
|
|
export const PublishButton = (props: ButtonProps) => {
|
2023-02-21 15:25:14 +01:00
|
|
|
const warningTextColor = useColorModeValue('red.300', 'red.600')
|
2022-06-12 19:16:35 +02:00
|
|
|
const { workspace } = useWorkspace()
|
2022-03-23 09:56:39 +01:00
|
|
|
const { push, query } = useRouter()
|
2022-06-12 19:16:35 +02:00
|
|
|
const { isOpen, onOpen, onClose } = useDisclosure()
|
2022-03-01 07:13:09 +01:00
|
|
|
const {
|
|
|
|
isPublishing,
|
|
|
|
isPublished,
|
|
|
|
publishTypebot,
|
|
|
|
publishedTypebot,
|
|
|
|
restorePublishedTypebot,
|
2022-06-12 19:16:35 +02:00
|
|
|
typebot,
|
2022-06-13 08:21:48 +02:00
|
|
|
isSavingLoading,
|
2022-10-06 08:33:46 +02:00
|
|
|
updateTypebot,
|
|
|
|
unpublishTypebot,
|
|
|
|
save,
|
2022-03-01 07:13:09 +01:00
|
|
|
} = useTypebot()
|
2021-12-23 16:31:56 +01:00
|
|
|
|
2022-06-12 19:16:35 +02:00
|
|
|
const hasInputFile = typebot?.groups
|
|
|
|
.flatMap((g) => g.blocks)
|
|
|
|
.some((b) => b.type === InputBlockType.FILE)
|
|
|
|
|
2022-03-23 09:56:39 +01:00
|
|
|
const handlePublishClick = () => {
|
2022-06-12 19:16:35 +02:00
|
|
|
if (isFreePlan(workspace) && hasInputFile) return onOpen()
|
2022-03-23 09:56:39 +01:00
|
|
|
publishTypebot()
|
|
|
|
if (!publishedTypebot) push(`/typebots/${query.typebotId}/share`)
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:33:46 +02:00
|
|
|
const closeTypebot = async () => {
|
|
|
|
updateTypebot({ isClosed: true })
|
|
|
|
await save()
|
|
|
|
}
|
|
|
|
|
|
|
|
const openTypebot = async () => {
|
|
|
|
updateTypebot({ isClosed: false })
|
|
|
|
await save()
|
|
|
|
}
|
|
|
|
|
2021-12-22 14:59:07 +01:00
|
|
|
return (
|
2022-03-01 07:13:09 +01:00
|
|
|
<HStack spacing="1px">
|
2022-09-17 16:37:33 +02:00
|
|
|
<ChangePlanModal
|
2022-06-12 19:16:35 +02:00
|
|
|
isOpen={isOpen}
|
|
|
|
onClose={onClose}
|
|
|
|
type={LimitReached.FILE_INPUT}
|
|
|
|
/>
|
2022-03-01 07:13:09 +01:00
|
|
|
<Tooltip
|
|
|
|
placement="bottom-end"
|
|
|
|
label={
|
|
|
|
<Stack>
|
2023-02-21 15:25:14 +01:00
|
|
|
{!publishedTypebot?.version ? (
|
|
|
|
<Text color={warningTextColor} fontWeight="semibold">
|
|
|
|
This will deploy your bot with an updated engine. Make sure to
|
|
|
|
test it properly in preview mode before publishing.
|
|
|
|
</Text>
|
|
|
|
) : (
|
|
|
|
<Text>There are non published changes.</Text>
|
|
|
|
)}
|
2022-03-01 07:13:09 +01:00
|
|
|
<Text fontStyle="italic">
|
|
|
|
Published version from{' '}
|
|
|
|
{publishedTypebot &&
|
|
|
|
timeSince(publishedTypebot.updatedAt.toString())}{' '}
|
|
|
|
ago
|
|
|
|
</Text>
|
|
|
|
</Stack>
|
|
|
|
}
|
2022-10-06 08:33:46 +02:00
|
|
|
isDisabled={isNotDefined(publishedTypebot) || isPublished}
|
2022-03-01 07:13:09 +01:00
|
|
|
>
|
|
|
|
<Button
|
|
|
|
colorScheme="blue"
|
2022-06-13 08:21:48 +02:00
|
|
|
isLoading={isPublishing || isSavingLoading}
|
2022-03-01 07:13:09 +01:00
|
|
|
isDisabled={isPublished}
|
2022-03-23 09:56:39 +01:00
|
|
|
onClick={handlePublishClick}
|
2022-10-06 08:33:46 +02:00
|
|
|
borderRightRadius={publishedTypebot ? 0 : undefined}
|
2022-06-18 08:45:51 +02:00
|
|
|
{...props}
|
2022-03-01 07:13:09 +01:00
|
|
|
>
|
2022-10-06 08:33:46 +02:00
|
|
|
{isPublished
|
|
|
|
? typebot?.isClosed
|
|
|
|
? 'Closed'
|
|
|
|
: 'Published'
|
|
|
|
: 'Publish'}
|
2022-03-01 07:13:09 +01:00
|
|
|
</Button>
|
|
|
|
</Tooltip>
|
|
|
|
|
2022-10-06 08:33:46 +02:00
|
|
|
{publishedTypebot && (
|
2022-03-01 07:13:09 +01:00
|
|
|
<Menu>
|
|
|
|
<MenuButton
|
|
|
|
as={IconButton}
|
2022-10-06 08:33:46 +02:00
|
|
|
colorScheme={'blue'}
|
2022-03-01 07:13:09 +01:00
|
|
|
borderLeftRadius={0}
|
|
|
|
icon={<ChevronLeftIcon transform="rotate(-90deg)" />}
|
2022-10-06 08:33:46 +02:00
|
|
|
aria-label="Show published typebot menu"
|
2022-06-20 11:17:45 +02:00
|
|
|
size="sm"
|
2022-10-06 08:33:46 +02:00
|
|
|
isDisabled={isPublishing || isSavingLoading}
|
2022-03-01 07:13:09 +01:00
|
|
|
/>
|
|
|
|
<MenuList>
|
2022-10-06 08:33:46 +02:00
|
|
|
{!isPublished && (
|
|
|
|
<MenuItem onClick={restorePublishedTypebot}>
|
|
|
|
Restore published version
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
|
|
|
{!typebot?.isClosed ? (
|
|
|
|
<MenuItem onClick={closeTypebot} icon={<LockedIcon />}>
|
|
|
|
Close typebot to new responses
|
|
|
|
</MenuItem>
|
|
|
|
) : (
|
|
|
|
<MenuItem onClick={openTypebot} icon={<UnlockedIcon />}>
|
|
|
|
Reopen typebot to new responses
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
|
|
|
<MenuItem onClick={unpublishTypebot} icon={<CloudOffIcon />}>
|
|
|
|
Unpublish typebot
|
2022-03-01 07:13:09 +01:00
|
|
|
</MenuItem>
|
|
|
|
</MenuList>
|
|
|
|
</Menu>
|
|
|
|
)}
|
|
|
|
</HStack>
|
2021-12-22 14:59:07 +01:00
|
|
|
)
|
|
|
|
}
|