2
0
Files
bot/apps/builder/components/share/customDomain/CustomDomainsDropdown.tsx

134 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-02-18 14:57:10 +01:00
import {
Button,
IconButton,
Menu,
MenuButton,
MenuButtonProps,
MenuItem,
MenuList,
Stack,
Text,
useDisclosure,
useToast,
} from '@chakra-ui/react'
import { ChevronLeftIcon, PlusIcon, TrashIcon } from 'assets/icons'
import React, { useState } from 'react'
import { CustomDomainModal } from './CustomDomainModal'
2022-02-24 11:13:19 +01:00
import { deleteCustomDomain, useCustomDomains } from 'services/user'
2022-05-13 15:22:44 -07:00
import { useWorkspace } from 'contexts/WorkspaceContext'
2022-02-18 14:57:10 +01:00
type Props = Omit<MenuButtonProps, 'type'> & {
currentCustomDomain?: string
onCustomDomainSelect: (domain: string) => void
}
export const CustomDomainsDropdown = ({
currentCustomDomain,
onCustomDomainSelect,
...props
}: Props) => {
const [isDeleting, setIsDeleting] = useState('')
const { isOpen, onOpen, onClose } = useDisclosure()
2022-05-13 15:22:44 -07:00
const { workspace } = useWorkspace()
2022-02-18 14:57:10 +01:00
const toast = useToast({
position: 'top-right',
status: 'error',
})
2022-05-13 15:22:44 -07:00
const { customDomains, mutate } = useCustomDomains({
workspaceId: workspace?.id,
onError: (error) =>
toast({ title: error.name, description: error.message }),
})
2022-02-18 14:57:10 +01:00
const handleMenuItemClick = (customDomain: string) => () =>
onCustomDomainSelect(customDomain)
const handleDeleteDomainClick =
(domainName: string) => async (e: React.MouseEvent) => {
2022-05-13 15:22:44 -07:00
if (!workspace) return
2022-02-18 14:57:10 +01:00
e.stopPropagation()
setIsDeleting(domainName)
2022-05-13 15:22:44 -07:00
const { error } = await deleteCustomDomain(workspace.id, domainName)
2022-02-18 14:57:10 +01:00
setIsDeleting('')
if (error) return toast({ title: error.name, description: error.message })
mutate({
customDomains: (customDomains ?? []).filter(
(cd) => cd.name !== domainName
),
})
}
const handleNewDomain = (domain: string) => {
2022-05-13 15:22:44 -07:00
if (!workspace) return
2022-02-18 14:57:10 +01:00
mutate({
customDomains: [
...(customDomains ?? []),
2022-05-13 15:22:44 -07:00
{ name: domain, workspaceId: workspace?.id },
2022-02-18 14:57:10 +01:00
],
})
handleMenuItemClick(domain)()
}
return (
<Menu isLazy placement="bottom-start" matchWidth>
2022-05-13 15:22:44 -07:00
{workspace?.id && (
2022-02-18 14:57:10 +01:00
<CustomDomainModal
2022-05-13 15:22:44 -07:00
workspaceId={workspace.id}
2022-02-18 14:57:10 +01:00
isOpen={isOpen}
onClose={onClose}
onNewDomain={handleNewDomain}
/>
)}
<MenuButton
as={Button}
rightIcon={<ChevronLeftIcon transform={'rotate(-90deg)'} />}
colorScheme="gray"
justifyContent="space-between"
textAlign="left"
{...props}
>
2022-05-13 09:18:25 -07:00
<Text noOfLines={0} overflowY="visible" h="20px">
2022-02-18 14:57:10 +01:00
{currentCustomDomain ?? 'Add my domain'}
</Text>
</MenuButton>
<MenuList maxW="500px" shadow="lg">
<Stack maxH={'35vh'} overflowY="scroll" spacing="0">
{(customDomains ?? []).map((customDomain) => (
<Button
role="menuitem"
minH="40px"
key={customDomain.name}
onClick={handleMenuItemClick(customDomain.name)}
fontSize="16px"
fontWeight="normal"
rounded="none"
colorScheme="gray"
variant="ghost"
justifyContent="space-between"
>
{customDomain.name}
<IconButton
icon={<TrashIcon />}
aria-label="Remove domain"
size="xs"
onClick={handleDeleteDomainClick(customDomain.name)}
isLoading={isDeleting === customDomain.name}
/>
</Button>
))}
<MenuItem
maxW="500px"
overflow="hidden"
whiteSpace="nowrap"
textOverflow="ellipsis"
icon={<PlusIcon />}
onClick={onOpen}
>
Connect new
</MenuItem>
</Stack>
</MenuList>
</Menu>
)
}