Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix(web): refine mobile back motion and disable Android touch back (#1005)
Gener534193153e80
diff --git a/src/web/src/components/community/channels/message-header.dom.test.ts b/src/web/src/components/community/channels/message-header.dom.test.tsindex 6a754828b..3a7e4faeb 100644--- a/src/web/src/components/community/channels/message-header.dom.test.ts+++ b/src/web/src/components/community/channels/message-header.dom.test.ts@@ -1,4 +1,5 @@import { createElement } from "react"+import { renderToString } from "react-dom/server"import { describe, expect, it, vi } from "vitest"import { render, screen, setupUser } from "@/test/react-dom-harness"import { MessageHeader, MessageHeaderMobileBack } from "./message-header"@@ -31,6 +32,27 @@ describe("MessageHeader", () => {expect(onNavigate).toHaveBeenCalledOnce()})+ it("keeps the server Back disabled until the same control is hydrated", async () => {+ const onNavigate = vi.fn()+ const element = createElement(MessageHeaderMobileBack, { onNavigate })+ const container = document.createElement("div")+ container.innerHTML = renderToString(element)+ document.body.appendChild(container)+ const back = container.querySelector("button")!+ expect(back).toBeDisabled()+ expect(back).toHaveAttribute("aria-label", "Back")++ const user = setupUser()+ await user.click(back)+ expect(onNavigate).not.toHaveBeenCalled()++ render(element, { container, hydrate: true })+ expect(screen.getByRole("button", { name: "Back" })).toBe(back)+ expect(back).toBeEnabled()+ await user.click(back)+ expect(onNavigate).toHaveBeenCalledOnce()+ })+it("can keep the same 44px control visible inside an embedded mobile preview", () => {render(createElement(MessageHeaderMobileBack, {onNavigate: () => undefined,diff --git a/src/web/src/components/community/channels/message-header.tsx b/src/web/src/components/community/channels/message-header.tsxindex df7190016..3e0cdc00d 100644--- a/src/web/src/components/community/channels/message-header.tsx+++ b/src/web/src/components/community/channels/message-header.tsx@@ -1,6 +1,7 @@import type { ReactNode } from "react"import { ChevronLeft } from "lucide-react"import { Button } from "@/components/ui/button"+import { useHydratedClient } from "@/components/community/shell/use-hydrated-client"import { cn } from "@/lib/utils"export function MessageHeader({@@ -48,11 +49,13 @@ export function MessageHeaderMobileBack({label?: stringdisplay?: "viewport" | "always"}) {+ const hydrated = useHydratedClient()return (<Buttonvariant="ghost"size="icon"onClick={onNavigate}+ disabled={!hydrated}className={cn("size-11 shrink-0 text-muted-foreground hover:text-foreground",display === "viewport" && "sm:hidden",diff --git a/src/web/src/hooks/community/use-native-mobile-back-motion-owners.dom.test.tsx b/src/web/src/hooks/community/use-native-mobile-back-motion-owners.dom.test.tsxindex 53d3f6078..75306a559 100644--- a/src/web/src/hooks/community/use-native-mobile-back-motion-owners.dom.test.tsx+++ b/src/web/src/hooks/community/use-native-mobile-back-motion-owners.dom.test.tsx@@ -38,12 +38,23 @@ function back() {act(() => { window.dispatchEvent(new Event(NATIVE_MOBILE_BACK_EVENT, { cancelable: true })) })}+function transition(root: HTMLElement) {+ const target = root.querySelector<HTMLElement>('[data-panel][id="main"]')!+ const event = new Event("transitionend", { bubbles: true })+ Object.defineProperty(event, "propertyName", { value: "transform" })+ act(() => { target.dispatchEvent(event) })+}+const roots: HTMLElement[] = []function setup() {vi.useFakeTimers()const root = document.createElement("div")root.dataset.mobileBackSurface = "detail"Object.defineProperty(root, "clientWidth", { value: 390 })+ const foreground = document.createElement("div")+ foreground.dataset.panel = "true"+ foreground.id = "main"+ root.appendChild(foreground)document.body.appendChild(root)roots.push(root)const props = { replace: vi.fn(), close: vi.fn(), request: vi.fn(() => "blocked" as const) }@@ -89,7 +100,7 @@ describe("shared Back ownership during page motion", () => {const { root, props, view } = setup()if (kind === "touch") { touch("touchstart", 10); touch("touchmove", 200) }else { progress(0); progress(0.5) }- expect(root.dataset.mobileBackPhase).toBe("dragging")+ expect(root.dataset.mobileBackPhase).toBe(kind === "touch" ? undefined : "dragging")view.rerender(<Fixture {...props} owner />)expect(root.hasAttribute("data-mobile-back-phase")).toBe(false)expect(props.close).not.toHaveBeenCalled()@@ -112,20 +123,23 @@ describe("shared Back ownership during page motion", () => {if (kind === "touch") touch("touchend", 200)if (kind === "native") back()act(() => { vi.advanceTimersByTime(300) })+ transition(root)expect(props.request).not.toHaveBeenCalled()expect(props.replace).not.toHaveBeenCalled()expect(root.hasAttribute("data-mobile-back-phase")).toBe(false)back()act(() => { vi.advanceTimersByTime(200) })+ transition(root)expect(props.replace).toHaveBeenCalledExactlyOnceWith("/c/me")view.unmount()})it("does not use the settling timer to close a new real popup", () => {- const { props, view } = setup()+ const { root, props, view } = setup()touch("touchstart", 10); touch("touchmove", 200); touch("touchend", 200)view.rerender(<Fixture {...props} owner />)act(() => { vi.advanceTimersByTime(300) })+ transition(root)expect(props.close).not.toHaveBeenCalled()expect(props.replace).not.toHaveBeenCalled()back()diff --git a/src/web/src/hooks/community/use-native-mobile-back.dom.test.ts b/src/web/src/hooks/community/use-native-mobile-back.dom.test.tsindex decaaccd0..bb8767396 100644--- a/src/web/src/hooks/community/use-native-mobile-back.dom.test.ts+++ b/src/web/src/hooks/community/use-native-mobile-back.dom.test.ts@@ -297,6 +297,12 @@ function mobileHarness(parentPath: string | null = "/c/channels/s1") {return { props, renderer: render(createElement(Harness, props)) }}+function transition(target: HTMLElement, type = "transitionend", propertyName = "transform") {+ const event = new Event(type, { bubbles: true })+ Object.defineProperty(event, "propertyName", { value: propertyName })+ act(() => { target.dispatchEvent(event) })+}+describe("mobile edge parent gesture", () => {it("replaces the parent once at the shared edge boundary, on release only", () => {const { props, renderer } = mobileHarness()@@ -380,41 +386,90 @@ describe("layered mobile back motion", () => {const root = document.createElement("div")root.dataset.mobileBackSurface = "detail"Object.defineProperty(root, "clientWidth", { value: 390 })+ const foreground = document.createElement("div")+ foreground.dataset.panel = "true"+ foreground.id = "main"+ root.appendChild(foreground)document.body.append(root)roots.push(root)- return { ...mobileHarness(), root }+ return { ...mobileHarness(), root, foreground }}afterEach(() => {roots.splice(0).forEach((root) => root.remove())vi.useRealTimers()vi.unstubAllGlobals()+ vi.restoreAllMocks()})- it("tracks the finger, then completes once after the foreground settles", () => {+ it.each(["Web", "Native"])("keeps iOS %s touch painting and completes once after release", (platform) => {vi.useFakeTimers()- const { root, props, renderer } = mounted()+ vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)")+ if (platform === "Native") vi.stubGlobal("__TAURI__", {})+ const { root, foreground, props, renderer } = mounted()touch("touchstart", 10)+ touch("touchmove", 70)+ expect(root.style.getPropertyValue("--mobile-back-x")).toBe(platform === "Native" ? "60px" : "")+ expect(root.dataset.mobileBackPhase).toBe(platform === "Native" ? "dragging" : undefined)touch("touchmove", 180)- expect(root.style.getPropertyValue("--mobile-back-x")).toBe("170px")- expect(root.dataset.mobileBackPhase).toBe("dragging")+ expect(root.style.getPropertyValue("--mobile-back-x")).toBe(platform === "Native" ? "170px" : "")+ expect(root.style.getPropertyValue("--mobile-back-progress")).toBe(platform === "Native" ? `${170 / 390}` : "")+ expect(root.dataset.mobileBackPhase).toBe(platform === "Native" ? "dragging" : undefined)expect(props.replacePath).not.toHaveBeenCalled()touch("touchend", 180)expect(root.style.getPropertyValue("--mobile-back-x")).toBe("390px")expect(root.dataset.mobileBackPhase).toBe("settling")+ if (platform === "Native") transition(foreground)touch("touchstart", 10)touch("touchend", 180)vi.advanceTimersByTime(199)expect(props.replacePath).not.toHaveBeenCalled()vi.advanceTimersByTime(1)+ if (platform === "Web") {+ expect(props.replacePath).not.toHaveBeenCalled()+ transition(foreground)+ }expect(props.replacePath).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")expect(root.dataset.mobileBackPhase).toBe("committing")renderer.unmount()expect(root.style.getPropertyValue("--mobile-back-x")).toBe("")})+ it.each(["Web", "Native"])("leaves Android %s edge touch to the platform while retaining public Back", (platform) => {+ vi.useFakeTimers()+ vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Mozilla/5.0 (Linux; Android 16)")+ if (platform === "Native") vi.stubGlobal("__TAURI__", {})+ const { root, foreground, props } = mounted()+ touch("touchstart", 10)+ expect(touch("touchmove", 200).defaultPrevented).toBe(false)+ expect(touch("touchend", 200).defaultPrevented).toBe(false)+ touch("touchcancel", 200)+ expect(root.hasAttribute("data-mobile-back-phase")).toBe(false)+ expect(root.style.getPropertyValue("--mobile-back-x")).toBe("")+ expect(props.dismissShellOverlay).not.toHaveBeenCalled()+ expect(props.replacePath).not.toHaveBeenCalled()++ props.dismissShellOverlay.mockReturnValueOnce(true)+ dispatchBack()+ expect(props.dismissShellOverlay).toHaveBeenCalledOnce()+ expect(props.replacePath).not.toHaveBeenCalled()+ if (platform === "Native") {+ window.dispatchEvent(new CustomEvent(NATIVE_MOBILE_BACK_PROGRESS_EVENT, { detail: { progress: 0, started: true } }))+ window.dispatchEvent(new CustomEvent(NATIVE_MOBILE_BACK_PROGRESS_EVENT, { detail: { progress: 0.5 } }))+ expect(root.style.getPropertyValue("--mobile-back-x")).toBe("195px")+ }+ dispatchBack()+ expect(root.dataset.mobileBackPhase).toBe("settling")+ vi.advanceTimersByTime(200)+ if (platform === "Web") {+ expect(props.replacePath).not.toHaveBeenCalled()+ transition(foreground)+ }+ expect(props.replacePath).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")+ })+it("keeps settling across callback changes on the same committed route", () => {vi.useFakeTimers()- const { props, renderer } = mounted()+ const { foreground, props, renderer } = mounted()touch("touchstart", 10)touch("touchmove", 200)touch("touchend", 200)@@ -422,6 +477,8 @@ describe("layered mobile back motion", () => {renderer.rerender(createElement(Harness, { ...props, replacePath, dismissShellOverlay: () => false }))vi.advanceTimersByTime(200)expect(props.replacePath).not.toHaveBeenCalled()+ expect(replacePath).not.toHaveBeenCalled()+ transition(foreground)expect(replacePath).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")renderer.unmount()})@@ -453,6 +510,7 @@ describe("layered mobile back motion", () => {it.each(["route", "resize", "background"])("ignores a late native commit after %s interrupts its gesture", (reason) => {vi.useFakeTimers()+ vi.stubGlobal("__TAURI__", {})const { props, renderer } = mounted()const progress = (value: number) => window.dispatchEvent(new CustomEvent(NATIVE_MOBILE_BACK_PROGRESS_EVENT, { detail: { progress: value, started: value === 0 } }))progress(0)@@ -470,23 +528,109 @@ describe("layered mobile back motion", () => {renderer.unmount()})- it("returns a short held drag to the same page without navigating", () => {+ it.each(["Web", "Native"])("cancels a short held %s drag without navigating", (platform) => {vi.useFakeTimers()+ if (platform === "Native") vi.stubGlobal("__TAURI__", {})const { root, props, renderer } = mounted()touch("touchstart", 10)touch("touchmove", 70)vi.advanceTimersByTime(150)touch("touchend", 70)- expect(root.style.getPropertyValue("--mobile-back-x")).toBe("0px")+ expect(root.style.getPropertyValue("--mobile-back-x")).toBe(platform === "Native" ? "0px" : "")+ expect(root.dataset.mobileBackPhase).toBe(platform === "Native" ? "settling" : undefined)vi.advanceTimersByTime(200)expect(root.hasAttribute("data-mobile-back-phase")).toBe(false)expect(props.replacePath).not.toHaveBeenCalled()renderer.unmount()})+ it("accepts a new Web gesture immediately after touch cancellation", () => {+ vi.useFakeTimers()+ const { root, foreground, props } = mounted()+ touch("touchstart", 10)+ touch("touchmove", 200)+ touch("touchcancel", 200)+ expect(root.hasAttribute("data-mobile-back-phase")).toBe(false)+ touch("touchstart", 10)+ touch("touchmove", 200)+ touch("touchend", 200)+ expect(root.dataset.mobileBackPhase).toBe("settling")+ vi.advanceTimersByTime(200)+ expect(props.replacePath).not.toHaveBeenCalled()+ transition(foreground)+ expect(props.replacePath).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")+ })++ it("waits for the foreground transform completion instead of elapsed timers or other transitions", () => {+ vi.useFakeTimers()+ const { root, foreground, props } = mounted()+ const child = document.createElement("button")+ foreground.appendChild(child)+ touch("touchstart", 10)+ touch("touchmove", 200)+ touch("touchend", 200)+ vi.advanceTimersByTime(1000)+ transition(foreground, "transitionend", "opacity")+ transition(child)+ window.dispatchEvent(new CustomEvent(NATIVE_MOBILE_BACK_PROGRESS_EVENT, { detail: { progress: 0, started: true } }))+ dispatchBack()+ touch("touchstart", 10)+ touch("touchmove", 200)+ touch("touchend", 200)+ expect(root.dataset.mobileBackPhase).toBe("settling")+ expect(props.replacePath).not.toHaveBeenCalled()+ transition(foreground)+ transition(foreground)+ expect(props.replacePath).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")+ })++ it("uses the thread foreground completion instead of the containing main panel", () => {+ const { foreground, props } = mounted()+ const split = document.createElement("div")+ split.dataset.mobileBackThread = "true"+ const thread = document.createElement("div")+ thread.dataset.panel = "true"+ thread.id = "thread"+ split.appendChild(thread)+ foreground.appendChild(split)+ touch("touchstart", 10)+ touch("touchmove", 200)+ touch("touchend", 200)+ transition(foreground)+ expect(props.replacePath).not.toHaveBeenCalled()+ transition(thread)+ expect(props.replacePath).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")+ })++ it("retires a cancelled Web transition and permits a new confirmed gesture", () => {+ const { root, foreground, props } = mounted()+ touch("touchstart", 10)+ touch("touchmove", 200)+ touch("touchend", 200)+ transition(foreground, "transitioncancel")+ transition(foreground)+ expect(root.hasAttribute("data-mobile-back-phase")).toBe(false)+ expect(props.replacePath).not.toHaveBeenCalled()+ touch("touchstart", 10)+ touch("touchmove", 200)+ touch("touchend", 200)+ transition(foreground)+ expect(props.replacePath).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")+ })++ it("retains direct Back when no animatable foreground exists", () => {+ const { root, foreground, props } = mounted()+ foreground.remove()+ touch("touchstart", 10)+ touch("touchmove", 200)+ touch("touchend", 200)+ expect(root.hasAttribute("data-mobile-back-phase")).toBe(false)+ expect(props.replacePath).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")+ })+it.each(["touchcancel", "route", "resize", "background", "unmount"])("cancels queued navigation on %s", (reason) => {vi.useFakeTimers()- const { root, props, renderer } = mounted()+ const { root, foreground, props, renderer } = mounted()touch("touchstart", 10)touch("touchmove", 200)if (reason === "touchcancel") touch("touchcancel", 200)@@ -498,6 +642,7 @@ describe("layered mobile back motion", () => {if (reason === "unmount") renderer.unmount()}vi.advanceTimersByTime(300)+ transition(foreground)expect(props.replacePath).not.toHaveBeenCalled()expect(root.hasAttribute("data-mobile-back-phase")).toBe(false)if (reason !== "unmount") renderer.unmount()diff --git a/src/web/src/hooks/community/use-native-mobile-back.ts b/src/web/src/hooks/community/use-native-mobile-back.tsindex e3d60386f..082459d93 100644--- a/src/web/src/hooks/community/use-native-mobile-back.ts+++ b/src/web/src/hooks/community/use-native-mobile-back.ts@@ -1,5 +1,5 @@import { useEffect, useLayoutEffect, useRef } from "react"-import { isMobile } from "@alook/shared"+import { isMobile, isTauri } from "@alook/shared"import { hasNativeBackOwner, NATIVE_BACK_OWNER_CHANGE_EVENT, requestNativeBack } from "@/components/ui/native-back-layer"export const MOBILE_BACK_EDGE_PX = 24@@ -85,12 +85,15 @@ export function useNativeMobileBack({let pageBlocked = falselet nativeProgress = falselet timer: ReturnType<typeof setTimeout> | undefined+ let stopSettling: (() => void) | undefinedlet gesture: { id: number; x: number; y: number; horizontal: boolean; lastX: number; at: number; velocity: number } | null = nullconst reduced = () => window.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? falseconst occupied = () => composing.current === document.activeElement || hasNativeBackOwner(document)const clearMotion = () => {clearTimeout(timer)timer = undefined+ stopSettling?.()+ stopSettling = undefinedroot?.style.removeProperty("--mobile-back-x")root?.style.removeProperty("--mobile-back-progress")root?.removeAttribute("data-mobile-back-phase")@@ -115,10 +118,15 @@ export function useNativeMobileBack({const findRoot = () => actions.current.animate && parentPath && !occupied()? document.querySelector<HTMLElement>('[data-mobile-back-surface="detail"]') : nullconst settle = (commit: boolean) => {+ const waitForTransition = commit && !isTauri() && !nativeProgressgesture = nullnativeProgress = falseif (commit && (pageBlocked || occupied())) { reset(); return }- if (!root || reduced()) {+ const foreground = waitForTransition+ ? root?.querySelector<HTMLElement>('[data-mobile-back-thread] [data-panel][id="thread"]')+ ?? root?.querySelector<HTMLElement>('[data-panel][id="main"]')+ : null+ if (!root || reduced() || waitForTransition && !foreground) {reset()if (commit && parentPath) actions.current.replacePath(parentPath)return@@ -126,17 +134,32 @@ export function useNativeMobileBack({root.dataset.mobileBackPhase = "settling"root.style.setProperty("--mobile-back-x", commit ? `${root.clientWidth}px` : "0px")root.style.setProperty("--mobile-back-progress", commit ? "1" : "0")- timer = setTimeout(() => {+ const finish = () => {timer = undefined+ stopSettling?.()+ stopSettling = undefinedif (commit && parentPath && !pageBlocked && !occupied() && actions.current.animate) {root?.setAttribute("data-mobile-back-phase", "committing")actions.current.replacePath(parentPath)} else reset()- }, 200)+ }+ if (foreground) {+ const complete = (event: TransitionEvent) => {+ if (event.target !== foreground || event.propertyName !== "transform") return+ if (event.type === "transitionend") finish()+ else reset()+ }+ foreground.addEventListener("transitionend", complete)+ foreground.addEventListener("transitioncancel", complete)+ stopSettling = () => {+ foreground.removeEventListener("transitionend", complete)+ foreground.removeEventListener("transitioncancel", complete)+ }+ } else timer = setTimeout(finish, 200)}- const paint = (x: number) => {+ const paint = (x: number, follow = true) => {if (occupied() || !actions.current.animate) interrupt()- if (!root || pageBlocked) return+ if (!root || pageBlocked || !follow) returnroot.dataset.mobileBackPhase = "dragging"root.style.setProperty("--mobile-back-x", `${x}px`)root.style.setProperty("--mobile-back-progress", `${x / Math.max(1, root.clientWidth)}`)@@ -147,7 +170,7 @@ export function useNativeMobileBack({const stale = nativeGesture.current === "stale"nativeGesture.current = nullif (stale) { pageBlocked = false; return reset() }- if (timer !== undefined || root?.dataset.mobileBackPhase === "committing") return+ if (timer !== undefined || stopSettling || root?.dataset.mobileBackPhase === "committing") returnconst blocked = (gesture !== null || nativeProgress) && pageBlockedpageBlocked = falseif (composing.current === document.activeElement) return reset()@@ -164,7 +187,7 @@ export function useNativeMobileBack({if (nativeProgress) settle(false)return}- if (!Number.isFinite(value) || timer !== undefined || root?.dataset.mobileBackPhase === "committing") return+ if (!Number.isFinite(value) || timer !== undefined || stopSettling || root?.dataset.mobileBackPhase === "committing") returnif (!nativeProgress) {if (value !== 0 || detail?.started !== true) returnreset()@@ -176,11 +199,11 @@ export function useNativeMobileBack({if (root) paint(Math.max(0, Math.min(1, value!)) * root.clientWidth)}const cancel = () => {- if (gesture?.horizontal && root) settle(false)+ if (gesture?.horizontal && root && isTauri()) settle(false)else reset()}const start = (event: TouchEvent) => {- if (timer !== undefined || root?.dataset.mobileBackPhase === "committing") return+ if (timer !== undefined || stopSettling || root?.dataset.mobileBackPhase === "committing") returnreset()if (event.touches.length !== 1 || event.defaultPrevented) returnconst touch = event.touches[0]@@ -207,7 +230,7 @@ export function useNativeMobileBack({gesture.velocity = (touch.clientX - gesture.lastX) / Math.max(1, now - gesture.at)gesture.lastX = touch.clientXgesture.at = now- paint(Math.max(0, Math.min(root?.clientWidth ?? 0, dx)))+ paint(Math.max(0, Math.min(root?.clientWidth ?? 0, dx)), isTauri())}const end = (event: TouchEvent) => {const current = gesture@@ -221,7 +244,7 @@ export function useNativeMobileBack({? dx >= root.clientWidth * 0.35 || dx >= 32 && current.velocity > 0.5 && performance.now() - current.at < 100: dx >= 64)if (commit) handleBack(event)- else { event.preventDefault(); settle(false) }+ else { event.preventDefault(); cancel() }}window.addEventListener(NATIVE_MOBILE_BACK_EVENT, handleBack)document.addEventListener(NATIVE_BACK_OWNER_CHANGE_EVENT, ownerChanged)@@ -229,10 +252,12 @@ export function useNativeMobileBack({window.addEventListener("pagehide", invalidate)if (mobile) {window.addEventListener(NATIVE_MOBILE_BACK_PROGRESS_EVENT, progress)- document.addEventListener("touchstart", start, { passive: true, capture: true })- document.addEventListener("touchmove", move, { passive: false, capture: true })- document.addEventListener("touchend", end, { passive: false, capture: true })- document.addEventListener("touchcancel", cancel, true)+ if (!/android/i.test(navigator.userAgent)) {+ document.addEventListener("touchstart", start, { passive: true, capture: true })+ document.addEventListener("touchmove", move, { passive: false, capture: true })+ document.addEventListener("touchend", end, { passive: false, capture: true })+ document.addEventListener("touchcancel", cancel, true)+ }document.addEventListener("visibilitychange", invalidate)window.addEventListener("resize", invalidate)}