🎥 Make videos programmatically with React
Elements: Add Element Library controls to Studio settings
JonnyBurgerb7b6521a284b
diff --git a/packages/studio-server/src/preview-server/routes/update-config.ts b/packages/studio-server/src/preview-server/routes/update-config.tsindex 9bd3587eff..f1a4b40ba5 100644--- a/packages/studio-server/src/preview-server/routes/update-config.ts+++ b/packages/studio-server/src/preview-server/routes/update-config.ts@@ -74,6 +74,26 @@ const validateUpdates = (updates: unknown): string | null => {setters.add(setter);if (setter === 'addElementLibrary') {+ if (type === 'delete') {+ if (typeof update.value !== 'string') {+ return 'Deleting an Element Library requires its URL.';+ }++ try {+ const parsedUrl = new URL(update.value);+ if (+ parsedUrl.protocol !== 'http:' &&+ parsedUrl.protocol !== 'https:'+ ) {+ return 'Deleting an Element Library requires an HTTP or HTTPS URL.';+ }+ } catch {+ return 'Deleting an Element Library requires an HTTP or HTTPS URL.';+ }++ continue;+ }+if (type !== 'set' ||!('value' in update) ||@@ -110,6 +130,10 @@ const validateUpdates = (updates: unknown): string | null => {}if (type === 'delete') {+ if (update.value !== undefined) {+ return `Config.${setter}() does not accept a value when deleting.`;+ }+continue;}@@ -135,6 +159,14 @@ export const updateConfigFile = ({readonly updates: ConfigUpdate[];}) => {const setters = new Set(updates.map(({setter}) => setter));+ const deletedElementLibraryValue = updates.find(+ (update) =>+ update.setter === 'addElementLibrary' && update.type === 'delete',+ )?.value;+ const deletedElementLibraryUrl =+ typeof deletedElementLibraryValue === 'string'+ ? new URL(deletedElementLibraryValue).href+ : null;const elementLibraryUrls = new Set<string>();for (const url of existingElementLibraryUrls) {try {@@ -183,6 +215,15 @@ export const updateConfigFile = ({) {try {const parsedUrl = new URL(urlProperty.value.value);+ if (+ deletedElementLibraryUrl !== null &&+ parsedUrl.href === deletedElementLibraryUrl+ ) {+ path.prune();+ changed = true;+ return false;+ }+if (parsedUrl.protocol === 'http:' ||parsedUrl.protocol === 'https:'@@ -347,6 +388,19 @@ export const updateConfigHandler = async ({updates: input.updates,});if (updatedConfig === configContents) {+ if (+ input.updates.some(+ (update) =>+ update.setter === 'addElementLibrary' && update.type === 'delete',+ )+ ) {+ return {+ success: false,+ reason:+ 'Could not find a matching literal Element Library URL in the config file.',+ };+ }+return {success: true};}diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.tsindex 5caf89f89b..004ef69404 100644--- a/packages/studio-shared/src/api-requests.ts+++ b/packages/studio-shared/src/api-requests.ts@@ -1248,6 +1248,7 @@ export type ConfigUpdate =| {setter: string;type: 'delete';+ value?: string;}| {setter: string;diff --git a/packages/studio/src/components/ElementLibrariesSettings.tsx b/packages/studio/src/components/ElementLibrariesSettings.tsxnew file mode 100644index 0000000000..223789bfdf--- /dev/null+++ b/packages/studio/src/components/ElementLibrariesSettings.tsx@@ -0,0 +1,250 @@+import React, {useCallback, useContext, useState} from 'react';+import {StudioServerConnectionCtx} from '../helpers/client-id';+import {LIGHT_TEXT} from '../helpers/colors';+import {TrashIcon} from '../icons/trash';+import {ActionTooltip} from './ActionTooltip';+import {Button} from './Button';+import {callApi} from './call-api';+import {InlineAction} from './InlineAction';+import {sectionHeader} from './InspectorPanel/styles';+import {RemotionInput} from './NewComposition/RemInput';+import {ValidationMessage} from './NewComposition/ValidationMessage';+import {useSettings} from './SettingsContext';+import {Spinner} from './Spinner';++const heading: React.CSSProperties = {+ ...sectionHeader,+ margin: '16px 0 0',+ padding: '4px 16px',+};++const description: React.CSSProperties = {+ color: LIGHT_TEXT,+ fontSize: 13,+ lineHeight: 1.5,+ margin: '4px 16px 12px',+};++const libraryRow: React.CSSProperties = {+ alignItems: 'center',+ display: 'flex',+ gap: 10,+ minHeight: 42,+ padding: '4px 16px',+};++const libraryDetails: React.CSSProperties = {+ flex: 1,+ minWidth: 0,+};++const libraryName: React.CSSProperties = {+ fontSize: 13,+ overflow: 'hidden',+ textOverflow: 'ellipsis',+ whiteSpace: 'nowrap',+};++const libraryUrlStyle: React.CSSProperties = {+ color: LIGHT_TEXT,+ fontSize: 12,+ overflow: 'hidden',+ textOverflow: 'ellipsis',+ whiteSpace: 'nowrap',+};++const inputRow: React.CSSProperties = {+ display: 'flex',+ gap: 8,+ padding: '4px 16px',+};++const input: React.CSSProperties = {+ flex: 1,+ minWidth: 0,+};++const message: React.CSSProperties = {+ padding: '4px 16px',+};++const trashIcon: React.CSSProperties = {+ height: 14,+ width: 14,+};++export const ElementLibrariesSettings: React.FC = () => {+ const {studioRuntimeConfig} = useSettings();+ const {previewServerState} = useContext(StudioServerConnectionCtx);+ const [url, setUrl] = useState('');+ const [displayName, setDisplayName] = useState('');+ const [busy, setBusy] = useState<string | null>(null);+ const [error, setError] = useState<string | null>(null);+ const libraries = studioRuntimeConfig?.elementLibraries ?? [];+ let normalizedUrl: string | null = null;+ try {+ const parsedUrl = new URL(url.trim());+ if (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') {+ normalizedUrl = parsedUrl.href;+ }+ } catch {+ // Keep the Add button disabled until a valid URL is entered.+ }++ const duplicate = libraries.some((library) => library.url === normalizedUrl);+ const canSave = previewServerState.type === 'connected' && busy === null;+ const addLibrary = useCallback(async () => {+ if (!canSave || normalizedUrl === null || duplicate) {+ return;+ }++ setBusy('add');+ setError(null);+ try {+ const response = await callApi('/api/update-config', {+ clientId: previewServerState.clientId,+ updates: [+ {+ setter: 'addElementLibrary',+ type: 'set',+ value: displayName.trim()+ ? {url: normalizedUrl, displayName: displayName.trim()}+ : {url: normalizedUrl},+ },+ ],+ });+ if (!response.success) {+ setError(response.reason);+ return;+ }++ setUrl('');+ setDisplayName('');+ } catch (err) {+ setError((err as Error).message);+ } finally {+ setBusy(null);+ }+ }, [canSave, displayName, duplicate, normalizedUrl, previewServerState]);++ const removeLibrary = useCallback(+ async (libraryUrl: string) => {+ if (previewServerState.type !== 'connected' || busy !== null) {+ return;+ }++ setBusy(libraryUrl);+ setError(null);+ try {+ const response = await callApi('/api/update-config', {+ clientId: previewServerState.clientId,+ updates: [+ {+ setter: 'addElementLibrary',+ type: 'delete',+ value: libraryUrl,+ },+ ],+ });+ if (!response.success) {+ setError(response.reason);+ }+ } catch (err) {+ setError((err as Error).message);+ } finally {+ setBusy(null);+ }+ },+ [busy, previewServerState],+ );++ const renderTrash = useCallback((color: string) => {+ return <TrashIcon color={color} style={trashIcon} />;+ }, []);++ return (+ <section>+ <h3 style={heading}>Element Libraries</h3>+ <p style={description}>+ Add libraries to browse their elements in Studio. Changes save to+ remotion.config.ts.+ </p>+ <div role="list" aria-label="Element Libraries">+ {libraries.map((library) => (+ <div key={library.url} role="listitem" style={libraryRow}>+ <div style={libraryDetails}>+ <div style={libraryName}>+ {library.displayName ?? new URL(library.url).host}+ </div>+ <div style={libraryUrlStyle} title={library.url}>+ {library.url}+ </div>+ </div>+ {busy === library.url ? (+ <Spinner duration={0.5} size={14} />+ ) : (+ <ActionTooltip+ label="Remove"+ shortcut={null}+ delay={800}+ dismissOnClick+ >+ <InlineAction+ aria-label={`Remove ${library.displayName ?? library.url}`}+ title=""+ disabled={!canSave}+ onClick={() => removeLibrary(library.url)}+ renderAction={renderTrash}+ variant={null}+ />+ </ActionTooltip>+ )}+ </div>+ ))}+ </div>+ <div style={inputRow}>+ <RemotionInput+ aria-label="Element Library URL"+ placeholder="https://example.com/elements"+ status="ok"+ rightAlign={false}+ style={input}+ value={url}+ onChange={(event) => setUrl(event.target.value)}+ />+ <RemotionInput+ aria-label="Element Library name"+ placeholder="Name (optional)"+ status="ok"+ rightAlign={false}+ style={input}+ value={displayName}+ onChange={(event) => setDisplayName(event.target.value)}+ />+ </div>+ <div style={inputRow}>+ <Button+ disabled={!canSave || normalizedUrl === null || duplicate}+ onClick={addLibrary}+ size="compact"+ >+ {busy === 'add' ? 'Adding…' : '+ Add Element Library'}+ </Button>+ </div>+ {duplicate ? (+ <div style={message}>+ <ValidationMessage+ align="flex-start"+ type="warning"+ message="This library is already added."+ />+ </div>+ ) : null}+ {error ? (+ <div style={message}>+ <ValidationMessage align="flex-start" type="error" message={error} />+ </div>+ ) : null}+ </section>+ );+};diff --git a/packages/studio/src/components/StudioSettings.tsx b/packages/studio/src/components/StudioSettings.tsxindex bd65e4dea8..57ff01314f 100644--- a/packages/studio/src/components/StudioSettings.tsx+++ b/packages/studio/src/components/StudioSettings.tsx@@ -10,6 +10,7 @@ import {UndoIcon} from '../icons/undo';import {Button} from './Button';import {Checkbox} from './Checkbox';import {ConfigSelect} from './ConfigSelect';+import {ElementLibrariesSettings} from './ElementLibrariesSettings';import {sectionHeader} from './InspectorPanel/styles';import {Spacing} from './layout';import {InputDragger} from './NewComposition/InputDragger';@@ -347,6 +348,7 @@ export const StudioSettings: React.FC = () => {/></div></label>+ <ElementLibrariesSettings />{(error ?? settingsError) ? (<>