Skip to content

🎥 Make videos programmatically with React

8.7 KB
Raw Blame
import React, {
	useCallback,
	useContext,
	useImperativeHandle,
	useMemo,
	useState,
} from 'react';
import type {_InternalTypes} from 'remotion';
import {Internals} from 'remotion';
import {BACKGROUND} from '../helpers/colors';
import {useMobileLayout} from '../helpers/mobile-layout';
import {resolvedStackToSymbolicated} from '../helpers/resolved-stack-to-symbolicated';
import {useZodIfPossible, useZodTypesIfPossible} from './get-zod-if-possible';
import {InspectorPanel} from './InspectorPanel';
import {
	getSameSequenceInspectorSelection,
	isSequenceSectionSelection,
} from './InspectorPanel/inspector-selection';
import {showNotification} from './Notifications/NotificationCenter';
import {optionsSidebarTabs} from './options-sidebar-tabs';
import {deepEqual} from './RenderModal/SchemaEditor/deep-equal';
import {extractEnumJsonPaths} from './RenderModal/SchemaEditor/extract-enum-json-paths';
import {resolveCompositionSchema} from './RenderModal/SchemaEditor/infer-zod-schema-from-value';
import {SchemaResetButton} from './RenderModal/SchemaEditor/SchemaResetButton';
import type {UpdaterFunction} from './RenderModal/SchemaEditor/ZodSwitch';
import {RenderQueue} from './RenderQueue';
import {callUpdateDefaultPropsApi} from './RenderQueue/actions';
import {RendersTab} from './RendersTab';
import {Tab, Tabs} from './Tabs';
import {useTimelineSelection} from './Timeline/TimelineSelection';
import {
	getCachedResolvedStack,
	useResolvedStack,
} from './Timeline/use-resolved-stack';

type OptionsSidebarPanel = 'inspector' | 'renders';

const localStorageKey = 'remotion.sidebarPanel';

const getSelectedPanel = (): OptionsSidebarPanel => {
	const panel = localStorage.getItem(localStorageKey);
	if (panel === 'renders') {
		return 'renders';
	}

	return 'inspector';
};

const tabsContainer: React.CSSProperties = {
	backgroundColor: BACKGROUND,
};

export const persistSelectedOptionsSidebarPanel = (
	panel: OptionsSidebarPanel,
) => {
	localStorage.setItem(localStorageKey, panel);
};

export const OptionsPanel: React.FC<{
	readonly readOnlyStudio: boolean;
}> = ({readOnlyStudio}) => {
	const {props, updateProps} = useContext(Internals.EditorPropsContext);

	const isMobileLayout = useMobileLayout();

	const container: React.CSSProperties = useMemo(
		() => ({
			height: '100%',
			width: '100%',
			display: 'flex',
			position: isMobileLayout ? 'relative' : 'absolute',
			flexDirection: 'column',
			flex: 1,
		}),
		[isMobileLayout],
	);

	const [panel, setPanel] = useState<OptionsSidebarPanel>(getSelectedPanel);
	const onInspectorSelected = useCallback(() => {
		setPanel('inspector');
		persistSelectedOptionsSidebarPanel('inspector');
	}, []);

	const onRendersSelected = useCallback(() => {
		setPanel('renders');
		persistSelectedOptionsSidebarPanel('renders');
	}, []);

	useImperativeHandle(optionsSidebarTabs, () => {
		return {
			selectInspectorPanel: () => {
				setPanel('inspector');
				persistSelectedOptionsSidebarPanel('inspector');
			},
			selectRendersPanel: () => {
				setPanel('renders');
				persistSelectedOptionsSidebarPanel('renders');
			},
		};
	}, []);

	const {compositions, canvasContent} = useContext(
		Internals.CompositionManager,
	);

	const composition = useMemo((): _InternalTypes['AnyComposition'] | null => {
		if (canvasContent === null || canvasContent.type !== 'composition') {
			return null;
		}

		for (const comp of compositions) {
			if (comp.id === canvasContent.compositionId) {
				return comp;
			}
		}

		return null;
	}, [canvasContent, compositions]);

	const {selectedItems} = useTimelineSelection();
	const inspectorTitle = useMemo(() => {
		if (selectedItems.length === 0) {
			if (canvasContent?.type === 'asset') {
				return 'Asset';
			}

			return composition === null ? 'Selection' : 'Composition';
		}

		const selection =
			selectedItems.length === 1
				? selectedItems[0]
				: getSameSequenceInspectorSelection(selectedItems);
		if (selection === null) {
			return selectedItems.every((item) => item.type === 'sequence')
				? 'Sequences'
				: 'Selection';
		}

		if (isSequenceSectionSelection(selection)) {
			return 'Sequence';
		}

		switch (selection.type) {
			case 'keyframe':
				return 'Keyframe';
			case 'easing':
				return 'Easing';
			case 'guide':
				return 'Guide';
			default:
				throw new Error(
					`Unexpected inspector selection: ${selection satisfies never}`,
				);
		}
	}, [canvasContent, composition, selectedItems]);

	// Re-renders once the stack is resolved. Read from the cache so the location
	// always belongs to the current composition, not the previously selected one.
	useResolvedStack(composition?.stack ?? null);
	const resolvedLocation = getCachedResolvedStack(composition?.stack ?? null);
	const symbolicatedStack = useMemo(
		() => resolvedStackToSymbolicated(resolvedLocation),
		[resolvedLocation],
	);

	const z = useZodIfPossible();
	const zodTypes = useZodTypesIfPossible();

	const noComposition = !composition;

	const schema = useMemo(() => {
		if (noComposition) {
			return 'no-composition' as const;
		}

		return resolveCompositionSchema({
			explicitSchema: composition.schema,
			defaultProps: composition.defaultProps ?? {},
			z,
			zodTypes,
		});
	}, [composition, noComposition, z, zodTypes]);

	const currentDefaultProps = useMemo(() => {
		if (composition === null) {
			return {};
		}

		return props[composition.id] ?? composition.defaultProps ?? {};
	}, [composition, props]);

	const saveToFile = useCallback(
		(updater: (old: Record<string, unknown>) => Record<string, unknown>) => {
			if (readOnlyStudio) {
				return;
			}

			if (
				schema === 'no-zod' ||
				schema === 'no-schema' ||
				schema === 'no-composition' ||
				z === null
			) {
				showNotification('Cannot update default props: No Zod schema', 2000);
				return;
			}

			if (!composition) {
				throw new Error('Composition is not found');
			}

			const oldDefaultProps = currentDefaultProps;
			const newDefaultProps = updater(
				oldDefaultProps as Record<string, unknown>,
			);
			callUpdateDefaultPropsApi(
				composition.id,
				newDefaultProps,
				extractEnumJsonPaths({
					schema,
					zodRuntime: z,
					currentPath: [],
					zodTypes,
				}),
				symbolicatedStack,
			)
				.then((response) => {
					if (!response.success) {
						// eslint-disable-next-line no-console
						console.log(response.stack);
						showNotification(
							`Cannot update default props: ${response.reason}. See console for more information.`,
							2000,
						);
					}
				})
				.catch((err) => {
					showNotification(`Cannot update default props: ${err.message}`, 2000);
				});
		},
		[
			composition,
			currentDefaultProps,
			readOnlyStudio,
			schema,
			symbolicatedStack,
			z,
			zodTypes,
		],
	);

	const compositionId = useMemo(() => {
		return composition?.id ?? '';
	}, [composition?.id]);

	const compositionDefaultProps = useMemo(() => {
		return composition?.defaultProps ?? {};
	}, [composition?.defaultProps]);

	const hasLocalModifications = useMemo(() => {
		if (!readOnlyStudio || !composition || !composition.defaultProps) {
			return false;
		}

		return !deepEqual(composition.defaultProps, currentDefaultProps);
	}, [readOnlyStudio, composition, currentDefaultProps]);

	const resetToOriginal = useCallback(() => {
		if (!composition) {
			return;
		}

		updateProps({
			id: composition.id,
			defaultProps: (composition.defaultProps ?? {}) as Record<string, unknown>,
			newProps: (composition.defaultProps ?? {}) as Record<string, unknown>,
		});
	}, [composition, updateProps]);

	const setDefaultProps: UpdaterFunction<Record<string, unknown>> = useCallback(
		(updater, {shouldSave}) => {
			updateProps({
				id: compositionId,
				defaultProps: compositionDefaultProps as Record<string, unknown>,
				newProps: updater,
			});

			if (shouldSave) {
				saveToFile(updater);
			}
		},
		[compositionId, compositionDefaultProps, saveToFile, updateProps],
	);

	const onPointerDown = useCallback((e: React.PointerEvent<HTMLDivElement>) => {
		// Prevent deselection of currently selected items
		e.stopPropagation();
	}, []);

	return (
		<div style={container} className="css-reset" onPointerDown={onPointerDown}>
			<div style={tabsContainer}>
				<Tabs>
					<Tab
						selected={panel === 'inspector'}
						onClick={onInspectorSelected}
						style={{justifyContent: 'space-between'}}
					>
						{inspectorTitle}
						{hasLocalModifications ? (
							<SchemaResetButton onClick={resetToOriginal} />
						) : null}
					</Tab>
					<RendersTab
						onClick={onRendersSelected}
						selected={panel === 'renders'}
					/>
				</Tabs>
			</div>
			{panel === 'inspector' ? (
				<InspectorPanel
					composition={composition}
					currentDefaultProps={currentDefaultProps}
					readOnlyStudio={readOnlyStudio}
					setDefaultProps={setDefaultProps}
				/>
			) : (
				<RenderQueue />
			)}
		</div>
	);
};