Skip to content

🎥 Make videos programmatically with React

5.8 KB
Raw Blame
import type {EditorPickerId} from '@remotion/studio-shared';
import type {SetStateAction} from 'react';
import type {ResolvedStackLocation, _InternalTypes} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
import {copyText} from '../helpers/copy-text';
import {
	formatContextForAgents,
	formatFileLocation,
} from '../helpers/format-file-location';
import {getFolderId} from '../helpers/get-folder-id';
import {openOriginalPositionInEditor} from '../helpers/open-in-editor';
import type {PreviewServerConnectionState} from '../helpers/preview-server-events';
import type {ModalState} from '../state/modals';
import type {ComboboxValue} from './NewComposition/ComboBox';
import {showNotification} from './Notifications/NotificationCenter';

export const getFolderMenuItems = ({
	closeMenu,
	connectionStatus,
	folder,
	readOnlyStudio,
	resolvedLocation,
	setSelectedModal,
	editorId,
	editorName,
}: {
	closeMenu: () => void;
	connectionStatus: PreviewServerConnectionState['type'];
	folder: _InternalTypes['TFolder'];
	readOnlyStudio: boolean;
	resolvedLocation: ResolvedStackLocation | null;
	setSelectedModal: (value: SetStateAction<ModalState | null>) => void;
	editorId: EditorPickerId | null;
	editorName: string | null;
}): ComboboxValue[] => {
	const folderId = getFolderId({
		folderName: folder.name,
		parentName: folder.parent,
	});
	const fileLocation = formatFileLocation({
		location: resolvedLocation,
		root: window.remotion_cwd,
	});
	const contextForAgents = formatContextForAgents({
		location: resolvedLocation,
		name: folderId,
		root: window.remotion_cwd,
	});
	const showInEditorDisabled =
		!editorId || connectionStatus !== 'connected' || !resolvedLocation;
	const copyFileLocationDisabled = !fileLocation;
	const codemodDisabled = readOnlyStudio || !folder.stack;

	return [
		editorName
			? {
					id: 'show-folder-in-editor',
					keyHint: null,
					label: `Show folder in ${editorName}`,
					leftItem: null,
					onClick: async () => {
						closeMenu();
						if (!resolvedLocation || !editorId) {
							return;
						}

						try {
							await openOriginalPositionInEditor(resolvedLocation, editorId);
						} catch (err) {
							showNotification((err as Error).message, 2000);
						}
					},
					quickSwitcherLabel: `Show folder in ${editorName}`,
					subMenu: null,
					type: 'item' as const,
					value: 'show-folder-in-editor',
					disabled: showInEditorDisabled,
				}
			: null,
		{
			id: 'copy-context-for-agents',
			keyHint: null,
			label: `Copy context for agents`,
			leftItem: null,
			onClick: () => {
				closeMenu();
				if (!contextForAgents) {
					return;
				}

				copyText(contextForAgents).catch((err) => {
					showNotification(
						`Could not copy to clipboard: ${(err as Error).message}`,
						1000,
					);
				});
			},
			quickSwitcherLabel: null,
			subMenu: null,
			type: 'item' as const,
			value: 'copy-context-for-agents',
			disabled: !contextForAgents,
		},
		{
			id: 'copy-folder-file-location',
			keyHint: null,
			label: `Copy file location`,
			leftItem: null,
			onClick: () => {
				closeMenu();
				if (!fileLocation) {
					return;
				}

				copyText(fileLocation)
					.then(() => {
						showNotification('Copied file location to clipboard', 1000);
					})
					.catch((err) => {
						showNotification(
							`Could not copy to clipboard: ${(err as Error).message}`,
							1000,
						);
					});
			},
			quickSwitcherLabel: 'Copy folder file location',
			subMenu: null,
			type: 'item' as const,
			value: 'copy-folder-file-location',
			disabled: copyFileLocationDisabled,
		},
		{
			id: 'copy-folder-id',
			keyHint: null,
			label: `Copy ID`,
			leftItem: null,
			onClick: () => {
				closeMenu();
				copyText(folderId)
					.then(() => {
						showNotification('Copied to clipboard', 1000);
					})
					.catch((err) => {
						showNotification(
							`Could not copy to clipboard: ${(err as Error).message}`,
							1000,
						);
					});
			},
			quickSwitcherLabel: 'Copy folder ID',
			subMenu: null,
			type: 'item' as const,
			value: 'copy-folder-id',
			disabled: false,
		},
		{type: 'divider' as const, id: 'show-folder-in-editor-divider'},
		{
			id: 'new-composition-in-folder',
			keyHint: null,
			label: `New composition...`,
			leftItem: null,
			onClick: () => {
				closeMenu();
				setSelectedModal({
					type: 'new-comp',
					folderName: folder.name,
					parentName: folder.parent,
					stack: folder.stack,
					canvasCapture: null,
				});
			},
			quickSwitcherLabel: 'New composition in folder...',
			subMenu: null,
			type: 'item' as const,
			value: 'new-composition-in-folder',
			disabled: codemodDisabled,
		},
		{
			id: 'new-folder-in-folder',
			keyHint: null,
			label: `New folder...`,
			leftItem: null,
			onClick: () => {
				closeMenu();
				setSelectedModal({
					type: 'new-folder',
					parentName: folderId,
					stack: folder.stack,
				});
			},
			quickSwitcherLabel: 'New folder in folder...',
			subMenu: null,
			type: 'item' as const,
			value: 'new-folder-in-folder',
			disabled: codemodDisabled,
		},
		{
			id: 'rename-folder',
			keyHint: null,
			label: `Rename...`,
			leftItem: null,
			onClick: () => {
				closeMenu();
				setSelectedModal({
					type: 'rename-folder',
					folderName: folder.name,
					parentName: folder.parent,
					stack: folder.stack,
				});
			},
			quickSwitcherLabel: 'Rename folder...',
			subMenu: null,
			type: 'item' as const,
			value: 'rename-folder',
			disabled: codemodDisabled,
		},
		{
			id: 'delete-folder',
			keyHint: null,
			label: `Delete...`,
			leftItem: null,
			onClick: () => {
				closeMenu();
				setSelectedModal({
					type: 'delete-folder',
					folderName: folder.name,
					parentName: folder.parent,
					stack: folder.stack,
				});
			},
			quickSwitcherLabel: 'Delete folder...',
			subMenu: null,
			type: 'item' as const,
			value: 'delete-folder',
			disabled: codemodDisabled,
		},
	].filter(NoReactInternals.truthy);
};