mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-02-01 08:13:37 +00:00
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
57 lines
1.7 KiB
TypeScript
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 };
|
|
}
|