import { useState, useCallback } from 'react' import { trpc } from '@/trpc/client' export function UserConnectRoute() { const [search, setSearch] = useState('') const [searchInput, setSearchInput] = useState('') const [currentPage, setCurrentPage] = useState(1) const [visitedCursors, setVisitedCursors] = useState<(number | undefined)[]>([undefined]) const [itemsPerPage, setItemsPerPage] = useState(50) const cursor = visitedCursors[currentPage - 1] const { data, isLoading, error } = trpc.admin.user.getAllUsers.useQuery({ limit: itemsPerPage, cursor, search, }) const handleSearch = () => { setSearch(searchInput) setCurrentPage(1) setVisitedCursors([undefined]) } const handleNextPage = () => { if (data?.nextCursor) { const newCursors = [...visitedCursors] newCursors[currentPage] = data.nextCursor setVisitedCursors(newCursors) setCurrentPage(currentPage + 1) } } const handlePrevPage = () => { if (currentPage > 1) { setCurrentPage(currentPage - 1) } } const handlePageSelect = (page: number) => { setCurrentPage(page) } const handleClearSearch = () => { setSearch('') setSearchInput('') setCurrentPage(1) setVisitedCursors([undefined]) } const handleItemsPerPageChange = (newLimit: number) => { setItemsPerPage(newLimit) setCurrentPage(1) setVisitedCursors([undefined]) } const totalPages = Math.min(currentPage + (data?.hasMore ? 3 : 0), 20) const pageNumbers = Array.from({ length: totalPages }, (_, i) => i + 1) if (isLoading) { return (

Loading users...

) } if (error) { return (

Error loading users: {error.message}

) } return (

User Connect

View all registered users and their contact information

setSearchInput(e.target.value)} placeholder="Search by mobile number..." className="flex-1 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" onKeyDown={(e) => { if (e.key === 'Enter') { handleSearch() } }} /> {search && ( )}

Page {currentPage} • Showing {data?.users.length || 0} users

{pageNumbers.length > 1 && ( <> {pageNumbers.map((page) => ( ))} {data?.hasMore && currentPage >= totalPages - 2 && ( ... )} )}
{data?.users.map((user) => ( ))}
ID Name Mobile Total Orders Last Order Status
{user.id} {user.name || '-'} {user.mobile ? ( {user.mobile} ) : ( - )} {user.totalOrders} {user.lastOrderDate ? new Date(user.lastOrderDate).toLocaleDateString() : '-'} {user.isSuspended ? ( Suspended ) : ( Active )}
{data?.users.length === 0 && (

No users found

)}

Page {currentPage} • Showing {data?.users.length || 0} users

{pageNumbers.length > 1 && ( <> {pageNumbers.map((page) => ( ))} {data?.hasMore && currentPage >= totalPages - 2 && ( ... )} )}
) }