Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 14x 1x 1x 1x 1x 1x 1x 1x 14x | import { useState } from 'react'
import { toast } from 'sonner'
import { api, type AIStatus, type AIModel } from '../api/client'
import { useAIStatus } from '../contexts/AIStatusContext'
interface UseAPIKeyHandlersOptions {
onAIStatusChange: (status: AIStatus) => void
onModelsChange: (models: AIModel[]) => void
}
interface UseAPIKeyHandlersReturn {
apiKey: string
setApiKey: (key: string) => void
testingKey: boolean
savingKey: boolean
handleTestApiKey: () => Promise<void>
handleSaveApiKey: () => Promise<void>
}
export function useAPIKeyHandlers({
onAIStatusChange,
onModelsChange,
}: UseAPIKeyHandlersOptions): UseAPIKeyHandlersReturn {
const globalAIStatus = useAIStatus()
const [apiKey, setApiKey] = useState('')
const [testingKey, setTestingKey] = useState(false)
const [savingKey, setSavingKey] = useState(false)
const handleTestApiKey = async () => {
Iif (!apiKey.trim()) {
toast.error('Please enter an API key')
return
}
setTestingKey(true)
try {
const result = await api.ai.testApiKey(apiKey.trim())
Iif (result.success) {
toast.success(result.message)
} else {
toast.error(result.message)
}
} catch (error) {
console.error('Failed to test API key:', error)
toast.error('Failed to test API key')
} finally {
setTestingKey(false)
}
}
const handleSaveApiKey = async () => {
Iif (!apiKey.trim()) {
toast.error('Please enter an API key')
return
}
setSavingKey(true)
try {
const result = await api.ai.saveApiKey(apiKey.trim())
Iif (result.success) {
toast.success(result.message)
setApiKey('')
const statusData = await api.ai.status()
onAIStatusChange(statusData)
await globalAIStatus.refresh()
const modelsData = await api.ai.models()
onModelsChange(modelsData)
} else {
toast.error(result.message)
}
} catch (error) {
console.error('Failed to save API key:', error)
toast.error('Failed to save API key')
} finally {
setSavingKey(false)
}
}
return {
apiKey,
setApiKey,
testingKey,
savingKey,
handleTestApiKey,
handleSaveApiKey,
}
}
|