Files
automaker/apps/ui/src/components/views/setup-view/hooks/use-token-save.ts
SuperComboGamer 8d578558ff style: fix formatting with Prettier
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-21 20:31:57 -05:00

57 lines
1.7 KiB
TypeScript

import { useState, useCallback } from 'react';
import { toast } from 'sonner';
import { getElectronAPI } from '@/lib/electron';
interface UseTokenSaveOptions {
provider: string; // e.g., "anthropic_oauth_token", "anthropic", "openai"
onSuccess?: () => void;
}
export function useTokenSave({ provider, onSuccess }: UseTokenSaveOptions) {
const [isSaving, setIsSaving] = useState(false);
const saveToken = useCallback(
async (tokenValue: string) => {
if (!tokenValue.trim()) {
toast.error('Please enter a valid token');
return false;
}
setIsSaving(true);
try {
const api = getElectronAPI();
const setupApi = api.setup;
if (setupApi?.storeApiKey) {
const result = await setupApi.storeApiKey(provider, tokenValue);
console.log(`[Token Save] Store result for ${provider}:`, result);
if (result.success) {
const tokenType = provider.includes('oauth') ? 'subscription token' : 'API key';
toast.success(`${tokenType} saved successfully`);
onSuccess?.();
return true;
} else {
toast.error('Failed to save token', { description: result.error });
return false;
}
} else {
// Web mode fallback - just show success
toast.success('Token saved');
onSuccess?.();
return true;
}
} catch (error) {
console.error(`[Token Save] Failed to save ${provider}:`, error);
toast.error('Failed to save token');
return false;
} finally {
setIsSaving(false);
}
},
[provider, onSuccess]
);
return { isSaving, saveToken };
}