|
1 | 1 | "use client"; |
2 | 2 |
|
| 3 | +import { |
| 4 | + ArrowClockwiseIcon, |
| 5 | + BookOpenIcon, |
| 6 | + KeyIcon, |
| 7 | + PlusIcon, |
| 8 | + ShieldCheckIcon, |
| 9 | +} from "@phosphor-icons/react"; |
| 10 | +import { useQuery } from "@tanstack/react-query"; |
3 | 11 | import { useState } from "react"; |
4 | 12 | import { ApiKeyCreateDialog } from "@/components/organizations/api-key-create-dialog"; |
5 | 13 | import { ApiKeyDetailDialog } from "@/components/organizations/api-key-detail-dialog"; |
6 | | -import { ApiKeyList } from "@/components/organizations/api-key-list"; |
7 | | - |
| 14 | +import { Button } from "@/components/ui/button"; |
| 15 | +import { Skeleton } from "@/components/ui/skeleton"; |
8 | 16 | import type { Organization } from "@/hooks/use-organizations"; |
| 17 | +import { orpc } from "@/lib/orpc"; |
| 18 | +import { ApiKeyRow } from "./api-key-row"; |
9 | 19 |
|
10 | 20 | interface ApiKeySettingsProps { |
11 | 21 | organization: Organization; |
12 | 22 | } |
13 | 23 |
|
| 24 | +function SkeletonRow() { |
| 25 | + return ( |
| 26 | + <div className="grid grid-cols-[auto_1fr_auto_auto] items-center gap-4 px-5 py-4"> |
| 27 | + <Skeleton className="h-10 w-10 rounded" /> |
| 28 | + <div className="space-y-2"> |
| 29 | + <Skeleton className="h-4 w-32" /> |
| 30 | + <Skeleton className="h-3 w-48" /> |
| 31 | + </div> |
| 32 | + <Skeleton className="h-6 w-16 rounded-full" /> |
| 33 | + <Skeleton className="h-4 w-4" /> |
| 34 | + </div> |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +function ApiKeysSkeleton() { |
| 39 | + return ( |
| 40 | + <div className="h-full lg:grid lg:grid-cols-[1fr_18rem]"> |
| 41 | + <div className="divide-y border-b lg:border-b-0 lg:border-r"> |
| 42 | + <SkeletonRow /> |
| 43 | + <SkeletonRow /> |
| 44 | + <SkeletonRow /> |
| 45 | + </div> |
| 46 | + <div className="space-y-4 bg-muted/30 p-5"> |
| 47 | + <Skeleton className="h-10 w-full" /> |
| 48 | + <Skeleton className="h-18 w-full rounded" /> |
| 49 | + <Skeleton className="h-10 w-full" /> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function EmptyState({ onCreateNew }: { onCreateNew: () => void }) { |
| 56 | + return ( |
| 57 | + <div className="flex h-full flex-col items-center justify-center p-8 text-center"> |
| 58 | + <div className="mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-primary/10"> |
| 59 | + <KeyIcon className="text-primary" size={28} weight="duotone" /> |
| 60 | + </div> |
| 61 | + <h3 className="mb-1 font-semibold text-lg">No API keys yet</h3> |
| 62 | + <p className="mb-6 max-w-sm text-muted-foreground text-sm"> |
| 63 | + Create your first API key to start integrating with our platform |
| 64 | + </p> |
| 65 | + <Button onClick={onCreateNew}> |
| 66 | + <PlusIcon className="mr-2" size={16} /> |
| 67 | + Create API Key |
| 68 | + </Button> |
| 69 | + </div> |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +function ErrorState({ onRetry }: { onRetry: () => void }) { |
| 74 | + return ( |
| 75 | + <div className="flex h-full flex-col items-center justify-center p-8 text-center"> |
| 76 | + <div className="mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-destructive/10"> |
| 77 | + <KeyIcon className="text-destructive" size={28} weight="duotone" /> |
| 78 | + </div> |
| 79 | + <h3 className="mb-1 font-semibold text-lg">Failed to load</h3> |
| 80 | + <p className="mb-6 max-w-sm text-muted-foreground text-sm"> |
| 81 | + Something went wrong while loading your API keys |
| 82 | + </p> |
| 83 | + <Button onClick={onRetry} variant="outline"> |
| 84 | + <ArrowClockwiseIcon className="mr-2" size={16} /> |
| 85 | + Try again |
| 86 | + </Button> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
14 | 91 | export function ApiKeySettings({ organization }: ApiKeySettingsProps) { |
15 | | - // API Key dialog state |
16 | | - const [showCreateApiKeyDialog, setShowCreateApiKeyDialog] = useState(false); |
17 | | - const [showApiKeyDetailDialog, setShowApiKeyDetailDialog] = useState(false); |
18 | | - const [apiKeyId, setApiKeyId] = useState<string | null>(null); |
| 92 | + const [showCreateDialog, setShowCreateDialog] = useState(false); |
| 93 | + const [showDetailDialog, setShowDetailDialog] = useState(false); |
| 94 | + const [selectedKeyId, setSelectedKeyId] = useState<string | null>(null); |
| 95 | + |
| 96 | + const { data, isLoading, isError, refetch } = useQuery({ |
| 97 | + ...orpc.apikeys.list.queryOptions({ input: { organizationId: organization.id } }), |
| 98 | + refetchOnMount: true, |
| 99 | + refetchOnReconnect: true, |
| 100 | + staleTime: 0, |
| 101 | + }); |
19 | 102 |
|
20 | | - // API Key handlers |
21 | | - const handleCreateApiKey = () => { |
22 | | - setShowCreateApiKeyDialog(true); |
23 | | - }; |
| 103 | + const items = data ?? []; |
| 104 | + const activeCount = items.filter((k) => k.enabled && !k.revokedAt).length; |
24 | 105 |
|
25 | | - const handleSelectApiKey = (id: string) => { |
26 | | - setApiKeyId(id); |
27 | | - setShowApiKeyDetailDialog(true); |
28 | | - }; |
| 106 | + if (isLoading) return <ApiKeysSkeleton />; |
| 107 | + if (isError) return <ErrorState onRetry={refetch} />; |
| 108 | + if (items.length === 0) return <EmptyState onCreateNew={() => setShowCreateDialog(true)} />; |
29 | 109 |
|
30 | 110 | return ( |
31 | | - <div className="h-full p-6"> |
32 | | - <ApiKeyList |
33 | | - onCreateNew={handleCreateApiKey} |
34 | | - onSelect={handleSelectApiKey} |
35 | | - organizationId={organization.id} |
36 | | - /> |
| 111 | + <> |
| 112 | + <div className="h-full lg:grid lg:grid-cols-[1fr_18rem]"> |
| 113 | + {/* Keys List */} |
| 114 | + <div className="flex flex-col border-b lg:border-b-0 lg:border-r"> |
| 115 | + <div className="flex-1 divide-y overflow-y-auto"> |
| 116 | + {items.map((apiKey) => ( |
| 117 | + <ApiKeyRow |
| 118 | + apiKey={apiKey} |
| 119 | + key={apiKey.id} |
| 120 | + onSelect={(id) => { |
| 121 | + setSelectedKeyId(id); |
| 122 | + setShowDetailDialog(true); |
| 123 | + }} |
| 124 | + /> |
| 125 | + ))} |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + |
| 129 | + {/* Sidebar */} |
| 130 | + <aside className="flex flex-col gap-4 bg-muted/30 p-5"> |
| 131 | + {/* Create Button */} |
| 132 | + <Button className="w-full" onClick={() => setShowCreateDialog(true)}> |
| 133 | + <PlusIcon className="mr-2" size={16} /> |
| 134 | + Create New Key |
| 135 | + </Button> |
| 136 | + |
| 137 | + {/* Stats Card */} |
| 138 | + <div className="flex items-center gap-3 rounded border bg-background p-4"> |
| 139 | + <div className="flex h-10 w-10 items-center justify-center rounded bg-primary/10"> |
| 140 | + <ShieldCheckIcon className="text-primary" size={20} weight="duotone" /> |
| 141 | + </div> |
| 142 | + <div> |
| 143 | + <p className="font-semibold tabular-nums"> |
| 144 | + {activeCount} <span className="font-normal text-muted-foreground">/ {items.length}</span> |
| 145 | + </p> |
| 146 | + <p className="text-muted-foreground text-sm">Active keys</p> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + |
| 150 | + {/* Actions */} |
| 151 | + <Button asChild className="w-full justify-start" variant="outline"> |
| 152 | + <a |
| 153 | + href="https://www.databuddy.cc/docs/api-keys" |
| 154 | + rel="noopener noreferrer" |
| 155 | + target="_blank" |
| 156 | + > |
| 157 | + <BookOpenIcon className="mr-2" size={16} /> |
| 158 | + Documentation |
| 159 | + </a> |
| 160 | + </Button> |
| 161 | + |
| 162 | + {/* Tips */} |
| 163 | + <div className="mt-auto rounded border border-dashed bg-background/50 p-4"> |
| 164 | + <p className="mb-2 font-medium text-sm">Security reminder</p> |
| 165 | + <p className="text-muted-foreground text-xs leading-relaxed"> |
| 166 | + Keep your API keys secure. Never share them publicly or commit them to version control. |
| 167 | + </p> |
| 168 | + </div> |
| 169 | + </aside> |
| 170 | + </div> |
37 | 171 |
|
38 | | - {/* API Key Dialogs */} |
39 | 172 | <ApiKeyCreateDialog |
40 | | - onOpenChange={setShowCreateApiKeyDialog} |
41 | | - open={showCreateApiKeyDialog} |
| 173 | + onOpenChange={setShowCreateDialog} |
| 174 | + open={showCreateDialog} |
42 | 175 | organizationId={organization.id} |
43 | 176 | /> |
44 | 177 | <ApiKeyDetailDialog |
45 | | - keyId={apiKeyId} |
46 | | - onOpenChange={setShowApiKeyDetailDialog} |
47 | | - open={showApiKeyDetailDialog} |
| 178 | + keyId={selectedKeyId} |
| 179 | + onOpenChangeAction={setShowDetailDialog} |
| 180 | + open={showDetailDialog} |
48 | 181 | /> |
49 | | - </div> |
| 182 | + </> |
50 | 183 | ); |
51 | 184 | } |
0 commit comments