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.
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 49s
Build and Deploy Static Documentation Site / build-and-push (push) Successful in 22s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
khannurien
2026-09-29 21:19:33 +00:00
parent 69d9bf899b
commit 07ac4bc629
81 changed files with 2016 additions and 2210 deletions

View File

@@ -22,8 +22,9 @@ import {
import { fetchPublicUser } from "../../utils/shared/fetchPublicUser.ts";
import type { LoadState } from "../../utils/shared/loadState.ts";
import {
fetchFirstPage,
type PagedList,
pageUrl,
readFirstPage,
restoredList,
} from "../../utils/feed/pagedList.ts";
import { useAuth } from "../auth/useAuth.ts";
@@ -36,8 +37,8 @@ import { useUpvotedList } from "../dump/useUpvotedList.ts";
import { usePlaylistListSync } from "../playlist/usePlaylistListSync.ts";
/**
* The profile's lists, each holding its first page only: each has its own page
* for the rest.
* Loaded by {@link useProfile} for `UserPublicProfile`. Each list holds its
* first page only, and has its own page for the rest.
*/
export interface ProfileData {
user: PublicUser;
@@ -49,13 +50,19 @@ export interface ProfileData {
/** Returned by {@link useProfile} to `UserPublicProfile`. */
export interface UseProfileResult {
state: LoadState<ProfileData>;
/** Whether the signed-in user is the profile's. False while it loads. */
/** It is false while the profile loads. */
isOwnProfile: boolean;
/** For the header's edits, which the server echoes to other browsers. */
/** It shows the header's edits, which the server echoes to other browsers. */
setUser: (fn: (user: PublicUser) => PublicUser) => void;
/** For `NewPlaylistButton`, whose playlist the socket echoes later. */
/**
* It shows the playlist `NewPlaylistButton` made, which the socket echoes
* later.
*/
setPlaylists: (fn: (items: Playlist[]) => Playlist[]) => void;
/** From `useUpvotedList`, for the upvoted list's `DumpCardList`. */
/**
* It comes from `useUpvotedList`, and goes to the upvoted list's
* `DumpCardList`.
*/
upvotedCardClassName: (dump: Dump) => string | undefined;
}
@@ -81,7 +88,7 @@ function updateList<K extends ListKey>(
* refetching only the user.
*/
export function useProfile(username: string | undefined): UseProfileResult {
const { user: me, token } = useAuth();
const { user: me, authFetch } = useAuth();
const { lastDumpEvent, lastPlaylistEvent, lastUserEvent } = useWS();
const dumpsCache = useFeedCache<Dump>(
@@ -174,14 +181,10 @@ export function useProfile(username: string | undefined): UseProfileResult {
if (!username) return;
const controller = new AbortController();
const { signal } = controller;
const headers: HeadersInit = token
? { Authorization: `Bearer ${token}` }
: {};
const firstPageOf = <T>(list: string, deserialize: (w: Wire<T>) => T) =>
fetchFirstPage(`${API_URL}/api/users/${username}/${list}`, deserialize, {
headers,
authFetch(pageUrl(`${API_URL}/api/users/${username}/${list}`, 1), {
signal,
});
}).then((res) => readFirstPage(res, deserialize));
// Back on the page, the lists come from the cache and only the user is
// fetched again.
@@ -206,7 +209,7 @@ export function useProfile(username: string | undefined): UseProfileResult {
setState({ status: "error", error: friendlyFetchError(err) });
});
return () => controller.abort();
}, [username, cachedDumps, cachedVotes, cachedPlaylists, token]);
}, [username, cachedDumps, cachedVotes, cachedPlaylists, authFetch]);
const saveDumps = dumpsCache.saveState;
const saveVotes = votesCache.saveState;
@@ -216,9 +219,9 @@ export function useProfile(username: string | undefined): UseProfileResult {
useCallback((y) => {
if (state.status !== "loaded") return;
const { dumps, votes, playlists } = state;
saveDumps(dumps.items, dumps.page, dumps.hasMore, y);
saveVotes(votes.items, votes.page, votes.hasMore, y);
savePlaylists(playlists.items, playlists.page, playlists.hasMore, y);
saveDumps(dumps, y);
saveVotes(votes, y);
savePlaylists(playlists, y);
}, [state, saveDumps, saveVotes, savePlaylists]),
);
useScrollRestore(cachedDumps?.scrollY, state.status === "loaded");