Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): stabilize drawer geometry and warm channel suggestions (#791)

Gustavo Yedd2bf8a7544c
diff --git a/src/web/src/components/community/messages/use-composer-suggestions.dom.test.ts b/src/web/src/components/community/messages/use-composer-suggestions.dom.test.ts
index 2d8d3bf7f..a5a9c38ac 100644
--- a/src/web/src/components/community/messages/use-composer-suggestions.dom.test.ts
+++ b/src/web/src/components/community/messages/use-composer-suggestions.dom.test.ts
@@ -200,6 +200,7 @@ describe("useComposerSuggestions", () => {
})
channelOptions.setPopup({
items: [],
+ query: "gen",
selectedIndex: 0,
command: vi.fn(),
getRect: null,
@@ -685,6 +686,7 @@ describe("useComposerSuggestions", () => {
expect(resultRef.current!.channelRefPopup.items[0].serverDiscriminator).toBe("0002")
const second = channel({ id: "channel-2", name: "random" })
+ mocks.rankChannel.mockReturnValue([channel(), second])
await act(async () => {
channelOptions.setPopup({
items: [channel(), second],
@@ -819,7 +821,35 @@ describe("useComposerSuggestions", () => {
expect(resultRef.current!.channelRefPresentation).toBeUndefined()
})
- it("keeps loading until newly resolved candidates are ranked into the live popup", async () => {
+ it("reconciles a stale suggestion snapshot against warm candidates without loading", async () => {
+ const resultRef: { current: Result | null } = { current: null }
+ const ready = channel()
+ mocks.rankChannel.mockReturnValue([ready])
+ await act(async () => {
+ rtlRender(createElement(Harness, {
+ members: [],
+ context: "dm",
+ channelRefCandidates: [ready],
+ channelRefCandidateSource: channelRefSource(),
+ resultRef,
+ }))
+ })
+ const channelOptions = mocks.buildChannel.mock.calls[0][0]
+ await act(async () => {
+ channelOptions.setPopup({
+ items: [],
+ query: "",
+ selectedIndex: 0,
+ command: vi.fn(),
+ getRect: null,
+ })
+ })
+ expect(resultRef.current!.channelRefPresentation).toEqual({ status: "ready" })
+ expect(resultRef.current!.channelRefPopup.items).toEqual([ready])
+ expect(channelOptions.popupRef.current.items).toEqual([ready])
+ })
+
+ it("ranks resolved candidates before presenting the live popup", async () => {
const resultRef: { current: Result | null } = { current: null }
const presentationHistory: Array<Result["channelRefPresentation"]> = []
let renderer!: ReturnType<typeof rtlRender>
@@ -857,8 +887,8 @@ describe("useComposerSuggestions", () => {
}))
})
- expect(presentationHistory.map((presentation) => presentation?.status))
- .toEqual(["loading", "ready"])
+ expect(presentationHistory.length).toBeGreaterThan(0)
+ expect(presentationHistory.every((presentation) => presentation?.status === "ready")).toBe(true)
expect(resultRef.current!.channelRefPopup.items).toEqual([ready])
})
})
diff --git a/src/web/src/components/community/messages/use-composer-suggestions.ts b/src/web/src/components/community/messages/use-composer-suggestions.ts
index 3e357928e..4628ef4a9 100644
--- a/src/web/src/components/community/messages/use-composer-suggestions.ts
+++ b/src/web/src/components/community/messages/use-composer-suggestions.ts
@@ -1,4 +1,4 @@
-import { useEffect, useLayoutEffect, useRef, useState } from "react"
+import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"
import {
buildCommunityMentionExtension,
EMPTY_MENTION_STATE,
@@ -86,10 +86,23 @@ export function useComposerSuggestions({
mentionPopupRef.current = mentionPopup
}, [mentionPopup])
- const [channelRefPopup, setChannelRefPopup] =
+ const [channelRefPopupState, setChannelRefPopup] =
useState<ChannelRefPopupState>(EMPTY_CHANNEL_REF_STATE)
+ const channelRefPopup = useMemo(() => {
+ if (!channelRefPopupState.command) return channelRefPopupState
+ const items = rankChannelRefItems(channelRefCandidates, channelRefPopupState.query ?? "")
+ if (channelRefItemsEqual(channelRefPopupState.items, items)) return channelRefPopupState
+ return {
+ ...channelRefPopupState,
+ items,
+ selectedIndex: channelRefPopupState.selectedIndex < items.length
+ ? channelRefPopupState.selectedIndex
+ : 0,
+ }
+ }, [channelRefCandidates, channelRefPopupState])
+ if (channelRefPopup !== channelRefPopupState) setChannelRefPopup(channelRefPopup)
const channelRefPopupRef = useRef(channelRefPopup)
- useEffect(() => {
+ useLayoutEffect(() => {
channelRefPopupRef.current = channelRefPopup
}, [channelRefPopup])
@@ -136,18 +149,12 @@ export function useComposerSuggestions({
channelRefCandidatesRef.current = channelRefCandidates
}, [channelRefCandidates])
- const channelRefItemsAligned = !channelRefCandidateSource
- || !channelRefPopup.command
- || channelRefItemsEqual(
- channelRefPopup.items,
- rankChannelRefItems(channelRefCandidates, channelRefPopup.query ?? ""),
- )
const channelRefPresentation: ChannelRefCandidatePresentation | undefined =
channelRefCandidateSource
? {
status: channelRefCandidateSource.failed
? "error"
- : channelRefCandidateSource.loading || !channelRefItemsAligned
+ : channelRefCandidateSource.loading
? "loading"
: channelRefPopup.items.length > 0
? "ready"
@@ -233,22 +240,6 @@ export function useComposerSuggestions({
return { status: mentionPopup.items.length > 0 ? "ready" : "empty" }
})()
- useEffect(() => {
- const current = channelRefPopupRef.current
- if (!current.command) return
- const items = rankChannelRefItems(
- channelRefCandidates,
- channelRefQueryRef.current,
- )
- if (channelRefItemsEqual(current.items, items)) return
- setChannelRefPopup({
- ...current,
- items,
- selectedIndex:
- current.selectedIndex < items.length ? current.selectedIndex : 0,
- })
- }, [channelRefCandidates])
-
const resetPopups = () => {
mentionCandidates?.search("")
setMentionPopup(EMPTY_MENTION_STATE)
diff --git a/src/web/src/test/e2e-ui/53-mobile-inbox-surface.spec.ts b/src/web/src/test/e2e-ui/53-mobile-inbox-surface.spec.ts
index 1919e7243..a35bbc9b2 100644
--- a/src/web/src/test/e2e-ui/53-mobile-inbox-surface.spec.ts
+++ b/src/web/src/test/e2e-ui/53-mobile-inbox-surface.spec.ts
@@ -1,6 +1,6 @@
import type { Page, Request } from "@playwright/test"
import { expect, sessionCookie, test } from "./_fixtures/community-fixture"
-import { gotoAfterUserWsAuth, ignoreNextDevToolsPointerCapture } from "./_fixtures/actions"
+import { GEOMETRY_EPSILON, gotoAfterUserWsAuth, ignoreNextDevToolsPointerCapture, waitForElementMotion } from "./_fixtures/actions"
import { proxyCommunityWebSockets } from "./_fixtures/community-ws-proxy"
import { WEB_URL } from "./_setup/paths"
import {
@@ -248,12 +248,13 @@ test.describe.serial("mobile Inbox interactive user-bar base", () => {
const current = await surfaceGeometry(bob.page)
return Math.abs(current.card.bottom - current.userBar.top)
}).toBeLessThanOrEqual(1)
+ await waitForElementMotion(mobileSurface)
const geometry = await surfaceGeometry(bob.page)
expectJoinedBorderContract(geometry)
expect(geometry.userBar.bottom).toBe(844)
expect(geometry.userBar.surfaceBottom).toBe(844 - 34 - 12)
expect(geometry.userBarOwnsCenter).toBe(true)
- expect(geometry.card.height).toBeLessThanOrEqual(448)
+ expect(geometry.card.height).toBeLessThanOrEqual(448 + GEOMETRY_EPSILON)
expect(geometry.card.top).toBeGreaterThanOrEqual(20)
expect(geometry.seam.cardTopLeft).not.toBe("0px")
expect(geometry.seam.cardTopRight).not.toBe("0px")
diff --git a/src/web/src/test/e2e-ui/56-inbox-friend-requests.spec.ts b/src/web/src/test/e2e-ui/56-inbox-friend-requests.spec.ts
index f0b334d06..e2b1bc3b9 100644
--- a/src/web/src/test/e2e-ui/56-inbox-friend-requests.spec.ts
+++ b/src/web/src/test/e2e-ui/56-inbox-friend-requests.spec.ts
@@ -1,6 +1,6 @@
import type { Locator, Page } from "@playwright/test"
import { expect, test, userId, userName } from "./_fixtures/community-fixture"
-import { gotoAfterUserWsAuth } from "./_fixtures/actions"
+import { GEOMETRY_EPSILON, gotoAfterUserWsAuth, waitForElementMotion } from "./_fixtures/actions"
import {
seedCancelFriendRequest,
seedChannel,
@@ -64,8 +64,8 @@ function expectMinimumRect(
rect: { width: number; height: number },
minimum: number,
) {
- expect.soft(rect.width, `${label} width`).toBeGreaterThanOrEqual(minimum)
- expect.soft(rect.height, `${label} height`).toBeGreaterThanOrEqual(minimum)
+ expect.soft(rect.width, `${label} width`).toBeGreaterThanOrEqual(minimum - GEOMETRY_EPSILON)
+ expect.soft(rect.height, `${label} height`).toBeGreaterThanOrEqual(minimum - GEOMETRY_EPSILON)
}
async function expectExactRect(
@@ -289,14 +289,12 @@ test.describe.serial("actionable Inbox friend requests", () => {
const reject = bob.page.getByTestId(tid.inboxFriendRequestReject(friendshipId))
await expect(accept).toHaveAccessibleName(/Accept .+ friend request/)
await expect(reject).toHaveAccessibleName(/Reject .+ friend request/)
- await inboxSurface.evaluate(async (element) => {
- await Promise.all(element.getAnimations().map((animation) => animation.finished))
- })
+ await waitForElementMotion(inboxSurface)
for (const action of [accept, reject]) {
const box = await action.boundingBox()
expect(box).not.toBeNull()
- expect(box!.width).toBeGreaterThanOrEqual(44)
- expect(box!.height).toBeGreaterThanOrEqual(44)
+ expect(box!.width).toBeGreaterThanOrEqual(44 - GEOMETRY_EPSILON)
+ expect(box!.height).toBeGreaterThanOrEqual(44 - GEOMETRY_EPSILON)
}
await reject.focus()
diff --git a/src/web/src/test/e2e-ui/_fixtures/actions.ts b/src/web/src/test/e2e-ui/_fixtures/actions.ts
index 496de66b8..7e87f1ef5 100644
--- a/src/web/src/test/e2e-ui/_fixtures/actions.ts
+++ b/src/web/src/test/e2e-ui/_fixtures/actions.ts
@@ -3,6 +3,22 @@ import { tid } from "./testids"
const HOVER_FINE_QUERY = "(hover: hover) and (pointer: fine)"
+export async function waitForElementMotion(locator: Locator): Promise<void> {
+ await locator.evaluate(async (element) => {
+ await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))
+ await new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))
+ const animations: Animation[] = []
+ for (let ancestor: Element | null = element; ancestor; ancestor = ancestor.parentElement) {
+ animations.push(...ancestor.getAnimations().filter((animation) => (
+ animation.effect?.getTiming().iterations !== Infinity
+ )))
+ }
+ await Promise.all(animations.map((animation) => animation.finished.catch(() => {})))
+ })
+}
+
+export const GEOMETRY_EPSILON = 0.01
+
export function observeUserWsAuth(page: Page): {
authenticated: Promise<void>
cleanup: () => void