2
0
Files
bot/apps/builder/components/share/EditableUrl.tsx

81 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-12-23 15:20:21 +01:00
import {
HStack,
Tooltip,
EditablePreview,
EditableInput,
Text,
Editable,
Button,
ButtonProps,
useEditableControls,
} from '@chakra-ui/react'
import { EditIcon } from 'assets/icons'
import { CopyButton } from 'components/shared/buttons/CopyButton'
import { useToast } from 'components/shared/hooks/useToast'
import React, { useState } from 'react'
2021-12-23 15:20:21 +01:00
type EditableUrlProps = {
2022-02-18 14:57:10 +01:00
hostname: string
pathname?: string
onPathnameChange: (pathname: string) => void
2021-12-23 15:20:21 +01:00
}
export const EditableUrl = ({
2022-02-18 14:57:10 +01:00
hostname,
pathname,
onPathnameChange,
2021-12-23 15:20:21 +01:00
}: EditableUrlProps) => {
const { showToast } = useToast()
const [value, setValue] = useState(pathname)
const handleSubmit = (newPathname: string) => {
if (/^[a-z0-9-]*$/.test(newPathname)) return onPathnameChange(newPathname)
setValue(pathname)
showToast({
title: 'Invalid ID',
description: 'Should contain only contain letters, numbers and dashes.',
})
}
2021-12-23 15:20:21 +01:00
return (
<Editable
as={HStack}
spacing={3}
value={value}
onChange={setValue}
onSubmit={handleSubmit}
2021-12-23 15:20:21 +01:00
>
<HStack spacing={1}>
2022-02-18 14:57:10 +01:00
<Text>{hostname}/</Text>
2021-12-23 15:20:21 +01:00
<Tooltip label="Edit">
<EditablePreview
mx={1}
borderWidth="1px"
2021-12-23 15:20:21 +01:00
px={3}
rounded="md"
cursor="text"
2021-12-23 15:20:21 +01:00
display="flex"
fontWeight="semibold"
/>
</Tooltip>
<EditableInput px={2} />
</HStack>
<HStack>
<EditButton size="xs" />
<CopyButton size="xs" textToCopy={`${hostname}/${pathname ?? ''}`} />
2021-12-23 15:20:21 +01:00
</HStack>
</Editable>
)
}
const EditButton = (props: ButtonProps) => {
const { isEditing, getEditButtonProps } = useEditableControls()
return isEditing ? null : (
2021-12-23 15:20:21 +01:00
<Button leftIcon={<EditIcon />} {...props} {...getEditButtonProps()}>
Edit
</Button>
)
}