import { Editor } from '@tiptap/react' import { useState } from 'react' import { TwitterTweetEmbed } from 'react-twitter-embed' import { Button } from '../button' import { Col } from '../layout/col' import { Modal } from '../layout/modal' import { Row } from '../layout/row' import { Spacer } from '../layout/spacer' // A valid tweet URL looks like 'https://twitter.com/username/status/123456789' // Return the tweetId if the URL is valid, otherwise return null. function getTweetId(text: string) { const match = text.match(/^https?:\/\/twitter\.com\/.*\/status\/(\d+)$/) return match ? match[1] : null } export function TweetModal(props: { editor: Editor | null open: boolean setOpen: (open: boolean) => void }) { const { editor, open, setOpen } = props const [input, setInput] = useState('') const tweetId = getTweetId(input) // Append a leading 't', to prevent tweetId from being interpreted as a number. // If it's a number, there may be numeric precision issues. const tweetCode = `` return ( setInput(e.target.value)} /> {/* Preview the embed if it's valid */} {tweetId ? : } ) }