Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): return to mobile parent with edge swipe (#974)

Gustavo Yecd7cdbca0690
diff --git a/src/web/src/components/community/messages/mobile-message-gesture.ts b/src/web/src/components/community/messages/mobile-message-gesture.ts
index 8d8d515bd..b38ef0db9 100644
--- a/src/web/src/components/community/messages/mobile-message-gesture.ts
+++ b/src/web/src/components/community/messages/mobile-message-gesture.ts
@@ -1,4 +1,4 @@
-const MOBILE_REPLY_EDGE_GUARD_PX = 24
+import { MOBILE_BACK_EDGE_PX } from "@/hooks/community/use-native-mobile-back"
const MOBILE_REPLY_INTENT_PX = 8
const MOBILE_REPLY_THRESHOLD_PX = 64
export const MOBILE_REPLY_MAX_OFFSET_PX = 88
@@ -17,7 +17,7 @@ export function beginMobileReplyGesture(
clientX: number,
clientY: number,
): MobileReplyGesture | null {
- if (clientX <= MOBILE_REPLY_EDGE_GUARD_PX) return null
+ if (clientX <= MOBILE_BACK_EDGE_PX) return null
return {
startX: clientX,
startY: clientY,
diff --git a/src/web/src/components/community/shell/shell-frame.dom.test.ts b/src/web/src/components/community/shell/shell-frame.dom.test.ts
index 0666cfe78..1c34d2c4f 100644
--- a/src/web/src/components/community/shell/shell-frame.dom.test.ts
+++ b/src/web/src/components/community/shell/shell-frame.dom.test.ts
@@ -452,6 +452,36 @@ describe("ShellFrame orchestration", () => {
expect(mocks.replace).toHaveBeenCalledWith("/c/me")
})
+ it.each([
+ ["/c/channels/s1/thread1", { id: "thread1", serverId: "s1", parentChannelId: "parent1" }, "/c/channels/s1/parent1"],
+ ["/c/channels/s1/c1", { id: "c1", serverId: "s1", parentChannelId: null }, "/c/channels/s1"],
+ ["/c/channels/s1/c1", { id: "c1", serverId: "s2", parentChannelId: "wrong" }, "/c/channels/s1"],
+ ["/c/channels/s1", undefined, null],
+ ["/c/me/dm1", undefined, "/c/me"],
+ ["/c/me", undefined, null],
+ ])("returns to the structural parent from %s and stops at list roots", (href, channel, expected) => {
+ mocks.breakpoint.current = "mobile"
+ mocks.currentHref.current = href
+ mocks.projectedChannel.current = channel
+ const renderer = render(createElement(ShellFrame, { ...baseProps, frameHref: href }))
+ const event = new Event("alook:native-back", { cancelable: true })
+ act(() => { window.dispatchEvent(event) })
+ expect(event.defaultPrevented).toBe(true)
+ if (expected) expect(mocks.replace).toHaveBeenCalledExactlyOnceWith(expected)
+ else expect(mocks.replace).not.toHaveBeenCalled()
+ renderer.unmount()
+ })
+
+ it("supersedes an in-flight navigation with the committed page before returning further", () => {
+ mocks.breakpoint.current = "mobile"
+ mocks.navigationPending.current = true
+ mocks.pendingHref.current = "/c/channels/s1/c2"
+ const renderer = render(createElement(ShellFrame, { ...baseProps, frameHref: "/c/channels/s1" }))
+ act(() => { window.dispatchEvent(new Event("alook:native-back", { cancelable: true })) })
+ expect(mocks.replace).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")
+ renderer.unmount()
+ })
+
it("keeps mobile server onboarding stable on an existing list surface", () => {
mocks.breakpoint.current = "mobile"
mocks.currentHref.current = "/c/me"
diff --git a/src/web/src/components/community/shell/shell-frame.tsx b/src/web/src/components/community/shell/shell-frame.tsx
index 03f87af7d..2b789d510 100644
--- a/src/web/src/components/community/shell/shell-frame.tsx
+++ b/src/web/src/components/community/shell/shell-frame.tsx
@@ -11,6 +11,7 @@ import { useBreakpoint } from "@/hooks/use-mobile"
import { useCommunityOnboarding } from "@/lib/community-onboarding"
import {
advanceCommunityCommittedFrame,
+ channelHref,
isPublishedNonStructuralCommit,
normalizeCommunityHref,
resolveCommunityCheckpointPlan,
@@ -95,6 +96,16 @@ const [committedFrame, setCommittedFrame] = useAtom(useCreateAtom(initialCommitt
const navigation = useCommunityNavigationController(committedFrame)
const replacePath = navigation.replace
const route = resolveCommunityRoute(committedFrame.pathname)
+ const committedModule = resolveCommunityModulePlan(committedFrame.pathname)
+ const committedChannelId = committedModule.main.kind === "server-conversation"
+ ? committedModule.main.leafId : null
+ const committedChannel = useRouteChannelProjection(committedChannelId)
+ const mobileParentPath = committedModule.sidebar.kind === "server"
+ && committedChannel?.id === committedChannelId
+ && committedChannel.serverId === committedModule.sidebar.serverId
+ && committedChannel.parentChannelId
+ ? channelHref(committedModule.sidebar.serverId, committedChannel.parentChannelId)
+ : route.parentPath
const target = navigation.pendingHref
? normalizeCommunityHref(navigation.pendingHref)
: null
@@ -290,18 +301,24 @@ const [committedFrame, setCommittedFrame] = useAtom(useCreateAtom(initialCommitt
dismissUserBarExtension()
}, [breakpoint, checkpoint.surface, dismissUserBarExtension, userBarExtension.active])
const dismissNativeBackShellOverlay = useCallback(() => {
+ if (navigation.navigationPending) {
+ replacePath(committedFrame.href)
+ return true
+ }
if (breakpoint === "mobile" && checkpoint.surface === "detail") return false
if (userBarExtension.active === "none") return false
dismissUserBarExtension()
return true
- }, [breakpoint, checkpoint.surface, dismissUserBarExtension, userBarExtension.active])
+ }, [breakpoint, checkpoint.surface, committedFrame.href, dismissUserBarExtension, navigation.navigationPending, replacePath, userBarExtension.active])
const goBackMobile = useCallback(() => {
if (route.parentPath) navigation.replace(route.parentPath)
}, [navigation, route.parentPath])
useNativeMobileBack({
+ mobile: breakpoint === "mobile",
+ gestureKey: committedFrame.href,
dismissShellOverlay: dismissNativeBackShellOverlay,
- parentPath: route.parentPath,
+ parentPath: mobileParentPath,
replacePath,
})
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.ts
index a01e79929..713a7b980 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
@@ -61,7 +61,7 @@ describe("useNativeMobileBack", () => {
renderer.unmount()
})
- it("replaces a detail route and leaves a root route to the system", () => {
+ it("replaces a detail route and consumes back at a root route", () => {
const dismissShellOverlay = vi.fn(() => false)
const replacePath = vi.fn()
const renderer = render(createElement(Harness, {
@@ -78,8 +78,101 @@ describe("useNativeMobileBack", () => {
parentPath: null,
replacePath,
}))
- expect(dispatchBack().defaultPrevented).toBe(false)
+ expect(dispatchBack().defaultPrevented).toBe(true)
renderer.unmount()
expect(dispatchBack().defaultPrevented).toBe(false)
})
})
+
+function touch(type: string, x: number, y = 100, count = 1, cancelable = true) {
+ const points = Array.from({ length: count }, (_, identifier) => ({ identifier, clientX: x, clientY: y }))
+ const event = new Event(type, { bubbles: true, cancelable })
+ Object.defineProperties(event, {
+ touches: { value: type === "touchend" ? [] : points },
+ changedTouches: { value: points },
+ })
+ document.dispatchEvent(event)
+ return event
+}
+
+function mobileHarness(parentPath: string | null = "/c/channels/s1") {
+ const props = { mobile: true, gestureKey: "channel-1", parentPath, replacePath: vi.fn(), dismissShellOverlay: vi.fn(() => false) }
+ return { props, renderer: render(createElement(Harness, props)) }
+}
+
+describe("mobile edge parent gesture", () => {
+ it("replaces the parent once at the shared edge boundary, on release only", () => {
+ const { props, renderer } = mobileHarness()
+ touch("touchstart", 24)
+ expect(touch("touchmove", 100).defaultPrevented).toBe(true)
+ expect(props.replacePath).not.toHaveBeenCalled()
+ touch("touchend", 100)
+ touch("touchend", 100)
+ expect(props.replacePath).toHaveBeenCalledExactlyOnceWith("/c/channels/s1")
+ renderer.unmount()
+ })
+
+ it.each([
+ [25, 110, 100, 110, 100],
+ [0, 63, 100, 63, 100],
+ [12, 8, 125, 100, 130],
+ [12, 0, 100, 100, 100],
+ [12, 100, 100, 20, 100],
+ [12, 100, 100, 100, 220],
+ ])("rejects outside/short/vertical/left/reversed/diagonal (%s,%s,%s,%s,%s)", (start, moveX, moveY, endX, endY) => {
+ const { props, renderer } = mobileHarness()
+ touch("touchstart", start)
+ touch("touchmove", moveX, moveY)
+ touch("touchend", endX, endY)
+ expect(props.replacePath).not.toHaveBeenCalled()
+ renderer.unmount()
+ })
+
+ it("leaves vertical edge scrolling and taps uncancelled", () => {
+ const { props, renderer } = mobileHarness()
+ expect(touch("touchstart", 10).defaultPrevented).toBe(false)
+ expect(touch("touchmove", 12, 130).defaultPrevented).toBe(false)
+ expect(touch("touchend", 12, 150).defaultPrevented).toBe(false)
+ expect(props.replacePath).not.toHaveBeenCalled()
+ renderer.unmount()
+ })
+
+ it.each(["touchcancel", "multitouch", "browser-scroll", "route", "desktop", "unmount"])("cancels on %s", (reason) => {
+ const { props, renderer } = mobileHarness()
+ touch("touchstart", 10)
+ touch("touchmove", 100)
+ if (reason === "touchcancel") touch("touchcancel", 100)
+ if (reason === "browser-scroll") touch("touchmove", 110, 100, 1, false)
+ if (reason === "multitouch") touch("touchmove", 100, 100, 2)
+ if (reason === "route") renderer.rerender(createElement(Harness, { ...props, gestureKey: "channel-2" }))
+ if (reason === "desktop") renderer.rerender(createElement(Harness, { ...props, mobile: false }))
+ if (reason === "unmount") renderer.unmount()
+ touch("touchend", 120)
+ expect(props.replacePath).not.toHaveBeenCalled()
+ if (reason !== "unmount") renderer.unmount()
+ })
+
+ it("rejects a multi-finger start and consumes native back at the picker", () => {
+ const { props, renderer } = mobileHarness(null)
+ touch("touchstart", 10, 100, 2)
+ expect(touch("touchmove", 100).defaultPrevented).toBe(false)
+ touch("touchend", 100)
+ touch("touchstart", 10)
+ touch("touchmove", 100)
+ touch("touchend", 100)
+ expect(dispatchBack().defaultPrevented).toBe(true)
+ expect(props.replacePath).not.toHaveBeenCalled()
+ renderer.unmount()
+ })
+
+ it("dismisses overlays before a parent navigation", () => {
+ const { props, renderer } = mobileHarness()
+ props.dismissShellOverlay.mockReturnValue(true)
+ touch("touchstart", 10)
+ touch("touchmove", 100)
+ touch("touchend", 100)
+ expect(props.dismissShellOverlay).toHaveBeenCalledOnce()
+ expect(props.replacePath).not.toHaveBeenCalled()
+ 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.ts
index d24937ec2..24f7e8c41 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,7 @@
import { useEffect } from "react"
+export const MOBILE_BACK_EDGE_PX = 24
+
export const NATIVE_MOBILE_BACK_EVENT = "alook:native-back"
const dismissibleLayerSelector = [
@@ -21,10 +23,14 @@ function dismissTopDocumentLayer(): boolean {
}
export function useNativeMobileBack({
+ mobile = false,
+ gestureKey,
dismissShellOverlay,
parentPath,
replacePath,
}: {
+ mobile?: boolean
+ gestureKey?: string
dismissShellOverlay: () => boolean
parentPath: string | null
replacePath: (href: string) => void
@@ -32,16 +38,56 @@ export function useNativeMobileBack({
useEffect(() => {
const handleBack = (event: Event) => {
if (!event.cancelable) return
- if (dismissTopDocumentLayer() || dismissShellOverlay()) {
- event.preventDefault()
- return
- }
- if (!parentPath) return
event.preventDefault()
- replacePath(parentPath)
+ if (dismissTopDocumentLayer() || dismissShellOverlay()) return
+ if (parentPath) replacePath(parentPath)
}
+ let gesture: { id: number; x: number; y: number; horizontal: boolean } | null = null
+ const cancel = () => { gesture = null }
+ const start = (event: TouchEvent) => {
+ cancel()
+ if (event.touches.length !== 1) return
+ const touch = event.touches[0]
+ if (touch.clientX < 0 || touch.clientX > MOBILE_BACK_EDGE_PX) return
+ gesture = { id: touch.identifier, x: touch.clientX, y: touch.clientY, horizontal: false }
+ }
+ const move = (event: TouchEvent) => {
+ if (!gesture) return
+ if (!event.cancelable) return cancel()
+ const touch = event.touches[0]
+ if (event.touches.length !== 1 || touch.identifier !== gesture.id) return cancel()
+ const dx = touch.clientX - gesture.x
+ const dy = Math.abs(touch.clientY - gesture.y)
+ if (!gesture.horizontal && Math.max(Math.abs(dx), dy) >= 8) {
+ if (dx <= dy) return cancel()
+ gesture.horizontal = true
+ }
+ if (gesture.horizontal) event.preventDefault()
+ }
+ const end = (event: TouchEvent) => {
+ const current = gesture
+ cancel()
+ if (!current?.horizontal || event.touches.length !== 0) return
+ const touch = Array.from(event.changedTouches).find((item) => item.identifier === current.id)
+ if (!touch) return
+ event.preventDefault()
+ if (touch.clientX - current.x < 64 || Math.abs(touch.clientY - current.y) >= touch.clientX - current.x) return
+ handleBack(event)
+ }
window.addEventListener(NATIVE_MOBILE_BACK_EVENT, handleBack)
- return () => window.removeEventListener(NATIVE_MOBILE_BACK_EVENT, handleBack)
- }, [dismissShellOverlay, parentPath, replacePath])
+ if (mobile) {
+ 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)
+ }
+ return () => {
+ window.removeEventListener(NATIVE_MOBILE_BACK_EVENT, handleBack)
+ document.removeEventListener("touchstart", start, true)
+ document.removeEventListener("touchmove", move, true)
+ document.removeEventListener("touchend", end, true)
+ document.removeEventListener("touchcancel", cancel, true)
+ }
+ }, [mobile, gestureKey, dismissShellOverlay, parentPath, replacePath])
}