Skip to content

🎥 Make videos programmatically with React

Studio: Optimize premount selection subscriptions

JonnyBurger25f5668595c8
diff --git a/packages/studio/src/components/Timeline/TimelineSelection.tsx b/packages/studio/src/components/Timeline/TimelineSelection.tsx
index f65da81d12..7bbda5f762 100644
--- a/packages/studio/src/components/Timeline/TimelineSelection.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSelection.tsx
@@ -1872,6 +1872,46 @@ export const useTimelineRowSelection = (
});
};
+export const useIsPremountingSelected = (
+ nodePathInfo: SequenceNodePathInfo | null,
+): boolean => {
+ const selectionContext = useContext(TimelineRowSelectionContext);
+ if (selectionContext === null) {
+ throw new Error(
+ 'useIsPremountingSelected must be used inside TimelineSelectionProvider',
+ );
+ }
+
+ const sequenceSelectionKey = useMemo(
+ () =>
+ nodePathInfo === null
+ ? null
+ : getTimelineSequenceSelectionKey(nodePathInfo),
+ [nodePathInfo],
+ );
+ const getIsPremountingSelected = useCallback(() => {
+ if (sequenceSelectionKey === null) {
+ return false;
+ }
+
+ return selectionContext
+ .getSnapshot()
+ .selectedItems.some(
+ (item) =>
+ item.type === 'sequence-prop' &&
+ item.key === 'premountFor' &&
+ getTimelineSequenceSelectionKey(item.nodePathInfo) ===
+ sequenceSelectionKey,
+ );
+ }, [selectionContext, sequenceSelectionKey]);
+
+ return useSyncExternalStore(
+ selectionContext.subscribe,
+ getIsPremountingSelected,
+ getIsPremountingSelected,
+ );
+};
+
const useTimelineSelectionItem = <
TSelectionItem extends TimelineSelection | null,
>({
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index 6e27c677b6..59b6668bab 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -95,6 +95,7 @@ import {
isTimelineSelectionModifierEvent,
shouldSelectTimelineRowOnPointerDown,
TIMELINE_MARQUEE_ITEM_ATTR,
+ useIsPremountingSelected,
useTimelineMarqueeSelectableItem,
useTimelineRowContainsSelection,
useTimelineRowSelection,
@@ -989,16 +990,11 @@ const useTimelineSequenceState = (props: TimelineSequenceProps) => {
)?.durationInFrames?.status === 'static',
);
const {previewServerState} = useContext(StudioServerConnectionCtx);
- const premountNodePathInfo = useMemo(
- (): SequenceNodePathInfo | null =>
- nodePathInfo === null
- ? null
- : {...nodePathInfo, auxiliaryKeys: ['controls', 'premountFor']},
- [nodePathInfo],
+ const showPremountingSetting = getStudioShowPremounting();
+ const premountSelected = useIsPremountingSelected(
+ showPremountingSetting ? null : nodePathInfo,
);
- const {selected: premountSelected} =
- useTimelineRowSelection(premountNodePathInfo);
- const showPremounting = getStudioShowPremounting() || premountSelected;
+ const showPremounting = showPremountingSetting || premountSelected;
const previewConnected = previewServerState.type === 'connected';
const previewInteractive = previewConnected && isStudioInteractivityEnabled();
const {setPropStatuses} = useContext(Internals.VisualModeSettersContext);