diff --git a/src/app/components/CallEmbedProvider.tsx b/src/app/components/CallEmbedProvider.tsx index 8ee389ccb..c0621e123 100644 --- a/src/app/components/CallEmbedProvider.tsx +++ b/src/app/components/CallEmbedProvider.tsx @@ -731,8 +731,24 @@ export function CallEmbedProvider({ children }: CallEmbedProviderProps) { // When screenshare ends, release the spotlight we auto-enabled. const pipAutoSpotlightRef = React.useRef(false); useEffect(() => { - if (!pipMode || !callEmbed) return; - if (pipScreenshare) { + if (!callEmbed) { + // The embed (and its spotlight) is torn down with the call; drop the latch + // so a stale ref can't act on the next call's fresh embed. + pipAutoSpotlightRef.current = false; + return; + } + // Spotlight is wanted only while in pip with an active screenshare. Release + // it when EITHER ends — including leaving pip (returning to the call room). + // The release must not sit behind a `!pipMode` early-return, or a + // screenshare→pip→back sequence leaves the auto-enabled spotlight stuck on + // with pipAutoSpotlightRef latched true. The ref gates release so we only + // ever undo a spotlight we turned on (never one the user set). + // NB: `control.spotlight` is read below but deliberately NOT a dependency — + // this effect reacts to pip/screenshare *intent*, not to spotlight changes. + // Adding it as a dep would re-run on every manual spotlight toggle and fight + // the user. + const wantSpotlight = pipMode && pipScreenshare; + if (wantSpotlight) { if (!callEmbed.control.spotlight) { callEmbed.control.toggleSpotlight(); pipAutoSpotlightRef.current = true;