"use client"; import { useState } from "react"; import Link from "next/link"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { BookOpen } from "lucide-react"; import { http, toUserError } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { toast } from "@/components/providers"; import { formatFaNumber, formatJalaliStamp } from "@/lib/utils"; const PAGE_SIZE = 8; type BotKey = { id: number; botId: number; name: string; createdAt: string; active: boolean; requestCount: number; }; type BotList = { items: BotKey[]; total: number; page: number; pageSize: number; }; export function BotPanel() { const qc = useQueryClient(); const [name, setName] = useState(""); const [token, setToken] = useState(""); const [busy, setBusy] = useState(false); const [page, setPage] = useState(1); const [toggling, setToggling] = useState(null); const listQ = useQuery({ queryKey: ["admin-bots", page], queryFn: () => http.get(`/api/admin/bots?page=${page}&pageSize=${PAGE_SIZE}`), }); const items = listQ.data?.items ?? []; const total = listQ.data?.total ?? 0; const pageSize = listQ.data?.pageSize ?? PAGE_SIZE; const totalPages = Math.max(1, Math.ceil(total / pageSize)); const pageSafe = Math.min(Math.max(page, 1), totalPages); async function copyToken() { try { await navigator.clipboard.writeText(token); toast.success("توکن کپی شد"); } catch { toast.error("کپی ممکن نشد"); } } function setActive(row: BotKey, active: boolean) { setToggling(row.id); void http .put(`/api/admin/bots/${row.id}`, { active }) .then(() => { toast.success(active ? "کلید فعال شد" : "کلید متوقف شد"); return qc.invalidateQueries({ queryKey: ["admin-bots"] }); }) .catch((e) => toast.error(toUserError(e, "تغییر وضعیت ممکن نشد"))) .finally(() => setToggling(null)); } return (
کلید API

توکن فقط یک بار نشان داده می‌شود.

setName(e.target.value)} placeholder="نام ربات" /> {token && (

توکن فقط یک بار نشان داده می‌شود.

)}
کلیدها {listQ.isPending ? (

در حال بارگذاری…

) : listQ.isError ? (

{toUserError(listQ.error, "بارگذاری کلیدها ممکن نشد")}

) : items.length === 0 ? (

هنوز کلیدی نیست.

) : (
    {items.map((row) => (
  • {row.name || "ربات"}

    {formatJalaliStamp(row.createdAt)}

    {row.active ? "فعال" : "متوقف"} · {formatFaNumber(row.requestCount)} درخواست

  • ))}
{totalPages > 1 && (

{formatFaNumber(pageSafe)} از {formatFaNumber(totalPages)}

)}
)}
); }