"use client"; import { ThreeDotsLoader } from "@/components/Loading"; import { AdminPageTitle } from "@/components/admin/Title"; import { KeyIcon } from "@/components/icons/icons"; import { errorHandlingFetcher } from "@/lib/fetcher"; import { ErrorCallout } from "@/components/ErrorCallout"; import useSWR, { mutate } from "swr"; import { Separator } from "@/components/ui/separator"; import { TableBody, TableCell, TableHead, TableHeader, TableRow, Table, } from "@/components/ui/table"; import Text from "@/components/ui/text"; import Title from "@/components/ui/title"; import { usePopup } from "@/components/admin/connectors/Popup"; import { useState } from "react"; import { DeleteButton } from "@/components/DeleteButton"; import { FiCopy, FiEdit2, FiRefreshCw } from "react-icons/fi"; import { Modal } from "@/components/Modal"; import { Spinner } from "@/components/Spinner"; import { deleteApiKey, regenerateApiKey } from "./lib"; import { OnyxApiKeyForm } from "./OnyxApiKeyForm"; import { APIKey } from "./types"; import CreateButton from "@/components/ui/createButton"; const API_KEY_TEXT = `API Keys allow you to access Onyx APIs programmatically. Click the button below to generate a new API Key.`; function NewApiKeyModal({ apiKey, onClose, }: { apiKey: string; onClose: () => void; }) { const [copyClicked, setCopyClicked] = useState(false); return (
Make sure you copy your new API key. You won’t be able to see this key again.
{apiKey}
{ setCopyClicked(true); navigator.clipboard.writeText(apiKey); setTimeout(() => { setCopyClicked(false); }, 10000); }} >
{copyClicked && ( API Key copied! )}
); } function Main() { const { popup, setPopup } = usePopup(); const { data: apiKeys, isLoading, error, } = useSWR("/api/admin/api-key", errorHandlingFetcher); const [fullApiKey, setFullApiKey] = useState(null); const [keyIsGenerating, setKeyIsGenerating] = useState(false); const [showCreateUpdateForm, setShowCreateUpdateForm] = useState(false); const [selectedApiKey, setSelectedApiKey] = useState(); const handleEdit = (apiKey: APIKey) => { setSelectedApiKey(apiKey); setShowCreateUpdateForm(true); }; if (isLoading) { return ; } if (!apiKeys || error) { return ( ); } const newApiKeyButton = ( setShowCreateUpdateForm(true)} text="Create API Key" /> ); if (apiKeys.length === 0) { return (
{popup} {API_KEY_TEXT} {newApiKeyButton} {showCreateUpdateForm && ( { setFullApiKey(apiKey.api_key); }} onClose={() => { setShowCreateUpdateForm(false); setSelectedApiKey(undefined); mutate("/api/admin/api-key"); }} setPopup={setPopup} apiKey={selectedApiKey} /> )}
); } return (
{popup} {fullApiKey && ( setFullApiKey(null)} /> )} {keyIsGenerating && } {API_KEY_TEXT} {newApiKeyButton} Existing API Keys Name API Key Role Regenerate Delete {apiKeys.map((apiKey) => (
handleEdit(apiKey)} > {apiKey.api_key_name || null}
{apiKey.api_key_display} {apiKey.api_key_role.toUpperCase()}
{ setKeyIsGenerating(true); const response = await regenerateApiKey(apiKey); setKeyIsGenerating(false); if (!response.ok) { const errorMsg = await response.text(); setPopup({ type: "error", message: `Failed to regenerate API Key: ${errorMsg}`, }); return; } const newKey = (await response.json()) as APIKey; setFullApiKey(newKey.api_key); mutate("/api/admin/api-key"); }} > Refresh
{ const response = await deleteApiKey(apiKey.api_key_id); if (!response.ok) { const errorMsg = await response.text(); setPopup({ type: "error", message: `Failed to delete API Key: ${errorMsg}`, }); return; } mutate("/api/admin/api-key"); }} />
))}
{showCreateUpdateForm && ( { setFullApiKey(apiKey.api_key); }} onClose={() => { setShowCreateUpdateForm(false); setSelectedApiKey(undefined); mutate("/api/admin/api-key"); }} setPopup={setPopup} apiKey={selectedApiKey} /> )}
); } export default function Page() { return (
} />
); }