Skip to content

Commit d0c2bcd

Browse files
committed
fix(record-page): the phone's long-press sheet follows the comment's actions, and the press asks for them (#36076)
L1-c REWORK: the sheet kept the actions from the moment of the hold, and the updatesOthers question started on that same hold, so a moderator's first sheet on someone else's comment had no Mark resolved and no Edit even after the answer came. The press now engages the host (pointer down, context menu, Enter), and CommentThread reports the held comment's current actions (heldId / onHeldActions) whenever what they offer changes. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L6bhgTfxZyB5T41DdoPaBy
1 parent 07ba1c3 commit d0c2bcd

7 files changed

Lines changed: 165 additions & 21 deletions

File tree

‎.changeset/36076-phone-comment-page.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
feat(record-page): on a phone, the record's comments open as a page of their own: the threads first, a thread's replies inside it, the comment box docked at the foot, and a long press for a comment's actions
88

9-
Clause-②: yes (widening) — `@object-ui/plugin-detail` adds the export `CommentsPage` and its type `CommentsPageProps`; `CommentThread` takes a third `mode`, `'page'`, and an optional `itemInteraction`; `CommentItem` an optional `interactionProps`; `CommentComposer` and `RichTextCommentInput` (exported) an optional `layout` (`'default' | 'phone'`). `@object-ui/app-shell` reserves the console URL parameter `comments` (`RECORD_COMMENTS_PARAM`, in `RESERVED_URL_PARAMS`). `@object-ui/i18n` adds four keys to every pack: `detail.addReactionEmoji`, `detail.commentThread`, `detail.formatBold`, `detail.formatItalic` (the toolbar's names without their shortcut). Nothing existing is removed or re-typed; the desktop's panel, toolbar and labels are unchanged.
9+
Clause-②: yes (widening) — `@object-ui/plugin-detail` adds the export `CommentsPage` and its type `CommentsPageProps`; `CommentThread` takes a third `mode`, `'page'`, and optional `itemInteraction`, `heldId` and `onHeldActions`; `CommentItem` an optional `interactionProps`; `CommentComposer` and `RichTextCommentInput` (exported) an optional `layout` (`'default' | 'phone'`). `@object-ui/app-shell` reserves the console URL parameter `comments` (`RECORD_COMMENTS_PARAM`, in `RESERVED_URL_PARAMS`). `@object-ui/i18n` adds four keys to every pack: `detail.addReactionEmoji`, `detail.commentThread`, `detail.formatBold`, `detail.formatItalic` (the toolbar's names without their shortcut). Nothing existing is removed or re-typed; the desktop's panel, toolbar and labels are unchanged.
1010
Metadata-protocol: none
1111
Skills-docs: none — no skill under `skills/**` teaches where the console draws a record's comments; `content/docs/frontend/plugins/plugin-detail.mdx` and the plugin-detail README gain the phone's page in this change.
1212

‎ui/packages/app-shell/src/views/RecordDetailView.commentsPanel-36075.test.tsx‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import { MetadataCtx } from '@object-ui/react';
6060
import { MePermissionsProvider, type MePermissionsAuthenticatedResponse } from '@object-ui/permissions';
6161
import { toast } from 'sonner';
6262
import { RecordDetailView } from './RecordDetailView';
63+
import { __clearRecordEditableCache } from '../../../plugin-detail/src/useRecordEditable';
6364

6465
const OBJECT_NAME = 'crm_case';
6566
const NO_FEEDS = 'crm_note';
@@ -386,6 +387,62 @@ describe("the phone's comment page (objectstack#36076)", () => {
386387
expect(within(container).queryByText('Ctrl+Enter', { exact: false })).toBeNull();
387388
});
388389

390+
it("a moderator's first long press on another's comment: the sheet gains Edit when the late answer lands", async () => {
391+
__clearRecordEditableCache();
392+
// The record's update is answered at once (the viewer moderates); the row
393+
// floor's answer for a comment the viewer did not write comes later.
394+
let answerFloor!: () => void;
395+
const floor = new Promise<void>((resolve) => {
396+
answerFloor = resolve;
397+
});
398+
const explained: string[] = [];
399+
const allow = () =>
400+
new Response(JSON.stringify({ allowed: true, record: { visible: true } }), {
401+
status: 200,
402+
headers: { 'content-type': 'application/json' },
403+
});
404+
vi.stubGlobal(
405+
'fetch',
406+
vi.fn(async (url: string, init?: RequestInit) => {
407+
if (String(url).endsWith('/security/explain')) {
408+
const body = JSON.parse(String(init?.body ?? '{}'));
409+
explained.push(body.object);
410+
if (body.object === 'sys_comment') await floor;
411+
return allow();
412+
}
413+
return new Response(JSON.stringify({ data: [] }), { status: 200, headers: { 'content-type': 'application/json' } });
414+
}),
415+
);
416+
try {
417+
await mount({ search: '?comments=' });
418+
await waitFor(() => expect(page()).toHaveAttribute('data-comments-page', 'list'));
419+
const grace = (await within(page()!).findByText(GRACE_BODY)).closest('[data-comment-id]') as HTMLElement;
420+
// Opening the page asks nothing about other people's comments.
421+
await act(async () => {});
422+
expect(explained).not.toContain('sys_comment');
423+
vi.useFakeTimers();
424+
fireEvent.pointerDown(grace, { pointerType: 'touch', button: 0, clientX: 10, clientY: 10 });
425+
act(() => {
426+
vi.advanceTimersByTime(600);
427+
});
428+
fireEvent.pointerUp(grace, { pointerType: 'touch' });
429+
vi.useRealTimers();
430+
// The press asked; the hold opened before the answer.
431+
expect(explained).toContain('sys_comment');
432+
const sheet = () => document.querySelector('[data-comment-action-sheet]') as HTMLElement | null;
433+
const rows = () =>
434+
Array.from(sheet()?.querySelectorAll('[data-comment-action]') ?? []).map((row) => row.getAttribute('data-comment-action'));
435+
await waitFor(() => expect(rows()).toEqual(['delete']));
436+
await act(async () => {
437+
answerFloor();
438+
});
439+
await waitFor(() => expect(rows()).toEqual(['edit', 'delete']));
440+
} finally {
441+
vi.useRealTimers();
442+
__clearRecordEditableCache();
443+
}
444+
});
445+
389446
it('no icon on an object with feeds off', async () => {
390447
const { container } = await mount({ objectName: NO_FEEDS });
391448
await act(async () => {});

‎ui/packages/plugin-detail/README.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,16 +980,19 @@ lays them out another way, such as a phone's comment page.
980980
(`role="dialog"`, fixed over the host's page) that reads the same
981981
`useRecordComments`. Props: `threadId` (`null` for the list, else a thread's
982982
first comment's id), `onOpenThread(id)`, `onBack()`, `backLabel` (what the
983-
list's 「‹」 returns to) and `onEngage` (the first long press, when the host may
984-
ask what the actions take). The list draws each thread with
983+
list's 「‹」 returns to) and `onEngage` (a press begins on a comment, when the
984+
host may ask what the actions take; the 500 ms hold covers the answer). The list draws each thread with
985985
`CommentThread mode="summary"`; a thread draws `mode="page"`, its comment and
986986
every reply with no reply box of its own, since the page docks one
987987
`CommentComposer layout="phone"` at its foot (a new comment in the list, a reply
988988
in a thread, each its own draft). The page follows the on-screen keyboard
989989
through `visualViewport`. A long press of 500 ms that travels under 10 px, or
990990
the context menu, raises `CommentActionSheet` with the comment's
991991
`CommentItemActions`, the same object `renderActions` hands the desktop's hover
992-
row, so the gates are the thread's. `RichTextCommentInput` takes
992+
row, so the gates are the thread's. The sheet follows them while it is up:
993+
`CommentThread` takes `heldId` and reports the held comment's current actions to
994+
`onHeldActions` whenever what they offer changes, so an answer that lands after
995+
the hold adds its rows. `RichTextCommentInput` takes
993996
`layout="phone"`: the toolbar under the text, at touch size, with no preview and
994997
no shortcut hints. The console mounts the page from `?comments=` on the phone's
995998
record page (`RECORD_COMMENTS_PARAM`).

‎ui/packages/plugin-detail/src/comments/CommentThread.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ export interface RecordCommentThreadProps {
7676
* press and tap (`useCommentPress`). Spread on each comment's box.
7777
*/
7878
itemInteraction?: (actions: CommentItemActions) => React.HTMLAttributes<HTMLDivElement>;
79+
/**
80+
* The comment whose actions the surface holds open, by id (the phone's long
81+
* press sheet). While it is one of this thread's, its current actions go to
82+
* `onHeldActions` each time what they offer changes, so a gate that opens
83+
* while the sheet is up (the host's answer to `updatesOthers`) adds its row.
84+
*/
85+
heldId?: string | null;
86+
onHeldActions?: (actions: CommentItemActions) => void;
7987
className?: string;
8088
}
8189

@@ -126,6 +134,8 @@ export const CommentThread: React.FC<RecordCommentThreadProps> = ({
126134
onOpen,
127135
renderActions,
128136
itemInteraction,
137+
heldId,
138+
onHeldActions,
129139
className,
130140
}) => {
131141
const { t } = useDetailTranslation();
@@ -171,6 +181,21 @@ export const CommentThread: React.FC<RecordCommentThreadProps> = ({
171181
onDelete: onDeleteComment && deletableComment(item, moderates) ? () => void onDeleteComment(item.id) : undefined,
172182
});
173183

184+
// The held comment's actions, reported again whenever what they offer changes.
185+
const heldItem = heldId == null ? undefined : [root, ...replies].find((item) => String(item.id) === heldId);
186+
const held = heldItem ? actionsOf(heldItem, heldItem === root) : undefined;
187+
const heldOffer = held
188+
? [!!held.onReact, !!held.onResolve, !!held.resolved, !!held.onEdit, !!held.onDelete].join()
189+
: null;
190+
const heldReport = React.useRef({ held, onHeldActions });
191+
React.useLayoutEffect(() => {
192+
heldReport.current = { held, onHeldActions };
193+
});
194+
React.useLayoutEffect(() => {
195+
const { held: current, onHeldActions: report } = heldReport.current;
196+
if (current) report?.(current);
197+
}, [heldItem, heldOffer]);
198+
174199
const itemOf = (item: FeedItem, isRoot: boolean, footer?: React.ReactNode, itemClassName?: string) => {
175200
const actions = renderActions || itemInteraction ? actionsOf(item, isRoot) : undefined;
176201
return (

‎ui/packages/plugin-detail/src/comments/CommentsPage.test.tsx‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
* - A long press (or the context menu) raises the comment's actions: the six
2222
* reactions, Mark resolved on a thread's comment only, Edit and Delete only
2323
* where the comment thread hands them (the viewer's own comment). The click
24-
* that ends the hold opens nothing. The first hold tells the host.
24+
* that ends the hold opens nothing. The press tells the host, and the sheet
25+
* follows the comment's actions while it is up.
2526
* - The filter (resolved shown, only mine) is the desktop panel's.
2627
*/
2728

@@ -254,6 +255,40 @@ describe('CommentsPage — the long press', () => {
254255
expect(sheet()).not.toBeNull();
255256
});
256257

258+
it('the press engages before the hold opens, and a verdict that lands while the sheet is up adds its rows', () => {
259+
const writes = {
260+
onToggleReaction: vi.fn(),
261+
onEditComment: vi.fn(),
262+
onDeleteComment: vi.fn(),
263+
onResolveComment: vi.fn(),
264+
};
265+
const onEngage = vi.fn();
266+
const view = (updatesOthers: boolean | undefined) => (
267+
<DiscussionContextProvider items={ITEMS as any[]} {...writes} moderatesComments updatesOthersComments={updatesOthers}>
268+
<CommentsPage threadId={null} backLabel="Acme" onOpenThread={vi.fn()} onBack={vi.fn()} onEngage={onEngage} />
269+
</DiscussionContextProvider>
270+
);
271+
const { rerender } = render(view(undefined));
272+
const el = commentEl('c2');
273+
fireEvent.pointerDown(el, { pointerType: 'touch', button: 0, clientX: 10, clientY: 10 });
274+
// The host is told at the press, not at the end of the hold.
275+
expect(onEngage).toHaveBeenCalledTimes(1);
276+
act(() => {
277+
vi.advanceTimersByTime(COMMENT_LONG_PRESS_MS);
278+
});
279+
fireEvent.pointerUp(el, { pointerType: 'touch' });
280+
fireEvent.click(el);
281+
const rows = () =>
282+
Array.from(sheet()!.querySelectorAll('[data-comment-action]')).map((row) => row.getAttribute('data-comment-action'));
283+
// Grace's comment, before the host has its answer: the viewer moderates, so Delete only.
284+
expect(rows()).toEqual(['delete']);
285+
// The answer lands with the sheet still up.
286+
rerender(view(true));
287+
expect(rows()).toEqual(['resolve', 'edit', 'delete']);
288+
fireEvent.click(sheet()!.querySelector('[data-comment-action="resolve"]')!);
289+
expect(writes.onResolveComment).toHaveBeenCalledWith('c2', true);
290+
});
291+
257292
it('a read-only discussion raises no sheet and docks no box', () => {
258293
render(
259294
<DiscussionContextProvider items={ITEMS as any[]}>

‎ui/packages/plugin-detail/src/comments/CommentsPage.tsx‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ export interface CommentsPageProps {
2828
/** The list's 「‹」 names where it returns: the record. */
2929
backLabel: string;
3030
/**
31-
* The reader first held a comment: the host may now ask what the comment's
32-
* actions take (`updatesOthers`), and not before (objectstack#28470).
31+
* The reader began a press on a comment: the host may now ask what the
32+
* comment's actions take (`updatesOthers`), and not before
33+
* (objectstack#28470). The hold that opens them lasts long enough for the
34+
* answer.
3335
*/
3436
onEngage?: () => void;
3537
className?: string;
@@ -99,20 +101,26 @@ export const CommentsPage: React.FC<CommentsPageProps> = ({
99101
// A thread that is not (or no longer) here — deleted, or a stale link — shows the list.
100102
const inThread = !!thread;
101103

104+
// The held comment's actions. The thread that holds it hands them again when
105+
// what they offer changes (`onHeldActions`), so the sheet is never the
106+
// snapshot of the press: a verdict that lands while it is up adds its rows.
102107
const [sheet, setSheet] = React.useState<CommentItemActions | null>(null);
103-
const engage = React.useCallback(
104-
(actions: CommentItemActions) => {
105-
onEngage?.();
106-
// A comment with nothing to do on it (a read-only discussion) raises nothing.
107-
if (actions.onReact || actions.onResolve || actions.onEdit || actions.onDelete) setSheet(actions);
108-
},
109-
[onEngage],
110-
);
108+
const hold = React.useCallback((actions: CommentItemActions) => {
109+
// A comment with nothing to do on it (a read-only discussion) raises nothing.
110+
if (actions.onReact || actions.onResolve || actions.onEdit || actions.onDelete) setSheet(actions);
111+
}, []);
112+
const refreshHeld = React.useCallback((actions: CommentItemActions) => {
113+
setSheet((current) => (current && String(current.item.id) === String(actions.item.id) ? actions : current));
114+
}, []);
115+
const heldId = sheet ? String(sheet.item.id) : null;
116+
// The press itself engages, not the hold it may become: the 500 ms hold
117+
// covers the host's question about the comment's actions.
111118
const listPress = useCommentPress({
112-
onLongPress: engage,
119+
onPressStart: onEngage,
120+
onLongPress: hold,
113121
onTap: (actions) => onOpenThread(String(actions.item.id)),
114122
});
115-
const threadPress = useCommentPress({ onLongPress: engage });
123+
const threadPress = useCommentPress({ onPressStart: onEngage, onLongPress: hold });
116124
// Moving between the list and a thread closes a sheet left open (adjusted while rendering).
117125
const [sheetThread, setSheetThread] = React.useState(threadId);
118126
if (sheetThread !== threadId) {
@@ -205,6 +213,8 @@ export const CommentsPage: React.FC<CommentsPageProps> = ({
205213
mode="summary"
206214
onOpen={() => onOpenThread(String(th.root.id))}
207215
itemInteraction={listPress.bind}
216+
heldId={heldId}
217+
onHeldActions={refreshHeld}
208218
/>
209219
))}
210220
</div>
@@ -263,6 +273,8 @@ export const CommentsPage: React.FC<CommentsPageProps> = ({
263273
mode="page"
264274
onOpen={() => setComposerFocus((n) => n + 1)}
265275
itemInteraction={threadPress.bind}
276+
heldId={heldId}
277+
onHeldActions={refreshHeld}
266278
/>
267279
</div>
268280
)}

‎ui/packages/plugin-detail/src/comments/useCommentPress.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export interface CommentPressOptions {
2222
onLongPress: (actions: CommentItemActions) => void;
2323
/** The comment was tapped outside its own controls (the phone's list: open the thread). */
2424
onTap?: (actions: CommentItemActions) => void;
25+
/**
26+
* A press began (a finger or the mouse went down, the context menu was asked
27+
* for, Enter or Space on a focused comment): the host may start asking what
28+
* the comment's actions take, so the answer is in by the time a hold opens
29+
* them.
30+
*/
31+
onPressStart?: () => void;
2532
}
2633

2734
export interface CommentPress {
@@ -36,18 +43,20 @@ export interface CommentPress {
3643
* nothing else. The context menu opens them too: Android's own long press,
3744
* the keyboard's menu key (Shift+F10) on a focused comment, a right click. A
3845
* tap outside the comment's own controls is `onTap`, and so is Enter on a
39-
* focused comment. One press at a time, so one record serves every comment.
46+
* focused comment. Each of these first calls `onPressStart`, so the host can
47+
* begin asking what the comment's actions take before the hold completes. One
48+
* press at a time, so one record serves every comment.
4049
*/
41-
export function useCommentPress({ onLongPress, onTap }: CommentPressOptions): CommentPress {
50+
export function useCommentPress({ onLongPress, onTap, onPressStart }: CommentPressOptions): CommentPress {
4251
const press = React.useRef<{ timer: ReturnType<typeof setTimeout> | null; x: number; y: number; fired: boolean }>({
4352
timer: null,
4453
x: 0,
4554
y: 0,
4655
fired: false,
4756
});
48-
const latest = React.useRef({ onLongPress, onTap });
57+
const latest = React.useRef({ onLongPress, onTap, onPressStart });
4958
React.useLayoutEffect(() => {
50-
latest.current = { onLongPress, onTap };
59+
latest.current = { onLongPress, onTap, onPressStart };
5160
});
5261
const cancel = React.useCallback(() => {
5362
const p = press.current;
@@ -63,6 +72,7 @@ export function useCommentPress({ onLongPress, onTap }: CommentPressOptions): Co
6372
tabIndex: 0,
6473
onPointerDown: (e) => {
6574
if (e.pointerType === 'mouse' && e.button !== 0) return;
75+
latest.current.onPressStart?.();
6676
cancel();
6777
const p = press.current;
6878
p.fired = false;
@@ -84,6 +94,7 @@ export function useCommentPress({ onLongPress, onTap }: CommentPressOptions): Co
8494
onPointerLeave: cancel,
8595
onContextMenu: (e) => {
8696
e.preventDefault();
97+
latest.current.onPressStart?.();
8798
const p = press.current;
8899
const alreadyOpen = p.fired;
89100
cancel();
@@ -110,6 +121,7 @@ export function useCommentPress({ onLongPress, onTap }: CommentPressOptions): Co
110121
if (e.target !== e.currentTarget || !latest.current.onTap) return;
111122
if (e.key === 'Enter' || e.key === ' ') {
112123
e.preventDefault();
124+
latest.current.onPressStart?.();
113125
latest.current.onTap(actions);
114126
}
115127
},

0 commit comments

Comments
 (0)