+
{resolvedTitle && (
diff --git a/src/components/SwipeCommentViewer.test.tsx b/src/components/SwipeCommentViewer.test.tsx
index 6e3fd83..a41616c 100644
--- a/src/components/SwipeCommentViewer.test.tsx
+++ b/src/components/SwipeCommentViewer.test.tsx
@@ -108,10 +108,10 @@ describe('SwipeCommentViewer', () => {
}, { timeout: 30000 });
}, 35000);
- it('renders single panel when comment has no parent', async () => {
+ it('renders a single panel for an item with no parent', async () => {
vi.spyOn(hnSdk, 'readItem').mockImplementation(async (id) => {
if (Number(id) === 7777) {
- return { id: 7777, by: 'solo', text: 'Top-level comment with no parent.', time: Math.floor(Date.now() / 1000) - 300, type: 'comment' };
+ return { id: 7777, by: 'solo', text: 'An item with no parent.', time: Math.floor(Date.now() / 1000) - 300, type: 'story' };
}
return null;
});
@@ -121,9 +121,9 @@ describe('SwipeCommentViewer', () => {
http.get(`${ALGOLIA_API}/items/:id`, () => {
return HttpResponse.json({
id: 7777,
- type: 'comment',
+ type: 'story',
author: 'solo',
- text: 'Top-level comment with no parent.',
+ text: 'An item with no parent.',
created_at_i: Math.floor(Date.now() / 1000) - 300,
parent_id: null,
story_id: null,
diff --git a/src/hooks/usePrefetchItem.test.ts b/src/hooks/usePrefetchItem.test.ts
index 41da5b7..afbaa31 100644
--- a/src/hooks/usePrefetchItem.test.ts
+++ b/src/hooks/usePrefetchItem.test.ts
@@ -147,6 +147,23 @@ describe('usePrefetchItems', () => {
renderHook(() => usePrefetchItems(0, undefined as unknown as Item[], 3));
}).not.toThrow();
});
+
+ it('does not restart an in-flight prefetch when the viewer remounts', async () => {
+ const spy = vi.spyOn(hn, 'prefetchItemComments').mockReturnValue(new Promise(() => { /* in flight */ }));
+ const items = [{ id: 1 }, { id: 2 }, { id: 3 }] as Item[];
+
+ const first = renderHook(() => usePrefetchItems(0, items, 2));
+ await act(async () => { vi.advanceTimersByTime(250); });
+ // A tab switch or Back mounts a new viewer with the same stories ahead.
+ first.unmount();
+ renderHook(() => usePrefetchItems(0, items, 2));
+ await act(async () => { vi.advanceTimersByTime(250); });
+
+ expect(spy.mock.calls.map(([id]) => id)).toEqual([2, 3]);
+ cancelAllPrefetches();
+ expect(spy.mock.calls.every(([, signal]) => signal?.aborted)).toBe(true);
+ spy.mockRestore();
+ });
});
describe('cancelAllPrefetches', () => {
diff --git a/src/hooks/usePrefetchItem.ts b/src/hooks/usePrefetchItem.ts
index 0f5cd74..09f9afb 100644
--- a/src/hooks/usePrefetchItem.ts
+++ b/src/hooks/usePrefetchItem.ts
@@ -72,6 +72,8 @@ async function processQueue() {
if (bestId === null) break;
prefetchQueue.delete(bestId);
+ // A remounted swipe viewer re-queues stories that may still be downloading.
+ if (activePrefetches.has(bestId)) continue;
const cached = getCachedItem(bestId);
if (cached?.isFresh) {
diff --git a/src/hooks/useSiblingComments.test.ts b/src/hooks/useSiblingComments.test.ts
index cff13d9..80e99e4 100644
--- a/src/hooks/useSiblingComments.test.ts
+++ b/src/hooks/useSiblingComments.test.ts
@@ -113,10 +113,10 @@ describe('useSiblingComments', () => {
});
});
- it('returns single comment when parent_id is null', async () => {
+ it('returns just this item when it has no parent', async () => {
vi.spyOn(hnSdk, 'readItem').mockImplementation(async (id) => {
if (Number(id) === 7777) {
- return { id: 7777, by: 'someone', text: 'A top-level comment.', time: Math.floor(Date.now() / 1000) - 300, type: 'comment' };
+ return { id: 7777, by: 'someone', text: 'An item with no parent.', time: Math.floor(Date.now() / 1000) - 300, type: 'story' };
}
return null;
});
@@ -188,7 +188,7 @@ describe('useSiblingComments', () => {
vi.spyOn(hnSdk, 'readItem').mockImplementation(async (id) => {
if (Number(id) === 7777) {
- return { id: 7777, by: 'someone', text: 'A top-level comment.', time: Math.floor(Date.now() / 1000) - 300, type: 'comment' };
+ return { id: 7777, by: 'someone', text: 'An item with no parent.', time: Math.floor(Date.now() / 1000) - 300, type: 'story' };
}
return null;
});
diff --git a/src/hooks/useSiblingComments.ts b/src/hooks/useSiblingComments.ts
index 158558e..d15fe14 100644
--- a/src/hooks/useSiblingComments.ts
+++ b/src/hooks/useSiblingComments.ts
@@ -39,7 +39,8 @@ export function useSiblingComments(commentId: number | string): UseSiblingCommen
const item = await fetchFirebaseItem(numericId, signal);
if (signal.aborted) return;
- // No parent → top-level comment or deleted; show just this one.
+ // Every comment has a parent, even a deleted one, so this is an item that
+ // isn't a comment (a story, job, poll or poll option); show just this one.
if (item.parent == null) {
setSiblingIds([numericId]);
setCurrentIndex(0);