Update ChatTable.tsx
This commit is contained in:
@@ -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(() => {
|
||||||
fetchChats();
|
// Skip fetch if this effect was triggered by a manual refresh
|
||||||
|
// since we'll call fetchChats directly in that case
|
||||||
|
if (!isManualRefresh.current) {
|
||||||
|
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 ? (
|
||||||
|
|||||||
Reference in New Issue
Block a user