2022-08-08 08:21:36 +02:00
|
|
|
import { useState } from 'react'
|
2022-08-12 08:34:53 +02:00
|
|
|
import { Button, Flex, HStack, Stack } from '@chakra-ui/react'
|
2022-01-20 16:14:47 +01:00
|
|
|
import { UploadButton } from '../buttons/UploadButton'
|
2022-08-12 08:34:53 +02:00
|
|
|
import { GiphySearchForm } from './GiphySearchForm'
|
2022-05-12 13:05:43 -07:00
|
|
|
import { Input } from '../Textbox/Input'
|
2022-08-08 08:21:36 +02:00
|
|
|
import { EmojiSearchableList } from './emoji/EmojiSearchableList'
|
2022-01-20 16:14:47 +01:00
|
|
|
|
|
|
|
type Props = {
|
2022-11-07 08:25:09 +01:00
|
|
|
filePath: string
|
|
|
|
includeFileName?: boolean
|
|
|
|
defaultUrl?: string
|
2022-04-01 16:28:09 +02:00
|
|
|
isEmojiEnabled?: boolean
|
2022-01-25 18:19:37 +01:00
|
|
|
isGiphyEnabled?: boolean
|
2022-04-01 16:28:09 +02:00
|
|
|
onSubmit: (url: string) => void
|
|
|
|
onClose?: () => void
|
2022-01-20 16:14:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 18:19:37 +01:00
|
|
|
export const ImageUploadContent = ({
|
2022-11-07 08:25:09 +01:00
|
|
|
filePath,
|
|
|
|
includeFileName,
|
|
|
|
defaultUrl,
|
2022-01-25 18:19:37 +01:00
|
|
|
onSubmit,
|
2022-04-01 16:28:09 +02:00
|
|
|
isEmojiEnabled = false,
|
2022-01-25 18:19:37 +01:00
|
|
|
isGiphyEnabled = true,
|
2022-04-01 16:28:09 +02:00
|
|
|
onClose,
|
2022-01-25 18:19:37 +01:00
|
|
|
}: Props) => {
|
2022-04-01 16:28:09 +02:00
|
|
|
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>
|
2022-04-01 16:28:09 +02:00
|
|
|
{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>
|
2022-02-07 07:13:16 +01:00
|
|
|
{isGiphyEnabled && (
|
2022-01-20 16:14:47 +01:00
|
|
|
<Button
|
|
|
|
variant={currentTab === 'giphy' ? 'solid' : 'ghost'}
|
|
|
|
onClick={() => setCurrentTab('giphy')}
|
|
|
|
size="sm"
|
|
|
|
>
|
|
|
|
Giphy
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</HStack>
|
|
|
|
|
2022-11-07 08:25:09 +01:00
|
|
|
<BodyContent
|
|
|
|
filePath={filePath}
|
|
|
|
includeFileName={includeFileName}
|
|
|
|
tab={currentTab}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
defaultUrl={defaultUrl}
|
|
|
|
/>
|
2022-01-20 16:14:47 +01:00
|
|
|
</Stack>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const BodyContent = ({
|
2022-11-07 08:25:09 +01:00
|
|
|
includeFileName,
|
|
|
|
filePath,
|
2022-01-20 16:14:47 +01:00
|
|
|
tab,
|
2022-11-07 08:25:09 +01:00
|
|
|
defaultUrl,
|
2022-01-20 16:14:47 +01:00
|
|
|
onSubmit,
|
|
|
|
}: {
|
2022-11-07 08:25:09 +01:00
|
|
|
includeFileName?: boolean
|
|
|
|
filePath: string
|
2022-04-01 16:28:09 +02:00
|
|
|
tab: 'upload' | 'link' | 'giphy' | 'emoji'
|
2022-11-07 08:25:09 +01:00
|
|
|
defaultUrl?: string
|
2022-01-20 16:14:47 +01:00
|
|
|
onSubmit: (url: string) => void
|
|
|
|
}) => {
|
|
|
|
switch (tab) {
|
|
|
|
case 'upload':
|
2022-11-07 08:25:09 +01:00
|
|
|
return (
|
|
|
|
<UploadFileContent
|
|
|
|
filePath={filePath}
|
|
|
|
includeFileName={includeFileName}
|
|
|
|
onNewUrl={onSubmit}
|
|
|
|
/>
|
|
|
|
)
|
2022-01-20 16:14:47 +01:00
|
|
|
case 'link':
|
2022-11-07 08:25:09 +01:00
|
|
|
return <EmbedLinkContent defaultUrl={defaultUrl} onNewUrl={onSubmit} />
|
2022-01-20 16:14:47 +01:00
|
|
|
case 'giphy':
|
|
|
|
return <GiphyContent onNewUrl={onSubmit} />
|
2022-04-01 16:28:09 +02:00
|
|
|
case 'emoji':
|
2022-08-08 08:21:36 +02:00
|
|
|
return <EmojiSearchableList onEmojiSelected={onSubmit} />
|
2022-01-20 16:14:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 08:25:09 +01:00
|
|
|
type ContentProps = { onNewUrl: (url: string) => void }
|
2022-01-20 16:14:47 +01:00
|
|
|
|
2022-11-07 08:25:09 +01:00
|
|
|
const UploadFileContent = ({
|
|
|
|
filePath,
|
|
|
|
includeFileName,
|
|
|
|
onNewUrl,
|
|
|
|
}: ContentProps & { filePath: string; includeFileName?: boolean }) => (
|
|
|
|
<Flex justify="center" py="2">
|
|
|
|
<UploadButton
|
|
|
|
filePath={filePath}
|
|
|
|
onFileUploaded={onNewUrl}
|
|
|
|
includeFileName={includeFileName}
|
|
|
|
colorScheme="blue"
|
|
|
|
>
|
|
|
|
Choose an image
|
|
|
|
</UploadButton>
|
|
|
|
</Flex>
|
|
|
|
)
|
2022-01-20 16:14:47 +01:00
|
|
|
|
2022-11-07 08:25:09 +01:00
|
|
|
const EmbedLinkContent = ({
|
|
|
|
defaultUrl,
|
|
|
|
onNewUrl,
|
|
|
|
}: ContentProps & { defaultUrl?: string }) => (
|
2022-05-12 13:05:43 -07:00
|
|
|
<Stack py="2">
|
|
|
|
<Input
|
|
|
|
placeholder={'Paste the image link...'}
|
|
|
|
onChange={onNewUrl}
|
2022-11-07 08:25:09 +01:00
|
|
|
defaultValue={defaultUrl ?? ''}
|
2022-05-12 13:05:43 -07:00
|
|
|
/>
|
|
|
|
</Stack>
|
|
|
|
)
|
2022-01-20 16:14:47 +01:00
|
|
|
|
2022-08-12 08:34:53 +02:00
|
|
|
const GiphyContent = ({ onNewUrl }: ContentProps) => (
|
|
|
|
<GiphySearchForm onSubmit={onNewUrl} />
|
|
|
|
)
|