Open source CapCut alternative.
TypeScript
feat(site): streamline API keys settings page
David Le3e0a3ff7c3d6
diff --git a/site/src/app/app/settings/_components/ApiKeysManager.tsx b/site/src/app/app/settings/_components/ApiKeysManager.tsxindex c2440916..a448932e 100644--- a/site/src/app/app/settings/_components/ApiKeysManager.tsx+++ b/site/src/app/app/settings/_components/ApiKeysManager.tsx@@ -1,5 +1,7 @@"use client";+import { Info } from "lucide-react";+import Link from "next/link";import { useState } from "react";import {@@ -8,15 +10,8 @@ import {useDeleteApiKey,type CreatedApiKey,} from "@/queries/apiKeys";-import { useSubscription, useUsage } from "@/queries/billing";import { Button } from "@/components/ui/button";-import {- Card,- CardContent,- CardDescription,- CardHeader,- CardTitle,-} from "@/components/ui/card";+import { Card, CardContent } from "@/components/ui/card";import {Dialog,DialogContent,@@ -38,21 +33,14 @@ import {} from "@/components/ui/table";export function ApiKeysManager() {- const subscription = useSubscription();- const usage = useUsage();const keys = useApiKeys();const createKey = useCreateApiKey();const deleteKey = useDeleteApiKey();const [name, setName] = useState("");+ const [createOpen, setCreateOpen] = useState(false);const [createdKey, setCreatedKey] = useState<CreatedApiKey | null>(null);- // Keys can always be created, but only return data when the user has capacity:- // an active subscription or remaining extra calls.- const hasCapacity =- (subscription.data?.isActive ?? false) ||- (usage.data?.extraRemaining ?? 0) > 0;-const handleCreate = () => {const trimmed = name.trim();if (!trimmed) {@@ -62,91 +50,105 @@ export function ApiKeysManager() {onSuccess: (result) => {setCreatedKey(result);setName("");+ setCreateOpen(false);},});};return (- <Card>- <CardHeader>- <CardTitle>Your keys</CardTitle>- <CardDescription>- Secrets are shown only once at creation. Store them securely.- </CardDescription>- </CardHeader>- <CardContent className="space-y-6">- <div className="space-y-3">- <div className="flex items-end gap-3">- <div className="grid gap-1.5">- <Label htmlFor="key-name">New key name</Label>- <Input- id="key-name"- placeholder="Production server"- value={name}- onChange={(event) => setName(event.target.value)}- />- </div>+ <div className="space-y-8">+ <div className="flex items-center justify-between gap-4">+ <h1 className="text-2xl font-semibold">API Keys</h1>+ <Button onClick={() => setCreateOpen(true)}>Create key</Button>+ </div>++ <Card>+ <CardContent className="pt-6">+ {keys.isLoading ? (+ <Skeleton className="h-24 w-full" />+ ) : keys.data && keys.data.length > 0 ? (+ <Table>+ <TableHeader>+ <TableRow>+ <TableHead>Name</TableHead>+ <TableHead>Key</TableHead>+ <TableHead>Created</TableHead>+ <TableHead>Last used</TableHead>+ <TableHead className="text-right">Actions</TableHead>+ </TableRow>+ </TableHeader>+ <TableBody>+ {keys.data.map((key) => (+ <TableRow key={key.id}>+ <TableCell>{key.name ?? "—"}</TableCell>+ <TableCell className="font-mono text-xs">+ {(key.prefix ?? "") + (key.start ?? "")}…+ </TableCell>+ <TableCell>+ {new Date(key.createdAt).toLocaleDateString()}+ </TableCell>+ <TableCell>+ {key.lastRequest+ ? new Date(key.lastRequest).toLocaleDateString()+ : "Never"}+ </TableCell>+ <TableCell className="text-right">+ <Button+ disabled={deleteKey.isPending}+ onClick={() => deleteKey.mutate(key.id)}+ size="sm"+ variant="destructive"+ >+ Revoke+ </Button>+ </TableCell>+ </TableRow>+ ))}+ </TableBody>+ </Table>+ ) : (+ <p className="text-sm text-muted-foreground">No API keys yet.</p>+ )}+ </CardContent>+ </Card>++ <Link+ href="/donkeyvision#api"+ className="inline-flex items-center gap-1.5 text-sm text-muted-foreground transition-colors hover:text-foreground"+ >+ <Info className="h-4 w-4" />+ Learn how to use your API keys+ </Link>++ <Dialog open={createOpen} onOpenChange={setCreateOpen}>+ <DialogContent aria-describedby={undefined}>+ <DialogHeader>+ <DialogTitle>Create API key</DialogTitle>+ </DialogHeader>+ <div className="grid gap-1.5">+ <Label htmlFor="key-name">Name</Label>+ <Input+ id="key-name"+ required+ value={name}+ onChange={(event) => setName(event.target.value)}+ onKeyDown={(event) => {+ if (event.key === "Enter") {+ handleCreate();+ }+ }}+ />+ </div>+ <DialogFooter><Buttondisabled={createKey.isPending || !name.trim()}onClick={handleCreate}>{createKey.isPending ? "Creating…" : "Create key"}</Button>- </div>- {!hasCapacity ? (- <p className="text-sm text-muted-foreground">- Keys won't return data until you have available calls —- subscribe or use your free calls.- </p>- ) : null}- </div>-- {keys.isLoading ? (- <Skeleton className="h-24 w-full" />- ) : keys.data && keys.data.length > 0 ? (- <Table>- <TableHeader>- <TableRow>- <TableHead>Name</TableHead>- <TableHead>Key</TableHead>- <TableHead>Created</TableHead>- <TableHead>Last used</TableHead>- <TableHead className="text-right">Actions</TableHead>- </TableRow>- </TableHeader>- <TableBody>- {keys.data.map((key) => (- <TableRow key={key.id}>- <TableCell>{key.name ?? "—"}</TableCell>- <TableCell className="font-mono text-xs">- {(key.prefix ?? "") + (key.start ?? "")}…- </TableCell>- <TableCell>- {new Date(key.createdAt).toLocaleDateString()}- </TableCell>- <TableCell>- {key.lastRequest- ? new Date(key.lastRequest).toLocaleDateString()- : "Never"}- </TableCell>- <TableCell className="text-right">- <Button- disabled={deleteKey.isPending}- onClick={() => deleteKey.mutate(key.id)}- size="sm"- variant="destructive"- >- Revoke- </Button>- </TableCell>- </TableRow>- ))}- </TableBody>- </Table>- ) : (- <p className="text-sm text-muted-foreground">No API keys yet.</p>- )}- </CardContent>+ </DialogFooter>+ </DialogContent>+ </Dialog><Dialogopen={createdKey !== null}@@ -160,7 +162,7 @@ export function ApiKeysManager() {<DialogHeader><DialogTitle>API key created</DialogTitle><DialogDescription>- Copy this secret now. You won't be able to see it again.+ Secrets are shown only once at creation. Store them securely.</DialogDescription></DialogHeader><div className="rounded-md border bg-muted p-3 font-mono text-sm break-all">@@ -181,6 +183,6 @@ export function ApiKeysManager() {</DialogFooter></DialogContent></Dialog>- </Card>+ </div>);}diff --git a/site/src/app/app/settings/api-keys/page.tsx b/site/src/app/app/settings/api-keys/page.tsxindex aada6845..4620c155 100644--- a/site/src/app/app/settings/api-keys/page.tsx+++ b/site/src/app/app/settings/api-keys/page.tsx@@ -3,20 +3,5 @@import { ApiKeysManager } from "@/app/app/settings/_components/ApiKeysManager";export default function ApiKeysPage() {- return (- <div className="space-y-8">- <div>- <h1 className="text-2xl font-semibold">API keys</h1>- <p className="mt-1 text-sm text-muted-foreground">- Create keys to authenticate Vision API requests. Send a key as a bearer- token:{" "}- <code className="rounded bg-muted px-1 py-0.5">- Authorization: Bearer <key>- </code>- .- </p>- </div>- <ApiKeysManager />- </div>- );+ return <ApiKeysManager />;}