diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index dbd00743f4..bb341dfa0e 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -213,6 +213,11 @@ import { resolveEffectiveEnvMode } from "./BranchToolbar.logic"; import { ProviderStatusBanner } from "./chat/ProviderStatusBanner"; import { ThreadErrorBanner } from "./chat/ThreadErrorBanner"; import { ComposerBannerStack, type ComposerBannerStackItem } from "./chat/ComposerBannerStack"; +import { + DRAFT_HERO_TRANSITION_ANIMATION_ID, + DRAFT_HERO_TRANSITION_DURATION_MS, + DRAFT_HERO_TRANSITION_EASING, +} from "./chat/draftHeroTransition"; import { MAX_HIDDEN_MOUNTED_TERMINAL_THREADS, buildExpiredTerminalContextToastCopy, @@ -257,6 +262,65 @@ const EMPTY_ACTIVITIES: OrchestrationThreadActivity[] = []; const EMPTY_PROVIDERS: ServerProvider[] = []; const EMPTY_PROVIDER_SKILLS: ServerProvider["skills"] = []; const EMPTY_PENDING_USER_INPUT_ANSWERS: Record = {}; +function useDraftHeroLayoutTransition(isDraftHeroState: boolean) { + const composerRef = useRef(null); + const lowerChromeRef = useRef(null); + const previousStateRef = useRef(isDraftHeroState); + const previousRectsRef = useRef>([null, null]); + const animationsRef = useRef([]); + const attachComposerRef = (element: HTMLDivElement | null) => { + composerRef.current = element; + }; + const attachLowerChromeRef = (element: HTMLDivElement | null) => { + lowerChromeRef.current = element; + }; + + useLayoutEffect(() => { + const elements = [composerRef.current, lowerChromeRef.current] as const; + const nextRects = elements.map((element) => element?.getBoundingClientRect() ?? null); + const stateChanged = previousStateRef.current !== isDraftHeroState; + const prefersReducedMotion = + typeof window !== "undefined" && + window.matchMedia?.("(prefers-reduced-motion: reduce)").matches; + + animationsRef.current.forEach((animation) => animation.cancel()); + animationsRef.current = []; + + if (stateChanged && !prefersReducedMotion) { + elements.forEach((element, index) => { + const previousRect = previousRectsRef.current[index]; + const nextRect = nextRects[index]; + if (!element || !previousRect || !nextRect || typeof element.animate !== "function") { + return; + } + + const translateX = previousRect.left - nextRect.left; + const translateY = previousRect.top - nextRect.top; + if (Math.abs(translateX) < 0.5 && Math.abs(translateY) < 0.5) { + return; + } + + const animation = element.animate( + [ + { transform: `translate3d(${translateX}px, ${translateY}px, 0)` }, + { transform: "translate3d(0, 0, 0)" }, + ], + { + duration: DRAFT_HERO_TRANSITION_DURATION_MS, + easing: DRAFT_HERO_TRANSITION_EASING, + }, + ); + animation.id = DRAFT_HERO_TRANSITION_ANIMATION_ID; + animationsRef.current.push(animation); + }); + } + + previousStateRef.current = isDraftHeroState; + previousRectsRef.current = nextRects; + }, [isDraftHeroState]); + + return [attachComposerRef, attachLowerChromeRef] as const; +} const PreviewPanel = lazy(() => import("./preview/PreviewPanel").then((module) => ({ default: module.PreviewPanel })), ); @@ -2065,6 +2129,8 @@ function ChatViewContent(props: ChatViewProps) { [activeThread?.proposedPlans, timelineMessages, workLogEntries], ); const isDraftHeroState = isLocalDraftThread && timelineEntries.length === 0 && !isWorking; + const [attachDraftHeroComposerRef, attachDraftHeroLowerChromeRef] = + useDraftHeroLayoutTransition(isDraftHeroState); const { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } = useTurnDiffSummaries(activeThread); const turnDiffSummaryByAssistantMessageId = useMemo(() => { @@ -5139,9 +5205,11 @@ function ChatViewContent(props: ChatViewProps) { {/* Equal flexible spacers keep the hero group vertically stable: whatever mounts below (branch toolbar, banners) grows into the bottom spacer instead of re-centering the headline. */} - {isDraftHeroState ?
: null} {isDraftHeroState ? ( -
+
+ ) : null} + {isDraftHeroState ? ( +
) : (
)} -
+
- {/* In the hero layout the banner hangs below the composer without - contributing height, so connect/reconnect churn cannot - re-center the headline and composer. */} - {isDraftHeroState ? null : ( - - )} +
)}
- {isDraftHeroState ? ( -
-
- -
-
- ) : null}
diff --git a/apps/web/src/components/chat/draftHeroTransition.test.ts b/apps/web/src/components/chat/draftHeroTransition.test.ts new file mode 100644 index 0000000000..9bd7df3c78 --- /dev/null +++ b/apps/web/src/components/chat/draftHeroTransition.test.ts @@ -0,0 +1,49 @@ +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; + +import { + DRAFT_HERO_TRANSITION_ANIMATION_ID, + waitForDraftHeroTransition, +} from "./draftHeroTransition"; + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +describe("waitForDraftHeroTransition", () => { + it("waits for active draft hero animations and ignores unrelated animations", async () => { + let finishTransition: (() => void) | undefined; + const transitionFinished = new Promise((resolve) => { + finishTransition = resolve; + }); + vi.stubGlobal("document", { + getAnimations: () => [ + { id: "unrelated-animation", finished: new Promise(() => undefined) }, + { id: DRAFT_HERO_TRANSITION_ANIMATION_ID, finished: transitionFinished }, + ], + }); + + let handoffComplete = false; + const handoff = waitForDraftHeroTransition().then(() => { + handoffComplete = true; + }); + await Promise.resolve(); + expect(handoffComplete).toBe(false); + + finishTransition?.(); + await handoff; + expect(handoffComplete).toBe(true); + }); + + it("allows the handoff when an active transition is cancelled", async () => { + vi.stubGlobal("document", { + getAnimations: () => [ + { + id: DRAFT_HERO_TRANSITION_ANIMATION_ID, + finished: Promise.reject(new Error("cancelled")), + }, + ], + }); + + await expect(waitForDraftHeroTransition()).resolves.toBeUndefined(); + }); +}); diff --git a/apps/web/src/components/chat/draftHeroTransition.ts b/apps/web/src/components/chat/draftHeroTransition.ts new file mode 100644 index 0000000000..d2e5fb394b --- /dev/null +++ b/apps/web/src/components/chat/draftHeroTransition.ts @@ -0,0 +1,23 @@ +export const DRAFT_HERO_TRANSITION_ANIMATION_ID = "t3-draft-hero-transition"; +export const DRAFT_HERO_TRANSITION_DURATION_MS = 180; +export const DRAFT_HERO_TRANSITION_EASING = "cubic-bezier(0.2, 0, 0, 1)"; + +export async function waitForDraftHeroTransition(): Promise { + if (typeof document === "undefined" || typeof document.getAnimations !== "function") { + return; + } + + const activeTransitions = document + .getAnimations() + .filter((animation) => animation.id === DRAFT_HERO_TRANSITION_ANIMATION_ID); + + await Promise.all( + activeTransitions.map(async (animation) => { + try { + await animation.finished; + } catch { + // A cancelled transition is already safe to hand off. + } + }), + ); +} diff --git a/apps/web/src/routes/_chat.draft.$draftId.tsx b/apps/web/src/routes/_chat.draft.$draftId.tsx index dd152ce1a9..2d2e44cf62 100644 --- a/apps/web/src/routes/_chat.draft.$draftId.tsx +++ b/apps/web/src/routes/_chat.draft.$draftId.tsx @@ -8,6 +8,7 @@ import { useComposerDraftStore, } from "../composerDraftStore"; import { SidebarInset } from "../components/ui/sidebar"; +import { waitForDraftHeroTransition } from "../components/chat/draftHeroTransition"; import { buildThreadRouteParams } from "../threadRoutes"; import { useThread, useThreadRefs } from "../state/entities"; @@ -40,11 +41,22 @@ function DraftChatThreadRouteView() { if (!canonicalThreadRef) { return; } - void navigate({ - to: "/$environmentId/$threadId", - params: buildThreadRouteParams(canonicalThreadRef), - replace: true, + + let cancelled = false; + void waitForDraftHeroTransition().then(() => { + if (cancelled) { + return; + } + void navigate({ + to: "/$environmentId/$threadId", + params: buildThreadRouteParams(canonicalThreadRef), + replace: true, + }); }); + + return () => { + cancelled = true; + }; }, [canonicalThreadRef, navigate]); useEffect(() => {