import {withRequiredAuxiliaryPackages} from '@remotion/studio-shared'; import React, {useCallback, useContext, useEffect, useState} from 'react'; import {installPackages} from '../api/install-package'; import {LIGHT_TEXT, WARNING_COLOR, WHITE} from '../helpers/colors'; import {SetSelectedModalContext} from '../state/modals'; import {Button} from './Button'; import {Flex, Row, Spacing} from './layout'; import {ModalButton} from './ModalButton'; import {ModalContainer} from './ModalContainer'; import {ModalFooterContainer} from './ModalFooter'; import {ModalHeader} from './ModalHeader'; import {Spinner} from './Spinner'; type InstallState = | {type: 'idle'} | {type: 'installing'} | {type: 'error'; error: Error}; const panelStyle: React.CSSProperties = { width: 'min(520px, calc(100vw - 40px))', maxHeight: 'calc(100vh - 40px)', display: 'flex', flexDirection: 'column', }; const contentStyle: React.CSSProperties = { color: LIGHT_TEXT, fontFamily: 'sans-serif', fontSize: 14, lineHeight: 1.5, padding: 16, minHeight: 0, overflowY: 'auto', }; const codeStyle: React.CSSProperties = { color: WHITE, fontFamily: 'monospace', fontSize: 'inherit', }; const statusStyle: React.CSSProperties = { alignItems: 'center', color: LIGHT_TEXT, display: 'flex', fontFamily: 'sans-serif', fontSize: 14, gap: 10, lineHeight: 1.5, }; const errorStyle: React.CSSProperties = { color: WARNING_COLOR, marginTop: 12, whiteSpace: 'pre-wrap', }; const cancelStyle: React.CSSProperties = {minWidth: 90}; export const isOptionalPackageInstalled = (packageName: string) => window.remotion_installedPackages?.includes(packageName) ?? false; const OPTIONAL_PACKAGE_INSTALLED_EVENT = 'remotion-optional-package-installed'; export const markOptionalPackageInstalled = (packageName: string) => { window.remotion_installedPackages = Array.from( new Set([...(window.remotion_installedPackages ?? []), packageName]), ); window.dispatchEvent( new CustomEvent(OPTIONAL_PACKAGE_INSTALLED_EVENT, { detail: packageName, }), ); }; export const installOptionalPackage = async (packageName: string) => { const dependencies = withRequiredAuxiliaryPackages([ {name: packageName, version: null}, ]); await installPackages(dependencies); for (const dependency of dependencies) { markOptionalPackageInstalled(dependency.name); } }; export const useOptionalPackageInstalled = (packageName: string) => { const [installed, setInstalled] = useState(() => isOptionalPackageInstalled(packageName), ); useEffect(() => { const onInstalled = (event: Event) => { if ((event as CustomEvent).detail === packageName) { setInstalled(true); } }; window.addEventListener(OPTIONAL_PACKAGE_INSTALLED_EVENT, onInstalled); return () => window.removeEventListener(OPTIONAL_PACKAGE_INSTALLED_EVENT, onInstalled); }, [packageName]); return installed; }; export const OptionalPackageModal: React.FC<{ readonly ariaLabel: string; readonly children: React.ReactNode; readonly installButtonText: string; readonly installMessage: React.ReactNode | null; readonly packageName: string; readonly title: string; }> = ({ ariaLabel, children, installButtonText, installMessage, packageName, title, }) => { const {setSelectedModal} = useContext(SetSelectedModalContext); const installed = useOptionalPackageInstalled(packageName); const [installState, setInstallState] = useState({ type: 'idle', }); const busy = installState.type === 'installing'; const packageNames = withRequiredAuxiliaryPackages([ {name: packageName, version: null}, ]).map((dependency) => dependency.name); const packagesLabel = packageNames.join(' and '); const dismiss = useCallback(() => { if (!busy) { setSelectedModal(null); } }, [busy, setSelectedModal]); const install = useCallback(async () => { setInstallState({type: 'installing'}); try { await installOptionalPackage(packageName); } catch (error) { setInstallState({type: 'error', error: error as Error}); } }, [packageName]); if (installed) { return children; } return (
{busy ? (
{`Installing ${packagesLabel}…`}
) : ( <> {installMessage ?? ( <> This requires installing{' '} {packageNames.map((name, index) => ( {index > 0 ? ' and ' : null} {name} ))} . Continue? )} {installState.type === 'error' ? (
{installState.error.message}
) : null} )}
{installState.type === 'error' ? 'Retry' : installButtonText}
); };