@@ -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];