import { EMBED_DEFINITIONS, EmbedDefinition, track, useEditor } from '@bigbluebutton/editor' import { useRef, useState } from 'react' import { TLEmbedResult, getEmbedInfo } from '../../utils/embeds/embeds' import { useAssetUrls } from '../hooks/useAssetUrls' import { TLUiDialogProps } from '../hooks/useDialogsProvider' import { untranslated, useTranslation } from '../hooks/useTranslation/useTranslation' import { Button } from './primitives/Button' import * as Dialog from './primitives/Dialog' import { Icon } from './primitives/Icon' import { Input } from './primitives/Input' export const EmbedDialog = track(function EmbedDialog({ onClose }: TLUiDialogProps) { const editor = useEditor() const msg = useTranslation() const assetUrls = useAssetUrls() // The embed definition for the user's selected embed (set by the user clicking on an embed in stage 1) const [embedDefinition, setEmbedDefinition] = useState(null) // The URL that the user has typed into (in stage 2) const [url, setUrl] = useState('') // The embed info for the user's selected embed (based on the URL they've entered in stage 2) const [embedInfoForUrl, setEmbedInfoForUrl] = useState(null) // Should we show the "invalid URL" error message? const [showError, setShowError] = useState(false) const rShowErrorTimeout = useRef(-1) return ( <> {embedDefinition ? `${msg('embed-dialog.title')} — ${embedDefinition.title}` : msg('embed-dialog.title')} {embedDefinition ? ( <> { // Set the url that the user has typed into the input setUrl(value) // Set the embed info to either the embed info for the URL (if // that embed info can be found and of a type that matches the // user's selected definition type) const embedInfo = getEmbedInfo(value) setEmbedInfoForUrl( embedInfo && embedInfo.definition.type === embedDefinition.type ? embedInfo : null ) // We want to hide the error when the user enters text, // and then show an error after a short delay if the user // has not yet entered a valid URL. setShowError(false) clearTimeout(rShowErrorTimeout.current) rShowErrorTimeout.current = setTimeout(() => setShowError(!embedInfo), 320) }} /> {url === '' ? (
{msg('embed-dialog.instruction')}{' '} {embedDefinition.instructionLink && ( Learn more. )}
) : (
{showError ? msg('embed-dialog.invalid-url') : '\xa0'}
)}
) })} )} ) })