Skip to content
Closed
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
75 changes: 54 additions & 21 deletions resources/js/components/commit-thread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<CursorPaginated<ThreadCommit>, '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)
Expand All @@ -286,32 +313,32 @@ 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) => {
if (isLoading || replies === null || nextCursor === null) {
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;
Expand All @@ -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;
Expand Down
141 changes: 141 additions & 0 deletions tests/Unit/ThreadReplies.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/// <reference types="node" />
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<ThreadCommit>;

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<Page>((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);
});
1 change: 1 addition & 0 deletions tests/draft-images.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
},
});
1 change: 1 addition & 0 deletions tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
Loading