import { useCallback, useEffect, useMemo, useState } from 'react'; import { HelpTab } from './HelpTab'; import { MediaLibraryTab } from './MediaLibraryTab'; import { ShortcodeTab } from './ShortcodeTab'; import { voltaxLog } from './debug'; import type { InsertPayload, ModalOpenOptions, PlaybackMethod, TabId } from './types'; import { buildShortcode, isInsertPayloadValid, isShortcodeTabContext, normalizePlaybackmethod, supportsPlaybackmethodUi, } from './utils'; type AppProps = { isOpen: boolean; options: ModalOpenOptions; onClose: () => void; }; export function App( { isOpen, options, onClose }: AppProps ) { const config = window.voltaxModalConfig!; const enableShortcode = options.enableShortcode !== false && isShortcodeTabContext( options.context ); const tabs = useMemo( () => { const list: { id: TabId; label: string }[] = []; if ( config.enableVideoLibrary ) { list.push( { id: 'videos', label: config.i18n.videos } ); } list.push( { id: 'playlists', label: config.i18n.playlists } ); if ( enableShortcode ) { list.push( { id: 'shortcode', label: config.i18n.shortcode } ); } if ( config.guideUrl ) { list.push( { id: 'help', label: config.i18n.help } ); } return list; }, [ config.enableVideoLibrary, config.guideUrl, config.i18n, enableShortcode ] ); const initialTab = options.tab && tabs.some( ( tab ) => tab.id === options.tab ) ? options.tab : tabs[ 0 ].id; const [ activeTab, setActiveTab ] = useState( initialTab ); const [ selection, setSelection ] = useState( null ); const [ playbackmethod, setPlaybackmethod ] = useState( 'autoplay' ); useEffect( () => { if ( ! isOpen ) { return; } setActiveTab( initialTab ); setSelection( null ); setPlaybackmethod( 'autoplay' ); }, [ isOpen, initialTab ] ); useEffect( () => { if ( ! isOpen ) { return; } const onKeyDown = ( event: KeyboardEvent ) => { if ( event.key === 'Escape' ) { onClose(); } }; document.addEventListener( 'keydown', onKeyDown ); document.body.style.overflow = 'hidden'; return () => { document.removeEventListener( 'keydown', onKeyDown ); document.body.style.overflow = ''; }; }, [ isOpen, onClose ] ); const handleInsert = useCallback( () => { if ( ! selection ) { window.alert( config.i18n.requiredId ); return; } const payload: InsertPayload = supportsPlaybackmethodUi( selection ) ? { ...selection, playbackmethod: normalizePlaybackmethod( playbackmethod ) } : selection; const shortcode = buildShortcode( payload ); if ( ! shortcode ) { window.alert( config.i18n.requiredId ); return; } const detail = { payload, shortcode, context: options.context || '', editorId: options.editorId || '', }; voltaxLog( 'handleInsert dispatch scheduled', detail ); onClose(); window.setTimeout( () => { voltaxLog( 'dispatching voltax-modal-insert', detail ); document.dispatchEvent( new CustomEvent( 'voltax-modal-insert', { detail, } ) ); }, 50 ); }, [ selection, playbackmethod, config.i18n.requiredId, options.context, options.editorId, onClose ] ); if ( ! isOpen ) { return null; } const showFooter = activeTab !== 'help'; const showPlayback = activeTab === 'videos' || activeTab === 'playlists'; return (
event.stopPropagation() }>

{ config.i18n.title }

{ activeTab === 'videos' ? ( ) : null } { activeTab === 'playlists' ? ( ) : null } { activeTab === 'shortcode' ? ( ) : null } { activeTab === 'help' ? : null }
{ showFooter ? (
{ showPlayback ? (
) : (
) : null }
); }