Skip to content

🎥 Make videos programmatically with React

32.5 KB
Raw Blame
import type {Caption} from '@remotion/captions';
import React, {
	useCallback,
	useContext,
	useEffect,
	useMemo,
	useRef,
	useState,
} from 'react';
import {Internals, type TSequence} from 'remotion';
import type {CodePosition} from '../error-overlay/react-overlay/utils/get-source-map';
import {StudioServerConnectionCtx} from '../helpers/client-id';
import {BLUE, LIGHT_TEXT, WHITE} from '../helpers/colors';
import {getPreviewFileType} from '../helpers/get-preview-file-type';
import type {SequenceNodePathInfo} from '../helpers/get-timeline-sequence-sort-key';
import {isStudioInteractivityEnabled} from '../helpers/interactivity-enabled';
import {
	flattenVisibleTreeNodes,
	SCHEMA_FIELD_GROUPS,
	type FlatTreeRow,
	type SchemaFieldGroupInfo,
	type TimelineTreeNode,
} from '../helpers/timeline-layout';
import {
	useRuntimeValue,
	useRuntimeValueSelector,
} from '../helpers/use-runtime-values';
import {BorderRadiusIcon} from '../icons/border-radius';
import {CubeIcon} from '../icons/cube';
import {FullscreenIcon} from '../icons/fullscreen';
import {Plus} from '../icons/plus';
import {SetSelectedModalContext} from '../state/modals';
import {Transform3DModeStateContext} from '../state/transform-3d-mode';
import {ActionTooltip} from './ActionTooltip';
import {AssetFileIcon} from './AssetFileIcon';
import {InlineAction} from './InlineAction';
import {InlineCaptionInspector} from './InlineCaptionInspector';
import {CollapsibleInspectorSectionHeader} from './InspectorPanel/CollapsibleInspectorSectionHeader';
import {InspectorSection} from './InspectorPanel/common';
import {
	getInspectorSectionActivity,
	isSmartCollapsibleInspectorGroup,
} from './InspectorPanel/inspector-section-collapse';
import {
	BORDER_RADIUS_SHORTHAND_KEY,
	getBorderRadiusConversion,
	getBorderRadiusConversionChanges,
} from './Timeline/border-radius-representation';
import {requestFocusInspectorField} from './Timeline/focus-inspector-field';
import {getCurrentFps, getCurrentFrame} from './Timeline/imperative-state';
import {saveSequenceProps} from './Timeline/save-sequence-prop';
import {
	getTimelineAssetLinkInfo,
	getTimelineAssetSrcFromSchema,
	openTimelineAssetLink,
	splitRemoteSourceForMiddleEllipsis,
} from './Timeline/timeline-asset-link';
import {
	AssetSelectionContext,
	type InspectorSourceAction,
} from './Timeline/TimelineAssetField';
import {TimelineExpandedRow} from './Timeline/TimelineExpandedRow';
import {
	INSPECTOR_TIMELINE_ROW_LAYOUT,
	TimelineRowLayoutContext,
} from './Timeline/TimelineRowLayoutContext';
import {
	getTimelineSelectionFromNodePathInfo,
	getTimelineSelectionKey,
	getTimelineSequenceSelectionKey,
	TimelineSelectionOrderProvider,
	useTimelineSelection,
	type TimelineSelection,
} from './Timeline/TimelineSelection';
import {propStatusHas3DTransformValue} from './Timeline/transform-3d-mode';
import {Transform3DModeContext} from './Timeline/Transform3DModeContext';
import {useTimelineExpandedTree} from './Timeline/use-timeline-expanded-tree';
import {useSelectAsset} from './use-select-asset';

const container: React.CSSProperties = {
	color: WHITE,
	display: 'flex',
	flexDirection: 'column',
	fontFamily: 'Arial, Helvetica, sans-serif',
	fontSize: 12,
};

const plusIcon: React.CSSProperties = {
	width: 15,
	height: 15,
};

const borderRadiusToggleIcon: React.CSSProperties = {
	flexShrink: 0,
	height: 15,
	width: 15,
};

const transform3DToggleIcon: React.CSSProperties = {
	flexShrink: 0,
	height: 14,
	width: 14,
};

const assetSelectorIcon: React.CSSProperties = {
	flexShrink: 0,
	height: 18,
	width: 18,
};

const remoteSourcePartsContainer: React.CSSProperties = {
	color: 'inherit',
	display: 'flex',
	fontFamily: 'Arial, Helvetica, sans-serif',
	fontSize: 12,
	lineHeight: '18px',
	minWidth: 0,
	overflow: 'hidden',
	whiteSpace: 'nowrap',
};

const remoteSourceLeading: React.CSSProperties = {
	color: LIGHT_TEXT,
	fontFamily: 'Arial, Helvetica, sans-serif',
	fontSize: 12,
	lineHeight: '18px',
	minWidth: 0,
	overflow: 'hidden',
	textOverflow: 'ellipsis',
	whiteSpace: 'nowrap',
};

const remoteSourceTrailing: React.CSSProperties = {
	color: LIGHT_TEXT,
	flexShrink: 0,
	fontFamily: 'Arial, Helvetica, sans-serif',
	fontSize: 12,
	lineHeight: '18px',
	maxWidth: '55%',
	overflow: 'hidden',
	textOverflow: 'ellipsis',
	whiteSpace: 'nowrap',
};

const isEffectsRoot = (
	node: TimelineTreeNode,
): node is Extract<TimelineTreeNode, {kind: 'group'}> => {
	if (node.kind !== 'group' || node.effectInfo !== null) {
		return false;
	}

	const {auxiliaryKeys} = node.nodePathInfo;
	return auxiliaryKeys[auxiliaryKeys.length - 1] === 'effects';
};

const INSPECTOR_COLLAPSED_ROWS_SESSION_STORAGE_KEY =
	'remotion.editor.inspectorCollapsedRows';
const INSPECTOR_SECTION_EXPANSION_OVERRIDES_SESSION_STORAGE_KEY =
	'remotion.editor.inspectorSectionExpansionOverrides';

const getInspectorExpansionKey = (nodePathInfo: SequenceNodePathInfo) => {
	return JSON.stringify(nodePathInfo);
};

const loadInspectorCollapsedKeys = (): ReadonlySet<string> => {
	if (typeof window === 'undefined') {
		return new Set();
	}

	try {
		const raw = window.sessionStorage.getItem(
			INSPECTOR_COLLAPSED_ROWS_SESSION_STORAGE_KEY,
		);
		if (raw === null) {
			return new Set();
		}

		const parsed: unknown = JSON.parse(raw);
		if (!Array.isArray(parsed)) {
			return new Set();
		}

		return new Set(parsed.filter((key) => typeof key === 'string'));
	} catch {
		return new Set();
	}
};

const persistInspectorCollapsedKeys = (keys: ReadonlySet<string>): void => {
	if (typeof window === 'undefined') {
		return;
	}

	try {
		window.sessionStorage.setItem(
			INSPECTOR_COLLAPSED_ROWS_SESSION_STORAGE_KEY,
			JSON.stringify([...keys]),
		);
	} catch {
		// Ignore quota errors or disabled storage.
	}
};

type InspectorSectionExpansionOverrides = Readonly<Record<string, boolean>>;
type AdditionalInspectorSectionId = 'captions' | 'effects';

const loadInspectorSectionExpansionOverrides =
	(): InspectorSectionExpansionOverrides => {
		if (typeof window === 'undefined') {
			return {};
		}

		try {
			const raw = window.sessionStorage.getItem(
				INSPECTOR_SECTION_EXPANSION_OVERRIDES_SESSION_STORAGE_KEY,
			);
			if (raw === null) {
				return {};
			}

			const parsed: unknown = JSON.parse(raw);
			if (
				parsed === null ||
				typeof parsed !== 'object' ||
				Array.isArray(parsed)
			) {
				return {};
			}

			return Object.fromEntries(
				Object.entries(parsed).filter((entry): entry is [string, boolean] => {
					return typeof entry[1] === 'boolean';
				}),
			);
		} catch {
			return {};
		}
	};

const persistInspectorSectionExpansionOverrides = (
	overrides: InspectorSectionExpansionOverrides,
): void => {
	if (typeof window === 'undefined') {
		return;
	}

	try {
		window.sessionStorage.setItem(
			INSPECTOR_SECTION_EXPANSION_OVERRIDES_SESSION_STORAGE_KEY,
			JSON.stringify(overrides),
		);
	} catch {
		// Ignore quota errors or disabled storage.
	}
};

const getInspectorSectionExpansionKey = ({
	nodePathInfo,
	sectionId,
}: {
	readonly nodePathInfo: SequenceNodePathInfo;
	readonly sectionId: SchemaFieldGroupInfo['id'] | AdditionalInspectorSectionId;
}) => {
	return JSON.stringify([
		nodePathInfo.sequenceSubscriptionKey.absolutePath,
		nodePathInfo.sequenceSubscriptionKey.nodePath,
		nodePathInfo.index,
		sectionId,
	]);
};

type SequenceWithControls = TSequence & {
	readonly controls: NonNullable<TSequence['controls']>;
};

type InspectorControlGroup = SchemaFieldGroupInfo & {
	readonly rows: FlatTreeRow[];
};

const getInspectorControlGroups = (
	rows: readonly FlatTreeRow[],
): InspectorControlGroup[] => {
	return SCHEMA_FIELD_GROUPS.map((group) => ({
		...group,
		rows: rows.filter(({node}) => {
			return node.kind === 'field' && node.field?.group === group.id;
		}),
	})).filter((group) => group.rows.length > 0);
};

export const getInspectorSelectableItems = (
	rows: readonly FlatTreeRow[],
): TimelineSelection[] => {
	return rows.flatMap(({node}): TimelineSelection[] => {
		const selection = getTimelineSelectionFromNodePathInfo(node.nodePathInfo);
		return selection ? [selection] : [];
	});
};

export const hasSequenceControls = (
	sequence: TSequence,
): sequence is SequenceWithControls => {
	return sequence.controls !== null;
};

export const InspectorSequenceSection: React.FC<{
	readonly sequence: SequenceWithControls;
	readonly readOnlyStudio: boolean;
	readonly validatedLocation: CodePosition;
	readonly nodePathInfo: SequenceNodePathInfo;
	readonly keyframeDisplayOffset: number;
	readonly keyframePlaybackRate: number;
	readonly sequenceFrameOffset: number;
	readonly renderTransformControls: () => React.ReactNode;
}> = ({
	sequence,
	readOnlyStudio,
	validatedLocation,
	nodePathInfo,
	keyframeDisplayOffset,
	keyframePlaybackRate,
	sequenceFrameOffset,
	renderTransformControls,
}) => {
	const {tree, propStatuses} = useTimelineExpandedTree({
		sequence,
		nodePathInfo,
		includeTextContent: true,
		includeSourceControls: true,
	});
	const {
		consumeInspectorRevealRequest,
		inspectorRevealRequest,
		selectedItems,
		selectItems,
	} = useTimelineSelection();
	const selectedEffect =
		selectedItems.length === 1 &&
		(selectedItems[0].type === 'sequence-effect' ||
			selectedItems[0].type === 'sequence-effect-prop')
			? selectedItems[0]
			: null;
	const selectedEffectKey =
		selectedEffect === null ? null : getTimelineSelectionKey(selectedEffect);
	const inspectorRevealItem = inspectorRevealRequest?.item;
	const inspectorRevealItemKey =
		inspectorRevealItem?.type === 'sequence-effect' ||
		inspectorRevealItem?.type === 'sequence-effect-prop'
			? getTimelineSelectionKey(inspectorRevealItem)
			: null;
	const inspectorRevealToken =
		inspectorRevealItemKey === selectedEffectKey
			? (inspectorRevealRequest?.token ?? null)
			: null;
	const captionsInspectorRef = useRef<HTMLDivElement>(null);
	const onInitialCaptionScrollRef = useRef<(() => void) | null>(null);
	const handledCaptionSelection = useRef<readonly TimelineSelection[] | null>(
		null,
	);
	const pendingCaptionScroll = useRef<{
		readonly selection: readonly TimelineSelection[];
		readonly index: number;
	} | null>(null);
	const selectedEffectRowRef = useRef<HTMLDivElement>(null);
	const scrolledInspectorRevealToken = useRef<number | null>(null);
	const [collapsedKeys, setCollapsedKeys] = useState<ReadonlySet<string>>(
		loadInspectorCollapsedKeys,
	);
	const [sectionExpansionOverrides, setSectionExpansionOverrides] = useState(
		loadInspectorSectionExpansionOverrides,
	);
	const [automaticSectionExpansion, setAutomaticSectionExpansion] = useState<
		Readonly<Record<string, boolean>>
	>({});
	const {manuallyEnabledSequenceKeys, setManuallyEnabled} = useContext(
		Transform3DModeStateContext,
	);
	const sequenceKey = getTimelineSequenceSelectionKey(nodePathInfo);
	const manuallyEnabled3DTransform =
		manuallyEnabledSequenceKeys.has(sequenceKey);
	const {previewServerState} = useContext(StudioServerConnectionCtx);
	const {getDragOverrides} = useContext(
		Internals.VisualModeDragOverridesContext,
	);
	const {setPropStatuses} = useContext(Internals.VisualModeSettersContext);
	const {setSelectedModal} = useContext(SetSelectedModalContext);
	const selectAsset = useSelectAsset();
	const selectMediaSrc = useCallback(
		(values: Readonly<Record<string, unknown>>) =>
			getTimelineAssetSrcFromSchema(sequence.controls, values),
		[sequence.controls],
	);
	const mediaSrc = useRuntimeValueSelector({
		controls: sequence.controls,
		selector: selectMediaSrc,
	});
	const getSourceAction = useCallback(
		(src: string): InspectorSourceAction | null => {
			const linkInfo = getTimelineAssetLinkInfo(src);
			if (linkInfo?.kind === 'local') {
				const fileName =
					linkInfo.assetPath.split('/').pop() ?? linkInfo.assetPath;

				return {
					children: fileName,
					disabled: false,
					onClick: () => openTimelineAssetLink(linkInfo, selectAsset),
					renderIcon: (color: string) => (
						<AssetFileIcon
							color={color}
							fileType={getPreviewFileType(linkInfo.assetPath)}
							style={assetSelectorIcon}
						/>
					),
					'aria-label': linkInfo.assetPath,
				};
			}

			if (linkInfo?.kind === 'remote') {
				const parts = splitRemoteSourceForMiddleEllipsis(linkInfo.href);

				return {
					children: (
						<span style={remoteSourcePartsContainer}>
							<span style={remoteSourceLeading}>{parts.leading}</span>
							<span style={remoteSourceTrailing}>{parts.trailing}</span>
						</span>
					),
					disabled: false,
					onClick: () => openTimelineAssetLink(linkInfo, selectAsset),
					'aria-label': linkInfo.href,
				};
			}

			return null;
		},
		[selectAsset],
	);
	const sourceAction = useMemo(() => {
		return mediaSrc ? getSourceAction(mediaSrc) : null;
	}, [getSourceAction, mediaSrc]);
	const assetSelectionContextValue = useMemo(
		() => ({
			getSourceAction,
			sourceAction,
		}),
		[getSourceAction, sourceAction],
	);

	const getIsExpanded = useCallback(
		(candidate: SequenceNodePathInfo) => {
			return !collapsedKeys.has(getInspectorExpansionKey(candidate));
		},
		[collapsedKeys],
	);

	const toggleTrack = useCallback((candidate: SequenceNodePathInfo) => {
		setCollapsedKeys((prev) => {
			const key = getInspectorExpansionKey(candidate);
			const next = new Set(prev);
			if (next.has(key)) {
				next.delete(key);
			} else {
				next.add(key);
			}

			persistInspectorCollapsedKeys(next);
			return next;
		});
	}, []);

	const isAdditionalSectionExpanded = useCallback(
		(sectionId: AdditionalInspectorSectionId): boolean => {
			const expansionKey = getInspectorSectionExpansionKey({
				nodePathInfo,
				sectionId,
			});
			return sectionExpansionOverrides[expansionKey] ?? true;
		},
		[nodePathInfo, sectionExpansionOverrides],
	);
	const setAdditionalSectionExpanded = useCallback(
		(sectionId: AdditionalInspectorSectionId, expanded: boolean) => {
			const expansionKey = getInspectorSectionExpansionKey({
				nodePathInfo,
				sectionId,
			});
			setSectionExpansionOverrides((previous) => {
				if (previous[expansionKey] === expanded) {
					return previous;
				}

				const next = {...previous, [expansionKey]: expanded};
				persistInspectorSectionExpansionOverrides(next);
				return next;
			});
		},
		[nodePathInfo],
	);
	const toggleAdditionalSection = useCallback(
		(sectionId: AdditionalInspectorSectionId) => {
			setAdditionalSectionExpanded(
				sectionId,
				!isAdditionalSectionExpanded(sectionId),
			);
		},
		[isAdditionalSectionExpanded, setAdditionalSectionExpanded],
	);
	const effectsExpanded = isAdditionalSectionExpanded('effects');
	const {controlRows, effectRows, hasEffects} = useMemo(() => {
		const controlNodes: TimelineTreeNode[] = [];
		let root: Extract<TimelineTreeNode, {kind: 'group'}> | null = null;

		for (const node of tree) {
			if (isEffectsRoot(node)) {
				root = node;
			} else {
				controlNodes.push(node);
			}
		}

		return {
			hasEffects: root !== null && root.children.length > 0,
			controlRows: flattenVisibleTreeNodes({
				nodes: controlNodes,
				getIsExpanded,
			}),
			effectRows:
				root === null || !effectsExpanded
					? []
					: flattenVisibleTreeNodes({
							nodes: root.children,
							getIsExpanded,
						}),
		};
	}, [effectsExpanded, getIsExpanded, tree]);

	useEffect(() => {
		if (
			selectedEffectKey === null ||
			inspectorRevealToken === null ||
			scrolledInspectorRevealToken.current === inspectorRevealToken
		) {
			return;
		}

		const rows = flattenVisibleTreeNodes({
			nodes: tree,
			getIsExpanded: () => true,
		});
		const targetIndex = rows.findIndex(({node}) => {
			const selection = getTimelineSelectionFromNodePathInfo(node.nodePathInfo);
			return (
				selection !== null &&
				getTimelineSelectionKey(selection) === selectedEffectKey
			);
		});
		if (targetIndex === -1) {
			return;
		}

		setAdditionalSectionExpanded('effects', true);
		const parentKeys: string[] = [];
		let {depth} = rows[targetIndex];
		for (let i = targetIndex - 1; i >= 0 && depth > 0; i--) {
			if (rows[i].depth < depth) {
				parentKeys.push(getInspectorExpansionKey(rows[i].node.nodePathInfo));
				depth = rows[i].depth;
			}
		}

		setCollapsedKeys((previous) => {
			if (!parentKeys.some((key) => previous.has(key))) {
				return previous;
			}

			const next = new Set(previous);
			for (const key of parentKeys) {
				next.delete(key);
			}

			persistInspectorCollapsedKeys(next);
			return next;
		});
	}, [
		inspectorRevealToken,
		selectedEffectKey,
		setAdditionalSectionExpanded,
		tree,
	]);

	useEffect(() => {
		if (inspectorRevealToken === null) {
			return;
		}

		if (
			scrolledInspectorRevealToken.current === inspectorRevealToken ||
			selectedEffectRowRef.current === null
		) {
			return;
		}

		selectedEffectRowRef.current.scrollIntoView({block: 'center'});
		scrolledInspectorRevealToken.current = inspectorRevealToken;
	}, [effectRows, inspectorRevealToken]);

	const effectSelectableItems = useMemo(
		() => getInspectorSelectableItems(effectRows),
		[effectRows],
	);
	const controlGroups = useMemo(
		() => getInspectorControlGroups(controlRows),
		[controlRows],
	);
	const layoutGroup = controlGroups.find((group) => group.id === 'layout');
	const controlGroupsWithoutLayout = controlGroups.filter(
		(group) => group.id !== 'layout',
	);
	const sequencePropStatuses = Internals.getPropStatusesCtx(
		propStatuses,
		nodePathInfo.sequenceSubscriptionKey,
	);
	const selectAutomaticallyEnabled3DTransform = useCallback(
		(values: Readonly<Record<string, unknown>>) =>
			[
				'style.translate',
				'style.scale',
				'style.rotate',
				'style.transformOrigin',
			].some((fieldKey) =>
				propStatusHas3DTransformValue({
					fieldKey,
					propStatus: sequencePropStatuses?.[fieldKey],
					runtimeValue: values[fieldKey],
				}),
			),
		[sequencePropStatuses],
	);
	const automaticallyEnabled3DTransform = useRuntimeValueSelector({
		controls: sequence.controls,
		selector: selectAutomaticallyEnabled3DTransform,
	});
	const show3DTransformControls =
		automaticallyEnabled3DTransform || manuallyEnabled3DTransform;
	const onToggle3DTransform = useCallback(() => {
		setManuallyEnabled(sequenceKey, !manuallyEnabled3DTransform);
	}, [manuallyEnabled3DTransform, sequenceKey, setManuallyEnabled]);
	const {schema} = sequence.controls;
	const getControlGroupActivity = useCallback(
		(group: InspectorControlGroup) => {
			if (!isSmartCollapsibleInspectorGroup(group.id)) {
				return 'active' as const;
			}

			return getInspectorSectionActivity({
				group: group.id,
				propStatuses: sequencePropStatuses,
				schema,
			});
		},
		[schema, sequencePropStatuses],
	);
	useEffect(() => {
		setAutomaticSectionExpansion((previous) => {
			let changed = false;
			const next = {...previous};
			for (const group of controlGroups) {
				if (!isSmartCollapsibleInspectorGroup(group.id)) {
					continue;
				}

				const expansionKey = getInspectorSectionExpansionKey({
					nodePathInfo,
					sectionId: group.id,
				});
				const activity = getControlGroupActivity(group);
				if (activity === 'active' && next[expansionKey] !== true) {
					next[expansionKey] = true;
					changed = true;
				} else if (
					activity === 'inactive' &&
					next[expansionKey] === undefined
				) {
					next[expansionKey] = false;
					changed = true;
				}
			}

			return changed ? next : previous;
		});
	}, [controlGroups, getControlGroupActivity, nodePathInfo]);
	const isControlGroupExpanded = useCallback(
		(group: InspectorControlGroup): boolean => {
			const expansionKey = getInspectorSectionExpansionKey({
				nodePathInfo,
				sectionId: group.id,
			});
			const override = sectionExpansionOverrides[expansionKey];
			if (override !== undefined) {
				return override;
			}

			if (!isSmartCollapsibleInspectorGroup(group.id)) {
				return true;
			}

			const automaticExpansion = automaticSectionExpansion[expansionKey];
			if (automaticExpansion !== undefined) {
				return automaticExpansion;
			}

			return getControlGroupActivity(group) !== 'inactive';
		},
		[
			automaticSectionExpansion,
			getControlGroupActivity,
			nodePathInfo,
			sectionExpansionOverrides,
		],
	);
	useEffect(() => {
		if (
			readOnlyStudio ||
			!isStudioInteractivityEnabled() ||
			inspectorRevealRequest?.item.type !== 'sequence-prop' ||
			inspectorRevealRequest.item.key !== 'children' ||
			selectedItems.length !== 1 ||
			getTimelineSelectionKey(selectedItems[0]) !==
				getTimelineSelectionKey(inspectorRevealRequest.item) ||
			getTimelineSequenceSelectionKey(
				inspectorRevealRequest.item.nodePathInfo,
			) !== sequenceKey
		) {
			return;
		}

		const group = controlGroups.find((candidate) =>
			candidate.rows.some(
				({node}) => node.kind === 'field' && node.field?.key === 'children',
			),
		);
		if (!group) {
			return;
		}

		if (!isControlGroupExpanded(group)) {
			const expansionKey = getInspectorSectionExpansionKey({
				nodePathInfo,
				sectionId: group.id,
			});
			setSectionExpansionOverrides((previous) => {
				const next = {...previous, [expansionKey]: true};
				persistInspectorSectionExpansionOverrides(next);
				return next;
			});
			return;
		}

		// Ref registration happens during commit, including when expanding Text
		// mounted the textarea. Consume the request so later remounts do not focus it.
		if (
			requestFocusInspectorField({
				fieldKey: 'children',
				nodePath: nodePathInfo.sequenceSubscriptionKey,
			})
		) {
			consumeInspectorRevealRequest(inspectorRevealRequest.token);
		}
	}, [
		consumeInspectorRevealRequest,
		controlGroups,
		inspectorRevealRequest,
		isControlGroupExpanded,
		nodePathInfo,
		readOnlyStudio,
		selectedItems,
		sequenceKey,
	]);
	const toggleControlGroup = useCallback(
		(group: InspectorControlGroup) => {
			const expansionKey = getInspectorSectionExpansionKey({
				nodePathInfo,
				sectionId: group.id,
			});
			const nextExpanded = !isControlGroupExpanded(group);
			setSectionExpansionOverrides((previous) => {
				const next = {...previous, [expansionKey]: nextExpanded};
				persistInspectorSectionExpansionOverrides(next);
				return next;
			});
		},
		[isControlGroupExpanded, nodePathInfo],
	);
	const captionsExpanded = isAdditionalSectionExpanded('captions');
	const visibleControlRows = controlGroups.flatMap((group) => {
		return isControlGroupExpanded(group) ? group.rows : [];
	});
	const controlSelectableItems = useMemo(
		() =>
			getInspectorSelectableItems(
				visibleControlRows.filter(
					({node}) => node.kind !== 'field' || node.field?.key !== 'src',
				),
			),
		[visibleControlRows],
	);

	const borderRadiusGroup = controlGroups.find(
		(group) => group.id === 'border-radius',
	);
	const borderRadiusUsesShorthand = borderRadiusGroup?.rows.some(
		({node}) =>
			node.kind === 'field' && node.field?.key === BORDER_RADIUS_SHORTHAND_KEY,
	);
	const borderRadiusConversion = getBorderRadiusConversion(
		sequencePropStatuses,
		getDragOverrides(nodePathInfo.sequenceSubscriptionKey),
	);
	const hasCaptionsSchema = schema.captions?.type === 'remotion-captions';
	const runtimeCaptions = useRuntimeValue(
		hasCaptionsSchema ? sequence.controls : null,
		'captions',
	);
	const inlineCaptions = useMemo(
		() =>
			hasCaptionsSchema
				? Array.isArray(runtimeCaptions)
					? (runtimeCaptions as Caption[])
					: []
				: null,
		[hasCaptionsSchema, runtimeCaptions],
	);
	const {
		getCurrentFrame: getCommittedSequenceFrame,
		frozenFrame: sequenceFrozenFrame,
		from: sequenceFrom,
		sequencePlaybackRate,
	} = sequence;
	const getSequenceFrame = useCallback(
		() =>
			getCommittedSequenceFrame?.() ??
			sequenceFrozenFrame ??
			(getCurrentFrame() - sequenceFrom) * sequencePlaybackRate +
				sequenceFrameOffset,
		[
			getCommittedSequenceFrame,
			sequenceFrozenFrame,
			sequenceFrom,
			sequencePlaybackRate,
			sequenceFrameOffset,
		],
	);
	useEffect(() => {
		if (handledCaptionSelection.current === selectedItems) {
			return;
		}

		pendingCaptionScroll.current = null;
		if (
			selectedItems.length !== 1 ||
			selectedItems[0].type !== 'sequence' ||
			getTimelineSequenceSelectionKey(selectedItems[0].nodePathInfo) !==
				sequenceKey ||
			inlineCaptions === null
		) {
			handledCaptionSelection.current = selectedItems;
			return;
		}

		if (inlineCaptions.length === 0) {
			return;
		}

		handledCaptionSelection.current = selectedItems;
		const timeMs = (getSequenceFrame() / getCurrentFps()) * 1000;
		const index = inlineCaptions.findIndex(
			(caption) => caption.startMs <= timeMs && caption.endMs > timeMs,
		);
		if (index === -1) {
			return;
		}

		pendingCaptionScroll.current = {selection: selectedItems, index};
		setAdditionalSectionExpanded('captions', true);
	}, [
		getSequenceFrame,
		inlineCaptions,
		selectedItems,
		sequenceKey,
		setAdditionalSectionExpanded,
	]);

	const revealPendingCaption = useCallback(() => {
		const pending = pendingCaptionScroll.current;
		if (
			!captionsExpanded ||
			pending === null ||
			pending.selection !== selectedItems
		) {
			return;
		}

		const input = captionsInspectorRef.current?.querySelector<HTMLInputElement>(
			`[data-caption-index="${pending.index}"]`,
		);
		if (!input) {
			return;
		}

		input.scrollIntoView({block: 'center'});
		onInitialCaptionScrollRef.current?.();
		pendingCaptionScroll.current = null;
	}, [captionsExpanded, selectedItems]);

	useEffect(() => {
		revealPendingCaption();
	}, [inlineCaptions, revealPendingCaption]);

	const showEffectsSection = nodePathInfo.supportsEffects || hasEffects;
	const canAddEffect =
		nodePathInfo.supportsEffects &&
		previewServerState.type === 'connected' &&
		isStudioInteractivityEnabled() &&
		Boolean(validatedLocation.source);

	const onAddEffect = useCallback(() => {
		if (!canAddEffect || previewServerState.type !== 'connected') {
			return;
		}

		setAdditionalSectionExpanded('effects', true);

		setSelectedModal({
			type: 'add-effect',
			clientId: previewServerState.clientId,
			fileName: validatedLocation.source,
			nodePathInfo,
			selectItems,
		});
	}, [
		canAddEffect,
		nodePathInfo,
		previewServerState,
		setAdditionalSectionExpanded,
		setSelectedModal,
		selectItems,
		validatedLocation.source,
	]);

	const onConvertBorderRadius = useCallback(() => {
		if (
			borderRadiusConversion === null ||
			previewServerState.type !== 'connected'
		) {
			return;
		}

		const common = {
			fileName: validatedLocation.source,
			nodePath: nodePathInfo.sequenceSubscriptionKey,
			defaultValue: null,
			schema,
		};
		const changes = getBorderRadiusConversionChanges(
			borderRadiusConversion,
		).map((change) => ({...common, ...change}));

		saveSequenceProps({
			changes,
			addedKeyframes: null,
			movedKeyframes: null,
			setPropStatuses,
			clientId: previewServerState.clientId,
			undoLabel: 'Change border radius representation',
			redoLabel: 'Change border radius representation again',
		});
	}, [
		borderRadiusConversion,
		nodePathInfo.sequenceSubscriptionKey,
		previewServerState,
		schema,
		setPropStatuses,
		validatedLocation.source,
	]);

	const borderRadiusActionLabel =
		borderRadiusConversion === null
			? borderRadiusUsesShorthand
				? 'A static border radius is required to use individual corners'
				: 'All four corners must have the same static value'
			: borderRadiusUsesShorthand
				? 'Use individual corner radii'
				: 'Use one border radius value';
	const borderRadiusAction = borderRadiusGroup ? (
		<ActionTooltip
			label={borderRadiusActionLabel}
			shortcut={null}
			delay={800}
			dismissOnClick
		>
			<InlineAction
				variant={null}
				disabled={
					borderRadiusConversion === null ||
					previewServerState.type !== 'connected'
				}
				onClick={onConvertBorderRadius}
				aria-label={borderRadiusActionLabel}
				renderAction={(color) =>
					borderRadiusUsesShorthand ? (
						<FullscreenIcon color={color} style={borderRadiusToggleIcon} />
					) : (
						<BorderRadiusIcon color={color} style={borderRadiusToggleIcon} />
					)
				}
			/>
		</ActionTooltip>
	) : null;

	const transform3DActionLabel = automaticallyEnabled3DTransform
		? '3D controls are required by the current transform values'
		: show3DTransformControls
			? 'Hide 3D controls'
			: 'Show 3D controls';
	const transform3DAction = (
		<ActionTooltip
			label={transform3DActionLabel}
			shortcut={null}
			delay={800}
			dismissOnClick
		>
			<InlineAction
				variant={null}
				disabled={automaticallyEnabled3DTransform}
				onClick={onToggle3DTransform}
				aria-label={transform3DActionLabel}
				hoveredColor={show3DTransformControls ? BLUE : undefined}
				unhoveredColor={show3DTransformControls ? BLUE : LIGHT_TEXT}
				renderAction={(color) => (
					<CubeIcon color={color} style={transform3DToggleIcon} />
				)}
			/>
		</ActionTooltip>
	);

	const effectsHeader = (
		<CollapsibleInspectorSectionHeader
			action={
				<ActionTooltip
					label="Add effect"
					shortcut={null}
					delay={800}
					dismissOnClick
				>
					<InlineAction
						variant={null}
						disabled={!canAddEffect}
						onClick={onAddEffect}
						aria-label="Add effect"
						renderAction={(color) => <Plus color={color} style={plusIcon} />}
					/>
				</ActionTooltip>
			}
			expanded={effectsExpanded}
			label="Effects"
			onToggle={hasEffects ? () => toggleAdditionalSection('effects') : null}
		/>
	);

	const renderRow = ({node, depth}: FlatTreeRow) => {
		return (
			<TimelineRowLayoutContext.Provider
				key={JSON.stringify(node.nodePathInfo)}
				value={INSPECTOR_TIMELINE_ROW_LAYOUT}
			>
				<TimelineExpandedRow
					node={node}
					depth={depth}
					nestedDepth={0}
					rowDepthBase={0}
					getIsExpanded={getIsExpanded}
					toggleTrack={toggleTrack}
					validatedLocation={validatedLocation}
					nodePath={nodePathInfo.sequenceSubscriptionKey}
					schema={schema}
					keyframeDisplayOffset={keyframeDisplayOffset}
					keyframePlaybackRate={keyframePlaybackRate}
					keyframeControlsMode="inspector"
				/>
			</TimelineRowLayoutContext.Provider>
		);
	};

	const renderControlGroupHeader = (group: InspectorControlGroup) => {
		const expanded = isControlGroupExpanded(group);
		return (
			<CollapsibleInspectorSectionHeader
				action={
					expanded
						? group.id === 'border-radius'
							? borderRadiusAction
							: group.id === 'transforms'
								? transform3DAction
								: null
						: null
				}
				expanded={expanded}
				label={group.label}
				onToggle={() => toggleControlGroup(group)}
			/>
		);
	};

	const captionsInspector = inlineCaptions ? (
		<div key={sequenceKey} ref={captionsInspectorRef}>
			<InlineCaptionInspector
				captions={inlineCaptions}
				controls={sequence.controls}
				expanded={captionsExpanded}
				getSequenceFrame={getSequenceFrame}
				nodePath={nodePathInfo.sequenceSubscriptionKey}
				onCaptionsRendered={revealPendingCaption}
				onInitialCaptionScrollRef={onInitialCaptionScrollRef}
				onToggle={() => toggleAdditionalSection('captions')}
				readOnlyStudio={readOnlyStudio}
				validatedLocation={validatedLocation}
			/>
		</div>
	) : null;

	if (
		controlRows.length === 0 &&
		!showEffectsSection &&
		inlineCaptions === null
	) {
		return null;
	}

	return (
		<AssetSelectionContext.Provider value={assetSelectionContextValue}>
			<div style={container}>
				{controlRows.length > 0 ? (
					<TimelineSelectionOrderProvider items={controlSelectableItems}>
						{controlGroupsWithoutLayout.map((group) => (
							<React.Fragment key={group.id}>
								{group.id === 'transforms' ? captionsInspector : null}
								<InspectorSection header={renderControlGroupHeader(group)}>
									{isControlGroupExpanded(group) ? (
										group.id === 'transforms' ? (
											<Transform3DModeContext.Provider
												value={show3DTransformControls}
											>
												{renderTransformControls()}
												{group.rows.map(renderRow)}
											</Transform3DModeContext.Provider>
										) : (
											group.rows.map(renderRow)
										)
									) : null}
								</InspectorSection>
							</React.Fragment>
						))}
					</TimelineSelectionOrderProvider>
				) : null}
				{controlGroupsWithoutLayout.every((group) => group.id !== 'transforms')
					? captionsInspector
					: null}
				{showEffectsSection ? (
					<InspectorSection header={effectsHeader}>
						{effectRows.length > 0 ? (
							<TimelineSelectionOrderProvider items={effectSelectableItems}>
								{effectRows.map((row) => {
									const selection = getTimelineSelectionFromNodePathInfo(
										row.node.nodePathInfo,
									);
									const selected =
										selection !== null &&
										getTimelineSelectionKey(selection) === selectedEffectKey;
									return (
										<div
											key={getInspectorExpansionKey(row.node.nodePathInfo)}
											ref={selected ? selectedEffectRowRef : null}
										>
											{renderRow(row)}
										</div>
									);
								})}
							</TimelineSelectionOrderProvider>
						) : null}
					</InspectorSection>
				) : null}
				{layoutGroup ? (
					<TimelineSelectionOrderProvider items={controlSelectableItems}>
						<InspectorSection header={renderControlGroupHeader(layoutGroup)}>
							{isControlGroupExpanded(layoutGroup)
								? layoutGroup.rows.map(renderRow)
								: null}
						</InspectorSection>
					</TimelineSelectionOrderProvider>
				) : null}
			</div>
		</AssetSelectionContext.Provider>
	);
};