diff --git a/resources/js/components/commit-thread.tsx b/resources/js/components/commit-thread.tsx index ff6a66c721..9c9523657e 100644 --- a/resources/js/components/commit-thread.tsx +++ b/resources/js/components/commit-thread.tsx @@ -278,6 +278,33 @@ export function useThread( } }; + /** + * Requests can overlap, for example a reply posted while the next page loads, so only the latest one is applied. + */ + const latestRequest = useRef(0); + + const load = ( + request: Promise< + Pick, 'data' | 'next_cursor'> + >, + ) => { + const id = ++latestRequest.current; + setIsLoading(true); + + request + .then((page) => { + if (id === latestRequest.current) { + applyPage(page.data, page.next_cursor); + } + }) + .catch(() => undefined) + .finally(() => { + if (id === latestRequest.current) { + setIsLoading(false); + } + }); + }; + const reload = (extra = 0) => { const minimum = replies === null || (isPreview && replies.length <= repliesPerPage) @@ -286,12 +313,15 @@ export function useThread( ? Infinity : replies.length + extra; - setIsLoading(true); - - fetchRepliesUntil(commit, minimum, repliesPerPage, isPreview, except) - .then((page) => applyPage(page.data, page.next_cursor)) - .catch(() => undefined) - .finally(() => setIsLoading(false)); + load( + fetchRepliesUntil( + commit, + minimum, + repliesPerPage, + isPreview, + except, + ), + ); }; const loadUntil = (minimum: number) => { @@ -299,19 +329,16 @@ export function useThread( return; } - setIsLoading(true); - - fetchRepliesAfter( - commit, - replies, - nextCursor, - minimum, - isPreview, - except, - ) - .then((page) => applyPage(page.data, page.next_cursor)) - .catch(() => undefined) - .finally(() => setIsLoading(false)); + load( + fetchRepliesAfter( + commit, + replies, + nextCursor, + minimum, + isPreview, + except, + ), + ); }; const commitId = commit.id; @@ -322,15 +349,21 @@ export function useThread( } let isCurrent = true; + const id = ++latestRequest.current; fetchReplies({ id: commitId }, null, isPreview, repliesPerPage, except) .then((page) => { - if (isCurrent) { + if (isCurrent && id === latestRequest.current) { setReplies(page.data); setNextCursor(page.next_cursor); } }) - .catch(() => undefined); + .catch(() => undefined) + .finally(() => { + if (isCurrent && id === latestRequest.current) { + setIsLoading(false); + } + }); return () => { isCurrent = false; diff --git a/tests/Unit/ThreadReplies.test.tsx b/tests/Unit/ThreadReplies.test.tsx new file mode 100644 index 0000000000..31ef2cb1a9 --- /dev/null +++ b/tests/Unit/ThreadReplies.test.tsx @@ -0,0 +1,141 @@ +/// +import assert from 'node:assert/strict'; +import * as React from 'react'; +import { afterAll, afterEach, test, vi } from 'vite-plus/test'; +import type { CursorPaginated, ThreadCommit } from '../../resources/js/types'; + +type Page = CursorPaginated; + +const states: { value: unknown }[] = []; +const counts: number[] = []; +const effects: (() => (() => void) | void)[] = []; + +vi.doMock('react', () => ({ + ...React, + useEffect: (effect: () => (() => void) | void) => effects.push(effect), + useEffectEvent: (callback: unknown) => callback, + useRef: (initial: unknown) => ({ current: initial }), + useState: (initial: unknown) => { + const state = { + value: typeof initial === 'function' ? initial() : initial, + }; + states.push(state); + + return [ + state.value, + (next: unknown) => { + state.value = + typeof next === 'function' ? next(state.value) : next; + }, + ]; + }, +})); +vi.doMock('@inertiajs/react', async () => ({ + ...(await vi.importActual('@inertiajs/react')), + usePage: () => ({ props: { repliesPerPage: 1 } }), +})); +vi.doMock('../../resources/js/hooks/use-shared-state', () => ({ + useSharedState: (_key: string, initial: number) => [ + initial, + (next: number | ((current: number) => number)) => + counts.push(typeof next === 'function' ? next(initial) : next), + ], +})); + +const { useThread } = + await import('../../resources/js/components/commit-thread'); + +const responses: ((page: Page) => void)[] = []; +vi.stubGlobal('fetch', () => + new Promise((resolve) => responses.push(resolve)).then((page) => ({ + ok: true, + json: async () => page, + })), +); +afterAll(() => vi.unstubAllGlobals()); +afterEach(() => { + states.length = 0; + counts.length = 0; + effects.length = 0; + responses.length = 0; +}); + +const reply = (id: string) => ({ id, parent_id: 'parent' }) as ThreadCommit; +const page = (ids: string[], next_cursor: string | null): Page => ({ + data: ids.map(reply), + next_cursor, + prev_cursor: null, +}); +const settle = () => new Promise((resolve) => setTimeout(resolve)); + +function openThread() { + const initial = page(['first'], 'second-page'); + const thread = useThread( + { id: 'parent', replies_count: 2 } as ThreadCommit, + initial, + ); + const replies = states.find((state) => state.value === initial.data); + assert.ok(replies); + + return { thread, replies }; +} + +test('keeps the reply just posted when an older page arrives after the reload', async () => { + const { thread, replies } = openThread(); + + thread.loadMore(); + thread.onReplied({ + id: 'parent', + replies_count: 3, + continuation_id: null, + }); + responses[1](page(['posted', 'first'], 'second-page')); + await settle(); + responses[0](page(['second'], null)); + await settle(); + + assert.deepEqual( + (replies.value as ThreadCommit[]).map(({ id }) => id), + ['posted', 'first'], + ); + assert.deepEqual(counts, [3]); +}); + +test('applies only the newest of two overlapping reloads', async () => { + const { thread, replies } = openThread(); + + thread.onDeleted(); + thread.onDeleted(); + responses[1](page(['first'], 'second-page')); + await settle(); + responses[0](page(['deleted', 'first'], 'second-page')); + await settle(); + + assert.deepEqual( + (replies.value as ThreadCommit[]).map(({ id }) => id), + ['first'], + ); +}); + +test('clears loading when the first-load refetch replaces a page still loading', async () => { + const initial = page(['first'], 'second-page'); + const thread = useThread( + { id: 'parent', replies_count: 2 } as ThreadCommit, + initial, + true, + ); + const before = states.map((state) => state.value); + + thread.loadMore(); + const isLoading = states.find( + (state, index) => before[index] === false && state.value === true, + ); + assert.ok(isLoading); + + effects[0](); + responses[0](page(['second'], null)); + responses[1](page(['first'], 'second-page')); + await settle(); + + assert.equal(isLoading.value, false); +}); diff --git a/tests/draft-images.config.ts b/tests/draft-images.config.ts index cee1aa9610..6b442b01e3 100644 --- a/tests/draft-images.config.ts +++ b/tests/draft-images.config.ts @@ -25,6 +25,7 @@ export default defineConfig({ 'tests/Unit/TabSlide.test.tsx', 'tests/Unit/ReportResolution.test.tsx', 'tests/Unit/LinkifiedText.test.tsx', + 'tests/Unit/ThreadReplies.test.tsx', ], }, }); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 5390d6411f..b0cff47d7d 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -11,6 +11,7 @@ "Unit/SearchDialogComposition.test.tsx", "Unit/DraftImageReads.test.tsx", "Unit/ImageAttachments.test.tsx", + "Unit/ThreadReplies.test.tsx", "../resources/js/types/*.d.ts", "draft-images.config.ts" ]