/* eslint-disable no-useless-escape */
import {Editor} from '@tiptap/react'
import {DOMAIN} from 'common/envs/constants'
import {useState} from 'react'
import toast from 'react-hot-toast'
import {Button} from '../buttons/button'
import {Col} from '../layout/col'
import {Modal} from '../layout/modal'
import {Row} from '../layout/row'
import {Spacer} from '../layout/spacer'
type EmbedPattern = {
// Regex should have a single capture group.
regex: RegExp
rewrite: (text: string) => string
}
const embedPatterns: EmbedPattern[] = [
{
regex: /^https?:\/\/manifold\.markets\/([^\/]+\/[^\/]+)/,
rewrite: (slug) => ``,
},
{
regex: /^https?:\/\/www\.youtube\.com\/watch\?v=([^&]+)/,
rewrite: (id) => ``,
},
// Also rewrite youtube links like `https://youtu.be/IOlKZDgyQRQ`
{
regex: /^https?:\/\/youtu\.be\/([^&]+)/,
rewrite: (id) => ``,
},
// Twitch is a bit annoying, since it requires the `&parent=DOMAIN` to match
{
// Twitch: https://www.twitch.tv/videos/1445087149
regex: /^https?:\/\/www\.twitch\.tv\/videos\/(\d+)/,
rewrite: (id) =>
``,
},
{
// Twitch: https://www.twitch.tv/sirsalty
regex: /^https?:\/\/www\.twitch\.tv\/([^\/]+)/,
rewrite: (channel) =>
``,
},
{
// Tiktok: https://www.tiktok.com/@tiktok/video/6959980000000000001
regex: /^https?:\/\/www\.tiktok\.com\/@[^\/]+\/video\/(\d+)/,
rewrite: (id) => ``,
},
]
function embedCode(text: string) {
for (const pattern of embedPatterns) {
const match = text.trim().match(pattern.regex)
if (match) {
return pattern.rewrite(match[1])
}
}
return null
}
export function EmbedModal(props: {
editor: Editor | null
open: boolean
setOpen: (open: boolean) => void
}) {
const {editor, open, setOpen} = props
const [input, setInput] = useState('')
const embed = embedCode(input)
return (
) => setInput(e.target.value)}
/>
{embed && }
)
}