Files
gerbeur/src/contexts/social/CommunityFeedProvider.tsx
khannurien 51439f475d
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 51s
Build and Deploy Static Documentation Site / build-and-push (push) Successful in 26s
v4: the GeoCities marquee scrolls the community feed, what the site's members are doing — it crawled three canned lines, a welcome, "best viewed in Netscape Navigator" and "under construction", which said nothing of the site. The feed is a list any style may show (CommunityFeed.tsx), which GeoCities lays out along its marquee: who is online, the week's dumps, upvotes, comments and chat messages, linking to /community, its most upvoted dump, then the 12 latest things done in public, newest first, each ending on how long ago it was: a public dump posted, a public playlist created, a comment or an upvote on a public dump, someone joining and who invited them. Every name and title links to its page. A dump has one comment line and one upvote line at most, its latest, so that a thread or a popular dump doesn't fill the feed. GET /api/community/feed serves it, signed in only (community-service.ts): the events are read from the tables on each request, the week's figures are those /community already keeps, as of its last refresh. A signed-out visitor has no marquee. CommunityFeedProvider holds the feed around every page, so that it outlives the header each page renders anew, fetches it only while a component shows it, and fetches it again two seconds after the socket last told of a dump, a comment, a vote, a playlist, a rename, a deletion or a reconnection: the socket's messages don't carry a feed's lines, and the server's query stays the one place that decides what the feed holds. Someone joining comes with no message, and shows at the next of those. The crawl goes at 50 pixels a second however much the feed holds, Marquee.tsx measuring one copy for the stylesheet to work the loop's length out from, where a fixed 36 seconds would have sped up with every line. A loop shows what the feed held when it began: text that changed mid-crawl would jump, so what came meanwhile waits for the next loop, which also brings the times up to date. The next page's marquee carries on from where the last one's had got to, rather than from the start. The marquee was hidden from assistive technology as decoration; its first copy is now a labelled list of links, the second, which the seamless loop needs, hidden and out of the tab order. A link that takes the focus stops the crawl and the line scrolls to it, the pointer pauses it as before, and with reduced motion the line shows once, still, follows the feed as it changes and scrolls by hand. The sentences a notification also says, "posted", "upvoted", "commented on", share its translations. The tests' DOM gets an AnimationEvent before React loads (src/test/before-react.ts): finding none, React listened for the animation events under their WebKit names, and a test's "animationiteration" reached no handler.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 07:38:44 +00:00

119 lines
3.9 KiB
TypeScript

/**
* Keeps the community feed for the components that show it, loaded from
* `GET /api/community/feed` and again whenever the socket tells of something
* the feed may hold. `App` mounts it around every page, so that the feed
* outlives the header it is shown in, which every page renders anew.
*
* @module
*/
import {
type ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import type { Wire } from "../../../shared/api.ts";
import type { CommunityFeed } from "../../../shared/community.ts";
import { COMMUNITY_FEED_URL } from "../../config/api.ts";
import { useAuth } from "../../hooks/auth/useAuth.ts";
import { useFetchState } from "../../hooks/shared/useFetchState.ts";
import { useOnReconnect, useWS, useWSEvent } from "../../hooks/ws/useWS.ts";
import { expectOk } from "../../utils/shared/api.ts";
import { deserializeCommunityFeed } from "../../utils/shared/deserialize.ts";
import {
CommunityFeedContext,
type CommunityFeedContextValue,
} from "./CommunityFeedContext.ts";
/** Passed to {@link CommunityFeedProvider} by `App`. */
export interface CommunityFeedProviderProps {
children: ReactNode;
}
// The feed is fetched again this long after the last change the socket told
// of, so that a run of upvotes costs one request.
const RELOAD_MS = 2000;
/**
* Provides {@link CommunityFeedContext}. It fetches the feed once a component
* watches it, for the signed-in user alone. The socket's messages don't carry
* a feed's lines, an upvote's naming neither its dump nor its voter, so each
* one that may change the feed has it fetched again, and the server's query
* stays the one place that decides what the feed holds. A change that comes
* while nothing watches is caught up with on the next watch.
*/
export function CommunityFeedProvider(
{ children }: CommunityFeedProviderProps,
): React.JSX.Element {
const { user, authFetch } = useAuth();
const [watched, setWatched] = useState(false);
const load = useCallback(
async (signal: AbortSignal) =>
deserializeCommunityFeed(
await expectOk<Wire<CommunityFeed>>(
await authFetch(COMMUNITY_FEED_URL, { signal }),
),
),
[authFetch],
);
const { state, reload } = useFetchState(user?.id, load, {
enabled: watched,
});
const watchers = useRef(0);
// A change came that the feed hasn't been fetched again for.
const due = useRef(false);
const timer = useRef<ReturnType<typeof setTimeout>>(undefined);
const refresh = useCallback(() => {
if (!due.current || watchers.current === 0) return;
due.current = false;
reload();
}, [reload]);
const changed = useCallback(() => {
due.current = true;
clearTimeout(timer.current);
timer.current = setTimeout(refresh, RELOAD_MS);
}, [refresh]);
useEffect(() => () => clearTimeout(timer.current), []);
useWSEvent("dump", changed);
useWSEvent("vote", changed);
useWSEvent("comment", (e) => e.type !== "updated" && changed());
useWSEvent("playlist", (e) => e.type !== "dumps_updated" && changed());
// A rename changes the names the feed writes.
useWSEvent("user", changed);
useOnReconnect(changed);
// A deleted dump comes as no event, only as a change of this set.
const { deletedDumpIds } = useWS();
const seenDeleted = useRef(deletedDumpIds);
useEffect(() => {
if (seenDeleted.current === deletedDumpIds) return;
seenDeleted.current = deletedDumpIds;
changed();
}, [deletedDumpIds, changed]);
const watch = useCallback(() => {
watchers.current++;
setWatched(true);
refresh();
return () => {
watchers.current--;
};
}, [refresh]);
const feed = state.status === "loaded" ? state.data : null;
const value: CommunityFeedContextValue = useMemo(
() => ({ feed, watch }),
[feed, watch],
);
return (
<CommunityFeedContext.Provider value={value}>
{children}
</CommunityFeedContext.Provider>
);
}