import type {ConfigUpdate} from '@remotion/studio-shared'; import React, {useCallback, useEffect, useMemo, useState} from 'react'; import {BLUE, LIGHT_TEXT} from '../helpers/colors'; import {Spacing} from './layout'; import {LicenseExplanation} from './LicenseExplanation'; import { fetchLicenseKeyDetails, hasActiveCompanyLicense, LicenseKeyDetailsDisplay, validateLicenseKey, type LicenseKeyDetails, } from './LicenseKeyValidation'; import {RemotionInput} from './NewComposition/RemInput'; import {ValidationMessage} from './NewComposition/ValidationMessage'; import {RadioButton} from './RadioButton'; import {useSettings} from './SettingsContext'; import {useAutoSaveConfig} from './use-auto-save-config'; type LicenseType = 'free' | 'company' | null; const container: React.CSSProperties = { display: 'flex', flex: 1, flexDirection: 'column', minWidth: 0, }; const content: React.CSSProperties = { flex: 1, padding: 16, }; const description: React.CSSProperties = { color: LIGHT_TEXT, fontFamily: 'sans-serif', fontSize: 14, lineHeight: 1.5, margin: 0, }; const externalDescriptionLink: React.CSSProperties = { color: BLUE, fontFamily: 'sans-serif', fontSize: 14, lineHeight: '21px', }; const externalLinkIndicator: React.CSSProperties = { display: 'inline-block', height: 12, marginLeft: 4, verticalAlign: -2, width: 12, }; const freeLicenseMessage: React.CSSProperties = { ...description, marginBottom: 5, marginLeft: 28, }; const companyLicenseContent: React.CSSProperties = { marginLeft: 28, }; const inputLabel: React.CSSProperties = { color: LIGHT_TEXT, display: 'block', fontFamily: 'sans-serif', fontSize: 13, lineHeight: '20px', marginBottom: 6, marginTop: 10, }; export const LicenseSettings: React.FC = () => { const {error: settingsError, publicLicenseKey, revision} = useSettings(); const initialLicenseType: LicenseType = publicLicenseKey === 'free-license' ? 'free' : publicLicenseKey === null ? null : 'company'; const [licenseType, setLicenseType] = useState(initialLicenseType); const [companyLicenseKey, setCompanyLicenseKey] = useState( initialLicenseType === 'company' ? (publicLicenseKey ?? '') : '', ); const [companyLicenseKeyToSave, setCompanyLicenseKeyToSave] = useState(companyLicenseKey); const [error, setError] = useState(null); const [syncedRevision, setSyncedRevision] = useState(revision); const [isValidatingLicenseKey, setIsValidatingLicenseKey] = useState(false); const [remoteValidationMessage, setRemoteValidationMessage] = useState< string | null >(null); const [licenseKeyDetails, setLicenseKeyDetails] = useState(null); useEffect(() => { const nextLicenseType: LicenseType = publicLicenseKey === 'free-license' ? 'free' : publicLicenseKey === null ? null : 'company'; const nextCompanyLicenseKey = nextLicenseType === 'company' ? (publicLicenseKey ?? '') : ''; setLicenseType(nextLicenseType); setCompanyLicenseKey(nextCompanyLicenseKey); setCompanyLicenseKeyToSave(nextCompanyLicenseKey); setSyncedRevision(revision); setError(null); }, [publicLicenseKey, revision]); const toggleLicenseType = useCallback( (newLicenseType: Exclude) => { setLicenseType((currentLicenseType) => currentLicenseType === newLicenseType ? null : newLicenseType, ); setError(null); }, [], ); const toggleFreeLicense = useCallback(() => { toggleLicenseType('free'); }, [toggleLicenseType]); const toggleCompanyLicense = useCallback(() => { toggleLicenseType('company'); }, [toggleLicenseType]); const localLicenseKeyValidation = useMemo( () => validateLicenseKey(companyLicenseKey.trim()), [companyLicenseKey], ); useEffect(() => { let cancelled = false; setLicenseKeyDetails(null); setRemoteValidationMessage(null); if (licenseType !== 'company' || !localLicenseKeyValidation.valid) { setIsValidatingLicenseKey(false); return () => { cancelled = true; }; } setIsValidatingLicenseKey(true); fetchLicenseKeyDetails(companyLicenseKey.trim()) .then((details) => { if (cancelled) { return; } setIsValidatingLicenseKey(false); if (details.isValid) { setLicenseKeyDetails(details); } else { setRemoteValidationMessage( 'License key is invalid or has been reset', ); } }) .catch(() => { if (cancelled) { return; } setIsValidatingLicenseKey(false); setRemoteValidationMessage('Failed to fetch license key details'); }); return () => { cancelled = true; }; }, [companyLicenseKey, licenseType, localLicenseKeyValidation.valid]); const companyLicenseKeyIsValid = localLicenseKeyValidation.valid && licenseKeyDetails?.isValid === true && !isValidatingLicenseKey; const publicLicenseKeyToSave = useMemo(() => { if (licenseType === 'free') { return 'free-license'; } if (licenseType === 'company') { return companyLicenseKeyToSave; } return ''; }, [companyLicenseKeyToSave, licenseType]); const updates = useMemo((): ConfigUpdate[] => { if (licenseType === null) { return [{setter: 'setPublicLicenseKey', type: 'delete'}]; } return [ { setter: 'setPublicLicenseKey', type: 'set', value: publicLicenseKeyToSave, }, ]; }, [licenseType, publicLicenseKeyToSave]); useAutoSaveConfig({ enabled: licenseType === null || (publicLicenseKeyToSave.length > 0 && (licenseType !== 'company' || (companyLicenseKeyToSave === companyLicenseKey.trim() && companyLicenseKeyIsValid))), onError: setError, ready: syncedRevision === revision, syncRevision: syncedRevision, updates, }); const displayedError = error ?? settingsError; return (
I am eligible for the Free License {licenseType === 'free' ? (

That's it! Enjoy Remotion.

) : null} I need a Company License {licenseType === 'company' ? (

Visit{' '} License & Pricing {' '} to obtain a license key. Then enter it here.

{localLicenseKeyValidation.message ? ( <> ) : null} {remoteValidationMessage ? ( <> ) : null} {isValidatingLicenseKey ? ( <>

Loading license key details...

) : null} {licenseKeyDetails ? ( ) : null}
) : null}
{displayedError ? ( <> ) : null}
); };