v3: every paged list loads its pages one way, PlaylistDetail is split like the profile, and the comments keep the README's rules — Index, FollowedFeed, useUserDumpFeed, UserPlaylists, UserListPopover, Notifications, Search and the profile's followed users each hand-wrote "fetch the next page, append it, clear loadingMore on failure". A list the API pages is now a PagedList whose next page useLoadMore appends, and a list that loads on its own goes through usePagedList, which is useFetchState (now taking an initial value, as a restored cache, that starts loaded) plus useLoadMore. Pages are read by pageUrl with readPage (a refusal throws) or readFirstPage (a refusal is empty, as fetchFirstPage was), signed-in reads go through authFetch, and useFeedCache's saveState takes the list. Notifications keeps the socket's arrivals as their own list merged at render, as Index does with recentDumps, and marks them read in one effect. PlaylistDetail (963 lines) only puts the pieces together (146): usePlaylist holds the playlist, its live sync and its edits, PlaylistHeader, PlaylistEditForm and PlaylistDumpList are one file each, and LiveDumpCard is the vote wiring DumpCardList and the playlist both use. What it re-invented is shared: the load is useFetchState, a dump going private and back is usePositionAwareSync, deletes and edits are useDumpListSync (which also keeps comment counts, which the page didn't). A removal is sent when its fade ends, or as the page is left, so an undo sends nothing: the removal/re-add/reorder dance, the pending-undo order and the 80 ms debounce of its echoes are gone. A refused reorder goes back to the server's last order and says so, instead of reloading the page. The comments written since the documentation pass broke its rules in places: module docs as a label and a colon ("The "dumps" tab of a profile: …"), noun-phrase fragments on types, fields and constants, stale lists of callers, over-long lines; they are sentences now, and the ones the refactor made wrong (PlaylistDetail as useFading's and FollowPlaylistButton's caller, "the profiles" as usePositionAwareSync's only users) say what is true. 650 frontend and 214 backend tests pass (new: usePagedList, useFetchState's initial, pageUrl/readPage, the deferred removal, a dump returning at its place in a playlist), and tsc, eslint, doc:lint, deno check, deno lint and deno fmt are clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -76,7 +76,7 @@ describe("PlaylistDetail", () => {
|
||||
});
|
||||
|
||||
test("not found", async () => {
|
||||
route("GET /api/playlists/:id", () => fail("nope", 404));
|
||||
route("GET /api/playlists/:id", () => fail("Playlist not found", 404));
|
||||
renderApp(<PlaylistDetail />, {
|
||||
route: "/playlists/x",
|
||||
path: "/playlists/:playlistId",
|
||||
@@ -212,15 +212,13 @@ describe("PlaylistDetail", () => {
|
||||
expect(screen.getByRole("heading", { name: "Mix" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test("removing a dump fades it out, with an undo", async () => {
|
||||
test("a removal is sent once its fade ends, and an undo sends nothing", async () => {
|
||||
vi.useFakeTimers({ shouldAdvanceTime: true });
|
||||
try {
|
||||
route(
|
||||
"DELETE /api/playlists/:pid/dumps/:did",
|
||||
() => new Response(null, { status: 204 }),
|
||||
);
|
||||
route("POST /api/playlists/:pid/dumps/:did", () => ({}));
|
||||
route("PUT /api/playlists/:pid/order", () => ({}));
|
||||
const { user } = open();
|
||||
await screen.findByText("Bravo");
|
||||
const bravo = screen.getByText("Bravo").closest(
|
||||
@@ -229,33 +227,30 @@ describe("PlaylistDetail", () => {
|
||||
await user.click(
|
||||
within(bravo).getByRole("button", { name: "Remove from playlist" }),
|
||||
);
|
||||
expect(calls("DELETE /api/playlists/:pid/dumps/:did")[0].path).toBe(
|
||||
"/api/playlists/mix/dumps/b",
|
||||
);
|
||||
await user.click(
|
||||
within(bravo).getByRole("button", { name: "Cancel removal" }),
|
||||
);
|
||||
await waitFor(() =>
|
||||
expect(calls("PUT /api/playlists/:pid/order")).toHaveLength(1)
|
||||
);
|
||||
expect(calls("PUT /api/playlists/:pid/order")[0].body).toEqual({
|
||||
dumpIds: ["a", "b", "c"],
|
||||
});
|
||||
act(() => vi.advanceTimersByTime(2_400));
|
||||
expect(screen.getByText("Bravo")).toBeInTheDocument();
|
||||
expect(calls("DELETE /api/playlists/:pid/dumps/:did")).toHaveLength(0);
|
||||
|
||||
await user.click(
|
||||
within(bravo).getByRole("button", { name: "Remove from playlist" }),
|
||||
);
|
||||
act(() => vi.advanceTimersByTime(2_400));
|
||||
expect(screen.queryByText("Bravo")).not.toBeInTheDocument();
|
||||
expect(calls("DELETE /api/playlists/:pid/dumps/:did")[0].path).toBe(
|
||||
"/api/playlists/p1/dumps/b",
|
||||
);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
test("a refused removal puts the dump back and says so", async () => {
|
||||
test("leaving the page during the fade still sends the removal", async () => {
|
||||
route(
|
||||
"DELETE /api/playlists/:pid/dumps/:did",
|
||||
() => fail("Forbidden", 403),
|
||||
() => new Response(null, { status: 204 }),
|
||||
);
|
||||
const { user } = open();
|
||||
await screen.findByText("Bravo");
|
||||
@@ -265,14 +260,36 @@ describe("PlaylistDetail", () => {
|
||||
await user.click(
|
||||
within(bravo).getByRole("button", { name: "Remove from playlist" }),
|
||||
);
|
||||
expect(await screen.findByRole("alert")).toHaveTextContent(
|
||||
"couldn't be removed",
|
||||
);
|
||||
expect(within(bravo).getByRole("button", { name: "Remove from playlist" }))
|
||||
.toBeInTheDocument();
|
||||
act(() => globalThis.dispatchEvent(new Event("pagehide")));
|
||||
expect(calls("DELETE /api/playlists/:pid/dumps/:did")).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("dragging reorders, and a refused reorder reloads the real order", async () => {
|
||||
test("a refused removal puts the dump back and says so", async () => {
|
||||
vi.useFakeTimers({ shouldAdvanceTime: true });
|
||||
try {
|
||||
route(
|
||||
"DELETE /api/playlists/:pid/dumps/:did",
|
||||
() => fail("Forbidden", 403),
|
||||
);
|
||||
const { user } = open();
|
||||
await screen.findByText("Bravo");
|
||||
const bravo = screen.getByText("Bravo").closest(
|
||||
".playlist-dump-item",
|
||||
) as HTMLElement;
|
||||
await user.click(
|
||||
within(bravo).getByRole("button", { name: "Remove from playlist" }),
|
||||
);
|
||||
act(() => vi.advanceTimersByTime(2_400));
|
||||
expect(await screen.findByRole("alert")).toHaveTextContent(
|
||||
"couldn't be removed",
|
||||
);
|
||||
expect(titles()).toEqual(["Alpha", "Bravo", "Charlie"]);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
test("dragging reorders, and a refused reorder goes back to the server's order", async () => {
|
||||
route("PUT /api/playlists/:pid/order", () => fail("Forbidden", 403));
|
||||
open();
|
||||
await screen.findByText("Charlie");
|
||||
@@ -292,12 +309,23 @@ describe("PlaylistDetail", () => {
|
||||
expect(calls("PUT /api/playlists/:pid/order")[0].body).toEqual({
|
||||
dumpIds: ["b", "c", "a"],
|
||||
});
|
||||
await waitFor(() =>
|
||||
expect(calls("GET /api/playlists/:id")).toHaveLength(2)
|
||||
);
|
||||
await waitFor(() =>
|
||||
expect(titles()).toEqual(["Alpha", "Bravo", "Charlie"])
|
||||
expect(await screen.findByRole("alert")).toHaveTextContent(
|
||||
"order couldn't be saved",
|
||||
);
|
||||
expect(titles()).toEqual(["Alpha", "Bravo", "Charlie"]);
|
||||
});
|
||||
|
||||
test("a dump that goes private leaves, and comes back at its place", async () => {
|
||||
const { socket } = open({ session: visitor });
|
||||
await screen.findByText("Bravo");
|
||||
socket.receive({ type: "dump_deleted", dumpId: "b" });
|
||||
expect(titles()).toEqual(["Alpha", "Charlie"]);
|
||||
socket.receive({
|
||||
type: "dump_created",
|
||||
dump: wire(makeDump({ id: "b", title: "Bravo" })),
|
||||
});
|
||||
expect(await screen.findByText("Bravo")).toBeInTheDocument();
|
||||
expect(titles()).toEqual(["Alpha", "Bravo", "Charlie"]);
|
||||
});
|
||||
|
||||
test("deleting the playlist asks, then goes home", async () => {
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
makeDump,
|
||||
makePlaylist,
|
||||
makeSession,
|
||||
makeToken,
|
||||
makeUser,
|
||||
page,
|
||||
wire,
|
||||
@@ -316,9 +317,10 @@ describe("UserPublicProfile", () => {
|
||||
});
|
||||
|
||||
test("change password keeps you signed in with the fresh tokens", async () => {
|
||||
const freshToken = makeToken({ sub: "ann" });
|
||||
route(
|
||||
"POST /api/users/me/change-password",
|
||||
() => ({ token: "fresh-token", mediaToken: "fresh-media" }),
|
||||
() => ({ token: freshToken, mediaToken: "fresh-media" }),
|
||||
);
|
||||
const { user } = open({
|
||||
session: annSession,
|
||||
@@ -353,7 +355,7 @@ describe("UserPublicProfile", () => {
|
||||
expect(await within(dialog).findByText("Password changed successfully."))
|
||||
.toBeInTheDocument();
|
||||
expect(JSON.parse(localStorage.getItem(AUTH_STORAGE_KEY)!).token).toBe(
|
||||
"fresh-token",
|
||||
freshToken,
|
||||
);
|
||||
expect(calls("POST /api/users/me/change-password")[0].body).toEqual({
|
||||
currentPassword: "oldpassword",
|
||||
|
||||
Reference in New Issue
Block a user