Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/39195-thread-past-first-page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@object-ui/app-shell': patch
---

fix(console): a comment thread past the first page of its record's discussion opens, and is not reported gone

Clause-②: no
Metadata-protocol: none

On a deployment with a data page size (`OS_DATA_DEFAULT_PAGE_SIZE` / `OS_DATA_MAX_PAGE_SIZE`), a record's discussion is now read past its first page: while the comment read answers `hasMore`, the next page is read, each the size of the first. The record page (when it reads the discussion itself) and both list comment surfaces now show the record's newest comments and every thread, not only the oldest page. A later page that fails fails the read as before; a later page refused with a `400` (the deployment's row budget) ends the paging and the pages read are shown.

On the phone list page's comments page, a thread is reported gone, and the page goes back to the cards, only when its own top-level comment, read by its id, is missing or is not a top-level comment of that record. A thread the record's discussion read could not reach stays open. A deleted thread, or a link that names a reply, still goes back to the cards.
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,45 @@ afterEach(() => {
try { window.localStorage.removeItem(VIEW_COMMENTS_OPEN_KEY); } catch { /* none */ }
});

function mount(opts: { enable?: Record<string, unknown>; door?: boolean; search?: string; discussion?: unknown[]; threads?: unknown } = {}) {
function mount(opts: {
enable?: Record<string, unknown>;
door?: boolean;
search?: string;
discussion?: unknown[];
threads?: unknown;
/** The deployment's data page size: a read naming no `$top` gets this many rows and `hasMore`. */
pageCap?: number;
/** The deployment's row budget: a read whose `$skip` plus `$top` walks past it is refused with a 400. */
rowBudget?: number;
} = {}) {
const object = taskObject(opts.enable);
const i18n = createI18n({ defaultLanguage: 'en', detectBrowserLanguage: false });
const queryCommentThreads = vi.fn(async () => opts.threads ?? THREADS);
const discussion = opts.discussion ?? DISCUSSION;
const find = vi.fn(async (resource: string) => {
const find = vi.fn(async (resource: string, params?: { $top?: number; $skip?: number }) => {
if (resource === 'tracker_task') return { data: ROWS, total: ROWS.length };
if (resource === 'sys_comment') return { data: discussion };
if (resource === 'sys_comment') {
if (opts.pageCap === undefined) return { data: discussion };
const skip = params?.$skip ?? 0;
const top = params?.$top ?? opts.pageCap;
if (top > opts.pageCap) throw Object.assign(new Error('HTTP 400 MAX_RECORDS_EXCEEDED'), { status: 400 });
if (opts.rowBudget !== undefined && skip + top > opts.rowBudget) {
throw Object.assign(new Error('HTTP 400 QUERY_ROW_BUDGET_EXCEEDED'), { status: 400 });
}
const data = discussion.slice(skip, skip + top);
return { data, total: discussion.length, hasMore: skip + data.length < discussion.length };
}
return { data: [] };
});
// The comment by its id, as the data door answers it: `null` once it is deleted.
const findOne = vi.fn(async (resource: string, id: string) =>
resource === 'sys_comment'
? (discussion as Array<{ id: string }>).find((row) => row.id === id) ?? null
: null);
const create = vi.fn(async (_resource: string, data: Record<string, unknown>) => ({ ...data }));
const dataSource = {
find,
findOne: vi.fn(async () => null),
findOne,
create,
update: vi.fn(async () => ({})),
delete: vi.fn(async () => ({})),
Expand All @@ -207,7 +232,7 @@ function mount(opts: { enable?: Record<string, unknown>; door?: boolean; search?
</ExpressionProvider>
</I18nProvider>,
);
return { queryCommentThreads, find, create };
return { queryCommentThreads, find, findOne, create };
}

const settle = () => act(async () => {
Expand Down Expand Up @@ -407,3 +432,59 @@ describe('objectstack#39086 — the phone list page comments page', () => {
expect(new URLSearchParams(href().split('?')[1]).get('comments')).toBe('');
});
});

/**
* objectstack#39195 — on a deployment with a data page size, a record's
* discussion is read past its first page, and a thread is judged gone by its
* own top-level comment, never by its absence from a page.
*/
describe('objectstack#39195 — a thread past the first page of its record discussion', () => {
/** Three threads on `tracker_task:t1`, oldest first; `c3` starts past a two-row page. */
const LONG_DISCUSSION = [
...DISCUSSION,
{ id: 'c2', thread_id: 'tracker_task:t1', author_id: 'u3', author_name: 'Linus', body: 'Second thread', parent_id: null, created_at: '2026-12-02T10:00:00Z', is_resolved: false },
{ id: 'c3', thread_id: 'tracker_task:t1', author_id: 'u2', author_name: 'Grace', body: 'Third thread', parent_id: null, created_at: '2026-12-02T11:00:00Z', is_resolved: false },
{ id: 'r3', thread_id: 'tracker_task:t1', author_id: 'u3', author_name: 'Linus', body: 'Late reply', parent_id: 'c3', created_at: '2026-12-02T11:05:00Z' },
];

it('opens a thread that starts past the first page, with its replies, and stays on it', async () => {
const { find } = mount({ search: '?comments=c3&commentsRecord=t1', discussion: LONG_DISCUSSION, pageCap: 2 });
await waitFor(() => expect(threadPage()?.getAttribute('data-comments-page')).toBe('thread'), { timeout: 5000 });
await waitFor(() => expect(threadPage()!.querySelector('[data-comment-id="r3"]')).not.toBeNull(), { timeout: 5000 });
expect(threadPage()!.textContent).toContain('Third thread');
await settle();
expect(threadPage()?.getAttribute('data-comments-page')).toBe('thread');
expect(new URLSearchParams(href().split('?')[1]).get('comments')).toBe('c3');
// Read page after page, each the first page's size, from where the rows read end.
const pages = find.mock.calls
.filter(([resource]) => resource === 'sys_comment')
.map(([, params]) => [params?.$skip, params?.$top]);
expect(pages).toEqual(expect.arrayContaining([[undefined, undefined], [2, 2], [4, 2]]));
});

it('still falls back to the cards for a deleted thread, judged by its own comment', async () => {
const { findOne } = mount({ search: '?comments=c9&commentsRecord=t1', discussion: LONG_DISCUSSION, pageCap: 2 });
await waitFor(() => expect(document.querySelector('[data-view-comment-card="c1"]')).not.toBeNull(), { timeout: 5000 });
await waitFor(() => expect(threadPage()).toBeNull(), { timeout: 5000 });
expect(page()!.getAttribute('data-view-comments-page')).toBe('list');
expect(new URLSearchParams(href().split('?')[1]).get('comments')).toBe('');
expect(findOne).toHaveBeenCalledWith('sys_comment', 'c9');
});

it("does not report gone a thread the deployment's row budget keeps out of the read", async () => {
const { findOne } = mount({ search: '?comments=c3&commentsRecord=t1', discussion: LONG_DISCUSSION, pageCap: 2, rowBudget: 2 });
await waitFor(() => expect(findOne).toHaveBeenCalledWith('sys_comment', 'c3'), { timeout: 5000 });
await settle();
// The page stays on the thread's record, not back on the cards.
expect(threadPage()).not.toBeNull();
expect(page()!.getAttribute('data-view-comments-page')).toBe('thread');
expect(new URLSearchParams(href().split('?')[1]).get('comments')).toBe('c3');
});

it('falls back to the cards for a link that names a reply, not a thread', async () => {
mount({ search: '?comments=r3&commentsRecord=t1', discussion: LONG_DISCUSSION, pageCap: 2 });
await waitFor(() => expect(document.querySelector('[data-view-comment-card="c1"]')).not.toBeNull(), { timeout: 5000 });
await waitFor(() => expect(threadPage()).toBeNull(), { timeout: 5000 });
expect(new URLSearchParams(href().split('?')[1]).get('comments')).toBe('');
});
});
55 changes: 47 additions & 8 deletions ui/packages/app-shell/src/views/objectViewCommentsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,27 +239,66 @@ function ViewCommentThreadPage({
threadTitle={recordName ? { label: recordName, onPress: () => onOpenRecord(thread.recordId) } : undefined}
onEngage={engage}
/>
<ThreadGone threadId={thread.id} onGone={onBack} />
<ThreadGone
threadId={thread.id}
discussionId={`${objectName}:${thread.recordId}`}
dataSource={dataSource}
onGone={onBack}
/>
</>
)}
</ViewRecordDiscussion>
);
}

/**
* The named thread is not in its record's discussion once that is read
* (deleted, here or elsewhere, or a stale link): back to the cards, rather
* than the record's own list of threads.
* The named thread is gone (deleted, here or elsewhere, or a stale link):
* back to the cards, rather than the record's own list of threads.
*
* Gone is judged by the thread's own top-level comment, read by its id, and
* asked only once the record's discussion is read without it: a discussion
* the deployment serves only in part may lack a thread that exists. The
* comment answers gone when no row comes back, or the row is not a top-level
* comment of this record's discussion. A refused or failed read says nothing
* about the thread, so the page stays.
*/
function ThreadGone({ threadId, onGone }: { threadId: string; onGone: () => void }) {
function ThreadGone({
threadId,
discussionId,
dataSource,
onGone,
}: {
threadId: string;
/** The record's discussion key, `object:recordId`, as `sys_comment.thread_id` stores it. */
discussionId: string;
dataSource: DataSource;
onGone: () => void;
}) {
const { threads, loading, denied, failed } = useRecordComments();
const gone = !loading && !denied && !failed && !threads.some((candidate) => String(candidate.root.id) === threadId);
const absent = !loading && !denied && !failed && !threads.some((candidate) => String(candidate.root.id) === threadId);
const goneRef = useRef(onGone);
goneRef.current = onGone;
useEffect(() => {
if (gone) goneRef.current();
}, [gone]);
if (!absent) return undefined;
let live = true;
Promise.resolve(dataSource.findOne('sys_comment', threadId)).then(
(row) => {
if (live && !isThreadRoot(row, discussionId)) goneRef.current();
},
() => undefined,
);
return () => {
live = false;
};
}, [absent, threadId, discussionId, dataSource]);
return null;
}

/** `row` is a top-level comment of the discussion `discussionId`. */
function isThreadRoot(row: unknown, discussionId: string): boolean {
if (!row || typeof row !== 'object') return false;
const { thread_id: discussion, parent_id: parent } = row as { thread_id?: unknown; parent_id?: unknown };
return String(discussion) === discussionId && (parent == null || parent === '');
}

export default ObjectViewCommentsPage;
47 changes: 46 additions & 1 deletion ui/packages/app-shell/src/views/useRecordDiscussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,51 @@ async function readCommentReactionRows(
return answers.flatMap((res) => (Array.isArray(res?.data) ? res.data : []));
}

/**
* A record's whole discussion from `sys_comment` (`thread_id` is the
* record's `object:id` key), oldest first, as one `{ data }` answer.
*
* A deployment with a data page size (`OS_DATA_DEFAULT_PAGE_SIZE` /
* `OS_DATA_MAX_PAGE_SIZE`) answers a read that names no page with its first
* page and `hasMore`; the discussion is every comment, including the newest
* and every thread that starts past that page (a surface that opens a thread
* by its id judges it gone from these rows). So while the answer says there
* is more, the next page is read, each the size the first page was
* (never over the deployment's maximum) and starting where the rows read so
* far end. With no page size the first answer is the whole discussion, read
* once, as before.
*
* A later page that fails (a 5xx, a network failure) or is refused fails the
* whole read, as the first page's would: a discussion missing comments it
* cannot name is not the discussion. A later page the server declines for a
* reason no retry changes (a `400`: the deployment's row budget stops any read
* that walks this deep) ends the paging, and the pages read stand: the reader
* sees as much of the discussion as the deployment serves anyone.
*/
async function readRecordComments(
dataSource: Pick<DataSource<Record<string, unknown>>, 'find'>,
threadId: string,
): Promise<{ data: unknown[] }> {
const query = { $filter: { thread_id: threadId }, $orderby: { created_at: 'asc' as const } };
const first = await dataSource.find(COMMENT_OBJECT, query);
const rows: unknown[] = Array.isArray(first?.data) ? [...first.data] : [];
if (first?.hasMore !== true || rows.length === 0) return { data: rows };
const pageSize = rows.length;
for (;;) {
let page: Awaited<ReturnType<typeof dataSource.find>>;
try {
page = await dataSource.find(COMMENT_OBJECT, { ...query, $top: pageSize, $skip: rows.length });
} catch (err) {
if (feedReadVerdictOf(err) === 'answered') break;
throw err;
}
const data: unknown[] = Array.isArray(page?.data) ? page.data : [];
rows.push(...data);
if (page?.hasMore !== true || data.length === 0) break;
}
return { data: rows };
}

/**
* Reaction rows grouped per comment into the `{ emoji: userIds[] }` shape the
* `sys_comment.reactions` column stores, so both stores reach the screen
Expand Down Expand Up @@ -617,7 +662,7 @@ export function useRecordDiscussion({
};

const commentsRead: Promise<{ data?: any[]; reactionRows?: Array<Record<string, unknown>> | null }> =
served ?? dataSource.find('sys_comment', { $filter: { thread_id: threadId }, $orderby: { created_at: 'asc' } });
served ?? readRecordComments(dataSource, threadId);
return commentsRead
.then(async (res: any) => {
recordVerdict('answered');
Expand Down
Loading