mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-04 13:49:57 -05:00
app: video comments and description distinguish pending from empty (#727)
* app: video comments and description distinguish pending from empty useVideoExtras initialised both values to null and the page treated null the same as "none", so every video open (and every prev/next) showed "No comments have been downloaded. Refresh the video?" with a live Refresh button until the comments arrived. The Description tab showed "No description available." the same way. A failed fetch was also rendered as "none", and the previous video's data lingered until the next video's responses arrived. Invariants now enforced and tested: * null = pending, undefined = fetch failed, [] / '' = loaded and empty. The API serialises a missing info_json field as null, so the hook normalises that to empty; consumers never see null for "none". * Switching video resets both to pending, and a slow response for the previous video is dropped. * The page renders a placeholder while pending, an error message on failure, and offers Refresh only once the server confirmed there are no comments. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * app: useVideoExtras drops any request that is not the newest Matching on the video id let a stale request land when the user left and returned to the same video, or saved an edit while the page's first description request was still in flight. Each fetch now takes a generation number and only the newest generation may write state, per value, so a description refetch never invalidates a comments request. The edit dialog adopts a description that arrives while it is open only if the user has not typed, so a draft written over the placeholder is not wiped by the empty or failed value that follows. Tests: 7 -> none -> 8 -> 7 with the first request rejecting last; a save refetch beating the initial request; the draft surviving; the pending placeholder asserted; the console spy restored in finally. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
25e00f75fd
commit
83362a65c1
6 changed files with 286 additions and 18 deletions
|
|
@ -767,34 +767,58 @@ export const useVideo = (fileGroupId) => {
|
|||
export const useVideoExtras = (fileGroupId) => {
|
||||
// Fetches extra data to display on a Video's page. These are served separately from the
|
||||
// Video itself so list responses stay small and never read files from disk.
|
||||
//
|
||||
// Each value is null while pending, undefined when the fetch failed, and empty ([] or '') when
|
||||
// the server confirmed there is nothing. The API serializes a missing info_json field as null,
|
||||
// so a null response is normalized to empty here; consumers must never see null for "none".
|
||||
const [comments, setComments] = useState(null);
|
||||
const [description, setDescription] = useState(null);
|
||||
// Every fetch takes the next generation number; only the newest generation may write state.
|
||||
// Matching on the video id is not enough: leaving and returning to the same video, or a save
|
||||
// that refetches the description, must both beat an older request still in flight.
|
||||
const commentsGen = useRef(0);
|
||||
const descriptionGen = useRef(0);
|
||||
|
||||
const fetchComments = async () => {
|
||||
const gen = ++commentsGen.current;
|
||||
try {
|
||||
const result = await getVideoComments(fileGroupId);
|
||||
setComments(result.comments);
|
||||
if (gen === commentsGen.current) {
|
||||
setComments(result.comments || []);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
if (gen === commentsGen.current) {
|
||||
setComments(undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fetchDescription = async () => {
|
||||
const gen = ++descriptionGen.current;
|
||||
try {
|
||||
const result = await getVideoDescription(fileGroupId);
|
||||
setDescription(result.description);
|
||||
if (gen === descriptionGen.current) {
|
||||
setDescription(result.description || '');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
if (gen === descriptionGen.current) {
|
||||
setDescription(undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
// Whether switching to a new video or to none, the previous video's data must not linger,
|
||||
// and any request still in flight for it must not land.
|
||||
commentsGen.current++;
|
||||
descriptionGen.current++;
|
||||
setComments(null);
|
||||
setDescription(null);
|
||||
if (fileGroupId) {
|
||||
fetchComments();
|
||||
fetchDescription();
|
||||
} else {
|
||||
setComments(null);
|
||||
setDescription(null);
|
||||
}
|
||||
}, [fileGroupId]);
|
||||
|
||||
|
|
|
|||
|
|
@ -306,4 +306,138 @@ describe('useVideoExtras', () => {
|
|||
expect(result.current.comments).toBeNull();
|
||||
expect(result.current.description).toBeNull();
|
||||
});
|
||||
|
||||
// State contract, matching the rest of the hooks: null = pending, undefined = error,
|
||||
// [] / '' = loaded and empty. The page branches on these to choose between a placeholder,
|
||||
// an error, and "No comments have been downloaded".
|
||||
test('both are pending (null) until the responses arrive', () => {
|
||||
getVideoComments.mockReturnValue(new Promise(() => {
|
||||
}));
|
||||
getVideoDescription.mockReturnValue(new Promise(() => {
|
||||
}));
|
||||
const {result} = renderHook(() => useVideoExtras(7));
|
||||
expect(result.current.comments).toBeNull();
|
||||
expect(result.current.description).toBeNull();
|
||||
});
|
||||
|
||||
test('a video with no comments or description loads as empty, not pending', async () => {
|
||||
// The API serializes a missing info_json field as null.
|
||||
getVideoComments.mockResolvedValue({comments: null});
|
||||
getVideoDescription.mockResolvedValue({description: null});
|
||||
const {result} = renderHook(() => useVideoExtras(7));
|
||||
await act(async () => {
|
||||
});
|
||||
expect(result.current.comments).toEqual([]);
|
||||
expect(result.current.description).toBe('');
|
||||
});
|
||||
|
||||
test('a failed fetch yields undefined so the page can show an error instead of "no comments"', async () => {
|
||||
getVideoComments.mockRejectedValue(new Error('boom'));
|
||||
getVideoDescription.mockRejectedValue(new Error('boom'));
|
||||
const spy = jest.spyOn(console, 'error').mockImplementation(() => {
|
||||
});
|
||||
try {
|
||||
const {result} = renderHook(() => useVideoExtras(7));
|
||||
await act(async () => {
|
||||
});
|
||||
expect(result.current.comments).toBeUndefined();
|
||||
expect(result.current.description).toBeUndefined();
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
test('switching video resets both to pending before the new responses arrive', async () => {
|
||||
getVideoComments.mockResolvedValue({comments: [{id: 'c1', parent: 'root'}]});
|
||||
getVideoDescription.mockResolvedValue({description: 'first'});
|
||||
const {result, rerender} = renderHook(({id}) => useVideoExtras(id), {initialProps: {id: 7}});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(result.current.description).toBe('first');
|
||||
|
||||
// The next video's responses are slow: the previous video's data must not linger.
|
||||
getVideoComments.mockReturnValue(new Promise(() => {
|
||||
}));
|
||||
getVideoDescription.mockReturnValue(new Promise(() => {
|
||||
}));
|
||||
rerender({id: 8});
|
||||
expect(result.current.comments).toBeNull();
|
||||
expect(result.current.description).toBeNull();
|
||||
});
|
||||
|
||||
test('a slow response for the previous video does not overwrite the current one', async () => {
|
||||
// Video 7's responses are slow; the user moves on to video 8 before they arrive.
|
||||
let resolveComments, resolveDescription;
|
||||
getVideoComments.mockReturnValueOnce(new Promise(res => resolveComments = res));
|
||||
getVideoDescription.mockReturnValueOnce(new Promise(res => resolveDescription = res));
|
||||
const {result, rerender} = renderHook(({id}) => useVideoExtras(id), {initialProps: {id: 7}});
|
||||
|
||||
getVideoComments.mockResolvedValue({comments: [{id: 'c8', parent: 'root'}]});
|
||||
getVideoDescription.mockResolvedValue({description: 'eighth'});
|
||||
rerender({id: 8});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(result.current.description).toBe('eighth');
|
||||
|
||||
await act(async () => {
|
||||
resolveComments({comments: [{id: 'c7', parent: 'root'}]});
|
||||
resolveDescription({description: 'seventh'});
|
||||
});
|
||||
expect(result.current.comments).toEqual([{id: 'c8', parent: 'root'}]);
|
||||
expect(result.current.description).toBe('eighth');
|
||||
});
|
||||
|
||||
test('returning to a video does not let its first, still-pending request land', async () => {
|
||||
// The route clears the video between pages, so the id goes 7 -> undefined -> 8 -> 7 while
|
||||
// the very first request for 7 is still in flight (the API timeout is a minute).
|
||||
let rejectComments, rejectDescription;
|
||||
getVideoComments.mockReturnValueOnce(new Promise((_, rej) => rejectComments = rej));
|
||||
getVideoDescription.mockReturnValueOnce(new Promise((_, rej) => rejectDescription = rej));
|
||||
const {result, rerender} = renderHook(({id}) => useVideoExtras(id), {initialProps: {id: 7}});
|
||||
|
||||
rerender({id: undefined});
|
||||
getVideoComments.mockResolvedValue({comments: [{id: 'c8', parent: 'root'}]});
|
||||
getVideoDescription.mockResolvedValue({description: 'eighth'});
|
||||
rerender({id: 8});
|
||||
getVideoComments.mockResolvedValue({comments: [{id: 'c7b', parent: 'root'}]});
|
||||
getVideoDescription.mockResolvedValue({description: 'seventh again'});
|
||||
rerender({id: 7});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(result.current.comments).toEqual([{id: 'c7b', parent: 'root'}]);
|
||||
expect(result.current.description).toBe('seventh again');
|
||||
|
||||
// The first request finally times out. Same id, but it is not the newest request.
|
||||
const spy = jest.spyOn(console, 'error').mockImplementation(() => {
|
||||
});
|
||||
try {
|
||||
await act(async () => {
|
||||
rejectComments(new Error('timeout'));
|
||||
rejectDescription(new Error('timeout'));
|
||||
});
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
expect(result.current.comments).toEqual([{id: 'c7b', parent: 'root'}]);
|
||||
expect(result.current.description).toBe('seventh again');
|
||||
});
|
||||
|
||||
test('a refetch after saving beats the initial description request still in flight', async () => {
|
||||
let resolveInitial;
|
||||
getVideoComments.mockResolvedValue({comments: []});
|
||||
getVideoDescription.mockReturnValueOnce(new Promise(res => resolveInitial = res));
|
||||
const {result} = renderHook(() => useVideoExtras(7));
|
||||
|
||||
// The user edits and saves before the page's first description request returned.
|
||||
getVideoDescription.mockResolvedValue({description: 'edited'});
|
||||
await act(async () => {
|
||||
await result.current.fetchDescription();
|
||||
});
|
||||
expect(result.current.description).toBe('edited');
|
||||
|
||||
await act(async () => {
|
||||
resolveInitial({description: 'pre-edit'});
|
||||
});
|
||||
expect(result.current.description).toBe('edited');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue