2023-02-15 18:05:19 +01:00
|
|
|
import { useTypebot } from '@/features/editor'
|
2023-02-17 16:30:02 +01:00
|
|
|
import { Button, Flex, HStack, Stack, Text } from '@chakra-ui/react'
|
2023-02-15 18:05:19 +01:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
|
import { sendRequest } from 'utils'
|
|
|
|
|
import { PackageIcon } from './icons'
|
|
|
|
|
|
2023-02-17 16:30:02 +01:00
|
|
|
const intervalDuration = 1000 * 60 // 30 seconds
|
2023-02-15 18:05:19 +01:00
|
|
|
|
|
|
|
|
export const NewVersionPopup = () => {
|
2023-02-17 16:30:02 +01:00
|
|
|
const { typebot, save } = useTypebot()
|
2023-02-15 18:05:19 +01:00
|
|
|
const [currentVersion, setCurrentVersion] = useState<string>()
|
|
|
|
|
const [isNewVersionAvailable, setIsNewVersionAvailable] = useState(false)
|
|
|
|
|
const [isReloading, setIsReloading] = useState(false)
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isNewVersionAvailable) return
|
|
|
|
|
let cancelRequest = false
|
|
|
|
|
const interval = setInterval(async () => {
|
|
|
|
|
const { data } = await sendRequest<{
|
|
|
|
|
commitSha: string | undefined
|
|
|
|
|
}>('/api/version')
|
|
|
|
|
if (!data || cancelRequest) return
|
|
|
|
|
if (!currentVersion) {
|
|
|
|
|
setCurrentVersion(data.commitSha)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (currentVersion !== data.commitSha) {
|
|
|
|
|
setIsNewVersionAvailable(true)
|
|
|
|
|
}
|
|
|
|
|
}, intervalDuration)
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelRequest = true
|
|
|
|
|
clearInterval(interval)
|
|
|
|
|
}
|
|
|
|
|
}, [currentVersion, isNewVersionAvailable])
|
|
|
|
|
|
|
|
|
|
const saveAndReload = async () => {
|
|
|
|
|
if (isReloading) return
|
|
|
|
|
setIsReloading(true)
|
|
|
|
|
if (save) await save()
|
|
|
|
|
window.location.reload()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isNewVersionAvailable) return null
|
|
|
|
|
|
|
|
|
|
return (
|
2023-02-17 16:30:02 +01:00
|
|
|
<Stack
|
2023-02-15 18:05:19 +01:00
|
|
|
pos="fixed"
|
2023-02-17 16:30:02 +01:00
|
|
|
bottom="18px"
|
|
|
|
|
left="18px"
|
2023-02-15 18:05:19 +01:00
|
|
|
bgColor="blue.400"
|
|
|
|
|
p="4"
|
|
|
|
|
px="4"
|
2023-02-17 16:30:02 +01:00
|
|
|
rounded="lg"
|
2023-02-15 18:05:19 +01:00
|
|
|
shadow="lg"
|
|
|
|
|
zIndex={10}
|
2023-02-17 16:30:02 +01:00
|
|
|
borderWidth="1px"
|
2023-02-15 18:05:19 +01:00
|
|
|
borderColor="blue.300"
|
2023-02-17 16:30:02 +01:00
|
|
|
maxW="320px"
|
2023-02-15 18:05:19 +01:00
|
|
|
>
|
|
|
|
|
<HStack spacing={3}>
|
2023-02-17 16:30:02 +01:00
|
|
|
<Stack spacing={4}>
|
|
|
|
|
<Stack spacing={1} color="white">
|
|
|
|
|
<HStack>
|
|
|
|
|
<PackageIcon />{' '}
|
|
|
|
|
<Text fontWeight="bold">New version available!</Text>
|
|
|
|
|
</HStack>
|
|
|
|
|
|
|
|
|
|
<Text fontSize="sm" color="gray.100">
|
|
|
|
|
An improved version of Typebot is available. Please reload now to
|
|
|
|
|
upgrade.
|
|
|
|
|
</Text>
|
|
|
|
|
</Stack>
|
|
|
|
|
<Flex justifyContent="flex-end">
|
|
|
|
|
<Button size="sm" onClick={saveAndReload}>
|
|
|
|
|
{typebot?.id ? 'Save and reload' : 'Reload'}
|
|
|
|
|
</Button>
|
|
|
|
|
</Flex>
|
2023-02-15 18:05:19 +01:00
|
|
|
</Stack>
|
|
|
|
|
</HStack>
|
2023-02-17 16:30:02 +01:00
|
|
|
</Stack>
|
2023-02-15 18:05:19 +01:00
|
|
|
)
|
|
|
|
|
}
|