Skip to content

🎥 Make videos programmatically with React

more insights by react scan

Jonny Burgera03b8e84998f
diff --git a/packages/studio/src/components/TimelineInOutToggle.tsx b/packages/studio/src/components/TimelineInOutToggle.tsx
index c6c942edf5..7ee1cd6cf5 100644
--- a/packages/studio/src/components/TimelineInOutToggle.tsx
+++ b/packages/studio/src/components/TimelineInOutToggle.tsx
@@ -1,3 +1,4 @@
+import {PlayerInternals} from '@remotion/player';
import React, {
createRef,
useCallback,
@@ -45,11 +46,12 @@ export const inOutHandles = createRef<{
export const defaultInOutValue: InOutValue = {inFrame: null, outFrame: null};
export const TimelineInOutPointToggle: React.FC = () => {
- const timelinePosition = Internals.Timeline.useTimelinePosition();
const {inFrame, outFrame} = useTimelineInOutFramePosition();
const {setInAndOutFrames} = useTimelineSetInOutFramePosition();
const videoConfig = Internals.useUnsafeVideoConfig();
const keybindings = useKeybinding();
+ const {getCurrentFrame, isFirstFrame, isLastFrame} =
+ PlayerInternals.usePlayer();
const onInOutClear = useCallback(
(composition: string) => {
@@ -89,7 +91,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
const prevOut = prev[videoConfig.id]?.outFrame;
const biggestPossible =
prevOut === undefined || prevOut === null ? Infinity : prevOut - 1;
- const selected = Math.min(timelinePosition, biggestPossible);
+ const selected = Math.min(getCurrentFrame(), biggestPossible);
if (selected === 0) {
return {
@@ -124,7 +126,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
};
});
},
- [setInAndOutFrames, timelinePosition, videoConfig],
+ [getCurrentFrame, setInAndOutFrames, videoConfig],
);
const clearInMark = useCallback(
@@ -194,7 +196,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
prevInFrame === null || prevInFrame === undefined
? -Infinity
: prevInFrame + 1;
- const selected = Math.max(timelinePosition, smallestPossible);
+ const selected = Math.max(getCurrentFrame(), smallestPossible);
if (selected === videoConfig.durationInFrames - 1) {
return {
@@ -229,7 +231,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
};
});
},
- [setInAndOutFrames, timelinePosition, videoConfig],
+ [getCurrentFrame, setInAndOutFrames, videoConfig],
);
const confId = videoConfig?.id;
@@ -300,7 +302,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
aria-label={getTooltipText('In', 'I')}
onClick={(e) => onInMark(e)}
onContextMenu={clearInMark}
- disabled={!videoConfig || timelinePosition === 0}
+ disabled={!videoConfig || isFirstFrame}
>
<TimelineInPointer
color={inFrame === null ? 'white' : BLUE}
@@ -312,9 +314,7 @@ export const TimelineInOutPointToggle: React.FC = () => {
aria-label={getTooltipText('Out', 'O')}
onClick={onOutMark}
onContextMenu={clearOutMark}
- disabled={
- !videoConfig || timelinePosition === videoConfig.durationInFrames - 1
- }
+ disabled={!videoConfig || isLastFrame}
>
<TimelineOutPointer
color={outFrame === null ? 'white' : BLUE}
diff --git a/packages/studio/src/state/in-out.ts b/packages/studio/src/state/in-out.ts
index e4cbb34f00..386702bc1e 100644
--- a/packages/studio/src/state/in-out.ts
+++ b/packages/studio/src/state/in-out.ts
@@ -1,5 +1,5 @@
import type React from 'react';
-import {createContext, useContext} from 'react';
+import {createContext, useContext, useMemo} from 'react';
import {Internals} from 'remotion';
export type InOutValue = {
@@ -29,29 +29,35 @@ export const SetTimelineInOutContext =
export const useTimelineInOutFramePosition = (): InOutValue => {
const videoConfig = Internals.useUnsafeVideoConfig();
const state = useContext(TimelineInOutContext);
- if (!videoConfig) {
- return {inFrame: null, outFrame: null};
- }
- const maxFrame = videoConfig.durationInFrames - 1;
+ const id = videoConfig?.id;
+ const durationInFrames = videoConfig?.durationInFrames;
- const actualInFrame = state[videoConfig.id]?.inFrame ?? null;
- const actualOutFrame = state[videoConfig.id]?.outFrame ?? null;
+ return useMemo(() => {
+ if (!id || !durationInFrames) {
+ return {inFrame: null, outFrame: null};
+ }
- return {
- inFrame:
- actualInFrame === null
- ? null
- : actualInFrame >= maxFrame
+ const maxFrame = durationInFrames - 1;
+
+ const actualInFrame = state[id]?.inFrame ?? null;
+ const actualOutFrame = state[id]?.outFrame ?? null;
+
+ return {
+ inFrame:
+ actualInFrame === null
? null
- : actualInFrame,
- outFrame:
- actualOutFrame === null
- ? null
- : actualOutFrame >= maxFrame
+ : actualInFrame >= maxFrame
+ ? null
+ : actualInFrame,
+ outFrame:
+ actualOutFrame === null
? null
- : actualOutFrame,
- };
+ : actualOutFrame >= maxFrame
+ ? null
+ : actualOutFrame,
+ };
+ }, [durationInFrames, id, state]);
};
export const useTimelineSetInOutFramePosition =