refactor(pages): one page-comment store for web, desktop and mobile

Merged
HA
2mo ago
+1940-1398

Summary

  • Collapses three near-identical page-comment stores (web, desktop, mobile) into one @superset/cloud-client package, with the shared types moved to @superset/shared/page-comments and server shaping extracted to trpc/router/page-comment/shape.ts.
  • Opens the comment composer the moment a block is selected, with the quick actions on the card's top row; deletes the standalone SelectionToolbar and toolbarPlacement on both surfaces.
  • Fixes the hover highlight surviving a scroll, and two defects found while reviewing this branch.

Why

Each surface had grown its own optimistic React Query store against the same pageComment router — 247 lines on web, 208 on desktop, 112 on mobile — so every change to comment behaviour had to be made three times and kept in step by hand. They had already drifted. One store, one set of optimistic row helpers, one place to fix a bug.

How it works

@superset/cloud-client owns the store. usePageComments runs the optimistic mutations (create / reply / edit / resolve / delete) over pure row helpers in threadRows and optimisticRows, each mutation snapshotting the cache in onMutate and restoring it in onError. CloudClientProvider supplies the caller, so each app only has to hand it a tRPC client: web and desktop through their existing providers, mobile through createCloudCaller(apiClient).

Server-side, shapeThread/shapeComment are now the single definition of the wire shape, and create/reply return the shaped row rather than bare ids so the client can swap its placeholder for the real row without a refetch.

Review findings fixed in this branch

Two defects found reviewing the first two commits, each fixed in its own commit:

  1. reply reported the wrong failure (page-comment.ts:270) — the new empty-insert guard reused failedToCreateThread, so a failed reply told the reader their thread could not be created, about a thread already on screen. Added serverError.pageComment.failedToPostReply with translations for all 16 non-English locales.
  2. The store was rebuilt every render — usePageComments memoised on the useMutation results, which TanStack Query recreates each render. The memo never held, so CommentProvider's callbacks and context value churned and re-rendered every consumer. Now destructures the stable mutateAsync references.

Behaviour change worth flagging

pageComment.reply now returns a full shaped comment instead of { id }. Two callers outside the apps pass the result straight through, so their output gains body, authorName, authorImage and createdAt:

  • packages/cli/src/commands/pages/comments/reply/command.ts → superset pages comments reply --json
  • packages/mcp/src/tools/pages/comments/reply.ts → the MCP tool result

Additive — id is still present, so anything reading it keeps working — but it is an agent- and CLI-facing contract change.

Testing

  • packages/cloud-client 14/14, packages/shared 1021/1021, packages/ui 71/71
  • typecheck clean: cloud-client, shared, ui, i18n, trpc, apps/web, apps/desktop
  • bun run check:i18n — 5358 messages, 0 missing across all 17 locales, compile --strict passes
  • bunx sherif — 0 errors
  • biome check clean on every file touched

Known limitations

  • usePageCommentThreads sets isLoading from list.isPending, which stays true while the query is disabled (version > 0). A page at version 0 would spin forever. No caller appears to pass 0, so this is likely unreachable — left as-is rather than guessed at.
  • Concurrent mutations each snapshot the whole cache, so if two are in flight and the first fails, its rollback discards the second's optimistic row. Pre-existing in all three stores this replaces; not made worse here.
  • Manual QA covered the desktop pane only (hover, scroll, comment, reply). Web and mobile were not exercised by hand.

Pre-existing failures, not from this branch

  • packages/trpc lib/growth/sitemap.test.ts fails at module load on env validation; passes under dotenv -e ../../.env.
  • apps/mobile typecheck reports 5 errors for importFromCloud, materializeIntoWorkspace, customTitle and rename. Those procedures do not exist in @superset/trpc at all, none of the files are touched here, and this branch's only mobile change is an additive export const cloudCaller. Worth confirming main is already red before landing.

https://claude.ai/code/session_01YQXXfpsXYTcQwj6PiSptGm


Summary by cubic

Consolidates web, desktop, and mobile page comments into one optimistic data store in @superset/cloud-client, replacing three near-identical stores that had drifted apart. Selecting a block now opens the comment composer immediately, with quick actions on the card's top row.

Apps hand CloudClientProvider their plain tRPC client (React, vanilla, or mobile proxy), and the hooks call client.pageComment.* directly, so the package has both runtime and type-only independence from the router. The session-to-user mapping is folded into pageCommentUser() in @superset/shared/page-comments.

Behavior changes

  • pageComment.create and reply now return the shaped row instead of bare ids, so the optimistic placeholder is replaced without a refetch.
  • reply now returns body, authorName, authorImage, and createdAt in addition to id; this changes the superset pages comments reply --json output and the MCP tool result. The author now comes from the session instead of a post-insert lookup, removing a duplicate-on-retry ordering hazard.
  • Failed replies now show a "Failed to post reply" message, translated in all 17 locales, instead of the thread-creation error.
  • Fixed the store being rebuilt on every render, which re-rendered every consumer via the provider.
  • Fixed hover highlighting: the outline dismisses on scroll, and its geometry no longer flashes at the previous position for a frame.
  • Mobile now sends the comment intent (SUPER-2237) and clears a stale pending selection after the composer is dismissed.
  • Desktop reads through cloudTrpcClient instead of apiTrpcClient, keeping queries scoped to the window's organization; the org-switch purge and 30s stale time now recognize the store's flat cloud query keys.
  • Mutations now drain per page and invalidate the list once the last in-flight settles, and rollback removes the query when nothing was cached yet.

Notes for review

  • Tests now cover the optimistic rows, cache transitions, and the in-flight settlement race.
  • Manual QA covered only the desktop pane; web and mobile were not exercised by hand.
  • Pre-existing, unrelated failures: packages/trpc sitemap test fails without env vars, and mobile typecheck reports 5 errors for procedures missing from @superset/trpc.

Summary by CodeRabbit

  • New Features

    • Added consistent page comments across web, desktop, and mobile.
    • Added quick feedback actions for approvals, deletion requests, and preset responses.
    • Mobile selections now open the composer with the selected page anchor.
    • Added localized error messaging when replies fail to post.
  • Bug Fixes

    • Improved optimistic comment updates, refresh behavior, and rollback handling.
    • Improved comment hover outlines and selection highlighting.
    • Hover indicators now clear correctly while scrolling.
    • Improved comment refresh when returning to pages or switching collections.