mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 00:37:23 +00:00
pagination
This commit is contained in:
1 parent
1ffe78c80c
commit
04ae3d89c0
2 files changed
+76
-6
No files matched your search
@@ -153,10 +153,11 @@ const EmptyState = ({ isLoading }: { isLoading: boolean }) => {
|
|||||||
export const DMChatArea = ({ pubkey, onBack, className }: DMChatAreaProps) => {
|
export const DMChatArea = ({ pubkey, onBack, className }: DMChatAreaProps) => {
|
||||||
const { user } = useCurrentUser();
|
const { user } = useCurrentUser();
|
||||||
const { sendMessage, protocolMode, isLoading } = useDMContext();
|
const { sendMessage, protocolMode, isLoading } = useDMContext();
|
||||||
const { messages } = useConversationMessages(pubkey || '');
|
const { messages, hasMoreMessages, loadEarlierMessages } = useConversationMessages(pubkey || '');
|
||||||
|
|
||||||
const [messageText, setMessageText] = useState('');
|
const [messageText, setMessageText] = useState('');
|
||||||
const [isSending, setIsSending] = useState(false);
|
const [isSending, setIsSending] = useState(false);
|
||||||
|
const [isLoadingMore, setIsLoadingMore] = useState(false);
|
||||||
|
|
||||||
// Determine default protocol based on mode
|
// Determine default protocol based on mode
|
||||||
const getDefaultProtocol = () => {
|
const getDefaultProtocol = () => {
|
||||||
@@ -208,6 +209,32 @@ export const DMChatArea = ({ pubkey, onBack, className }: DMChatAreaProps) => {
|
|||||||
}
|
}
|
||||||
}, [handleSend]);
|
}, [handleSend]);
|
||||||
|
|
||||||
|
const handleLoadMore = useCallback(async () => {
|
||||||
|
if (!scrollAreaRef.current || isLoadingMore) return;
|
||||||
|
|
||||||
|
const scrollContainer = scrollAreaRef.current.querySelector('[data-radix-scroll-area-viewport]');
|
||||||
|
if (!scrollContainer) return;
|
||||||
|
|
||||||
|
// Store current scroll position and height
|
||||||
|
const previousScrollHeight = scrollContainer.scrollHeight;
|
||||||
|
const previousScrollTop = scrollContainer.scrollTop;
|
||||||
|
|
||||||
|
setIsLoadingMore(true);
|
||||||
|
|
||||||
|
// Load more messages
|
||||||
|
loadEarlierMessages();
|
||||||
|
|
||||||
|
// Wait for DOM to update, then restore relative scroll position
|
||||||
|
setTimeout(() => {
|
||||||
|
if (scrollContainer) {
|
||||||
|
const newScrollHeight = scrollContainer.scrollHeight;
|
||||||
|
const heightDifference = newScrollHeight - previousScrollHeight;
|
||||||
|
scrollContainer.scrollTop = previousScrollTop + heightDifference;
|
||||||
|
}
|
||||||
|
setIsLoadingMore(false);
|
||||||
|
}, 0);
|
||||||
|
}, [loadEarlierMessages, isLoadingMore]);
|
||||||
|
|
||||||
if (!pubkey) {
|
if (!pubkey) {
|
||||||
return (
|
return (
|
||||||
<Card className={cn("h-full", className)}>
|
<Card className={cn("h-full", className)}>
|
||||||
@@ -240,6 +267,26 @@ export const DMChatArea = ({ pubkey, onBack, className }: DMChatAreaProps) => {
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div>
|
<div>
|
||||||
|
{hasMoreMessages && (
|
||||||
|
<div className="flex justify-center mb-4">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={handleLoadMore}
|
||||||
|
disabled={isLoadingMore}
|
||||||
|
className="text-xs"
|
||||||
|
>
|
||||||
|
{isLoadingMore ? (
|
||||||
|
<>
|
||||||
|
<Loader2 className="h-3 w-3 animate-spin mr-2" />
|
||||||
|
Loading...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
'Load Earlier Messages'
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
{messages.map((message) => (
|
{messages.map((message) => (
|
||||||
<MessageBubble
|
<MessageBubble
|
||||||
key={message.id}
|
key={message.id}
|
||||||
|
|||||||
@@ -134,10 +134,13 @@ export function useDMContext(): DMContextType {
|
|||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MESSAGES_PER_PAGE = 25;
|
||||||
|
|
||||||
export function useConversationMessages(conversationId: string) {
|
export function useConversationMessages(conversationId: string) {
|
||||||
const { messages: allMessages } = useDMContext();
|
const { messages: allMessages } = useDMContext();
|
||||||
|
const [visibleCount, setVisibleCount] = useState(MESSAGES_PER_PAGE);
|
||||||
|
|
||||||
return useMemo(() => {
|
const result = useMemo(() => {
|
||||||
const conversationData = allMessages.get(conversationId);
|
const conversationData = allMessages.get(conversationId);
|
||||||
|
|
||||||
if (!conversationData) {
|
if (!conversationData) {
|
||||||
@@ -150,14 +153,34 @@ export function useConversationMessages(conversationId: string) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const totalMessages = conversationData.messages.length;
|
||||||
|
const hasMore = totalMessages > visibleCount;
|
||||||
|
|
||||||
|
// Return the most recent N messages (slice from the end)
|
||||||
|
const visibleMessages = conversationData.messages.slice(-visibleCount);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
messages: conversationData.messages,
|
messages: visibleMessages,
|
||||||
hasMoreMessages: false,
|
hasMoreMessages: hasMore,
|
||||||
totalCount: conversationData.messages.length,
|
totalCount: totalMessages,
|
||||||
lastMessage: conversationData.lastMessage,
|
lastMessage: conversationData.lastMessage,
|
||||||
lastActivity: conversationData.lastActivity,
|
lastActivity: conversationData.lastActivity,
|
||||||
};
|
};
|
||||||
}, [allMessages, conversationId]);
|
}, [allMessages, conversationId, visibleCount]);
|
||||||
|
|
||||||
|
const loadEarlierMessages = useCallback(() => {
|
||||||
|
setVisibleCount(prev => prev + MESSAGES_PER_PAGE);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Reset visible count when conversation changes
|
||||||
|
useEffect(() => {
|
||||||
|
setVisibleCount(MESSAGES_PER_PAGE);
|
||||||
|
}, [conversationId]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...result,
|
||||||
|
loadEarlierMessages,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DMProviderProps {
|
interface DMProviderProps {
|
||||||
|
|||||||
Reference in new issue
Block a user