xonecas.io
8058
Ir

irvc / commit 5cbe9f0

feat: load older messages on scroll

Reaching the top of the list now loads the page above, instead of asking the
reader to press a button. A page of 200px or less from the top triggers the
request, and a page request is deduped so one scroll cannot fetch twice.

The reader keeps their place because the prepend already restores the scroll
offset; nothing changes there.

xonecas2026-10-28 5cbe9f0813caeca229b40fa9e36432e9b0378a55 parent b666936

3 files changed

irvc.md+1−1
@@ -77,7 +77,7 @@ read_state (user_id, channel_id, last_read_message_id, updated_at)
7777
7878 - **Channels:** seeded (`#general` text, `general` voice, both permanent) plus on-demand 1-on-1 DMs opened by clicking a user. Text and voice channels can be created and deleted from the sidebar; permanent channels cannot be deleted, and a voice channel cannot be deleted while someone is in it. Ordered by `position`.
7979 - **Text:** GFM markdown; single newlines are hard breaks. Messages persist; live append over WS. Rendered as timestamp, author, content.
80-- **History:** a channel opens on its newest 100 messages. Reaching the top shows a "load 100 more" button and nothing loads by itself; a loaded page keeps the reader looking at the same messages.
80+- **History:** a channel opens on its newest 100 messages. Scrolling to the top loads the page above; a loaded page keeps the reader looking at the same messages.
8181 - **Mentions:** `@username` highlights the message for that user; typing `@` opens an autocomplete.
8282 - **Unread:** `read_state` stores each user's last-read message per channel; `/channels` returns the count of newer messages by others. Opening a channel marks it read, so the count survives reload and is shared across devices.
8383 - **Commands:** `/me <action>` renders an emote as `* user action`; `/collapse` hides media in the current channel; `/gif [query]` opens a picker with the top Giphy results (trending when no query) to insert one. The gif picker is disabled without `IRVC_GIPHY_API_KEY`.
web/src/components/MessageList.tsx+27−11
@@ -1,7 +1,7 @@
11 import { Fragment, useEffect, useLayoutEffect, useRef } from "react";
22 import { Heart, Laugh, MoreHorizontal, ThumbsDown, ThumbsUp } from "lucide-react";
33 import { cn } from "cn";
4-import { Button, IconButton, TextArea } from "@/ui";
4+import { IconButton, TextArea } from "@/ui";
55 import type { Message, Reaction } from "../lib/types";
66 import { Time, dayKey, formatDay } from "../lib/time";
77 import LinkPreview from "./LinkPreview";
@@ -15,6 +15,9 @@ export const reactions: { key: Reaction; label: string; Icon: typeof ThumbsUp }[
1515 { key: "laugh", label: "laugh", Icon: Laugh },
1616 ];
1717
18+// How close to the top, in pixels, counts as asking for the page above.
19+const topZone = 200;
20+
1821 // isMentioned reports whether body contains "@username" as a whole word.
1922 export function isMentioned(body: string, username: string) {
2023 return new RegExp(`(^|[^\\w-])@${username}(?![\\w-])`, "i").test(body);
@@ -102,17 +105,34 @@ export default function MessageList({
102105 measure();
103106 });
104107
108+ // The reader reaching the top asks for the page above. Loading a page moves
109+ // the reader down with the messages, so this only refires on a new scroll,
110+ // or when a page was too short to fill the view.
111+ const paging = useRef({ hasMore, loadingOlder, onLoadOlder });
112+ paging.current = { hasMore, loadingOlder, onLoadOlder };
113+
105114 useEffect(() => {
106115 const el = list.current;
107116 if (!el) return;
117+ const loadOlder = () => {
118+ const { hasMore, loadingOlder, onLoadOlder } = paging.current;
119+ if (!hasMore || loadingOlder) return;
120+ if (el.scrollTop > topZone && el.scrollHeight > el.clientHeight) return;
121+ onLoadOlder();
122+ };
108123 // Images load after the effect above, so follow the bottom on resize too.
109- const ro = new ResizeObserver(pin);
124+ const ro = new ResizeObserver(() => {
125+ pin();
126+ loadOlder();
127+ });
110128 ro.observe(el.firstElementChild ?? el);
111129 const onScroll = () => {
112130 // A scroll still sitting where we pinned is ours, so leave stick alone.
113- if (el.scrollTop === pinned.current) return;
114- stick.current = el.scrollTop + el.clientHeight >= el.scrollHeight - 2;
115- measure();
131+ if (el.scrollTop !== pinned.current) {
132+ stick.current = el.scrollTop + el.clientHeight >= el.scrollHeight - 2;
133+ measure();
134+ }
135+ loadOlder();
116136 };
117137 el.addEventListener("scroll", onScroll, { passive: true });
118138 return () => {
@@ -127,12 +147,8 @@ export default function MessageList({
127147 className="min-h-0 flex-1 overflow-y-auto overscroll-contain py-2 [scrollbar-gutter:stable]"
128148 >
129149 <div>
130- {hasMore && (
131- <div className="flex justify-center px-3 py-2">
132- <Button size="sm" disabled={loadingOlder} onClick={onLoadOlder}>
133- {loadingOlder ? "loading…" : "load 100 more"}
134- </Button>
135- </div>
150+ {loadingOlder && (
151+ <div className="py-2 text-center text-[0.7rem] text-faint">loading older messages…</div>
136152 )}
137153 {messages.map((m, i) => {
138154 const prev = messages[i - 1];
web/src/views/Chat.tsx+10−4
@@ -121,6 +121,8 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
121121 const selectedId = useRef<number | null>(null);
122122 // The channel the messages in state came from.
123123 const loaded = useRef<number | null>(null);
124+ // Set while a page request is in flight, so a fast scroll cannot ask twice.
125+ const paging = useRef(false);
124126 // The channel of the DM call we started, so we can tell the peer when it ends.
125127 const rangRef = useRef<number | null>(null);
126128 // The server drops our voice state when the socket dies, so keep the last
@@ -374,6 +376,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
374376 // The channel the loaded messages belong to, so a click on the hint can
375377 // never page a channel that is not on screen.
376378 loaded.current = null;
379+ paging.current = false;
377380 setHasMore(false);
378381 setLoadingOlder(false);
379382 api<MessagePage>(`/channels/${id}/messages`)
@@ -402,13 +405,15 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
402405 .catch(() => {});
403406 }, [selected]);
404407
405- // loadOlder appends the page above the first loaded message. It only runs
406- // when the reader asks for it, so a slow channel costs nothing to scroll.
408+ // loadOlder appends the page above the first loaded message. Scrolling to the
409+ // top calls it, and the guard keeps one scroll from fetching the same page
410+ // twice before the first request lands.
407411 async function loadOlder() {
408412 const id = selected?.id;
409413 const before = messages[0]?.id;
410- if (id === undefined || before === undefined || loaded.current !== id || loadingOlder || !hasMore)
414+ if (id === undefined || before === undefined || loaded.current !== id || paging.current || !hasMore)
411415 return;
416+ paging.current = true;
412417 setLoadingOlder(true);
413418 try {
414419 const page = await api<MessagePage>(`/channels/${id}/messages?before=${before}`);
@@ -416,8 +421,9 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
416421 setMessages((prev) => [...page.messages, ...prev]);
417422 setHasMore(page.hasMore);
418423 } catch {
419- // The hint stays put, so the reader can try again.
424+ // The reader is still at the top, so a later scroll tries again.
420425 } finally {
426+ paging.current = false;
421427 if (selectedId.current === id) setLoadingOlder(false);
422428 }
423429 }