The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { flushSync } from "react-dom"; |
| 2 | |
| 3 | export const DRAFT_HERO_TRANSITION_ANIMATION_ID = "t3-draft-hero-transition"; |
| 4 | export const DRAFT_HERO_TRANSITION_EASING = "cubic-bezier(0.4, 0, 0.2, 1)"; |
| 5 | export const MOBILE_COMPOSER_VIEW_TRANSITION_NAME = "t3-mobile-composer"; |
| 6 | export const MOBILE_DRAFT_HEADLINE_VIEW_TRANSITION_NAME = "t3-mobile-draft-headline"; |
| 7 | const MOBILE_COMPOSER_TRANSITION_DURATION_PROPERTY = "--mobile-composer-transition-duration"; |
| 8 | |
| 9 | type ComposerViewTransition = { |
| 10 | readonly finished: Promise<void>; |
| 11 | }; |
| 12 | |
| 13 | let activeMobileComposerTransition: Promise<void> | null = null; |
| 14 | |
| 15 | type ComposerViewTransitionDocument = Document & { |
| 16 | startViewTransition?: (update: () => void | Promise<void>) => ComposerViewTransition; |
| 17 | }; |
| 18 | |
| 19 | export async function waitForDraftHeroTransition(): Promise<void> { |
| 20 | const mobileComposerTransition = activeMobileComposerTransition; |
| 21 | if (typeof document === "undefined" || typeof document.getAnimations !== "function") { |
| 22 | await mobileComposerTransition; |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | const activeTransitions = document |
| 27 | .getAnimations() |
| 28 | .filter((animation) => animation.id === DRAFT_HERO_TRANSITION_ANIMATION_ID); |
| 29 | |
| 30 | await Promise.all([ |
| 31 | mobileComposerTransition, |
| 32 | ...activeTransitions.map(async (animation) => { |
| 33 | try { |
| 34 | await animation.finished; |
| 35 | } catch {} |
| 36 | }), |
| 37 | ]); |
| 38 | } |
| 39 | |
| 40 | // Only a mobile view transition flushes `update` (its new snapshot must show it) and returns a |
| 41 | // promise; otherwise it runs inline so its state batches with the caller's. Errors never block. |
| 42 | export function runMobileComposerTransition( |
| 43 | update: () => void, |
| 44 | options: { active: boolean; durationMs: number }, |
| 45 | ): Promise<void> | undefined { |
| 46 | if (typeof document === "undefined" || typeof window === "undefined") { |
| 47 | runInlineUpdate(update); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | const transitionDocument = document as ComposerViewTransitionDocument; |
| 52 | const mobileViewport = window.matchMedia?.("(max-width: 639px)").matches ?? false; |
| 53 | const prefersReducedMotion = |
| 54 | window.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false; |
| 55 | const startViewTransition = transitionDocument.startViewTransition?.bind(transitionDocument); |
| 56 | if (!options.active || !mobileViewport || prefersReducedMotion || !startViewTransition) { |
| 57 | runInlineUpdate(update); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | return new Promise<void>((resolveUpdated) => { |
| 62 | let updateStarted = false; |
| 63 | const runUpdate = () => { |
| 64 | if (updateStarted) return; |
| 65 | updateStarted = true; |
| 66 | try { |
| 67 | flushSync(update); |
| 68 | } finally { |
| 69 | resolveUpdated(); |
| 70 | } |
| 71 | }; |
| 72 | void runViewTransition(startViewTransition, runUpdate, options.durationMs); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | function runInlineUpdate(update: () => void) { |
| 77 | try { |
| 78 | update(); |
| 79 | } catch {} |
| 80 | } |
| 81 | |
| 82 | async function runViewTransition( |
| 83 | startViewTransition: (update: () => void) => ComposerViewTransition, |
| 84 | runUpdate: () => void, |
| 85 | durationMs: number, |
| 86 | ) { |
| 87 | const root = document.documentElement; |
| 88 | let transitionFinished: Promise<void> | null = null; |
| 89 | root.style.setProperty(MOBILE_COMPOSER_TRANSITION_DURATION_PROPERTY, `${String(durationMs)}ms`); |
| 90 | root.dataset.mobileComposerRouteTransition = "true"; |
| 91 | try { |
| 92 | const transition = startViewTransition(runUpdate); |
| 93 | transitionFinished = transition.finished.catch(() => undefined); |
| 94 | activeMobileComposerTransition = transitionFinished; |
| 95 | try { |
| 96 | await transition.finished; |
| 97 | } catch { |
| 98 | runInlineUpdate(runUpdate); |
| 99 | } |
| 100 | } catch { |
| 101 | runInlineUpdate(runUpdate); |
| 102 | } finally { |
| 103 | if (activeMobileComposerTransition === transitionFinished) { |
| 104 | activeMobileComposerTransition = null; |
| 105 | } |
| 106 | delete root.dataset.mobileComposerRouteTransition; |
| 107 | root.style.removeProperty(MOBILE_COMPOSER_TRANSITION_DURATION_PROPERTY); |
| 108 | } |
| 109 | } |