Skip to content

Commit

Permalink
Fix another null issue in useVideoTrimMode
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Jan 6, 2025
1 parent 795cf88 commit f4a71b9
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions packages/story-editor/src/components/videoTrim/useVideoTrimMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ function useVideoTrimMode() {
clearEditing,
})
);
const { selectedElement } = useStory(({ state: { selectedElements } }) => ({
selectedElement: selectedElements.length === 1 ? selectedElements[0] : null,
}));
const { selectedElement, selectedId, selectedResource, isVideo } = useStory(
({ state: { selectedElements } }) => {
const selectedElement =
selectedElements.length === 1 ? selectedElements[0] : null;
return {
selectedElement,
selectedId: selectedElement ? selectedElement?.id : null,
selectedResource: selectedElement?.resource || {},
isVideo: selectedElement?.type === 'video',
};
}
);

const {
actions: { getMediaById },
Expand All @@ -56,12 +65,11 @@ function useVideoTrimMode() {
);

const getVideoData = useCallback(() => {
const { resource } = selectedElement;
const { trimData } = resource;
const { trimData } = selectedResource;

Check warning on line 68 in packages/story-editor/src/components/videoTrim/useVideoTrimMode.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/videoTrim/useVideoTrimMode.js#L68

Added line #L68 was not covered by tests

const defaultVideoData = {
element: selectedElement,
resource,
resource: selectedResource,
start: 0,
end: null,
};
Expand All @@ -87,13 +95,13 @@ function useVideoTrimMode() {
} else {
setVideoData(defaultVideoData);
}
}, [getMediaById, selectedElement]);
}, [getMediaById, selectedElement, selectedResource]);

const toggleTrimMode = useCallback(() => {
if (isEditing) {
clearEditing();
} else if (selectedElement) {
setEditingElementWithState(selectedElement.id, {
} else if (selectedId) {
setEditingElementWithState(selectedId, {

Check warning on line 104 in packages/story-editor/src/components/videoTrim/useVideoTrimMode.js

View check run for this annotation

Codecov / codecov/patch

packages/story-editor/src/components/videoTrim/useVideoTrimMode.js#L104

Added line #L104 was not covered by tests
isTrimMode: true,
hasEditMenu: true,
showOverflow: false,
Expand All @@ -108,23 +116,28 @@ function useVideoTrimMode() {
isEditing,
clearEditing,
setEditingElementWithState,
selectedElement,
selectedId,
getVideoData,
]);

const { isTranscodingEnabled } = useFFmpeg();

const hasTrimMode = useMemo(() => {
if (selectedElement?.type !== 'video' || !selectedElement?.resource) {
if (!isVideo) {
return false;
}

const { id, isExternal } = selectedElement.resource;

return (
isTranscodingEnabled && !isExternal && !isCurrentResourceUploading(id)
isTranscodingEnabled &&
!selectedResource.isExternal &&
!isCurrentResourceUploading(selectedResource.id)
);
}, [selectedElement, isTranscodingEnabled, isCurrentResourceUploading]);
}, [
isVideo,
selectedResource,
isTranscodingEnabled,
isCurrentResourceUploading,
]);

return {
isTrimMode: Boolean(isEditing && isTrimMode),
Expand Down

0 comments on commit f4a71b9

Please sign in to comment.