import type {EventSourceEvent} from '@remotion/studio-shared'; import React, {useCallback, useContext, useEffect, useState} from 'react'; import {getBrowserStudioOperations} from '../helpers/browser-studio-operations'; import {StudioServerConnectionCtx} from '../helpers/client-id'; import {LIGHT_TEXT} from '../helpers/colors'; import { getOptimisticEffectHistorySnapshot, subscribeToOptimisticEffectHistory, clearOptimisticEffectHistoryIfIdle, } from '../helpers/optimistic-effect-mutations'; import {getSourceMutationRevision} from '../helpers/source-mutation-queue'; import {pushUrl} from '../helpers/url-state'; import { areKeyboardShortcutsDisabled, useKeybinding, } from '../helpers/use-keybinding'; import { useKeyboardShortcutAriaKeyShortcuts, useKeyboardShortcutLabel, } from '../helpers/use-keyboard-shortcut-label'; import {useSyncExternalStore} from '../helpers/use-sync-external-store'; import {RedoIcon} from '../icons/redo'; import {UndoIcon} from '../icons/undo'; import {ActionTooltip} from './ActionTooltip'; import {callApi} from './call-api'; import type {RenderInlineAction} from './InlineAction'; import {InlineAction} from './InlineAction'; const iconStyle: React.CSSProperties = { width: 14, height: 14, }; export const UndoRedoButtons: React.FC = () => { const [undoFile, setUndoFile] = useState(null); const [redoFile, setRedoFile] = useState(null); const {subscribeToEvent} = useContext(StudioServerConnectionCtx); const keybindings = useKeybinding(); useEffect(() => { const unsub = subscribeToEvent( 'undo-redo-stack-changed', (event: EventSourceEvent) => { if (event.type !== 'undo-redo-stack-changed') { return; } setUndoFile(event.undoFile); setRedoFile(event.redoFile); if (event.undoFile === null && event.redoFile === null) clearOptimisticEffectHistoryIfIdle(); }, ); return () => unsub(); }, [subscribeToEvent]); const onUndo = useCallback(() => { const browserStudioOperations = getBrowserStudioOperations(); const promise = browserStudioOperations ? browserStudioOperations.undo() : callApi('/api/undo', {}); const revision = getSourceMutationRevision(); promise .then((response) => { if ( revision === getSourceMutationRevision() && response.success && response.route !== null ) { pushUrl(response.route); } }) .catch(() => { // Ignore errors }); }, []); const onRedo = useCallback(() => { const browserStudioOperations = getBrowserStudioOperations(); const promise = browserStudioOperations ? browserStudioOperations.redo() : callApi('/api/redo', {}); const revision = getSourceMutationRevision(); promise .then((response) => { if ( revision === getSourceMutationRevision() && response.success && response.route !== null ) { pushUrl(response.route); } }) .catch(() => { // Ignore errors }); }, []); useEffect(() => { // Visual controls such as the color picker commit edits while their popup // remains open. Keep undo and redo available in that higher z-index context. const undo = keybindings.registerKeybinding({ event: 'keydown', action: 'undo', callback: () => { onUndo(); }, preventDefault: true, triggerIfInputFieldFocused: false, keepRegisteredWhenNotHighestContext: true, }); const redo = keybindings.registerKeybinding({ event: 'keydown', action: 'redo', callback: () => { onRedo(); }, preventDefault: true, triggerIfInputFieldFocused: false, keepRegisteredWhenNotHighestContext: true, }); return () => { undo.unregister(); redo.unregister(); }; }, [keybindings, onRedo, onUndo]); const undoShortcut = useKeyboardShortcutLabel('undo'); const redoShortcut = useKeyboardShortcutLabel('redo'); const undoAriaShortcut = useKeyboardShortcutAriaKeyShortcuts('undo'); const redoAriaShortcut = useKeyboardShortcutAriaKeyShortcuts('redo'); const shortcutsDisabled = areKeyboardShortcutsDisabled(); const renderUndo: RenderInlineAction = useCallback((color) => { return ; }, []); const renderRedo: RenderInlineAction = useCallback((color) => { return ; }, []); const optimisticHistory = useSyncExternalStore( subscribeToOptimisticEffectHistory, getOptimisticEffectHistorySnapshot, getOptimisticEffectHistorySnapshot, ); const canUndo = optimisticHistory.canUndo ?? undoFile !== null; const canRedo = optimisticHistory.canRedo ?? redoFile !== null; if (!canUndo && !canRedo) { return null; } return ( <> ); };