v3: the profile page is split by one rule, and what it re-invented is shared across the app — UserPublicProfile was 1,949 lines because it held its data, six tabs, seven subcomponents and hand-rolled copies of things the app already had or repeated elsewhere. The page now only picks the tab and puts the pieces together (253 lines); useProfile holds the user and the three lists with their live sync, cache and scroll; each tab and header piece is one file under components/profile/; the followed and invitees tabs load on first open through the new useFetchState, keyed by username, which keeps them across tab switches. Every piece taken out replaces its copies: DumpCardList, which reads the votes from WSContext, is every list of dump cards (the index feeds, followed, search, related dumps, UserDumps, UserUpvoted, the profile), so Index no longer threads vote props into the feeds; PlaylistCardList every list of playlist cards; FeedFooter the eight sentinel/loading-more/end blocks; ListState the loading/error/empty chains; Segmented the settings toggles, RoleSwitcher and the playlist edit form, with SegmentedField built on it; LoadMoreButton the three load-more buttons; NewDumpButton UserDumps' and the profile's; useScrollRestore four copies of the scroll restore, and the profile uses useScrollSave instead of saving its whole cache on every scroll. useFading takes an onFaded callback, which replaces PlaylistDetail's copy of its timers; useUpvotedList is the vote tracking UserUpvoted and the profile each had, and a withdrawn upvote now leaves the list and the cache once faded (it was only hidden, and came back after a tab switch). usePlaylistMemberships is the membership load and toggle AddToPlaylistModal and DumpCreateModal each had. pagedList.ts (PagedList, firstPage, restoredList, fetchFirstPage, withFirst), fetchPublicUser and dataOr are shared by the profile, UserPlaylists and useUserDumpFeed; a refused side list still shows empty, as documented, and only the user fails the page. FollowedFeed's two sections are one local hook, and its readPage is expectOk. A card is its owner's when the item is the signed-in user's, everywhere: the profile's upvoted column marked every dump as the owner's on one's own profile. No message changed; the catalogs only move their references. 643 frontend tests pass (11 new for useFetchState, useFading's onFaded and pagedList), and tsc, eslint, doc:lint, deno check and deno fmt are clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -52,4 +52,17 @@ describe("useFading", () => {
|
||||
act(() => result.current.cancelAll());
|
||||
expect(result.current.fading).toEqual({});
|
||||
});
|
||||
|
||||
test("onFaded hears of a fade that ended, not of a cancelled one", () => {
|
||||
const onFaded = vi.fn();
|
||||
const { result } = renderHook(() => useFading(onFaded));
|
||||
act(() => {
|
||||
result.current.startFading("a");
|
||||
result.current.startFading("b");
|
||||
});
|
||||
act(() => vi.advanceTimersByTime(2100));
|
||||
act(() => result.current.cancelFading("b"));
|
||||
act(() => vi.advanceTimersByTime(250));
|
||||
expect(onFaded.mock.calls).toEqual([["a"]]);
|
||||
});
|
||||
});
|
||||
|
||||
82
src/test/hooks/shared/useFetchState.test.tsx
Normal file
82
src/test/hooks/shared/useFetchState.test.tsx
Normal file
@@ -0,0 +1,82 @@
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
import { act, renderHook, waitFor } from "@testing-library/react";
|
||||
import { useFetchState } from "../../../hooks/shared/useFetchState.ts";
|
||||
import { deferred } from "../../fetch.ts";
|
||||
|
||||
describe("useFetchState", () => {
|
||||
test("loads once enabled, and only once per key", async () => {
|
||||
const load = vi.fn(() => Promise.resolve(1));
|
||||
const { result, rerender } = renderHook(
|
||||
({ enabled }) => useFetchState("a", load, enabled),
|
||||
{ initialProps: { enabled: false } },
|
||||
);
|
||||
expect(load).not.toHaveBeenCalled();
|
||||
expect(result.current.state).toEqual({ status: "loading" });
|
||||
rerender({ enabled: true });
|
||||
await waitFor(() =>
|
||||
expect(result.current.state).toEqual({ status: "loaded", data: 1 })
|
||||
);
|
||||
rerender({ enabled: false });
|
||||
rerender({ enabled: true });
|
||||
expect(load).toHaveBeenCalledTimes(1);
|
||||
expect(result.current.state).toEqual({ status: "loaded", data: 1 });
|
||||
});
|
||||
|
||||
test("turning it off doesn't stop a load under way", async () => {
|
||||
const d = deferred<number>();
|
||||
const { result, rerender } = renderHook(
|
||||
({ enabled }) => useFetchState("a", () => d.promise, enabled),
|
||||
{ initialProps: { enabled: true } },
|
||||
);
|
||||
rerender({ enabled: false });
|
||||
act(() => d.resolve(2));
|
||||
await waitFor(() =>
|
||||
expect(result.current.state).toEqual({ status: "loaded", data: 2 })
|
||||
);
|
||||
});
|
||||
|
||||
test("a new key starts over and aborts the last key's load", async () => {
|
||||
const signals: AbortSignal[] = [];
|
||||
const load = vi.fn((signal: AbortSignal) => {
|
||||
signals.push(signal);
|
||||
return signals.length === 1
|
||||
? new Promise<string>(() => {})
|
||||
: Promise.resolve("b");
|
||||
});
|
||||
const { result, rerender } = renderHook(
|
||||
({ key }) => useFetchState(key, load),
|
||||
{ initialProps: { key: "a" } },
|
||||
);
|
||||
rerender({ key: "b" });
|
||||
expect(signals[0].aborted).toBe(true);
|
||||
await waitFor(() =>
|
||||
expect(result.current.state).toEqual({ status: "loaded", data: "b" })
|
||||
);
|
||||
});
|
||||
|
||||
test("an undefined key loads nothing", () => {
|
||||
const load = vi.fn(() => Promise.resolve(1));
|
||||
renderHook(() => useFetchState(undefined, load));
|
||||
expect(load).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("a failed load shows its message", async () => {
|
||||
const { result } = renderHook(() =>
|
||||
useFetchState("a", () => Promise.reject(new Error("Nope")))
|
||||
);
|
||||
await waitFor(() =>
|
||||
expect(result.current.state).toEqual({ status: "error", error: "Nope" })
|
||||
);
|
||||
});
|
||||
|
||||
test("setData edits the loaded data, and nothing before", async () => {
|
||||
const d = deferred<number[]>();
|
||||
const { result } = renderHook(() => useFetchState("a", () => d.promise));
|
||||
act(() => result.current.setData((xs) => [...xs, 9]));
|
||||
expect(result.current.state).toEqual({ status: "loading" });
|
||||
act(() => d.resolve([1]));
|
||||
await waitFor(() => expect(result.current.state.status).toBe("loaded"));
|
||||
act(() => result.current.setData((xs) => [...xs, 2]));
|
||||
expect(result.current.state).toEqual({ status: "loaded", data: [1, 2] });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user