Add dedicated settings pages

This commit is contained in:
Alex Gleason committed 2025-09-13 14:47:53 -05:00
1 parent e6864cf8f3
commit a87df5d0aa
8 files changed
+170 -293

No files matched your search

+391
View File
@@ -0,0 +1,391 @@
import { useState } from 'react';
import { Check, Bot, ArrowLeft } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Card, CardContent } from '@/components/ui/card';
import { Separator } from '@/components/ui/separator';
import { Badge } from '@/components/ui/badge';
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { useAISettings } from '@/hooks/useAISettings';
import { AppLayout } from '@/components/AppLayout';
import { useNavigate } from 'react-router-dom';
import type { AIConnection } from '@/contexts/AISettingsContext';
interface PresetProvider {
id: string;
name: string;
baseURL: string;
apiKeysURL?: string;
nostr?: boolean;
}
const PRESET_PROVIDERS: PresetProvider[] = [
{
id: "lemon",
name: "Lemon",
baseURL: "https://internal-lucienne-nublar-6932a6f0.koyeb.app/v1",
nostr: true,
},
{
id: "openrouter",
name: "OpenRouter",
baseURL: "https://openrouter.ai/api/v1",
apiKeysURL: "https://openrouter.ai/settings/keys",
},
{
id: "ppq",
name: "PayPerQ",
baseURL: "https://api.ppq.ai",
apiKeysURL: "https://ppq.ai/api-docs",
},
{
id: "zai",
name: "Z.ai",
baseURL: "https://api.z.ai/api/paas/v4",
apiKeysURL: "https://z.ai/manage-apikey/apikey-list",
},
{
id: "moonshot",
name: "Moonshot",
baseURL: "https://api.moonshot.ai/v1",
apiKeysURL: "https://platform.moonshot.ai/console/api-keys",
},
{
id: "openai",
name: "OpenAI",
baseURL: "https://api.openai.com/v1",
apiKeysURL: "https://platform.openai.com/api-keys",
},
{
id: "anthropic",
name: "Anthropic",
baseURL: "https://api.anthropic.com/v1",
apiKeysURL: "https://console.anthropic.com/settings/keys",
},
{
id: "xai",
name: "xAI",
baseURL: "https://api.x.ai/v1",
apiKeysURL: "https://console.x.ai",
},
];
export function AISettings() {
const { settings, addProvider, removeProvider, updateProvider } = useAISettings();
const navigate = useNavigate();
const [customProviderName, setCustomProviderName] = useState('');
const [customBaseURL, setCustomBaseURL] = useState('');
const [customApiKey, setCustomApiKey] = useState('');
const [customAuthMethod, setCustomAuthMethod] = useState<'api-key' | 'nostr'>('api-key');
const [presetApiKeys, setPresetApiKeys] = useState<Record<string, string>>({});
const handleAddPresetProvider = (preset: PresetProvider) => {
const apiKey = presetApiKeys[preset.id] as string | undefined;
const newProvider: AIConnection = {
baseURL: preset.baseURL,
};
if (typeof apiKey === 'string') {
newProvider.apiKey = apiKey.trim();
}
if (typeof preset.nostr === 'boolean') {
newProvider.nostr = preset.nostr;
}
// Auto-save: Add provider immediately to persistent storage
addProvider(preset.id, newProvider);
// Clear the API key input for this preset
setPresetApiKeys(prev => ({
...prev,
[preset.id]: '',
}));
};
const handleAddCustomProvider = () => {
if (!customProviderName.trim() || !customBaseURL.trim()) return;
const newProvider: AIConnection = {
baseURL: customBaseURL.trim(),
apiKey: customAuthMethod === 'nostr' ? undefined : customApiKey.trim(),
nostr: customAuthMethod === 'nostr' || undefined,
};
// Auto-save: Add provider immediately to persistent storage
addProvider(customProviderName.trim(), newProvider);
setCustomProviderName('');
setCustomBaseURL('');
setCustomApiKey('');
setCustomAuthMethod('api-key');
};
const handleRemoveProvider = (name: string) => {
// Auto-save: Remove provider immediately from persistent storage
removeProvider(name);
};
const handleUpdateProvider = (name: string, connection: Partial<AIConnection>) => {
// Auto-save: Update provider immediately in persistent storage
updateProvider(name, connection);
};
const configuredProviderIds = Object.keys(settings.providers);
const availablePresets = PRESET_PROVIDERS.filter(preset => !configuredProviderIds.includes(preset.id));
return (
<AppLayout title="AI Settings" showSidebar={true}>
<div className="max-w-4xl mx-auto">
<Card className="bg-white">
<CardContent className="p-6 space-y-6">
<div className="space-y-2">
<div className="flex items-center gap-3">
<Button
variant="ghost"
size="sm"
onClick={() => navigate('/settings')}
className="h-8 w-8 p-0"
>
<ArrowLeft className="h-4 w-4" />
</Button>
<h1 className="text-3xl font-bold tracking-tight flex items-center gap-3">
<Bot className="h-8 w-8 text-primary" />
AI Settings
</h1>
</div>
<p className="text-muted-foreground ml-11">
Configure AI providers by adding your API keys. Settings are automatically saved and stored locally in your browser.
</p>
</div>
<div className="space-y-6">
{/* Configured Providers */}
{configuredProviderIds.length > 0 && (
<div className="space-y-3">
<h4 className="text-sm font-medium">Configured Providers</h4>
<Accordion type="multiple" className="w-full space-y-2">
{configuredProviderIds.map((providerId) => {
const preset = PRESET_PROVIDERS.find(p => p.id === providerId);
const provider = settings.providers[providerId];
const isCustom = !preset;
return (
<AccordionItem key={providerId} value={providerId} className="border rounded-lg">
<AccordionTrigger className="px-4 py-3 hover:no-underline">
<div className="flex items-center justify-between w-full mr-3">
<div className="flex items-center gap-2">
<span className="font-medium">
{preset?.name || providerId}
</span>
{isCustom && <Badge variant="outline">Custom</Badge>}
</div>
<Button
variant="ghost"
size="sm"
onClick={(e) => {
e.stopPropagation();
handleRemoveProvider(providerId);
}}
className="h-8 w-8 p-0 text-muted-foreground hover:text-destructive"
>
<svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</Button>
</div>
</AccordionTrigger>
<AccordionContent className="px-4 pb-4">
<div className="space-y-3">
<div className="grid gap-2">
<Label htmlFor={`${providerId}-baseURL`}>Base URL</Label>
<Input
id={`${providerId}-baseURL`}
placeholder="https://api.example.com/v1"
value={provider?.baseURL || ''}
onChange={(e) => handleUpdateProvider(providerId, { baseURL: e.target.value })}
/>
</div>
<div className="grid gap-2">
<Label htmlFor={`${providerId}-auth`}>Authentication</Label>
<Select
value={provider?.nostr ? 'nostr' : 'api-key'}
onValueChange={(value: 'api-key' | 'nostr') => handleUpdateProvider(providerId, {
nostr: value === 'nostr' || undefined,
apiKey: value === 'nostr' ? undefined : provider?.apiKey
})}
>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="api-key">API Key</SelectItem>
<SelectItem value="nostr">Nostr</SelectItem>
</SelectContent>
</Select>
</div>
{!provider?.nostr && (
<div className="grid gap-2">
<Label htmlFor={`${providerId}-apiKey`}>API Key</Label>
<Input
id={`${providerId}-apiKey`}
type="password"
placeholder="Enter your API key"
value={provider?.apiKey || ''}
onChange={(e) => handleUpdateProvider(providerId, { apiKey: e.target.value })}
/>
</div>
)}
</div>
</AccordionContent>
</AccordionItem>
);
})}
</Accordion>
</div>
)}
{configuredProviderIds.length > 0 && availablePresets.length > 0 && <Separator />}
{/* Available Preset Providers */}
{availablePresets.length > 0 && (
<div className="space-y-3">
<h4 className="text-sm font-medium">Add Provider</h4>
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
{availablePresets.map((preset) => (
<Card key={preset.id}>
<CardContent className="p-4 space-y-3">
<div className="flex items-center justify-between">
<h5 className="font-medium">{preset.name}</h5>
{preset.apiKeysURL && (
<button
type="button"
className="text-xs text-muted-foreground underline hover:text-foreground"
onClick={() => window.open(preset.apiKeysURL, '_blank')}
>
Get API key
</button>
)}
</div>
<div className="flex gap-2">
{!preset.nostr && (
<Input
placeholder="Enter your API key"
type="password"
className="flex-1"
value={presetApiKeys[preset.id] || ''}
onChange={(e) => setPresetApiKeys(prev => ({
...prev,
[preset.id]: e.target.value,
}))}
onKeyDown={(e) => {
if (e.key === 'Enter' && presetApiKeys[preset.id]?.trim()) {
handleAddPresetProvider(preset);
}
}}
/>
)}
<Button
onClick={() => handleAddPresetProvider(preset)}
disabled={!!preset.apiKeysURL && !presetApiKeys[preset.id]?.trim()}
size="sm"
>
Add
</Button>
</div>
</CardContent>
</Card>
))}
</div>
</div>
)}
{availablePresets.length > 0 && <Separator />}
{/* Custom Provider */}
<div className="space-y-3">
<h4 className="text-sm font-medium">Add Custom Provider</h4>
<Card>
<CardContent className="p-4 space-y-3">
<div className="grid gap-2">
<Label htmlFor="custom-name">Provider Name</Label>
<Input
id="custom-name"
placeholder="e.g., my-custom-api"
value={customProviderName}
onChange={(e) => setCustomProviderName(e.target.value)}
/>
</div>
<div className="grid gap-2">
<Label htmlFor="custom-baseurl">Base URL</Label>
<Input
id="custom-baseurl"
placeholder="https://api.example.com/v1"
value={customBaseURL}
onChange={(e) => setCustomBaseURL(e.target.value)}
/>
</div>
<div className="grid gap-2">
<Label htmlFor="custom-auth">Authentication</Label>
<Select
value={customAuthMethod}
onValueChange={(value: 'api-key' | 'nostr') => {
setCustomAuthMethod(value);
if (value === 'nostr') {
setCustomApiKey(''); // Clear API key when switching to Nostr auth
}
}}
>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="api-key">API Key</SelectItem>
<SelectItem value="nostr">Nostr</SelectItem>
</SelectContent>
</Select>
</div>
{customAuthMethod === 'api-key' && (
<div className="grid gap-2">
<Label htmlFor="custom-apikey">API Key</Label>
<Input
id="custom-apikey"
type="password"
placeholder="Enter your API key (optional)"
value={customApiKey}
onChange={(e) => setCustomApiKey(e.target.value)}
/>
</div>
)}
<Button
onClick={handleAddCustomProvider}
disabled={
!customProviderName.trim() ||
!customBaseURL.trim() ||
customProviderName.trim() in settings.providers
}
size="sm"
className="gap-2"
>
<Check className="h-4 w-4" />
Add Custom Provider
</Button>
{customProviderName.trim() in settings.providers && (
<p className="text-sm text-destructive">
Provider with this name already exists
</p>
)}
</CardContent>
</Card>
</div>
</div>
</CardContent>
</Card>
</div>
</AppLayout>
);
}
export default AISettings;
+315
View File
@@ -0,0 +1,315 @@
import { useState } from 'react';
import { Check, GitBranch, ArrowLeft } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Card, CardContent } from '@/components/ui/card';
import { Separator } from '@/components/ui/separator';
import { Badge } from '@/components/ui/badge';
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion';
import { useGitSettings } from '@/hooks/useGitSettings';
import { AppLayout } from '@/components/AppLayout';
import { useNavigate } from 'react-router-dom';
import type { GitCredential } from '@/contexts/GitSettingsContext';
interface PresetProvider {
id: string;
name: string;
origin: string;
username: string;
tokenURL?: string;
}
const PRESET_PROVIDERS: PresetProvider[] = [
{
id: "github",
name: "GitHub",
origin: "https://github.com",
username: "git",
tokenURL: "https://github.com/settings/tokens",
},
{
id: "gitlab",
name: "GitLab",
origin: "https://gitlab.com",
username: "git",
tokenURL: "https://gitlab.com/-/user_settings/personal_access_tokens",
},
];
export function GitSettings() {
const { settings, addCredential, removeCredential, updateCredential } = useGitSettings();
const navigate = useNavigate();
const [customOrigin, setCustomOrigin] = useState('');
const [customUsername, setCustomUsername] = useState('');
const [customPassword, setCustomPassword] = useState('');
const [presetTokens, setPresetTokens] = useState<Record<string, string>>({});
const handleAddPresetProvider = (preset: PresetProvider) => {
const token = presetTokens[preset.id] as string | undefined;
if (!token?.trim()) return;
const newCredential: GitCredential = {
username: preset.username,
password: token.trim(),
};
// Auto-save: Add credential immediately to persistent storage
addCredential(preset.origin, newCredential);
// Clear the token input for this preset
setPresetTokens(prev => ({
...prev,
[preset.id]: '',
}));
};
const handleAddCustomProvider = () => {
if (!customOrigin.trim() || !customUsername.trim() || !customPassword.trim()) return;
const newCredential: GitCredential = {
username: customUsername.trim(),
password: customPassword.trim(),
};
// Auto-save: Add credential immediately to persistent storage
addCredential(customOrigin.trim(), newCredential);
setCustomOrigin('');
setCustomUsername('');
setCustomPassword('');
};
const handleRemoveCredential = (origin: string) => {
// Auto-save: Remove credential immediately from persistent storage
removeCredential(origin);
};
const handleUpdateCredential = (origin: string, credential: Partial<GitCredential>) => {
// Auto-save: Update credential immediately in persistent storage
updateCredential(origin, credential);
};
const configuredOrigins = Object.keys(settings.credentials);
const availablePresets = PRESET_PROVIDERS.filter(preset => !configuredOrigins.includes(preset.origin));
return (
<AppLayout title="Git Settings" showSidebar={true}>
<div className="max-w-4xl mx-auto">
<Card className="bg-white">
<CardContent className="p-6 space-y-6">
<div className="space-y-2">
<div className="flex items-center gap-3">
<Button
variant="ghost"
size="sm"
onClick={() => navigate('/settings')}
className="h-8 w-8 p-0"
>
<ArrowLeft className="h-4 w-4" />
</Button>
<h1 className="text-3xl font-bold tracking-tight flex items-center gap-3">
<GitBranch className="h-8 w-8 text-primary" />
Git Settings
</h1>
</div>
<p className="text-muted-foreground ml-11">
Configure Git credentials for HTTP authentication. Settings are automatically saved and stored locally in your browser.
</p>
</div>
<div className="space-y-6">
{/* Configured Credentials */}
{configuredOrigins.length > 0 && (
<div className="space-y-3">
<h4 className="text-sm font-medium">Configured Credentials</h4>
<Accordion type="multiple" className="w-full space-y-2">
{configuredOrigins.map((origin) => {
const preset = PRESET_PROVIDERS.find(p => p.origin === origin);
const credential = settings.credentials[origin];
const isCustom = !preset;
return (
<AccordionItem key={origin} value={origin} className="border rounded-lg">
<AccordionTrigger className="px-4 py-3 hover:no-underline">
<div className="flex items-center justify-between w-full mr-3">
<div className="flex items-center gap-2">
<span className="font-medium">
{preset?.name || new URL(origin).hostname}
</span>
{isCustom && <Badge variant="outline">Custom</Badge>}
</div>
<button
type="button"
onClick={(e) => {
e.stopPropagation();
handleRemoveCredential(origin);
}}
className="h-8 w-8 p-0 text-muted-foreground hover:text-destructive rounded-md hover:bg-accent"
>
<svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</button>
</div>
</AccordionTrigger>
<AccordionContent className="px-4 pb-4">
<div className="space-y-3">
<div className="grid gap-2">
<Label htmlFor={`${origin}-origin`}>Origin</Label>
<Input
id={`${origin}-origin`}
placeholder="https://github.com"
value={origin}
disabled
className="bg-muted"
/>
</div>
<div className="grid gap-2">
<Label htmlFor={`${origin}-username`}>Username</Label>
<Input
id={`${origin}-username`}
placeholder="git"
value={credential?.username || ''}
onChange={(e) => handleUpdateCredential(origin, { username: e.target.value })}
/>
</div>
<div className="grid gap-2">
<Label htmlFor={`${origin}-password`}>Password</Label>
<Input
id={`${origin}-password`}
type="password"
placeholder="Enter your password/token"
value={credential?.password || ''}
onChange={(e) => handleUpdateCredential(origin, { password: e.target.value })}
/>
</div>
</div>
</AccordionContent>
</AccordionItem>
);
})}
</Accordion>
</div>
)}
{configuredOrigins.length > 0 && availablePresets.length > 0 && <Separator />}
{/* Available Preset Providers */}
{availablePresets.length > 0 && (
<div className="space-y-3">
<h4 className="text-sm font-medium">Add Provider</h4>
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
{availablePresets.map((preset) => (
<Card key={preset.id}>
<CardContent className="p-4 space-y-3">
<div className="flex items-center justify-between">
<h5 className="font-medium">{preset.name}</h5>
{preset.tokenURL && (
<button
type="button"
className="text-xs text-muted-foreground underline hover:text-foreground"
onClick={() => window.open(preset.tokenURL, '_blank')}
>
Get token
</button>
)}
</div>
<div className="flex gap-2">
<Input
placeholder="Enter your token"
type="password"
className="flex-1"
value={presetTokens[preset.id] || ''}
onChange={(e) => setPresetTokens(prev => ({
...prev,
[preset.id]: e.target.value,
}))}
onKeyDown={(e) => {
if (e.key === 'Enter' && presetTokens[preset.id]?.trim()) {
handleAddPresetProvider(preset);
}
}}
/>
<Button
onClick={() => handleAddPresetProvider(preset)}
disabled={!presetTokens[preset.id]?.trim()}
size="sm"
>
Add
</Button>
</div>
</CardContent>
</Card>
))}
</div>
</div>
)}
{availablePresets.length > 0 && <Separator />}
{/* Custom Provider */}
<div className="space-y-3">
<h4 className="text-sm font-medium">Add Custom Provider</h4>
<Card>
<CardContent className="p-4 space-y-3">
<div className="grid gap-2">
<Label htmlFor="custom-origin">Origin</Label>
<Input
id="custom-origin"
placeholder="https://git.example.com"
value={customOrigin}
onChange={(e) => setCustomOrigin(e.target.value)}
/>
</div>
<div className="grid gap-2">
<Label htmlFor="custom-username">Username</Label>
<Input
id="custom-username"
placeholder="git"
value={customUsername}
onChange={(e) => setCustomUsername(e.target.value)}
/>
</div>
<div className="grid gap-2">
<Label htmlFor="custom-password">Password</Label>
<Input
id="custom-password"
type="password"
placeholder="Enter your password/token"
value={customPassword}
onChange={(e) => setCustomPassword(e.target.value)}
/>
</div>
<Button
onClick={handleAddCustomProvider}
disabled={
!customOrigin.trim() ||
!customUsername.trim() ||
!customPassword.trim() ||
customOrigin.trim() in settings.credentials
}
size="sm"
className="gap-2"
>
<Check className="h-4 w-4" />
Add Custom Provider
</Button>
{customOrigin.trim() in settings.credentials && (
<p className="text-sm text-destructive">
Credentials for this origin already exist
</p>
)}
</CardContent>
</Card>
</div>
</div>
</CardContent>
</Card>
</div>
</AppLayout>
);
}
export default GitSettings;
+72
View File
@@ -0,0 +1,72 @@
import { AppLayout } from '@/components/AppLayout';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Settings as SettingsIcon, Bot, GitBranch } from 'lucide-react';
import { useNavigate } from 'react-router-dom';
export function Settings() {
const navigate = useNavigate();
const settingsItems = [
{
title: 'AI Settings',
description: 'Configure AI providers and API keys',
icon: Bot,
href: '/settings/ai',
},
{
title: 'Git Settings',
description: 'Configure Git credentials for HTTP authentication',
icon: GitBranch,
href: '/settings/git',
},
];
return (
<AppLayout title="Settings" showSidebar={true}>
<div className="max-w-4xl mx-auto">
<Card className="bg-white">
<CardContent className="p-6 space-y-6">
<div className="space-y-2">
<h1 className="text-3xl font-bold tracking-tight flex items-center gap-3">
<SettingsIcon className="h-8 w-8 text-primary" />
Settings
</h1>
<p className="text-muted-foreground">
Manage your application settings and preferences.
</p>
</div>
<div className="grid gap-4 md:grid-cols-2">
{settingsItems.map((item) => {
const Icon = item.icon;
return (
<Card
key={item.href}
className="cursor-pointer transition-all hover:shadow-md hover:scale-[1.02] hover:border-primary/20"
onClick={() => navigate(item.href)}
>
<CardHeader className="pb-3">
<CardTitle className="flex items-center gap-3">
<Icon className="h-5 w-5 text-primary" />
{item.title}
</CardTitle>
<CardDescription>{item.description}</CardDescription>
</CardHeader>
<CardContent>
<Button variant="outline" className="w-full">
Configure
</Button>
</CardContent>
</Card>
);
})}
</div>
</CardContent>
</Card>
</div>
</AppLayout>
);
}
export default Settings;