Matthias Nott
2026-03-07 8cdf33e27c633ac30e8851c4617f6063c141660d
contexts/ChatContext.tsx
....@@ -112,6 +112,8 @@
112112 saveTimer = setTimeout(() => persistMessages(map), 1000);
113113 }
114114
115
+const PAGE_SIZE = 50;
116
+
115117 // --- Context ---
116118
117119 interface ChatContextValue {
....@@ -131,6 +133,8 @@
131133 createSession: (opts?: { project?: string; path?: string }) => void;
132134 fetchProjects: () => void;
133135 projects: PaiProject[];
136
+ loadMoreMessages: () => void;
137
+ hasMoreMessages: boolean;
134138 unreadCounts: Record<string, number>;
135139 latestScreenshot: string | null;
136140 requestScreenshot: () => void;
....@@ -155,6 +159,8 @@
155159 const [isTyping, setIsTyping] = useState(false);
156160 // PAI projects list
157161 const [projects, setProjects] = useState<PaiProject[]>([]);
162
+ // Pagination: does the active session have more messages in storage?
163
+ const [hasMoreMessages, setHasMoreMessages] = useState(false);
158164
159165 const {
160166 status,
....@@ -184,8 +190,10 @@
184190 if (prev) {
185191 messagesMapRef.current[prev] = messages;
186192 }
187
- const stored = messagesMapRef.current[active.id] ?? [];
188
- setMessages(stored);
193
+ const all = messagesMapRef.current[active.id] ?? [];
194
+ const page = all.length > PAGE_SIZE ? all.slice(-PAGE_SIZE) : all;
195
+ setMessages(page);
196
+ setHasMoreMessages(all.length > PAGE_SIZE);
189197 setUnreadCounts((u) => {
190198 if (!u[active.id]) return u;
191199 const next = { ...u };
....@@ -550,6 +558,24 @@
550558 sendCommand("projects");
551559 }, [sendCommand]);
552560
561
+ const loadMoreMessages = useCallback(() => {
562
+ setActiveSessionId((sessId) => {
563
+ if (!sessId) return sessId;
564
+ const all = messagesMapRef.current[sessId] ?? [];
565
+ setMessages((current) => {
566
+ if (current.length >= all.length) {
567
+ setHasMoreMessages(false);
568
+ return current;
569
+ }
570
+ const nextSize = Math.min(current.length + PAGE_SIZE, all.length);
571
+ const page = all.slice(-nextSize);
572
+ setHasMoreMessages(nextSize < all.length);
573
+ return page;
574
+ });
575
+ return sessId;
576
+ });
577
+ }, []);
578
+
553579 // --- Screenshot / navigation ---
554580 const requestScreenshot = useCallback(() => {
555581 sendCommand("screenshot");
....@@ -581,6 +607,8 @@
581607 createSession,
582608 fetchProjects,
583609 projects,
610
+ loadMoreMessages,
611
+ hasMoreMessages,
584612 unreadCounts,
585613 latestScreenshot,
586614 requestScreenshot,