Update ChatTable.tsx

This commit is contained in:
NotII
2025-03-24 17:27:40 +00:00
parent 9f4e873d4a
commit f209fb6a3a

View File

@@ -67,6 +67,7 @@ export default function ChatTable() {
const [totalPages, setTotalPages] = useState(1); const [totalPages, setTotalPages] = useState(1);
const [totalChats, setTotalChats] = useState(0); const [totalChats, setTotalChats] = useState(0);
const [itemsPerPage, setItemsPerPage] = useState<number>(10); const [itemsPerPage, setItemsPerPage] = useState<number>(10);
const isManualRefresh = useRef(false);
// Initialize audio element for notifications // Initialize audio element for notifications
useEffect(() => { useEffect(() => {
@@ -112,7 +113,13 @@ export default function ChatTable() {
// Fetch chats when component mounts or page/limit changes // Fetch chats when component mounts or page/limit changes
useEffect(() => { useEffect(() => {
// Skip fetch if this effect was triggered by a manual refresh
// since we'll call fetchChats directly in that case
if (!isManualRefresh.current) {
fetchChats(); fetchChats();
}
isManualRefresh.current = false;
// Set up polling for unread messages // Set up polling for unread messages
const interval = setInterval(() => { const interval = setInterval(() => {
@@ -122,6 +129,13 @@ export default function ChatTable() {
return () => clearInterval(interval); return () => clearInterval(interval);
}, [currentPage, itemsPerPage]); }, [currentPage, itemsPerPage]);
// Handle refresh button click
const handleRefresh = () => {
isManualRefresh.current = true;
setCurrentPage(1);
fetchChats();
};
// Fetch unread counts // Fetch unread counts
const fetchUnreadCounts = async () => { const fetchUnreadCounts = async () => {
try { try {
@@ -192,21 +206,27 @@ export default function ChatTable() {
// Handle pagination // Handle pagination
const goToNextPage = () => { const goToNextPage = () => {
if (currentPage < totalPages) { if (currentPage < totalPages) {
isManualRefresh.current = true;
setCurrentPage(prev => prev + 1); setCurrentPage(prev => prev + 1);
fetchChats();
} }
}; };
const goToPrevPage = () => { const goToPrevPage = () => {
if (currentPage > 1) { if (currentPage > 1) {
isManualRefresh.current = true;
setCurrentPage(prev => prev - 1); setCurrentPage(prev => prev - 1);
fetchChats();
} }
}; };
// Handle items per page change // Handle items per page change
const handleItemsPerPageChange = (value: string) => { const handleItemsPerPageChange = (value: string) => {
const newLimit = parseInt(value); const newLimit = parseInt(value);
isManualRefresh.current = true;
setItemsPerPage(newLimit); setItemsPerPage(newLimit);
setCurrentPage(1); // Reset to first page when changing limit setCurrentPage(1); // Reset to first page when changing limit
fetchChats();
}; };
return ( return (
@@ -215,10 +235,7 @@ export default function ChatTable() {
<Button <Button
variant="outline" variant="outline"
size="sm" size="sm"
onClick={() => { onClick={handleRefresh}
setCurrentPage(1);
fetchChats();
}}
disabled={loading} disabled={loading}
> >
{loading ? ( {loading ? (