import {
Button,
Card,
Container,
Group,
Stack,
Table,
Text,
TextInput,
Title,
ScrollArea,
ActionIcon,
Tooltip,
Divider,
Loader,
Badge,
useMantineTheme,
} from "@mantine/core";
import { useEffect, useState } from "react";
import {
IconPlus,
IconCopy,
IconTrash,
IconKey,
IconDatabase,
IconLock,
} from "@tabler/icons-react";
import { showNotification } from "@mantine/notifications";
import apiFetch from "@/lib/apiFetch";
export default function ApiKeyPage() {
return (
API Key Management
);
}
function CreateApiKey() {
const theme = useMantineTheme();
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [expiredAt, setExpiredAt] = useState("");
const [loading, setLoading] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!name || !expiredAt) {
showNotification({
color: "red",
title: "Missing Information",
message: "Please provide a name and expiration date.",
});
return;
}
setLoading(true);
const res = await apiFetch.api.apikey.create.post({
name,
description,
expiredAt,
});
if (res.status === 200) {
setName("");
setDescription("");
setExpiredAt("");
showNotification({
title: "API Key Created",
message: "Your new API key is now active and ready to use.",
color: "teal",
});
} else {
showNotification({
title: "Error",
message: "Failed to create API key. Please try again.",
color: "red",
});
}
setLoading(false);
};
return (
Create New API Key
);
}
function ListApiKey() {
const theme = useMantineTheme();
const [apiKeys, setApiKeys] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchApiKeys = async () => {
const res = await apiFetch.api.apikey.list.get();
if (res.status === 200) {
setApiKeys(res.data?.apiKeys || []);
}
setLoading(false);
};
fetchApiKeys();
}, []);
const handleDelete = async (id: string) => {
const res = await apiFetch.api.apikey.delete.delete({ id });
if (res.status === 200) {
setApiKeys(apiKeys.filter((api: any) => api.id !== id));
showNotification({
title: "API Key Deleted",
message: "The API key has been successfully removed.",
color: "teal",
});
}
};
const handleCopy = (key: string) => {
navigator.clipboard.writeText(key);
showNotification({
title: "Copied",
message: "API key copied to clipboard.",
color: "cyan",
});
};
return (
Active API Keys
{apiKeys.length} Active
{loading ? (
) : apiKeys.length === 0 ? (
No API keys created yet.
) : (
Name
Description
Expires
Created
Updated
Actions
{apiKeys.map((apiKey: any, index: number) => (
(e.currentTarget.style.backgroundColor =
"rgba(0,255,200,0.05)")
}
onMouseLeave={(e) =>
(e.currentTarget.style.backgroundColor =
"rgba(255,255,255,0.03)")
}
>
{apiKey.name}
{apiKey.description || "-"}
{new Date(apiKey.expiredAt).toLocaleDateString()}
{new Date(apiKey.createdAt).toLocaleDateString()}
{new Date(apiKey.updatedAt).toLocaleDateString()}
handleCopy(apiKey.key)}
radius="md"
>
handleDelete(apiKey.id)}
radius="md"
>
))}
)}
);
}