feat: Add pagination to bot manager

This commit is contained in:
gpt-engineer-app[bot]
2025-10-16 20:43:36 +00:00
parent 765d9eff75
commit 38eaa04ee1
+74 -2
View File
@@ -1,5 +1,5 @@
import { useState } from "react"; import { useState } from "react";
import { Bot, Edit, MessageSquare, Trash2 } from "lucide-react"; import { Bot, Edit, MessageSquare, Trash2, ChevronLeft, ChevronRight } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
@@ -27,6 +27,8 @@ export const BotView = ({ onStartChat }: BotViewProps) => {
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false); const [isEditDialogOpen, setIsEditDialogOpen] = useState(false);
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
const [selectedBot, setSelectedBot] = useState<Bot | null>(null); const [selectedBot, setSelectedBot] = useState<Bot | null>(null);
const [currentPage, setCurrentPage] = useState(1);
const [itemsPerPage, setItemsPerPage] = useState(10);
const [formData, setFormData] = useState({ const [formData, setFormData] = useState({
name: "", name: "",
@@ -34,6 +36,18 @@ export const BotView = ({ onStartChat }: BotViewProps) => {
model: "ChatGPT 4.1", model: "ChatGPT 4.1",
}); });
// Pagination calculations
const totalPages = Math.ceil(bots.length / itemsPerPage);
const startIndex = (currentPage - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
const currentBots = bots.slice(startIndex, endIndex);
// Reset to page 1 when items per page changes
const handleItemsPerPageChange = (value: string) => {
setItemsPerPage(Number(value));
setCurrentPage(1);
};
const handleCreateBot = () => { const handleCreateBot = () => {
const newBot: Bot = { const newBot: Bot = {
id: Date.now().toString(), id: Date.now().toString(),
@@ -186,7 +200,7 @@ export const BotView = ({ onStartChat }: BotViewProps) => {
</TableRow> </TableRow>
</TableHeader> </TableHeader>
<TableBody> <TableBody>
{bots.map((bot) => ( {currentBots.map((bot) => (
<TableRow key={bot.id}> <TableRow key={bot.id}>
<TableCell className="font-medium">{bot.name}</TableCell> <TableCell className="font-medium">{bot.name}</TableCell>
<TableCell>{bot.model}</TableCell> <TableCell>{bot.model}</TableCell>
@@ -222,6 +236,64 @@ export const BotView = ({ onStartChat }: BotViewProps) => {
))} ))}
</TableBody> </TableBody>
</Table> </Table>
{/* Pagination Controls */}
<div className="flex items-center justify-between mt-6 border-t pt-4">
<div className="flex items-center gap-2">
<span className="text-sm text-muted-foreground">Itens por página:</span>
<Select value={itemsPerPage.toString()} onValueChange={handleItemsPerPageChange}>
<SelectTrigger className="w-20">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="10">10</SelectItem>
<SelectItem value="25">25</SelectItem>
<SelectItem value="50">50</SelectItem>
<SelectItem value="100">100</SelectItem>
</SelectContent>
</Select>
</div>
<div className="flex items-center gap-2">
<span className="text-sm text-muted-foreground">
Página {currentPage} de {totalPages} ({bots.length} total)
</span>
<div className="flex gap-1">
<Button
size="sm"
variant="outline"
onClick={() => setCurrentPage(1)}
disabled={currentPage === 1}
>
Primeira
</Button>
<Button
size="icon"
variant="outline"
onClick={() => setCurrentPage(currentPage - 1)}
disabled={currentPage === 1}
>
<ChevronLeft className="w-4 h-4" />
</Button>
<Button
size="icon"
variant="outline"
onClick={() => setCurrentPage(currentPage + 1)}
disabled={currentPage === totalPages}
>
<ChevronRight className="w-4 h-4" />
</Button>
<Button
size="sm"
variant="outline"
onClick={() => setCurrentPage(totalPages)}
disabled={currentPage === totalPages}
>
Última
</Button>
</div>
</div>
</div>
</div> </div>
)} )}
</div> </div>