2023-12-31 05:41:55 -05:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import axios from "axios";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import * as React from 'react';
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
const ApiKeyPage = () => {
|
|
|
|
const [apiKeyViewable, setApiKeyViewable] = useState<number>(-1)
|
|
|
|
const [apiKeys, setApiKeys] = useState<{ id: string, label: string, userId: string }[]>([])
|
2023-12-31 05:41:55 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchData = async () => {
|
2024-06-24 18:16:55 -04:00
|
|
|
await axios.get("/api/tokens")
|
|
|
|
.then(d => setApiKeys(d.data ?? []))
|
|
|
|
.catch(console.error)
|
2023-12-31 05:41:55 -05:00
|
|
|
};
|
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
fetchData();
|
|
|
|
}, []);
|
2023-12-31 05:41:55 -05:00
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
const onApiKeyAdd = async (label: string) => {
|
|
|
|
await axios.post("/api/token", { label })
|
|
|
|
.then(d => setApiKeys(apiKeys.concat([d.data])))
|
|
|
|
.catch(console.error)
|
2023-12-31 05:41:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const onApiKeyDelete = async (id: string) => {
|
2024-06-24 18:16:55 -04:00
|
|
|
await axios.delete("/api/token/" + id)
|
|
|
|
.then((d) => setApiKeys(apiKeys.filter(k => k.id != d.data.id)))
|
|
|
|
.catch(console.error)
|
2023-12-31 05:41:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="px-10 py-5 mx-5 my-10">
|
|
|
|
<div>
|
2024-06-24 18:16:55 -04:00
|
|
|
<div className="text-xl justify-left mt-10 text-center">API Keys</div>
|
|
|
|
<Table>
|
2023-12-31 05:41:55 -05:00
|
|
|
<TableCaption>A list of your secret API keys.</TableCaption>
|
|
|
|
<TableHeader>
|
|
|
|
<TableRow>
|
|
|
|
<TableHead>Label</TableHead>
|
|
|
|
<TableHead>Token</TableHead>
|
|
|
|
<TableHead>Action</TableHead>
|
|
|
|
</TableRow>
|
|
|
|
</TableHeader>
|
|
|
|
<TableBody>
|
|
|
|
{apiKeys.map((key, index) => (
|
|
|
|
<TableRow key={key.id}>
|
|
|
|
<TableCell className="font-medium">{key.label}</TableCell>
|
2024-06-24 18:16:55 -04:00
|
|
|
<TableCell>{apiKeyViewable == index ? key.id : "*".repeat(key.id.length)}</TableCell>
|
2023-12-31 05:41:55 -05:00
|
|
|
<TableCell>
|
2024-06-24 18:16:55 -04:00
|
|
|
<Button onClick={() => setApiKeyViewable((v) => v != index ? index : -1)}>
|
|
|
|
{apiKeyViewable == index ? "HIDE" : "VIEW"}
|
2023-12-31 05:41:55 -05:00
|
|
|
</Button>
|
2024-06-24 18:16:55 -04:00
|
|
|
<Button onClick={() => onApiKeyDelete(key.id)} className="ml-[10px] bg-red-500 hover:bg-red-700">DELETE</Button>
|
2023-12-31 05:41:55 -05:00
|
|
|
</TableCell>
|
2024-06-24 18:16:55 -04:00
|
|
|
<TableCell></TableCell>
|
2023-12-31 05:41:55 -05:00
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
<TableRow key="ADD">
|
|
|
|
<TableCell className="font-medium"></TableCell>
|
|
|
|
<TableCell></TableCell>
|
2024-06-24 18:16:55 -04:00
|
|
|
<TableCell><Button onClick={() => onApiKeyAdd("Key label")}>ADD</Button></TableCell>
|
2023-12-31 05:41:55 -05:00
|
|
|
</TableRow>
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
export default ApiKeyPage;
|