2
0
Files
bot/apps/builder/components/shared/ImageUploadContent/ImageUploadContent.tsx

135 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-08-08 08:21:36 +02:00
import { useState } from 'react'
import { Button, Flex, HStack, Stack, Text } from '@chakra-ui/react'
2022-01-20 16:14:47 +01:00
import { SearchContextManager } from '@giphy/react-components'
import { UploadButton } from '../buttons/UploadButton'
import { GiphySearch } from './GiphySearch'
import { useTypebot } from 'contexts/TypebotContext'
import { Input } from '../Textbox/Input'
import { env, isEmpty } from 'utils'
2022-08-08 08:21:36 +02:00
import { EmojiSearchableList } from './emoji/EmojiSearchableList'
2022-01-20 16:14:47 +01:00
type Props = {
url?: string
isEmojiEnabled?: boolean
isGiphyEnabled?: boolean
onSubmit: (url: string) => void
onClose?: () => void
2022-01-20 16:14:47 +01:00
}
export const ImageUploadContent = ({
url,
onSubmit,
isEmojiEnabled = false,
isGiphyEnabled = true,
onClose,
}: Props) => {
const [currentTab, setCurrentTab] = useState<
'link' | 'upload' | 'giphy' | 'emoji'
>(isEmojiEnabled ? 'emoji' : 'upload')
const handleSubmit = (url: string) => {
onSubmit(url)
onClose && onClose()
}
2022-01-20 16:14:47 +01:00
return (
<Stack>
<HStack>
{isEmojiEnabled && (
<Button
variant={currentTab === 'emoji' ? 'solid' : 'ghost'}
onClick={() => setCurrentTab('emoji')}
size="sm"
>
Emoji
</Button>
)}
2022-01-20 16:14:47 +01:00
<Button
variant={currentTab === 'upload' ? 'solid' : 'ghost'}
onClick={() => setCurrentTab('upload')}
size="sm"
>
Upload
</Button>
<Button
variant={currentTab === 'link' ? 'solid' : 'ghost'}
onClick={() => setCurrentTab('link')}
size="sm"
>
Embed link
</Button>
{isGiphyEnabled && (
2022-01-20 16:14:47 +01:00
<Button
variant={currentTab === 'giphy' ? 'solid' : 'ghost'}
onClick={() => setCurrentTab('giphy')}
size="sm"
>
Giphy
</Button>
)}
</HStack>
<BodyContent tab={currentTab} onSubmit={handleSubmit} url={url} />
2022-01-20 16:14:47 +01:00
</Stack>
)
}
const BodyContent = ({
tab,
url,
onSubmit,
}: {
tab: 'upload' | 'link' | 'giphy' | 'emoji'
2022-01-20 16:14:47 +01:00
url?: string
onSubmit: (url: string) => void
}) => {
switch (tab) {
case 'upload':
return <UploadFileContent onNewUrl={onSubmit} />
case 'link':
return <EmbedLinkContent initialUrl={url} onNewUrl={onSubmit} />
case 'giphy':
return <GiphyContent onNewUrl={onSubmit} />
case 'emoji':
2022-08-08 08:21:36 +02:00
return <EmojiSearchableList onEmojiSelected={onSubmit} />
2022-01-20 16:14:47 +01:00
}
}
type ContentProps = { initialUrl?: string; onNewUrl: (url: string) => void }
const UploadFileContent = ({ onNewUrl }: ContentProps) => {
const { typebot } = useTypebot()
return (
<Flex justify="center" py="2">
2022-01-20 16:14:47 +01:00
<UploadButton
filePath={`public/typebots/${typebot?.id}`}
2022-01-20 16:14:47 +01:00
onFileUploaded={onNewUrl}
includeFileName
colorScheme="blue"
>
Choose an image
</UploadButton>
</Flex>
2022-01-20 16:14:47 +01:00
)
}
const EmbedLinkContent = ({ initialUrl, onNewUrl }: ContentProps) => (
<Stack py="2">
<Input
placeholder={'Paste the image link...'}
onChange={onNewUrl}
defaultValue={initialUrl ?? ''}
/>
</Stack>
)
2022-01-20 16:14:47 +01:00
const GiphyContent = ({ onNewUrl }: ContentProps) => {
if (isEmpty(env('GIPHY_API_KEY')))
return <Text>NEXT_PUBLIC_GIPHY_API_KEY is missing in environment</Text>
return (
<SearchContextManager apiKey={env('GIPHY_API_KEY') as string}>
<GiphySearch onSubmit={onNewUrl} />
</SearchContextManager>
)
}