Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions client/src/components/keys/add-key-form.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useState } from 'react'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { apiFetch } from '@/lib/api'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import { FieldError } from '@/components/ui/field-error'
import type { Platform } from '../../../../shared/types'
import type { ApiKey, Platform } from '../../../../shared/types'
import { useI18n } from '@/i18n'
import { toast } from '@/lib/toast'
import { GetKeyLink, PLATFORMS } from './shared'
Expand All @@ -18,6 +19,10 @@ import { GetKeyLink, PLATFORMS } from './shared'
export function AddKeyForm({ onSuccess }: { onSuccess: () => void }) {
const { t } = useI18n()
const queryClient = useQueryClient()
const { data: keys = [] } = useQuery<ApiKey[]>({
queryKey: ['keys'],
queryFn: () => apiFetch('/api/keys'),
})
const [platform, setPlatform] = useState<Platform | ''>('')
const [apiKey, setApiKey] = useState('')
const [accountId, setAccountId] = useState('')
Expand All @@ -43,6 +48,10 @@ export function AddKeyForm({ onSuccess }: { onSuccess: () => void }) {

const needsAccountId = platform === 'cloudflare'
const isKeyless = PLATFORMS.find(p => p.value === platform)?.keyless ?? false
const keyCountByPlatform = keys.reduce<Partial<Record<Platform, number>>>((counts, key) => {
counts[key.platform] = (counts[key.platform] ?? 0) + 1
return counts
}, {})

// Field-level validation: the submit stays clickable and reveals what is
// missing instead of being silently disabled.
Expand Down Expand Up @@ -73,7 +82,12 @@ export function AddKeyForm({ onSuccess }: { onSuccess: () => void }) {
</SelectTrigger>
<SelectContent>
{PLATFORMS.map(p => (
<SelectItem key={p.value} value={p.value}>{p.label}</SelectItem>
<SelectItem key={p.value} value={p.value}>
<Badge variant="secondary" className="min-w-5 px-1.5 tabular-nums">
{keyCountByPlatform[p.value] ?? 0}
</Badge>
<span>{p.label}</span>
</SelectItem>
))}
</SelectContent>
</Select>
Expand Down