230 lines
9.1 KiB
TypeScript
230 lines
9.1 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import { motion } from 'framer-motion';
|
|
import { X, Key, Eye, EyeOff, Loader2 } from 'lucide-react';
|
|
import { useBodyScrollLock } from '../hooks/useBodyScrollLock';
|
|
import type { AIProvider, UserPreferences } from '../types';
|
|
|
|
interface ApiKeyModalProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
preferences: UserPreferences;
|
|
onSave: (prefs: Partial<UserPreferences>) => Promise<void>;
|
|
saving: boolean;
|
|
hasAIAccess: boolean;
|
|
}
|
|
|
|
export const ApiKeyModal: React.FC<ApiKeyModalProps> = ({
|
|
isOpen,
|
|
onClose,
|
|
preferences,
|
|
onSave,
|
|
saving,
|
|
hasAIAccess,
|
|
}) => {
|
|
useBodyScrollLock(isOpen);
|
|
const [localProvider, setLocalProvider] = useState<AIProvider>(preferences.aiProvider || 'gemini');
|
|
const [localGeminiKey, setLocalGeminiKey] = useState(preferences.geminiApiKey || '');
|
|
const [localOpenRouterKey, setLocalOpenRouterKey] = useState(preferences.openRouterApiKey || '');
|
|
const [localOpenRouterModel, setLocalOpenRouterModel] = useState(preferences.openRouterModel || '');
|
|
const [showGeminiKey, setShowGeminiKey] = useState(false);
|
|
const [showOpenRouterKey, setShowOpenRouterKey] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
setLocalProvider(preferences.aiProvider || 'gemini');
|
|
setLocalGeminiKey(preferences.geminiApiKey || '');
|
|
setLocalOpenRouterKey(preferences.openRouterApiKey || '');
|
|
setLocalOpenRouterModel(preferences.openRouterModel || '');
|
|
}
|
|
}, [isOpen, preferences]);
|
|
|
|
if (!isOpen) return null;
|
|
|
|
const handleSave = async () => {
|
|
await onSave({
|
|
aiProvider: localProvider,
|
|
geminiApiKey: localGeminiKey || undefined,
|
|
openRouterApiKey: localOpenRouterKey || undefined,
|
|
openRouterModel: localOpenRouterModel || undefined,
|
|
});
|
|
onClose();
|
|
};
|
|
|
|
return (
|
|
<motion.div
|
|
initial={{ opacity: 0 }}
|
|
animate={{ opacity: 1 }}
|
|
exit={{ opacity: 0 }}
|
|
className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
|
|
onClick={onClose}
|
|
>
|
|
<motion.div
|
|
initial={{ scale: 0.9, opacity: 0 }}
|
|
animate={{ scale: 1, opacity: 1 }}
|
|
exit={{ scale: 0.9, opacity: 0 }}
|
|
className="bg-white rounded-2xl max-w-md w-full shadow-xl overflow-hidden"
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
<div className="p-6 border-b border-gray-100 flex items-center justify-between bg-gradient-to-r from-gray-800 to-gray-900">
|
|
<div className="flex items-center gap-3 text-white">
|
|
<div className="p-2 bg-white/20 rounded-xl">
|
|
<Key size={24} />
|
|
</div>
|
|
<div>
|
|
<h2 className="text-xl font-black">AI Settings</h2>
|
|
<p className="text-sm opacity-80">Configure your AI provider</p>
|
|
</div>
|
|
</div>
|
|
<button
|
|
onClick={onClose}
|
|
className="p-2 hover:bg-white/20 rounded-xl transition text-white"
|
|
>
|
|
<X size={24} />
|
|
</button>
|
|
</div>
|
|
|
|
<div className="p-6 bg-gray-50 space-y-4">
|
|
{hasAIAccess ? (
|
|
<>
|
|
<div>
|
|
<label className="block font-bold text-gray-800 mb-2">AI Provider</label>
|
|
<div className="flex bg-gray-200 p-1 rounded-xl">
|
|
<button
|
|
type="button"
|
|
onClick={() => setLocalProvider('gemini')}
|
|
className={`flex-1 py-2 px-3 rounded-lg font-bold text-sm transition-all ${
|
|
localProvider === 'gemini'
|
|
? 'bg-white shadow-sm text-gray-800'
|
|
: 'text-gray-500 hover:text-gray-700'
|
|
}`}
|
|
>
|
|
Gemini
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setLocalProvider('openrouter')}
|
|
className={`flex-1 py-2 px-3 rounded-lg font-bold text-sm transition-all ${
|
|
localProvider === 'openrouter'
|
|
? 'bg-white shadow-sm text-gray-800'
|
|
: 'text-gray-500 hover:text-gray-700'
|
|
}`}
|
|
>
|
|
OpenRouter
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{localProvider === 'gemini' ? (
|
|
<div>
|
|
<label className="block font-bold text-gray-800 mb-2">
|
|
Gemini API Key
|
|
</label>
|
|
<p className="text-sm text-gray-500 mb-2">
|
|
Leave empty to use the system key.
|
|
</p>
|
|
<div className="relative">
|
|
<input
|
|
type={showGeminiKey ? 'text' : 'password'}
|
|
value={localGeminiKey}
|
|
onChange={(e) => setLocalGeminiKey(e.target.value)}
|
|
placeholder="AIza..."
|
|
className="w-full px-4 py-3 pr-12 rounded-xl border-2 border-gray-200 focus:border-theme-primary focus:outline-none text-gray-800"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowGeminiKey(!showGeminiKey)}
|
|
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
|
>
|
|
{showGeminiKey ? <EyeOff size={20} /> : <Eye size={20} />}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<>
|
|
<div>
|
|
<label className="block font-bold text-gray-800 mb-2">
|
|
OpenRouter API Key
|
|
</label>
|
|
<p className="text-sm text-gray-500 mb-2">
|
|
Get your key from{' '}
|
|
<a
|
|
href="https://openrouter.ai/keys"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="text-theme-primary hover:underline"
|
|
>
|
|
openrouter.ai/keys
|
|
</a>
|
|
</p>
|
|
<div className="relative">
|
|
<input
|
|
type={showOpenRouterKey ? 'text' : 'password'}
|
|
value={localOpenRouterKey}
|
|
onChange={(e) => setLocalOpenRouterKey(e.target.value)}
|
|
placeholder="sk-or-..."
|
|
className="w-full px-4 py-3 pr-12 rounded-xl border-2 border-gray-200 focus:border-theme-primary focus:outline-none text-gray-800"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowOpenRouterKey(!showOpenRouterKey)}
|
|
className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
|
>
|
|
{showOpenRouterKey ? <EyeOff size={20} /> : <Eye size={20} />}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<label className="block font-bold text-gray-800 mb-2">
|
|
Model
|
|
</label>
|
|
<p className="text-sm text-gray-500 mb-2">
|
|
Default: google/gemini-3-flash-preview
|
|
</p>
|
|
<input
|
|
type="text"
|
|
value={localOpenRouterModel}
|
|
onChange={(e) => setLocalOpenRouterModel(e.target.value)}
|
|
placeholder="google/gemini-3-flash-preview"
|
|
className="w-full px-4 py-3 rounded-xl border-2 border-gray-200 focus:border-theme-primary focus:outline-none text-gray-800"
|
|
/>
|
|
</div>
|
|
</>
|
|
)}
|
|
</>
|
|
) : (
|
|
<div className="text-center py-4">
|
|
<Key size={32} className="mx-auto mb-3 text-gray-400" />
|
|
<p className="text-gray-500 font-bold">API access not available</p>
|
|
<p className="text-sm text-gray-400 mt-1">Contact an administrator for access</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="p-6 border-t border-gray-100 flex gap-3">
|
|
<button
|
|
onClick={onClose}
|
|
className="flex-1 py-3 rounded-xl font-bold border-2 border-gray-200 text-gray-600 hover:bg-gray-50 transition"
|
|
>
|
|
Cancel
|
|
</button>
|
|
{hasAIAccess && (
|
|
<button
|
|
onClick={handleSave}
|
|
disabled={saving}
|
|
className="flex-1 py-3 rounded-xl font-bold bg-gray-800 text-white hover:bg-gray-700 transition disabled:opacity-50 flex items-center justify-center gap-2"
|
|
>
|
|
{saving ? (
|
|
<>
|
|
<Loader2 size={20} className="animate-spin" />
|
|
Saving...
|
|
</>
|
|
) : (
|
|
'Save'
|
|
)}
|
|
</button>
|
|
)}
|
|
</div>
|
|
</motion.div>
|
|
</motion.div>
|
|
);
|
|
};
|