@@ -9,7 +9,7 @@ import { useSettings } from "../lib/settings";
99 import { useTheme } from "../lib/theme";
1010 import { toast } from "../lib/toast";
1111 import { beep, useVoice, type VoiceState } from "../lib/voice";
12-import type { Channel, Message, Reaction, User, UserSummary, VoiceOccupant } from "../lib/types";
12+import type { Channel, Message, MessagePage, Reaction, User, UserSummary, VoiceOccupant } from "../lib/types";
1313 import CallView from "../components/CallView";
1414 import Composer from "../components/Composer";
1515 import MessageActions from "../components/MessageActions";
@@ -92,6 +92,8 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
9292 const [users, setUsers] = useState<UserSummary[]>([]);
9393 const [online, setOnline] = useState<Set<string>>(new Set());
9494 const [messages, setMessages] = useState<Message[]>([]);
95+ const [hasMore, setHasMore] = useState(false);
96+ const [loadingOlder, setLoadingOlder] = useState(false);
9597 const [unread, setUnread] = useState<Record<number, number>>({});
9698 const [collapsed, setCollapsed] = useState<Set<number>>(loadCollapsed);
9799 const [previewState, setPreviewState] = useState<Record<number, boolean>>(loadPreviewState);
@@ -104,6 +106,8 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
104106 const desktop = useRef(matchMedia("(min-width: 768px)").matches);
105107 const [sidebarOpen, setSidebarOpen] = useState(desktop.current);
106108 const selectedId = useRef<number | null>(null);
109+ // The channel the messages in state came from.
110+ const loaded = useRef<number | null>(null);
107111 // The channel of the DM call we started, so we can tell the peer when it ends.
108112 const rangRef = useRef<number | null>(null);
109113 // The server drops our voice state when the socket dies, so keep the last
@@ -338,13 +342,45 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
338342 useEffect(() => {
339343 if (selected?.type !== "text" && selected?.type !== "dm") {
340344 setMessages([]);
345+ setHasMore(false);
341346 return;
342347 }
343- api<Message[]>(`/channels/${selected.id}/messages`)
344- .then(setMessages)
348+ const id = selected.id;
349+ // The channel the loaded messages belong to, so a click on the hint can
350+ // never page a channel that is not on screen.
351+ loaded.current = null;
352+ setHasMore(false);
353+ setLoadingOlder(false);
354+ api<MessagePage>(`/channels/${id}/messages`)
355+ .then((page) => {
356+ if (selectedId.current !== id) return;
357+ loaded.current = id;
358+ setMessages(page.messages);
359+ setHasMore(page.hasMore);
360+ })
345361 .catch(() => {});
346362 }, [selected]);
347363
364+ // loadOlder appends the page above the first loaded message. It only runs
365+ // when the reader asks for it, so a slow channel costs nothing to scroll.
366+ async function loadOlder() {
367+ const id = selected?.id;
368+ const before = messages[0]?.id;
369+ if (id === undefined || before === undefined || loaded.current !== id || loadingOlder || !hasMore)
370+ return;
371+ setLoadingOlder(true);
372+ try {
373+ const page = await api<MessagePage>(`/channels/${id}/messages?before=${before}`);
374+ if (selectedId.current !== id) return;
375+ setMessages((prev) => [...page.messages, ...prev]);
376+ setHasMore(page.hasMore);
377+ } catch {
378+ // The hint stays put, so the reader can try again.
379+ } finally {
380+ if (selectedId.current === id) setLoadingOlder(false);
381+ }
382+ }
383+
348384 // Opening a channel clears its badge and tells the server, so the count stays
349385 // right on the next cold start and on the user's other devices.
350386 useEffect(() => {
@@ -617,6 +653,8 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
617653 <MessageList
618654 key={selected.id}
619655 messages={messages}
656+ hasMore={hasMore}
657+ loadingOlder={loadingOlder}
620658 username={user.username}
621659 collapsed={collapsed}
622660 previewCollapsed={previewCollapsed}
@@ -626,6 +664,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
626664 editing={editing}
627665 activeId={active?.id ?? null}
628666 onSelect={(m) => setActive((a) => (a?.id === m.id ? null : m))}
667+ onLoadOlder={loadOlder}
629668 onToggleImages={toggleImages}
630669 onEditBody={(body) => setEditing((e) => (e ? { ...e, body } : e))}
631670 onCancelEdit={() => setEditing(null)}