import React from "react"; import { Table, Spinner, Text, VStack, Button, HStack, IconButton, Heading, } from "@chakra-ui/react"; import { Tooltip } from "@/components/ui/tooltip"; import MyAlert from "./myChakra/MyAlert"; import { Trash2, RefreshCcwDot, CirclePlus } from "lucide-react"; import Cookies from "js-cookie"; import { useState, useEffect } from "react"; import { deleteAPKey } from "@/utils/userActions"; import AddAPIKey from "./AddAPIKey"; import { formatDateTime } from "@/utils/userFuncs"; const API_BASE = (import.meta as any).env?.VITE_BACKEND_URL || import.meta.env.VITE_BACKEND_URL || "http://localhost:8002"; type Items = { id: number; apiKey: string; user: string; entry_created_at: string; }; const APIKeyTable: React.FC = () => { const [items, setItems] = useState([]); const [errorStatus, setErrorStatus] = useState<"error" | "success">("error"); const [errorMessage, setErrorMessage] = useState(""); const [errorDsc, setErrorDsc] = useState(""); const [isError, setIsError] = useState(false); const [isLoading, setIsLoading] = useState(false); const [reload, setReload] = useState(false); const [addAPIForm, setAddAPIForm] = useState(false); const setError = ( status: "error" | "success", message: string, description: string ) => { setIsError(false); setErrorStatus(status); setErrorMessage(message); setErrorDsc(description); setIsError(true); }; useEffect(() => { const fetchData = async () => { setIsLoading(true); try { const response = await fetch(`${API_BASE}/api/apiKeys`, { method: "GET", headers: { Authorization: `Bearer ${Cookies.get("token")}`, }, }); const data = await response.json(); return data; } catch (error) { setError("error", "Failed to fetch items", "There is an error"); } finally { setIsLoading(false); } }; fetchData().then((data) => { if (Array.isArray(data)) { setItems(data); } }); }, [reload]); return ( <> {/* Action toolbar */} setReload(!reload)} > {/* End action toolbar */} Gegenstände {isError && ( )} {isLoading && ( Loading... )} {addAPIForm && ( { setAddAPIForm(false); setReload(!reload); }} alert={setError} /> )} # API Key Benutzer Eintrag erstellt am Aktionen {items.map((apiKey) => ( {apiKey.id} {apiKey.apiKey} {apiKey.user} {formatDateTime(apiKey.entry_created_at)} ))} ); }; export default APIKeyTable;