Skip to content

🎥 Make videos programmatically with React

Use existing validation setup

Pramod1a3d7f5f93e7
diff --git a/packages/studio/src/components/RenderModal/WebRenderModalLicense.tsx b/packages/studio/src/components/RenderModal/WebRenderModalLicense.tsx
index 529e5d776c..6828e30590 100644
--- a/packages/studio/src/components/RenderModal/WebRenderModalLicense.tsx
+++ b/packages/studio/src/components/RenderModal/WebRenderModalLicense.tsx
@@ -9,7 +9,11 @@ import {Checkbox} from '../Checkbox';
import {RemotionInput} from '../NewComposition/RemInput';
import {ValidationMessage} from '../NewComposition/ValidationMessage';
import {Spacing} from '../layout';
-import {WebRenderModalLicenseKeyValidation} from './WebRenderModalLicenseKeyValidation';
+import type {LicenseKeyDetails} from './WebRenderModalLicenseKeyDetails';
+import {
+ useLicenseKeyDetails,
+ WebRenderModalLicenseKeyDetails,
+} from './WebRenderModalLicenseKeyDetails';
type WebRenderModalLicenseProps = {
readonly licenseKey: string | null;
@@ -103,17 +107,25 @@ const codeLineSmall: React.CSSProperties = {
const LICENSE_KEY_LENGTH = 55;
const LICENSE_KEY_PREFIX = 'rm_pub_';
+type LicenseKeyValidation = {
+ valid: boolean;
+ message: string | null;
+ details: LicenseKeyDetails | null;
+};
+
const validateLicenseKey = (
key: string,
-): {valid: boolean; message: string | null} => {
+ details: LicenseKeyDetails | null,
+): LicenseKeyValidation => {
if (key.length === 0) {
- return {valid: false, message: null};
+ return {valid: false, message: null, details: null};
}
if (!key.startsWith(LICENSE_KEY_PREFIX)) {
return {
valid: false,
message: `License key must start with "${LICENSE_KEY_PREFIX}"`,
+ details: null,
};
}
@@ -123,6 +135,7 @@ const validateLicenseKey = (
valid: false,
message:
'License key must contain only alphanumeric characters after the prefix',
+ details: null,
};
}
@@ -130,10 +143,27 @@ const validateLicenseKey = (
return {
valid: false,
message: `License key must be ${LICENSE_KEY_LENGTH} characters long`,
+ details: null,
+ };
+ }
+
+ if (details && !details.isValid) {
+ return {
+ valid: false,
+ message: 'License key is invalid or has been reset',
+ details: null,
};
}
- return {valid: true, message: null};
+ if (details?.isValid) {
+ return {
+ valid: true,
+ message: null,
+ details,
+ };
+ }
+
+ return {valid: true, message: null, details: null};
};
export const WebRenderModalLicense: React.FC<WebRenderModalLicenseProps> = ({
@@ -141,6 +171,9 @@ export const WebRenderModalLicense: React.FC<WebRenderModalLicenseProps> = ({
setLicenseKey,
initialPublicLicenseKey,
}) => {
+ const {details: licenseKeyDetails, isLoading} =
+ useLicenseKeyDetails(licenseKey);
+
const onFreeLicenseChange = useCallback(() => {
setLicenseKey('free-license');
}, [setLicenseKey]);
@@ -159,11 +192,11 @@ export const WebRenderModalLicense: React.FC<WebRenderModalLicenseProps> = ({
const licenseValidation = useMemo(() => {
if (licenseKey === null || licenseKey === 'free-license') {
- return {valid: true, message: null};
+ return {valid: true, message: null, details: null};
}
- return validateLicenseKey(licenseKey);
- }, [licenseKey]);
+ return validateLicenseKey(licenseKey, licenseKeyDetails);
+ }, [licenseKey, licenseKeyDetails]);
return (
<div style={tabContainer}>
@@ -258,12 +291,20 @@ export const WebRenderModalLicense: React.FC<WebRenderModalLicenseProps> = ({
</div>
</>
) : null}
- {licenseValidation.valid && licenseKey.length > 0 ? (
+ {isLoading && (
<>
<Spacing y={1} block />
- <WebRenderModalLicenseKeyValidation licenseKey={licenseKey} />
+ Loading license key details...
</>
- ) : null}
+ )}
+ {licenseValidation.details && (
+ <>
+ <Spacing y={1} block />
+ <WebRenderModalLicenseKeyDetails
+ details={licenseValidation.details}
+ />
+ </>
+ )}
</div>
) : null}
{licenseKey === null ? (
diff --git a/packages/studio/src/components/RenderModal/WebRenderModalLicenseKeyDetails.tsx b/packages/studio/src/components/RenderModal/WebRenderModalLicenseKeyDetails.tsx
new file mode 100644
index 0000000000..456d225285
--- /dev/null
+++ b/packages/studio/src/components/RenderModal/WebRenderModalLicenseKeyDetails.tsx
@@ -0,0 +1,118 @@
+import React, {useEffect} from 'react';
+import {LIGHT_TEXT} from '../../helpers/colors';
+import {CheckCircleFilled} from '../../icons/check-circle-filled';
+
+const textStyle: React.CSSProperties = {
+ color: LIGHT_TEXT,
+ fontSize: 14,
+ fontFamily: 'sans-serif',
+ lineHeight: 1.5,
+ display: 'flex',
+ alignItems: 'center',
+};
+
+const linkStyle: React.CSSProperties = {
+ fontSize: 14,
+ fontFamily: 'sans-serif',
+ lineHeight: 1.5,
+ cursor: 'pointer',
+};
+
+const bulletStyle: React.CSSProperties = {
+ display: 'flex',
+ alignItems: 'center',
+ gap: 6,
+};
+
+const icon: React.CSSProperties = {
+ width: 14,
+ height: 14,
+ flexShrink: 0,
+};
+
+export type LicenseKeyDetails = {
+ readonly isValid: boolean;
+ readonly hasActiveSubscription: boolean;
+ readonly projectName: string;
+ readonly projectSlug: string;
+};
+
+type UseLicenseKeyDetailsState = {
+ readonly isLoading: boolean;
+ readonly details: LicenseKeyDetails | null;
+};
+
+type WebRenderModalLicenseKeyDetailsProps = {
+ readonly details: LicenseKeyDetails;
+};
+
+const PRO_HOST = 'https://remotion.pro';
+
+export const useLicenseKeyDetails = (
+ licenseKey: string | null,
+): UseLicenseKeyDetailsState => {
+ const [isLoading, setIsLoading] = React.useState(false);
+ const [details, setDetails] = React.useState<LicenseKeyDetails | null>(null);
+
+ useEffect(() => {
+ if (licenseKey) {
+ setIsLoading(true);
+ setDetails(null);
+ fetch(`${PRO_HOST}/api/validate-license-key`, {
+ method: 'POST',
+ body: JSON.stringify({
+ licenseKey,
+ }),
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ })
+ .then((res) => res.json())
+ .then((data) => {
+ setDetails(data);
+ setIsLoading(false);
+ })
+ .catch(() => {
+ setDetails(null);
+ });
+ }
+ }, [licenseKey]);
+
+ return {isLoading, details};
+};
+
+export const WebRenderModalLicenseKeyDetails: React.FC<
+ WebRenderModalLicenseKeyDetailsProps
+> = ({details}) => {
+ return (
+ <div>
+ <div style={bulletStyle}>
+ <CheckCircleFilled style={{...icon, fill: LIGHT_TEXT}} />
+ <div style={textStyle}>
+ Belongs to&nbsp;
+ <a
+ href={`${PRO_HOST}/projects/${details.projectSlug}`}
+ target="_blank"
+ style={linkStyle}
+ >
+ {details.projectName}
+ </a>
+ &nbsp;- View&nbsp;
+ <a
+ href={`${PRO_HOST}/projects/${details.projectSlug}/usage#client-renders-usage`}
+ target="_blank"
+ style={linkStyle}
+ >
+ usage
+ </a>
+ </div>
+ </div>
+ {details.hasActiveSubscription && (
+ <div style={bulletStyle}>
+ <CheckCircleFilled style={{...icon, fill: LIGHT_TEXT}} />
+ <div style={textStyle}>Active Company License</div>
+ </div>
+ )}
+ </div>
+ );
+};
diff --git a/packages/studio/src/components/RenderModal/WebRenderModalLicenseKeyValidation.tsx b/packages/studio/src/components/RenderModal/WebRenderModalLicenseKeyValidation.tsx
deleted file mode 100644
index 925475d97e..0000000000
--- a/packages/studio/src/components/RenderModal/WebRenderModalLicenseKeyValidation.tsx
+++ /dev/null
@@ -1,123 +0,0 @@
-import React, {useEffect} from 'react';
-import {LIGHT_TEXT, WARNING_COLOR} from '../../helpers/colors';
-import {CheckCircleFilled} from '../../icons/check-circle-filled';
-import {WarningTriangle} from '../NewComposition/ValidationMessage';
-
-const textStyle: React.CSSProperties = {
- color: LIGHT_TEXT,
- fontSize: 14,
- fontFamily: 'sans-serif',
- lineHeight: 1.5,
- display: 'flex',
- alignItems: 'center',
-};
-
-const linkStyle: React.CSSProperties = {
- fontSize: 14,
- fontFamily: 'sans-serif',
- lineHeight: 1.5,
- cursor: 'pointer',
-};
-
-const bulletStyle: React.CSSProperties = {
- display: 'flex',
- alignItems: 'center',
- gap: 6,
-};
-
-const icon: React.CSSProperties = {
- width: 14,
- height: 14,
- flexShrink: 0,
-};
-
-type WebRenderModalLicenseKeyValidationProps = {
- readonly licenseKey: string | null;
-};
-
-type LicenseKeyValidationResult = {
- readonly isValid: boolean;
- readonly hasActiveSubscription: boolean;
- readonly projectName: string;
- readonly projectSlug: string;
-};
-
-const PRO_HOST = 'https://remotion.pro';
-
-export const WebRenderModalLicenseKeyValidation: React.FC<
- WebRenderModalLicenseKeyValidationProps
-> = ({licenseKey}) => {
- const [isLoading, setIsLoading] = React.useState(false);
- const [validation, setValidation] =
- React.useState<LicenseKeyValidationResult | null>(null);
-
- useEffect(() => {
- if (licenseKey) {
- setIsLoading(true);
- setValidation(null);
- fetch(`${PRO_HOST}/api/validate-license-key`, {
- method: 'POST',
- body: JSON.stringify({
- licenseKey,
- }),
- headers: {
- 'Content-Type': 'application/json',
- },
- })
- .then((res) => res.json())
- .then((data) => {
- setValidation(data);
- setIsLoading(false);
- })
- .catch(() => {
- setValidation(null);
- });
- }
- }, [licenseKey]);
-
- return (
- <div style={textStyle}>
- {isLoading && 'Validating license key...'}
- {validation && !validation.isValid && (
- <div style={bulletStyle}>
- <WarningTriangle
- type="warning"
- style={{...icon, fill: WARNING_COLOR}}
- />
- <div style={textStyle}>Invalid license key</div>
- </div>
- )}
- {validation && validation.isValid && (
- <div>
- <div style={bulletStyle}>
- <CheckCircleFilled style={{...icon, fill: LIGHT_TEXT}} />
- <div style={textStyle}>
- Belongs to&nbsp;
- <a
- href={`${PRO_HOST}/projects/${validation.projectSlug}`}
- target="_blank"
- style={linkStyle}
- >
- {validation.projectName}
- </a>
- &nbsp;- View&nbsp;
- <a
- href={`${PRO_HOST}/projects/${validation.projectSlug}/usage#client-renders-usage`}
- target="_blank"
- style={linkStyle}
- >
- usage
- </a>
- </div>
- </div>
- {validation.hasActiveSubscription && (
- <div style={bulletStyle}>
- <CheckCircleFilled style={{...icon, fill: LIGHT_TEXT}} />
- <div style={textStyle}>Active Company License</div>
- </div>
- )}
- </div>
- )}
- </div>
- );
-};