Skip to content

The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.

1.1 KB
Raw Blame
import { useRef, type Dispatch, type MutableRefObject, type SetStateAction } from "react";
import {
  isHydratedFromUrlState,
  readStudioUrlStateFromWindow,
  type StudioUrlState,
} from "../utils/studioUrlState";

/**
 * Resets selection when `projectId` switches in-session. Runs during render, not an
 * effect, so the caller never sees a stale activeCompPath in the same commit.
 */
export function useResetSelectionOnProjectSwitch({
  projectId,
  initialUrlStateRef,
  setActiveCompPath,
  setActiveCompPathHydrated,
}: {
  projectId: string | null;
  initialUrlStateRef: MutableRefObject<StudioUrlState>;
  setActiveCompPath: Dispatch<SetStateAction<string | null>>;
  setActiveCompPathHydrated: Dispatch<SetStateAction<boolean>>;
}): void {
  const previousProjectIdRef = useRef(projectId);

  if (previousProjectIdRef.current !== projectId) {
    const isSwitch = previousProjectIdRef.current !== null;
    previousProjectIdRef.current = projectId;
    if (isSwitch) {
      initialUrlStateRef.current = readStudioUrlStateFromWindow();
      setActiveCompPath(null);
      setActiveCompPathHydrated(isHydratedFromUrlState(initialUrlStateRef.current));
    }
  }
}