Create polished demo videos without editing skills. Mac/Windows/Linux
import type { Dispatch, MutableRefObject, RefObject, SetStateAction } from "react";
import { useCallback } from "react";
import { toast } from "@/components/ui/toast";
import type { useI18n } from "@/contexts/I18nContext";
import type { useShortcuts } from "@/contexts/ShortcutsContext";
import { useVideoEditorAudio } from "../audio/useVideoEditorAudio";
import type { useAppearanceState } from "../state/useAppearanceState";
import type { useTimelineState } from "../state/useTimelineState";
import type { TimelineEditorHandle } from "../timeline/TimelineEditor";
import type { EditorEffectSection } from "../types";
import type { VideoPlaybackRef } from "../VideoPlayback";
import { getErrorMessage, summarizeErrorMessage } from "../videoEditorUtils";
import { useAnnotationRegionCommands } from "./useAnnotationRegionCommands";
import { useAudioRegionCommands } from "./useAudioRegionCommands";
import { useCaptionCommands } from "./useCaptionCommands";
import { useClipRegionCommands } from "./useClipRegionCommands";
import { useCursorTelemetry } from "./useCursorTelemetry";
import { useEditorGlobalInteractions } from "./useEditorGlobalInteractions";
import { useEditorPlaybackControls } from "./useEditorPlaybackControls";
import { useFreshRecordingAutoZoom } from "./useFreshRecordingAutoZoom";
import { useTimelineProjection } from "./useTimelineProjection";
import { useZoomRegionCommands } from "./useZoomRegionCommands";
type Input = {
t: ReturnType<typeof useI18n>["t"];
shortcuts: ReturnType<typeof useShortcuts>["shortcuts"];
isMac: boolean;
appPlatform: string;
timeline: ReturnType<typeof useTimelineState>;
appearance: ReturnType<typeof useAppearanceState>;
videoPath: string | null;
videoSourcePath: string | null;
currentSourcePath: string | null;
duration: number;
currentTime: number;
isPlaying: boolean;
previewVolume: number;
loading: boolean;
isPreviewReady: boolean;
setActiveEffectSection: Dispatch<SetStateAction<EditorEffectSection>>;
setAutoSuggestZoomsTrigger: Dispatch<SetStateAction<number>>;
videoPlaybackRef: RefObject<VideoPlaybackRef | null>;
timelineRef: RefObject<TimelineEditorHandle | null>;
nextZoomIdRef: MutableRefObject<number>;
nextClipIdRef: MutableRefObject<number>;
nextAudioIdRef: MutableRefObject<number>;
nextAnnotationIdRef: MutableRefObject<number>;
nextAnnotationZIndexRef: MutableRefObject<number>;
clipInitializedRef: MutableRefObject<boolean>;
autoFullTrackClipIdRef: MutableRefObject<string | null>;
autoFullTrackClipEndMsRef: MutableRefObject<number | null>;
autoSuggestedVideoPathRef: MutableRefObject<string | null>;
pendingFreshRecordingAutoZoomPathRef: MutableRefObject<string | null>;
pendingFreshRecordingAutoSuggestTimeoutRef: MutableRefObject<number | null>;
pendingFreshRecordingAutoSuggestTelemetryCountRef: MutableRefObject<number>;
handleUndo: () => void;
handleRedo: () => void;
};
export function useTimelineEditingController(input: Input) {
const { timeline } = input;
const handleSourceFallbackLoadError = useCallback((error: unknown) => {
toast.warning(
`Could not load companion audio source: ${summarizeErrorMessage(getErrorMessage(error))}`,
{ duration: 10000 },
);
}, []);
const cursor = useCursorTelemetry({
videoPath: input.videoPath,
videoSourcePath: input.videoSourcePath,
duration: input.duration,
loopCursor: input.appearance.loopCursor,
timeline,
pendingFreshRecordingAutoZoomPathRef: input.pendingFreshRecordingAutoZoomPathRef,
autoSuggestedVideoPathRef: input.autoSuggestedVideoPathRef,
});
const projection = useTimelineProjection({
timeline,
duration: input.duration,
currentTime: input.currentTime,
nextClipIdRef: input.nextClipIdRef,
initializedRef: input.clipInitializedRef,
autoFullTrackIdRef: input.autoFullTrackClipIdRef,
autoFullTrackEndRef: input.autoFullTrackClipEndMsRef,
});
const audio = useVideoEditorAudio({
currentSourcePath: input.currentSourcePath,
selectedClipId: timeline.selectedClipId,
clipRegions: timeline.clipRegions,
audioRegions: timeline.audioRegions,
sourceAudioTrackSettingsByClip: timeline.sourceAudioTrackSettingsByClip,
setSourceAudioTrackSettingsByClip: timeline.setSourceAudioTrackSettingsByClip,
defaultSourceAudioTrackSettings: timeline.defaultSourceAudioTrackSettings,
setDefaultSourceAudioTrackSettings: timeline.setDefaultSourceAudioTrackSettings,
timelineTime: projection.timelinePlayheadTime,
currentTime: projection.mapTimelineTimeToSourceTime(input.currentTime * 1000) / 1000,
duration: input.duration,
isPlaying: input.isPlaying,
previewVolume: input.previewVolume,
sourceAudioFallbackRefreshKey: timeline.sourceAudioFallbackRefreshKey,
summarizeErrorMessage,
onSourceFallbackLoadError: handleSourceFallbackLoadError,
});
const playback = useEditorPlaybackControls({
videoPlaybackRef: input.videoPlaybackRef,
timelineRef: input.timelineRef,
playSourceAudioPreview: audio.playSourceAudioPreview,
timelinePlayheadTime: projection.timelinePlayheadTime,
timelineDuration: projection.timelineDuration,
});
const captionCommands = useCaptionCommands({
clipRegions: timeline.clipRegions,
autoCaptions: timeline.autoCaptions,
setAutoCaptions: timeline.setAutoCaptions,
setAutoCaptionSettings: timeline.setAutoCaptionSettings,
setSelectedCaptionId: timeline.setSelectedCaptionId,
setSelectedZoomId: timeline.setSelectedZoomId,
setSelectedClipId: timeline.setSelectedClipId,
setSelectedAnnotationId: timeline.setSelectedAnnotationId,
setSelectedAudioId: timeline.setSelectedAudioId,
setActiveEffectSection: input.setActiveEffectSection,
videoPlaybackRef: input.videoPlaybackRef,
mapSourceTimeToTimelineTime: projection.mapSourceTimeToTimelineTime,
handleSeek: playback.handleSeek,
});
const zoomCommands = useZoomRegionCommands({
setSelectedClipId: timeline.setSelectedClipId,
videoPath: input.videoPath,
setZoomRegions: timeline.setZoomRegions,
selectedZoomId: timeline.selectedZoomId,
setSelectedZoomId: timeline.setSelectedZoomId,
setSelectedAnnotationId: timeline.setSelectedAnnotationId,
setSelectedAudioId: timeline.setSelectedAudioId,
setSelectedCaptionId: timeline.setSelectedCaptionId,
setActiveEffectSection: input.setActiveEffectSection,
nextZoomIdRef: input.nextZoomIdRef,
autoSuggestedVideoPathRef: input.autoSuggestedVideoPathRef,
pendingFreshRecordingAutoZoomPathRef: input.pendingFreshRecordingAutoZoomPathRef,
});
const handleSelectAnnotation = useCallback(
(id: string | null) => {
timeline.setSelectedAnnotationId(id);
if (id) {
timeline.setSelectedClipId(null);
timeline.setSelectedZoomId(null);
timeline.setSelectedAudioId(null);
timeline.setSelectedCaptionId(null);
}
},
[
timeline.setSelectedAnnotationId,
timeline.setSelectedClipId,
timeline.setSelectedZoomId,
timeline.setSelectedAudioId,
timeline.setSelectedCaptionId,
],
);
const freshZoom = useFreshRecordingAutoZoom({
appPlatform: input.appPlatform,
videoPath: input.videoPath,
loading: input.loading,
isPreviewReady: input.isPreviewReady,
duration: input.duration,
cursorTelemetryCount: timeline.cursorTelemetry.length,
normalizedCursorTelemetry: cursor.normalizedCursorTelemetry,
zoomRegions: timeline.zoomRegions,
setZoomRegions: timeline.setZoomRegions,
setAutoSuggestZoomsTrigger: input.setAutoSuggestZoomsTrigger,
videoPlaybackRef: input.videoPlaybackRef,
autoSuggestedVideoPathRef: input.autoSuggestedVideoPathRef,
pendingFreshRecordingAutoZoomPathRef: input.pendingFreshRecordingAutoZoomPathRef,
pendingFreshRecordingAutoSuggestTimeoutRef:
input.pendingFreshRecordingAutoSuggestTimeoutRef,
pendingFreshRecordingAutoSuggestTelemetryCountRef:
input.pendingFreshRecordingAutoSuggestTelemetryCountRef,
});
const clipCommands = useClipRegionCommands({
setAnnotationRegions: timeline.setAnnotationRegions,
setAudioRegions: timeline.setAudioRegions,
sourceDurationMs: input.duration * 1000,
clipRegions: timeline.clipRegions,
setClipRegions: timeline.setClipRegions,
zoomRegions: timeline.zoomRegions,
setZoomRegions: timeline.setZoomRegions,
selectedClipId: timeline.selectedClipId,
setSelectedClipId: timeline.setSelectedClipId,
setSelectedZoomId: timeline.setSelectedZoomId,
setSelectedAnnotationId: timeline.setSelectedAnnotationId,
setSelectedAudioId: timeline.setSelectedAudioId,
setSelectedCaptionId: timeline.setSelectedCaptionId,
setActiveEffectSection: input.setActiveEffectSection,
nextClipIdRef: input.nextClipIdRef,
t: input.t,
});
const audioCommands = useAudioRegionCommands({
setSelectedClipId: timeline.setSelectedClipId,
setAudioRegions: timeline.setAudioRegions,
selectedAudioId: timeline.selectedAudioId,
setSelectedAudioId: timeline.setSelectedAudioId,
setSelectedZoomId: timeline.setSelectedZoomId,
setSelectedAnnotationId: timeline.setSelectedAnnotationId,
setSelectedCaptionId: timeline.setSelectedCaptionId,
setActiveEffectSection: input.setActiveEffectSection,
nextAudioIdRef: input.nextAudioIdRef,
});
const annotationCommands = useAnnotationRegionCommands({
onSelectAnnotation: handleSelectAnnotation,
setAnnotationRegions: timeline.setAnnotationRegions,
selectedAnnotationId: timeline.selectedAnnotationId,
setSelectedAnnotationId: timeline.setSelectedAnnotationId,
setSelectedZoomId: timeline.setSelectedZoomId,
nextAnnotationIdRef: input.nextAnnotationIdRef,
nextAnnotationZIndexRef: input.nextAnnotationZIndexRef,
});
useEditorGlobalInteractions({
timeline,
videoPlaybackRef: input.videoPlaybackRef,
shortcuts: input.shortcuts,
isMac: input.isMac,
handleUndo: input.handleUndo,
handleRedo: input.handleRedo,
startPlayback: playback.startPlayback,
});
return {
cursor,
projection,
audio,
playback,
captionCommands,
zoomCommands,
clipCommands,
audioCommands,
annotationCommands,
handleSelectAnnotation,
handleAutoSuggestZoomsConsumed: freshZoom.handleAutoSuggestZoomsConsumed,
};
}