2022-11-15 09:35:48 +01:00
|
|
|
import { Input, SmartNumberInput } from '@/components/inputs'
|
2022-03-23 12:01:35 +01:00
|
|
|
import { HStack, Stack, Text } from '@chakra-ui/react'
|
|
|
|
import { EmbedBubbleContent } from 'models'
|
|
|
|
import { sanitizeUrl } from 'utils'
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
content: EmbedBubbleContent
|
|
|
|
onSubmit: (content: EmbedBubbleContent) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const EmbedUploadContent = ({ content, onSubmit }: Props) => {
|
|
|
|
const handleUrlChange = (url: string) => {
|
2022-03-24 10:12:49 +01:00
|
|
|
const iframeUrl = sanitizeUrl(
|
2022-03-23 12:01:35 +01:00
|
|
|
url.trim().startsWith('<iframe') ? extractUrlFromIframe(url) : url
|
|
|
|
)
|
|
|
|
onSubmit({ ...content, url: iframeUrl })
|
|
|
|
}
|
|
|
|
|
2023-02-19 09:53:57 +01:00
|
|
|
const handleHeightChange = (height?: EmbedBubbleContent['height']) =>
|
2022-03-23 12:01:35 +01:00
|
|
|
height && onSubmit({ ...content, height })
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Stack p="2" spacing={6}>
|
|
|
|
<Stack>
|
|
|
|
<Input
|
|
|
|
placeholder="Paste the link or code..."
|
|
|
|
defaultValue={content?.url ?? ''}
|
|
|
|
onChange={handleUrlChange}
|
|
|
|
/>
|
|
|
|
<Text fontSize="sm" color="gray.400" textAlign="center">
|
|
|
|
Works with PDFs, iframes, websites...
|
|
|
|
</Text>
|
|
|
|
</Stack>
|
|
|
|
|
2023-02-19 09:53:57 +01:00
|
|
|
<HStack>
|
2022-03-23 12:01:35 +01:00
|
|
|
<SmartNumberInput
|
2023-02-19 09:53:57 +01:00
|
|
|
label="Height:"
|
|
|
|
defaultValue={content?.height}
|
2022-03-23 12:01:35 +01:00
|
|
|
onValueChange={handleHeightChange}
|
|
|
|
/>
|
2023-02-19 09:53:57 +01:00
|
|
|
<Text>px</Text>
|
2022-03-23 12:01:35 +01:00
|
|
|
</HStack>
|
|
|
|
</Stack>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const extractUrlFromIframe = (iframe: string) =>
|
|
|
|
[...iframe.matchAll(/src="([^"]+)"/g)][0][1]
|