mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Use ProviderTile's for all settings pages
This commit is contained in:
10 files changed
+1268
-957
No files matched your search
@@ -0,0 +1,183 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Trash2, Bot } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { PasswordInput } from '@/components/ui/password-input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from '@/components/ui/dialog';
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
import type { AIProvider } from '@/contexts/AISettingsContext';
|
||||
import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
import { CreditsBadge } from '@/components/CreditsBadge';
|
||||
|
||||
interface AIProviderConfigDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
provider: AIProvider;
|
||||
onUpdate: (provider: AIProvider) => void;
|
||||
onRemove: () => void;
|
||||
onOpenCreditsDialog: () => void;
|
||||
}
|
||||
|
||||
export function AIProviderConfigDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
provider,
|
||||
onUpdate,
|
||||
onRemove,
|
||||
onOpenCreditsDialog,
|
||||
}: AIProviderConfigDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const [localProvider, setLocalProvider] = useState(provider);
|
||||
|
||||
// Reset local state when provider changes or dialog opens
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setLocalProvider(provider);
|
||||
}
|
||||
}, [provider, open]);
|
||||
|
||||
const handleSave = () => {
|
||||
onUpdate(localProvider);
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
onRemove();
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
{localProvider.baseURL ? (
|
||||
<ExternalFavicon
|
||||
url={localProvider.baseURL}
|
||||
size={20}
|
||||
fallback={<Bot size={20} />}
|
||||
/>
|
||||
) : (
|
||||
<Bot size={20} />
|
||||
)}
|
||||
<DialogTitle>{localProvider.name}</DialogTitle>
|
||||
<div className="ml-auto">
|
||||
<CreditsBadge
|
||||
provider={localProvider}
|
||||
onOpenDialog={onOpenCreditsDialog}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<DialogDescription>
|
||||
Configure your AI provider settings
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4 py-4">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="provider-name">
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="provider-name"
|
||||
placeholder="Provider name"
|
||||
value={localProvider.name || ''}
|
||||
onChange={(e) => setLocalProvider({ ...localProvider, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="provider-baseURL">
|
||||
{t('baseUrl')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="provider-baseURL"
|
||||
placeholder="https://api.example.com/v1"
|
||||
value={localProvider.baseURL || ''}
|
||||
onChange={(e) => setLocalProvider({ ...localProvider, baseURL: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="provider-auth">{t('authentication')}</Label>
|
||||
<Select
|
||||
value={localProvider.nostr ? 'nostr' : 'api-key'}
|
||||
onValueChange={(value: 'api-key' | 'nostr') => setLocalProvider({
|
||||
...localProvider,
|
||||
nostr: value === 'nostr' || undefined,
|
||||
apiKey: value === 'nostr' ? undefined : localProvider.apiKey
|
||||
})}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="api-key">{t('apiKey')}</SelectItem>
|
||||
<SelectItem value="nostr">Nostr</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{!localProvider.nostr && (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="provider-apiKey">{t('apiKey')}</Label>
|
||||
<PasswordInput
|
||||
id="provider-apiKey"
|
||||
placeholder={t('enterApiKey')}
|
||||
value={localProvider.apiKey || ''}
|
||||
onChange={(e) => setLocalProvider({ ...localProvider, apiKey: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="provider-proxy"
|
||||
checked={localProvider.proxy || false}
|
||||
onCheckedChange={(checked) => setLocalProvider({
|
||||
...localProvider,
|
||||
proxy: checked === true || undefined
|
||||
})}
|
||||
/>
|
||||
<Label htmlFor="provider-proxy" className="cursor-pointer">
|
||||
{t('useCorsProxy')}
|
||||
</Label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter className="gap-2 sm:gap-0">
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={handleDelete}
|
||||
className="sm:mr-auto"
|
||||
>
|
||||
<Trash2 className="h-4 w-4 mr-2" />
|
||||
{t('delete')}
|
||||
</Button>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
{t('cancel')}
|
||||
</Button>
|
||||
<Button onClick={handleSave}>
|
||||
{t('save')}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
import { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Bot } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import { ExternalInput } from '@/components/ui/external-input';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from '@/components/ui/dialog';
|
||||
import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
import { Link } from 'react-router-dom';
|
||||
import type { PresetProvider } from '@/lib/aiProviderPresets';
|
||||
|
||||
interface AddAIProviderDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
preset: PresetProvider;
|
||||
isLoggedIntoNostr: boolean;
|
||||
onAdd: (apiKey: string) => void;
|
||||
}
|
||||
|
||||
export function AddAIProviderDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
preset,
|
||||
isLoggedIntoNostr,
|
||||
onAdd,
|
||||
}: AddAIProviderDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const [apiKey, setApiKey] = useState('');
|
||||
const [termsAgreed, setTermsAgreed] = useState(false);
|
||||
|
||||
const showNostrLoginRequired = preset.nostr && !isLoggedIntoNostr;
|
||||
|
||||
const handleAdd = () => {
|
||||
onAdd(apiKey);
|
||||
setApiKey('');
|
||||
setTermsAgreed(false);
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<ExternalFavicon
|
||||
url={preset.baseURL}
|
||||
size={20}
|
||||
fallback={<Bot size={20} />}
|
||||
/>
|
||||
<DialogTitle>{preset.name}</DialogTitle>
|
||||
</div>
|
||||
<DialogDescription>
|
||||
Add {preset.name} as your AI provider
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4 py-4">
|
||||
{showNostrLoginRequired ? (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t('loginToNostrRequired')}
|
||||
</p>
|
||||
<Button asChild className="w-full">
|
||||
<Link to="/settings/nostr">
|
||||
{t('goToNostrSettings')}
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{!preset.nostr && preset.apiKeysURL && (
|
||||
<ExternalInput
|
||||
type="password"
|
||||
placeholder={preset.id === "routstr" ? t('enterCashuToken') : t('enterApiKey')}
|
||||
value={apiKey}
|
||||
onChange={(e) => setApiKey(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' && apiKey.trim() && termsAgreed) {
|
||||
handleAdd();
|
||||
}
|
||||
}}
|
||||
url={preset.apiKeysURL}
|
||||
urlTitle="Get Key"
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Terms of Service Agreement */}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<Checkbox
|
||||
id="agree-terms"
|
||||
checked={termsAgreed}
|
||||
onCheckedChange={(checked) => setTermsAgreed(checked === true)}
|
||||
className="size-3 [&_svg]:size-3"
|
||||
/>
|
||||
<label
|
||||
htmlFor="agree-terms"
|
||||
className="text-xs text-muted-foreground leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70 cursor-pointer"
|
||||
>
|
||||
{t('agreeToTermsOfService', { providerName: preset.name })}{' '}
|
||||
{preset.tosURL ? (
|
||||
<a
|
||||
href={preset.tosURL}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-muted-foreground underline hover:text-foreground hover:no-underline"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{t('termsOfService')}
|
||||
</a>
|
||||
) : (
|
||||
t('termsOfService')
|
||||
)}
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!showNostrLoginRequired && (
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
{t('cancel')}
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handleAdd}
|
||||
disabled={
|
||||
!termsAgreed ||
|
||||
(!!preset.apiKeysURL && !apiKey.trim())
|
||||
}
|
||||
>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
import { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Check } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { PasswordInput } from '@/components/ui/password-input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from '@/components/ui/dialog';
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
|
||||
interface AddCustomAIProviderDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
onAdd: (provider: {
|
||||
id: string;
|
||||
name: string;
|
||||
baseURL: string;
|
||||
apiKey?: string;
|
||||
nostr?: boolean;
|
||||
proxy?: boolean;
|
||||
}) => void;
|
||||
existingIds: string[];
|
||||
}
|
||||
|
||||
// Helper function to generate ID from name
|
||||
function generateIdFromName(name: string): string {
|
||||
return name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-') // Replace non-alphanumeric chars with hyphens
|
||||
.replace(/^-+|-+$/g, ''); // Remove leading/trailing hyphens
|
||||
}
|
||||
|
||||
export function AddCustomAIProviderDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
onAdd,
|
||||
existingIds,
|
||||
}: AddCustomAIProviderDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const [customProviderName, setCustomProviderName] = useState('');
|
||||
const [customProviderId, setCustomProviderId] = useState('');
|
||||
const [customIdManuallyEdited, setCustomIdManuallyEdited] = useState(false);
|
||||
const [customBaseURL, setCustomBaseURL] = useState('');
|
||||
const [customApiKey, setCustomApiKey] = useState('');
|
||||
const [customAuthMethod, setCustomAuthMethod] = useState<'api-key' | 'nostr'>('api-key');
|
||||
const [customProxy, setCustomProxy] = useState(false);
|
||||
|
||||
const handleAdd = () => {
|
||||
if (!customProviderName.trim() || !customProviderId.trim() || !customBaseURL.trim()) return;
|
||||
|
||||
const provider: any = {
|
||||
id: customProviderId.trim(),
|
||||
name: customProviderName.trim(),
|
||||
baseURL: customBaseURL.trim(),
|
||||
};
|
||||
|
||||
if (customAuthMethod === 'api-key' && customApiKey.trim()) {
|
||||
provider.apiKey = customApiKey.trim();
|
||||
} else if (customAuthMethod === 'nostr') {
|
||||
provider.nostr = true;
|
||||
}
|
||||
|
||||
if (customProxy) {
|
||||
provider.proxy = true;
|
||||
}
|
||||
|
||||
onAdd(provider);
|
||||
|
||||
// Reset form
|
||||
setCustomProviderName('');
|
||||
setCustomProviderId('');
|
||||
setCustomIdManuallyEdited(false);
|
||||
setCustomBaseURL('');
|
||||
setCustomApiKey('');
|
||||
setCustomAuthMethod('api-key');
|
||||
setCustomProxy(false);
|
||||
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
const isValid = customProviderName.trim() &&
|
||||
customProviderId.trim() &&
|
||||
customBaseURL.trim() &&
|
||||
!existingIds.includes(customProviderId.trim());
|
||||
|
||||
const idExists = existingIds.includes(customProviderId.trim());
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-md max-h-[90vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t('addCustomProvider')}</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure a custom AI provider with your own settings
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4 py-4">
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-name">
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-name"
|
||||
placeholder="e.g., My Custom API"
|
||||
value={customProviderName}
|
||||
onChange={(e) => {
|
||||
const newName = e.target.value;
|
||||
setCustomProviderName(newName);
|
||||
|
||||
// Auto-generate ID from name if ID hasn't been manually edited
|
||||
if (!customIdManuallyEdited) {
|
||||
setCustomProviderId(generateIdFromName(newName));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-id">
|
||||
{t('id')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-id"
|
||||
placeholder="e.g., my-custom-api"
|
||||
value={customProviderId}
|
||||
onChange={(e) => {
|
||||
setCustomProviderId(e.target.value);
|
||||
setCustomIdManuallyEdited(true);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-baseurl">
|
||||
{t('baseUrl')} <span className="text-destructive">*</span>
|
||||
</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">{t('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">{t('apiKey')}</SelectItem>
|
||||
<SelectItem value="nostr">Nostr</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{customAuthMethod === 'api-key' && (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-apikey">{t('apiKey')}</Label>
|
||||
<PasswordInput
|
||||
id="custom-apikey"
|
||||
placeholder={t('enterApiKey') + ' (optional)'}
|
||||
value={customApiKey}
|
||||
onChange={(e) => setCustomApiKey(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="custom-proxy"
|
||||
checked={customProxy}
|
||||
onCheckedChange={(checked) => setCustomProxy(checked === true)}
|
||||
/>
|
||||
<Label htmlFor="custom-proxy" className="cursor-pointer">
|
||||
{t('useCorsProxy')}
|
||||
</Label>
|
||||
</div>
|
||||
|
||||
{idExists && (
|
||||
<p className="text-sm text-destructive">
|
||||
{t('providerExists')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
{t('cancel')}
|
||||
</Button>
|
||||
<Button onClick={handleAdd} disabled={!isValid}>
|
||||
<Check className="h-4 w-4 mr-2" />
|
||||
{t('addCustomProviderButton')}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
import { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Check } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { PasswordInput } from '@/components/ui/password-input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from '@/components/ui/dialog';
|
||||
|
||||
interface AddCustomGitCredentialDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
onAdd: (credential: {
|
||||
name: string;
|
||||
origin: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}) => void;
|
||||
existingOrigins: string[];
|
||||
}
|
||||
|
||||
export function AddCustomGitCredentialDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
onAdd,
|
||||
existingOrigins,
|
||||
}: AddCustomGitCredentialDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const [customName, setCustomName] = useState('');
|
||||
const [customOrigin, setCustomOrigin] = useState('');
|
||||
const [customUsername, setCustomUsername] = useState('');
|
||||
const [customPassword, setCustomPassword] = useState('');
|
||||
|
||||
const handleAdd = () => {
|
||||
if (!customName.trim() || !customOrigin.trim() || !customUsername.trim() || !customPassword.trim()) return;
|
||||
|
||||
onAdd({
|
||||
name: customName.trim(),
|
||||
origin: customOrigin.trim(),
|
||||
username: customUsername.trim(),
|
||||
password: customPassword.trim(),
|
||||
});
|
||||
|
||||
// Reset form
|
||||
setCustomName('');
|
||||
setCustomOrigin('');
|
||||
setCustomUsername('');
|
||||
setCustomPassword('');
|
||||
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
const isValid = customName.trim() &&
|
||||
customOrigin.trim() &&
|
||||
customUsername.trim() &&
|
||||
customPassword.trim() &&
|
||||
!existingOrigins.includes(customOrigin.trim());
|
||||
|
||||
const originExists = existingOrigins.includes(customOrigin.trim());
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t('addCustomProvider')}</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure a custom Git server with your credentials
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4 py-4">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-name">
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-name"
|
||||
placeholder="My Git Server"
|
||||
value={customName}
|
||||
onChange={(e) => setCustomName(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-origin">
|
||||
{t('origin')} <span className="text-destructive">*</span>
|
||||
</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">
|
||||
{t('username')} <span className="text-destructive">*</span>
|
||||
</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">
|
||||
{t('password')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<PasswordInput
|
||||
id="custom-password"
|
||||
placeholder={t('enterPassword')}
|
||||
value={customPassword}
|
||||
onChange={(e) => setCustomPassword(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{originExists && (
|
||||
<p className="text-sm text-destructive">
|
||||
{t('credentialsExist')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
{t('cancel')}
|
||||
</Button>
|
||||
<Button onClick={handleAdd} disabled={!isValid}>
|
||||
<Check className="h-4 w-4 mr-2" />
|
||||
{t('addCustomProviderButton')}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
import { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { GitBranch, ChevronDown } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { ExternalInput } from '@/components/ui/external-input';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from '@/components/ui/dialog';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu';
|
||||
import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
|
||||
interface PresetProvider {
|
||||
id: string;
|
||||
name: string;
|
||||
origin: string;
|
||||
username: string;
|
||||
tokenURL?: string;
|
||||
}
|
||||
|
||||
interface OAuthHook {
|
||||
initiateOAuth: () => void;
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
isOAuthConfigured: boolean;
|
||||
}
|
||||
|
||||
interface AddGitCredentialDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
preset: PresetProvider;
|
||||
oauthHook: OAuthHook | null;
|
||||
forceManualEntry: boolean;
|
||||
onSetForceManualEntry: (force: boolean) => void;
|
||||
onAdd: (token: string) => void;
|
||||
}
|
||||
|
||||
export function AddGitCredentialDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
preset,
|
||||
oauthHook,
|
||||
forceManualEntry,
|
||||
onSetForceManualEntry,
|
||||
onAdd,
|
||||
}: AddGitCredentialDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const [token, setToken] = useState('');
|
||||
|
||||
const isOAuthConfigured = oauthHook?.isOAuthConfigured ?? false;
|
||||
const isOAuthLoading = oauthHook?.isLoading ?? false;
|
||||
const oauthError = oauthHook?.error ?? null;
|
||||
|
||||
const handleAdd = () => {
|
||||
onAdd(token);
|
||||
setToken('');
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<ExternalFavicon
|
||||
url={preset.origin}
|
||||
size={20}
|
||||
fallback={<GitBranch size={20} />}
|
||||
/>
|
||||
<DialogTitle>{preset.name}</DialogTitle>
|
||||
</div>
|
||||
<DialogDescription>
|
||||
Add {preset.name} credentials for Git operations
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4 py-4">
|
||||
{preset.id === 'github' && isOAuthConfigured && !forceManualEntry ? (
|
||||
// Special rendering for GitHub OAuth
|
||||
<div className="space-y-3">
|
||||
<div className="flex gap-0">
|
||||
<Button
|
||||
onClick={() => oauthHook?.initiateOAuth()}
|
||||
disabled={isOAuthLoading}
|
||||
className="flex-1 rounded-r-none gap-2"
|
||||
variant="default"
|
||||
>
|
||||
{isOAuthLoading ? (
|
||||
<>
|
||||
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
|
||||
Connecting...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<ExternalFavicon
|
||||
url="https://github.com"
|
||||
size={16}
|
||||
fallback={<GitBranch size={16} />}
|
||||
/>
|
||||
<span className="truncate text-ellipsis overflow-hidden">
|
||||
Connect to GitHub
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
variant="default"
|
||||
className="rounded-l-none border-l border-primary-foreground/20 px-2"
|
||||
disabled={isOAuthLoading}
|
||||
>
|
||||
<ChevronDown className="h-4 w-4" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => onSetForceManualEntry(true)}>
|
||||
Enter API key
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
{oauthError && (
|
||||
<p className="text-sm text-destructive">
|
||||
{oauthError}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
// Standard token input
|
||||
<ExternalInput
|
||||
type="password"
|
||||
placeholder={t('enterToken')}
|
||||
value={token}
|
||||
onChange={(e) => setToken(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' && token.trim()) {
|
||||
handleAdd();
|
||||
}
|
||||
}}
|
||||
url={preset.tokenURL}
|
||||
urlTitle="Get Token"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!(preset.id === 'github' && isOAuthConfigured && !forceManualEntry) && (
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
{t('cancel')}
|
||||
</Button>
|
||||
<Button onClick={handleAdd} disabled={!token.trim()}>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Plus } from 'lucide-react';
|
||||
|
||||
interface AddProviderTileProps {
|
||||
onClick: () => void;
|
||||
label?: string;
|
||||
}
|
||||
|
||||
export function AddProviderTile({ onClick, label = 'Add Custom' }: AddProviderTileProps) {
|
||||
return (
|
||||
<button
|
||||
onClick={onClick}
|
||||
className="flex flex-col items-center justify-center gap-2 p-4 rounded-lg border border-dashed bg-card hover:bg-muted transition-colors text-center min-h-[100px]"
|
||||
>
|
||||
<Plus size={32} className="text-muted-foreground" />
|
||||
<span className="text-sm font-medium text-muted-foreground overflow-hidden max-w-full text-ellipsis">
|
||||
{label}
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Trash2, GitBranch } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { PasswordInput } from '@/components/ui/password-input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from '@/components/ui/dialog';
|
||||
import type { GitCredential } from '@/contexts/GitSettingsContext';
|
||||
import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
|
||||
interface GitCredentialConfigDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
credential: GitCredential;
|
||||
onUpdate: (credential: GitCredential) => void;
|
||||
onRemove: () => void;
|
||||
isCustom?: boolean;
|
||||
}
|
||||
|
||||
export function GitCredentialConfigDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
credential,
|
||||
onUpdate,
|
||||
onRemove,
|
||||
isCustom = false,
|
||||
}: GitCredentialConfigDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const [localCredential, setLocalCredential] = useState(credential);
|
||||
|
||||
// Reset local state when credential changes or dialog opens
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setLocalCredential(credential);
|
||||
}
|
||||
}, [credential, open]);
|
||||
|
||||
const handleSave = () => {
|
||||
onUpdate(localCredential);
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
onRemove();
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<ExternalFavicon
|
||||
url={localCredential.origin}
|
||||
size={20}
|
||||
fallback={<GitBranch size={20} />}
|
||||
/>
|
||||
<DialogTitle>{localCredential.name}</DialogTitle>
|
||||
{isCustom && <Badge variant="outline">{t('custom')}</Badge>}
|
||||
</div>
|
||||
<DialogDescription>
|
||||
Configure your Git credentials
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4 py-4">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="credential-name">
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="credential-name"
|
||||
placeholder="GitHub"
|
||||
value={localCredential.name}
|
||||
onChange={(e) => setLocalCredential({ ...localCredential, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="credential-origin">
|
||||
{t('origin')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="credential-origin"
|
||||
placeholder="https://github.com"
|
||||
value={localCredential.origin}
|
||||
onChange={(e) => setLocalCredential({ ...localCredential, origin: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="credential-username">
|
||||
{t('username')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="credential-username"
|
||||
placeholder="git"
|
||||
value={localCredential.username}
|
||||
onChange={(e) => setLocalCredential({ ...localCredential, username: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="credential-password">
|
||||
{t('password')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<PasswordInput
|
||||
id="credential-password"
|
||||
placeholder={t('enterPassword')}
|
||||
value={localCredential.password}
|
||||
onChange={(e) => setLocalCredential({ ...localCredential, password: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter className="gap-2 sm:gap-0">
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={handleDelete}
|
||||
className="sm:mr-auto"
|
||||
>
|
||||
<Trash2 className="h-4 w-4 mr-2" />
|
||||
{t('delete')}
|
||||
</Button>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
{t('cancel')}
|
||||
</Button>
|
||||
<Button onClick={handleSave}>
|
||||
{t('save')}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { Bot } from 'lucide-react';
|
||||
import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
import { ReactNode } from 'react';
|
||||
|
||||
interface ProviderTileProps {
|
||||
icon?: ReactNode;
|
||||
iconUrl?: string | null;
|
||||
name: string;
|
||||
onClick: () => void;
|
||||
badge?: ReactNode;
|
||||
}
|
||||
|
||||
export function ProviderTile({ icon, iconUrl, name, onClick, badge }: ProviderTileProps) {
|
||||
return (
|
||||
<button
|
||||
onClick={onClick}
|
||||
className="flex flex-col items-center justify-center gap-2 p-4 rounded-lg border bg-card hover:bg-muted transition-colors text-center min-h-[100px] relative"
|
||||
>
|
||||
{iconUrl ? (
|
||||
<ExternalFavicon
|
||||
url={iconUrl}
|
||||
size={32}
|
||||
fallback={icon || <Bot size={32} />}
|
||||
/>
|
||||
) : (
|
||||
icon || <Bot size={32} />
|
||||
)}
|
||||
<span className="text-sm font-medium line-clamp-2 overflow-hidden max-w-full text-ellipsis">
|
||||
{name}
|
||||
</span>
|
||||
{badge && (
|
||||
<div className="absolute top-0 right-0">
|
||||
{badge}
|
||||
</div>
|
||||
)}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
+95
-522
@@ -1,224 +1,46 @@
|
||||
import { useState, useMemo, useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Check, Bot, Trash2, GripVertical, ChevronDown, RotateCcw, FileText, Plus, Edit, X } from 'lucide-react';
|
||||
import {
|
||||
DndContext,
|
||||
closestCenter,
|
||||
KeyboardSensor,
|
||||
PointerSensor,
|
||||
useSensor,
|
||||
useSensors,
|
||||
type DragEndEvent,
|
||||
} from '@dnd-kit/core';
|
||||
import {
|
||||
arrayMove,
|
||||
SortableContext,
|
||||
sortableKeyboardCoordinates,
|
||||
verticalListSortingStrategy,
|
||||
} from '@dnd-kit/sortable';
|
||||
import {
|
||||
useSortable,
|
||||
} from '@dnd-kit/sortable';
|
||||
import { CSS } from '@dnd-kit/utilities';
|
||||
import { Bot, ChevronDown, RotateCcw, FileText, Edit, X } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { PasswordInput } from '@/components/ui/password-input';
|
||||
import { ExternalInput } from '@/components/ui/external-input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { CreditsBadge } from '@/components/CreditsBadge';
|
||||
import { CreditsDialog } from '@/components/CreditsDialog';
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { ProviderTile } from '@/components/ProviderTile';
|
||||
import { AddProviderTile } from '@/components/AddProviderTile';
|
||||
import { AIProviderConfigDialog } from '@/components/AIProviderConfigDialog';
|
||||
import { AddAIProviderDialog } from '@/components/AddAIProviderDialog';
|
||||
import { AddCustomAIProviderDialog } from '@/components/AddCustomAIProviderDialog';
|
||||
import { CreditsBadge } from '@/components/CreditsBadge';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useAppContext } from '@/hooks/useAppContext';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { SettingsPageLayout } from '@/components/SettingsPageLayout';
|
||||
import type { AIProvider } from '@/contexts/AISettingsContext';
|
||||
import { AI_PROVIDER_PRESETS, type PresetProvider } from '@/lib/aiProviderPresets';
|
||||
import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
import { MCPServersSection } from '@/components/MCPServersSection';
|
||||
import { PluginsSection } from '@/components/PluginsSection';
|
||||
import { ProjectTemplatesSection } from '@/components/ProjectTemplatesSection';
|
||||
import { defaultSystemPrompt } from '@/lib/system';
|
||||
import { ModelInput } from '@/components/ModelInput';
|
||||
|
||||
interface SortableProviderItemProps {
|
||||
provider: AIProvider;
|
||||
preset?: PresetProvider;
|
||||
onRemove: (id: string) => void;
|
||||
onSetProvider: (provider: AIProvider) => void;
|
||||
onOpenCreditsDialog: (providerId: string) => void;
|
||||
showDragHandle: boolean;
|
||||
}
|
||||
|
||||
function SortableProviderItem({ provider, preset, onRemove, onSetProvider, onOpenCreditsDialog, showDragHandle }: SortableProviderItemProps) {
|
||||
const { t } = useTranslation();
|
||||
const {
|
||||
attributes,
|
||||
listeners,
|
||||
setNodeRef,
|
||||
transform,
|
||||
transition,
|
||||
isDragging,
|
||||
} = useSortable({ id: provider.id });
|
||||
|
||||
const style = {
|
||||
transform: CSS.Transform.toString(transform),
|
||||
transition,
|
||||
opacity: isDragging ? 0.5 : 1,
|
||||
};
|
||||
|
||||
return (
|
||||
<AccordionItem
|
||||
ref={setNodeRef}
|
||||
style={style}
|
||||
value={provider.id}
|
||||
className="border rounded-lg"
|
||||
>
|
||||
<AccordionTrigger className="px-4 py-3 hover:no-underline">
|
||||
<div className="flex items-center gap-2 w-full mr-3">
|
||||
{showDragHandle && (
|
||||
<div
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
className="cursor-grab active:cursor-grabbing p-1 -ml-1 text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
<GripVertical className="h-4 w-4" />
|
||||
</div>
|
||||
)}
|
||||
{provider.baseURL ? (
|
||||
<ExternalFavicon
|
||||
url={provider.baseURL}
|
||||
size={16}
|
||||
fallback={<Bot size={16} />}
|
||||
/>
|
||||
) : (
|
||||
<Bot size={16} />
|
||||
)}
|
||||
<span className="font-medium">
|
||||
{provider.name}
|
||||
</span>
|
||||
<div className="ml-auto">
|
||||
<CreditsBadge
|
||||
provider={provider}
|
||||
onOpenDialog={() => onOpenCreditsDialog(provider.id)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${provider.id}-name`}>
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id={`${provider.id}-name`}
|
||||
placeholder={preset?.name || provider.id}
|
||||
value={provider.name || ''}
|
||||
onChange={(e) => onSetProvider({ ...provider, name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${provider.id}-baseURL`}>
|
||||
{t('baseUrl')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id={`${provider.id}-baseURL`}
|
||||
placeholder="https://api.example.com/v1"
|
||||
value={provider.baseURL || ''}
|
||||
onChange={(e) => onSetProvider({ ...provider, baseURL: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${provider.id}-auth`}>{t('authentication')}</Label>
|
||||
<Select
|
||||
value={provider.nostr ? 'nostr' : 'api-key'}
|
||||
onValueChange={(value: 'api-key' | 'nostr') => onSetProvider({
|
||||
...provider,
|
||||
nostr: value === 'nostr' || undefined,
|
||||
apiKey: value === 'nostr' ? undefined : provider.apiKey
|
||||
})}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="api-key">{t('apiKey')}</SelectItem>
|
||||
<SelectItem value="nostr">Nostr</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{!provider.nostr && (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${provider.id}-apiKey`}>{t('apiKey')}</Label>
|
||||
<PasswordInput
|
||||
id={`${provider.id}-apiKey`}
|
||||
placeholder={t('enterApiKey')}
|
||||
value={provider.apiKey || ''}
|
||||
onChange={(e) => onSetProvider({ ...provider, apiKey: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id={`${provider.id}-proxy`}
|
||||
checked={provider.proxy || false}
|
||||
onCheckedChange={(checked) => onSetProvider({
|
||||
...provider,
|
||||
proxy: checked === true || undefined
|
||||
})}
|
||||
/>
|
||||
<Label htmlFor={`${provider.id}-proxy`} className="cursor-pointer">
|
||||
{t('useCorsProxy')}
|
||||
</Label>
|
||||
</div>
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
onClick={() => onRemove(provider.id)}
|
||||
>
|
||||
<Trash2 className="h-4 w-4 mr-2" />
|
||||
{t('delete')}
|
||||
</Button>
|
||||
</div>
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
);
|
||||
}
|
||||
|
||||
// Helper function to generate ID from name
|
||||
function generateIdFromName(name: string): string {
|
||||
return name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-') // Replace non-alphanumeric chars with hyphens
|
||||
.replace(/^-+|-+$/g, ''); // Remove leading/trailing hyphens
|
||||
}
|
||||
|
||||
export function AISettings() {
|
||||
const { t } = useTranslation();
|
||||
const { settings, updateSettings, setProvider, removeProvider, setProviders, isLoading } = useAISettings();
|
||||
const { settings, updateSettings, setProvider, removeProvider, isLoading } = useAISettings();
|
||||
const { user } = useCurrentUser();
|
||||
const { config, defaultConfig, updateConfig } = useAppContext();
|
||||
const [customProviderName, setCustomProviderName] = useState('');
|
||||
const [customProviderId, setCustomProviderId] = useState('');
|
||||
const [customIdManuallyEdited, setCustomIdManuallyEdited] = useState(false);
|
||||
const [customBaseURL, setCustomBaseURL] = useState('');
|
||||
const [customApiKey, setCustomApiKey] = useState('');
|
||||
const [customAuthMethod, setCustomAuthMethod] = useState<'api-key' | 'nostr'>('api-key');
|
||||
const [customProxy, setCustomProxy] = useState(false);
|
||||
const [presetApiKeys, setPresetApiKeys] = useState<Record<string, string>>({});
|
||||
const [presetTermsAgreements, setPresetTermsAgreements] = useState<Record<string, boolean>>({});
|
||||
const [activeCreditsDialog, setActiveCreditsDialog] = useState<string | null>(null);
|
||||
const [showAdvanced, setShowAdvanced] = useState(false);
|
||||
const [systemPromptInput, setSystemPromptInput] = useState(config.systemPrompt || defaultSystemPrompt);
|
||||
|
||||
// Dialog state
|
||||
const [selectedProviderId, setSelectedProviderId] = useState<string | null>(null);
|
||||
const [configDialogOpen, setConfigDialogOpen] = useState(false);
|
||||
const [selectedPreset, setSelectedPreset] = useState<PresetProvider | null>(null);
|
||||
const [addDialogOpen, setAddDialogOpen] = useState(false);
|
||||
const [customProviderDialogOpen, setCustomProviderDialogOpen] = useState(false);
|
||||
|
||||
// Check if system prompt differs from default
|
||||
const isSystemPromptModified = useMemo(() =>
|
||||
(config.systemPrompt || defaultSystemPrompt) !== (defaultConfig.systemPrompt || defaultSystemPrompt),
|
||||
@@ -233,26 +55,24 @@ export function AISettings() {
|
||||
});
|
||||
};
|
||||
|
||||
const sensors = useSensors(
|
||||
useSensor(PointerSensor),
|
||||
useSensor(KeyboardSensor, {
|
||||
coordinateGetter: sortableKeyboardCoordinates,
|
||||
})
|
||||
);
|
||||
const handleOpenProviderDialog = (providerId: string) => {
|
||||
setSelectedProviderId(providerId);
|
||||
setConfigDialogOpen(true);
|
||||
};
|
||||
|
||||
const handleAddPresetProvider = (preset: PresetProvider) => {
|
||||
const apiKey = presetApiKeys[preset.id] as string | undefined;
|
||||
|
||||
// Check if user agreed to terms
|
||||
if (!presetTermsAgreements[preset.id]) return;
|
||||
const handleOpenAddDialog = (preset: PresetProvider) => {
|
||||
setSelectedPreset(preset);
|
||||
setAddDialogOpen(true);
|
||||
};
|
||||
|
||||
const handleAddPresetProvider = (preset: PresetProvider, apiKey: string) => {
|
||||
const newProvider: AIProvider = {
|
||||
id: preset.id,
|
||||
name: preset.name,
|
||||
baseURL: preset.baseURL,
|
||||
};
|
||||
|
||||
if (typeof apiKey === 'string') {
|
||||
if (apiKey.trim()) {
|
||||
newProvider.apiKey = apiKey.trim();
|
||||
}
|
||||
if (typeof preset.nostr === 'boolean') {
|
||||
@@ -262,76 +82,23 @@ export function AISettings() {
|
||||
newProvider.proxy = preset.proxy;
|
||||
}
|
||||
|
||||
// Auto-save: Add provider immediately to persistent storage
|
||||
setProvider(newProvider);
|
||||
|
||||
// Clear the API key input and terms agreement for this preset
|
||||
setPresetApiKeys(prev => ({
|
||||
...prev,
|
||||
[preset.id]: '',
|
||||
}));
|
||||
setPresetTermsAgreements(prev => ({
|
||||
...prev,
|
||||
[preset.id]: false,
|
||||
}));
|
||||
};
|
||||
|
||||
const handleAddCustomProvider = () => {
|
||||
if (!customProviderName.trim() || !customProviderId.trim() || !customBaseURL.trim()) return;
|
||||
|
||||
const newProvider: AIProvider = {
|
||||
id: customProviderId.trim(),
|
||||
name: customProviderName.trim(),
|
||||
baseURL: customBaseURL.trim(),
|
||||
apiKey: customAuthMethod === 'nostr' ? undefined : customApiKey.trim(),
|
||||
nostr: customAuthMethod === 'nostr' || undefined,
|
||||
proxy: customProxy || undefined,
|
||||
};
|
||||
|
||||
// Auto-save: Add provider immediately to persistent storage
|
||||
const handleAddCustomProvider = (provider: any) => {
|
||||
const newProvider: AIProvider = provider;
|
||||
setProvider(newProvider);
|
||||
|
||||
setCustomProviderName('');
|
||||
setCustomProviderId('');
|
||||
setCustomIdManuallyEdited(false);
|
||||
setCustomBaseURL('');
|
||||
setCustomApiKey('');
|
||||
setCustomAuthMethod('api-key');
|
||||
setCustomProxy(false);
|
||||
};
|
||||
|
||||
const handleRemoveProvider = (id: string) => {
|
||||
// Auto-save: Remove provider immediately from persistent storage
|
||||
removeProvider(id);
|
||||
setConfigDialogOpen(false);
|
||||
};
|
||||
|
||||
const handleSetProvider = (provider: AIProvider) => {
|
||||
// Auto-save: Update provider immediately in persistent storage
|
||||
setProvider(provider);
|
||||
};
|
||||
|
||||
const handleOpenCreditsDialog = (providerId: string) => {
|
||||
setActiveCreditsDialog(providerId);
|
||||
};
|
||||
|
||||
const handleCloseCreditsDialog = () => {
|
||||
setActiveCreditsDialog(null);
|
||||
};
|
||||
|
||||
const handleDragEnd = (event: DragEndEvent) => {
|
||||
const { active, over } = event;
|
||||
|
||||
if (active.id !== over?.id) {
|
||||
const oldIndex = settings.providers.findIndex(provider => provider.id === active.id);
|
||||
const newIndex = settings.providers.findIndex(provider => provider.id === over?.id);
|
||||
|
||||
if (oldIndex !== -1 && newIndex !== -1) {
|
||||
const newProviders = arrayMove(settings.providers, oldIndex, newIndex);
|
||||
setProviders(newProviders);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const configuredProviderIds = settings.providers.map(p => p.id);
|
||||
const availablePresets = AI_PROVIDER_PRESETS.filter(preset => !configuredProviderIds.includes(preset.id));
|
||||
|
||||
@@ -383,276 +150,82 @@ export function AISettings() {
|
||||
) : (
|
||||
<>
|
||||
{/* Configured Providers */}
|
||||
{settings.providers.length > 0 && (
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">{t('configuredProviders')}</h4>
|
||||
<DndContext
|
||||
sensors={sensors}
|
||||
collisionDetection={closestCenter}
|
||||
onDragEnd={handleDragEnd}
|
||||
>
|
||||
<SortableContext
|
||||
items={settings.providers.map(p => p.id)}
|
||||
strategy={verticalListSortingStrategy}
|
||||
>
|
||||
<Accordion type="multiple" className="w-full space-y-2">
|
||||
{settings.providers.map((provider) => {
|
||||
const preset = AI_PROVIDER_PRESETS.find(p => p.id === provider.id);
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">{t('configuredProviders')}</h4>
|
||||
<div className="grid grid-cols-[repeat(auto-fill,minmax(120px,1fr))] gap-3">
|
||||
{settings.providers.map((provider) => (
|
||||
<ProviderTile
|
||||
key={provider.id}
|
||||
iconUrl={provider.baseURL}
|
||||
icon={<Bot size={32} />}
|
||||
name={provider.name}
|
||||
onClick={() => handleOpenProviderDialog(provider.id)}
|
||||
badge={
|
||||
<CreditsBadge
|
||||
provider={provider}
|
||||
onOpenDialog={() => setActiveCreditsDialog(provider.id)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
))}
|
||||
|
||||
return (
|
||||
<SortableProviderItem
|
||||
key={provider.id}
|
||||
provider={provider}
|
||||
preset={preset}
|
||||
onRemove={handleRemoveProvider}
|
||||
onSetProvider={handleSetProvider}
|
||||
onOpenCreditsDialog={handleOpenCreditsDialog}
|
||||
showDragHandle={settings.providers.length > 1}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</Accordion>
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
{/* Add Custom Provider Tile */}
|
||||
<AddProviderTile onClick={() => setCustomProviderDialogOpen(true)} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Provider Config Dialog */}
|
||||
{selectedProviderId !== null && (() => {
|
||||
const provider = settings.providers.find(p => p.id === selectedProviderId);
|
||||
return provider && (
|
||||
<AIProviderConfigDialog
|
||||
open={configDialogOpen}
|
||||
onOpenChange={setConfigDialogOpen}
|
||||
provider={provider}
|
||||
onUpdate={handleSetProvider}
|
||||
onRemove={() => handleRemoveProvider(provider.id)}
|
||||
onOpenCreditsDialog={() => setActiveCreditsDialog(provider.id)}
|
||||
/>
|
||||
);
|
||||
})()}
|
||||
|
||||
{/* Add Custom Provider Dialog */}
|
||||
<AddCustomAIProviderDialog
|
||||
open={customProviderDialogOpen}
|
||||
onOpenChange={setCustomProviderDialogOpen}
|
||||
onAdd={handleAddCustomProvider}
|
||||
existingIds={settings.providers.map(p => p.id)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Available Preset Providers */}
|
||||
{availablePresets.length > 0 && (
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">{t('addProvider')}</h4>
|
||||
<div className="grid grid-cols-[repeat(auto-fit,minmax(min(280px,100%),1fr))] gap-3">
|
||||
{availablePresets.map((preset) => {
|
||||
const isNostrPreset = preset.nostr;
|
||||
const isLoggedIntoNostr = !!user;
|
||||
const showNostrLoginRequired = isNostrPreset && !isLoggedIntoNostr;
|
||||
|
||||
return (
|
||||
<Card key={preset.id}>
|
||||
<CardContent className="p-4 space-y-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<ExternalFavicon
|
||||
url={preset.baseURL}
|
||||
size={16}
|
||||
fallback={<Bot size={16} />}
|
||||
/>
|
||||
<h5 className="font-medium">{preset.name}</h5>
|
||||
</div>
|
||||
|
||||
{showNostrLoginRequired ? (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t('loginToNostrRequired')}
|
||||
</p>
|
||||
<Button
|
||||
asChild
|
||||
className="w-full"
|
||||
>
|
||||
<Link to="/settings/nostr">
|
||||
{t('goToNostrSettings')}
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-2">
|
||||
<div className="flex gap-2">
|
||||
{!preset.nostr && (
|
||||
<ExternalInput
|
||||
type="password"
|
||||
className="flex-1"
|
||||
placeholder={preset.id === "routstr" ? t('enterCashuToken') : t('enterApiKey')}
|
||||
value={presetApiKeys[preset.id] || ''}
|
||||
onChange={(e) => setPresetApiKeys(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' &&
|
||||
presetApiKeys[preset.id]?.trim() &&
|
||||
presetTermsAgreements[preset.id]) {
|
||||
handleAddPresetProvider(preset);
|
||||
}
|
||||
}}
|
||||
url={preset.apiKeysURL}
|
||||
urlTitle="Get Key"
|
||||
/>
|
||||
)}
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
disabled={
|
||||
!presetTermsAgreements[preset.id] ||
|
||||
(!!preset.apiKeysURL && !presetApiKeys[preset.id]?.trim())
|
||||
}
|
||||
className={preset.nostr ? "w-full" : "h-10 px-4 ml-auto"}
|
||||
>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Terms of Service Agreement */}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<Checkbox
|
||||
id={`agree-terms-${preset.id}`}
|
||||
checked={presetTermsAgreements[preset.id] || false}
|
||||
onCheckedChange={(checked) => setPresetTermsAgreements(prev => ({
|
||||
...prev,
|
||||
[preset.id]: checked === true,
|
||||
}))}
|
||||
className="size-3 [&_svg]:size-3"
|
||||
/>
|
||||
<label
|
||||
htmlFor={`agree-terms-${preset.id}`}
|
||||
className="text-xs text-muted-foreground leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70 cursor-pointer"
|
||||
>
|
||||
{t('agreeToTermsOfService', { providerName: preset.name })}{' '}
|
||||
{preset.tosURL ? (
|
||||
<a
|
||||
href={preset.tosURL}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-muted-foreground underline hover:text-foreground hover:no-underline"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
{t('termsOfService')}
|
||||
</a>
|
||||
) : (
|
||||
t('termsOfService')
|
||||
)}
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
})}
|
||||
<div className="grid grid-cols-[repeat(auto-fill,minmax(120px,1fr))] gap-3">
|
||||
{availablePresets.map((preset) => (
|
||||
<ProviderTile
|
||||
key={preset.id}
|
||||
iconUrl={preset.baseURL}
|
||||
icon={<Bot size={32} />}
|
||||
name={preset.name}
|
||||
onClick={() => handleOpenAddDialog(preset)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Add Preset Provider Dialog */}
|
||||
{selectedPreset && (
|
||||
<AddAIProviderDialog
|
||||
open={addDialogOpen}
|
||||
onOpenChange={setAddDialogOpen}
|
||||
preset={selectedPreset}
|
||||
isLoggedIntoNostr={!!user}
|
||||
onAdd={(apiKey) => handleAddPresetProvider(selectedPreset, apiKey)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Custom Provider */}
|
||||
<div className="space-y-3">
|
||||
<Accordion type="single" collapsible className="w-full">
|
||||
<AccordionItem value="custom-provider">
|
||||
<AccordionTrigger className="px-4 py-3 hover:no-underline">
|
||||
<div className="flex items-center gap-2">
|
||||
<Plus className="h-4 w-4" />
|
||||
<h4 className="text-sm font-medium">{t('addCustomProvider')}</h4>
|
||||
</div>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="space-y-3">
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-name">
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-name"
|
||||
placeholder="e.g., My Custom API"
|
||||
value={customProviderName}
|
||||
onChange={(e) => {
|
||||
const newName = e.target.value;
|
||||
setCustomProviderName(newName);
|
||||
|
||||
// Auto-generate ID from name if ID hasn't been manually edited
|
||||
if (!customIdManuallyEdited) {
|
||||
setCustomProviderId(generateIdFromName(newName));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-id">
|
||||
{t('id')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-id"
|
||||
placeholder="e.g., my-custom-api"
|
||||
value={customProviderId}
|
||||
onChange={(e) => {
|
||||
setCustomProviderId(e.target.value);
|
||||
setCustomIdManuallyEdited(true);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-baseurl">
|
||||
{t('baseUrl')} <span className="text-destructive">*</span>
|
||||
</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">{t('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">{t('apiKey')}</SelectItem>
|
||||
<SelectItem value="nostr">Nostr</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{customAuthMethod === 'api-key' && (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-apikey">{t('apiKey')}</Label>
|
||||
<PasswordInput
|
||||
id="custom-apikey"
|
||||
placeholder={t('enterApiKey') + ' (optional)'}
|
||||
value={customApiKey}
|
||||
onChange={(e) => setCustomApiKey(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
id="custom-proxy"
|
||||
checked={customProxy}
|
||||
onCheckedChange={(checked) => setCustomProxy(checked === true)}
|
||||
/>
|
||||
<Label htmlFor="custom-proxy" className="cursor-pointer">
|
||||
{t('useCorsProxy')}
|
||||
</Label>
|
||||
</div>
|
||||
<Button
|
||||
onClick={handleAddCustomProvider}
|
||||
disabled={
|
||||
!customProviderName.trim() ||
|
||||
!customProviderId.trim() ||
|
||||
!customBaseURL.trim() ||
|
||||
settings.providers.some(p => p.id === customProviderId.trim())
|
||||
}
|
||||
className="gap-2 ml-auto"
|
||||
>
|
||||
<Check className="h-4 w-4" />
|
||||
{t('addCustomProviderButton')}
|
||||
</Button>
|
||||
{settings.providers.some(p => p.id === customProviderId.trim()) && (
|
||||
<p className="text-sm text-destructive">
|
||||
{t('providerExists')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
</Accordion>
|
||||
</div>
|
||||
|
||||
{/* Advanced Settings */}
|
||||
<div className="space-y-4">
|
||||
<Separator className="my-6" />
|
||||
@@ -772,7 +345,7 @@ export function AISettings() {
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Render credits dialog outside of accordion structure */}
|
||||
{/* Render credits dialog */}
|
||||
{activeCreditsDialog && (() => {
|
||||
const provider = settings.providers.find(p => p.id === activeCreditsDialog);
|
||||
return provider && (
|
||||
@@ -780,7 +353,7 @@ export function AISettings() {
|
||||
open={true}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) {
|
||||
handleCloseCreditsDialog();
|
||||
setActiveCreditsDialog(null);
|
||||
}
|
||||
}}
|
||||
provider={provider}
|
||||
|
||||
+104
-435
@@ -1,48 +1,24 @@
|
||||
import { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Check, GitBranch, Trash2, ChevronDown, Plus, User, GripVertical, Globe, X } from 'lucide-react';
|
||||
import {
|
||||
DndContext,
|
||||
closestCenter,
|
||||
KeyboardSensor,
|
||||
PointerSensor,
|
||||
useSensor,
|
||||
useSensors,
|
||||
type DragEndEvent,
|
||||
} from '@dnd-kit/core';
|
||||
import {
|
||||
arrayMove,
|
||||
SortableContext,
|
||||
sortableKeyboardCoordinates,
|
||||
verticalListSortingStrategy,
|
||||
} from '@dnd-kit/sortable';
|
||||
import {
|
||||
useSortable,
|
||||
} from '@dnd-kit/sortable';
|
||||
import { CSS } from '@dnd-kit/utilities';
|
||||
import { GitBranch, ChevronDown, Plus, User, Globe, X } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { Switch } from '@/components/ui/switch';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu';
|
||||
import { useGitSettings } from '@/hooks/useGitSettings';
|
||||
import { useGitHubOAuth } from '@/hooks/useGitHubOAuth';
|
||||
import { useAppContext } from '@/hooks/useAppContext';
|
||||
import { SettingsPageLayout } from '@/components/SettingsPageLayout';
|
||||
import type { GitCredential } from '@/contexts/GitSettingsContext';
|
||||
import { PasswordInput } from '@/components/ui/password-input';
|
||||
import { ExternalInput } from '@/components/ui/external-input';
|
||||
import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
import { ProviderTile } from '@/components/ProviderTile';
|
||||
import { AddProviderTile } from '@/components/AddProviderTile';
|
||||
import { GitCredentialConfigDialog } from '@/components/GitCredentialConfigDialog';
|
||||
import { AddGitCredentialDialog } from '@/components/AddGitCredentialDialog';
|
||||
import { AddCustomGitCredentialDialog } from '@/components/AddCustomGitCredentialDialog';
|
||||
|
||||
interface PresetProvider {
|
||||
id: string;
|
||||
@@ -69,151 +45,34 @@ const PRESET_PROVIDERS: PresetProvider[] = [
|
||||
},
|
||||
];
|
||||
|
||||
interface SortableCredentialItemProps {
|
||||
credential: GitCredential;
|
||||
onUpdate: (updatedCredential: GitCredential) => void;
|
||||
onRemove: (id: string) => void;
|
||||
showDragHandle: boolean;
|
||||
}
|
||||
|
||||
function SortableCredentialItem({ credential, onUpdate, onRemove, showDragHandle }: SortableCredentialItemProps) {
|
||||
const { t } = useTranslation();
|
||||
const preset = PRESET_PROVIDERS.find(p => p.origin === credential.origin);
|
||||
const isCustom = !preset;
|
||||
|
||||
const {
|
||||
attributes,
|
||||
listeners,
|
||||
setNodeRef,
|
||||
transform,
|
||||
transition,
|
||||
isDragging,
|
||||
} = useSortable({ id: credential.id });
|
||||
|
||||
const style = {
|
||||
transform: CSS.Transform.toString(transform),
|
||||
transition,
|
||||
opacity: isDragging ? 0.5 : 1,
|
||||
};
|
||||
|
||||
const updateCredential = (updates: Partial<GitCredential>) => {
|
||||
onUpdate({ ...credential, ...updates });
|
||||
};
|
||||
|
||||
return (
|
||||
<AccordionItem
|
||||
ref={setNodeRef}
|
||||
style={style}
|
||||
value={credential.id}
|
||||
className="border rounded-lg"
|
||||
>
|
||||
<AccordionTrigger className="px-4 py-3 hover:no-underline">
|
||||
<div className="flex items-center gap-2 w-full mr-3">
|
||||
{showDragHandle && (
|
||||
<div
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
className="cursor-grab active:cursor-grabbing p-1 -ml-1 text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
<GripVertical className="h-4 w-4" />
|
||||
</div>
|
||||
)}
|
||||
<ExternalFavicon
|
||||
url={credential.origin}
|
||||
size={16}
|
||||
fallback={<GitBranch size={16} />}
|
||||
/>
|
||||
<span className="font-medium">
|
||||
{credential.name}
|
||||
</span>
|
||||
{isCustom && <Badge variant="outline">{t('custom')}</Badge>}
|
||||
</div>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`credential-${credential.id}-name`}>
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id={`credential-${credential.id}-name`}
|
||||
placeholder="GitHub"
|
||||
value={credential.name}
|
||||
onChange={(e) => updateCredential({ name: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`credential-${credential.id}-origin`}>
|
||||
{t('origin')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id={`credential-${credential.id}-origin`}
|
||||
placeholder="https://github.com"
|
||||
value={credential.origin}
|
||||
onChange={(e) => updateCredential({ origin: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`credential-${credential.id}-username`}>
|
||||
{t('username')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id={`credential-${credential.id}-username`}
|
||||
placeholder="git"
|
||||
value={credential.username}
|
||||
onChange={(e) => updateCredential({ username: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`credential-${credential.id}-password`}>
|
||||
{t('password')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<PasswordInput
|
||||
id={`credential-${credential.id}-password`}
|
||||
placeholder={t('enterPassword')}
|
||||
value={credential.password}
|
||||
onChange={(e) => updateCredential({ password: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
onClick={() => onRemove(credential.id)}
|
||||
>
|
||||
<Trash2 className="h-4 w-4 mr-2" />
|
||||
{t('delete')}
|
||||
</Button>
|
||||
</div>
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
);
|
||||
}
|
||||
|
||||
export function GitSettings() {
|
||||
const { t } = useTranslation();
|
||||
const { settings, addCredential, removeCredential, setCredentials, updateSettings, isInitialized } = useGitSettings();
|
||||
const { initiateOAuth, isLoading: isOAuthLoading, error: oauthError, isOAuthConfigured } = useGitHubOAuth();
|
||||
const { config, updateConfig } = useAppContext();
|
||||
const [customName, setCustomName] = useState('');
|
||||
const [customOrigin, setCustomOrigin] = useState('');
|
||||
const [customUsername, setCustomUsername] = useState('');
|
||||
const [customPassword, setCustomPassword] = useState('');
|
||||
const [presetTokens, setPresetTokens] = useState<Record<string, string>>({});
|
||||
const [forceManualEntry, setForceManualEntry] = useState<Record<string, boolean>>({});
|
||||
const [showAdvanced, setShowAdvanced] = useState(false);
|
||||
const [newProxyOrigin, setNewProxyOrigin] = useState('');
|
||||
|
||||
const sensors = useSensors(
|
||||
useSensor(PointerSensor),
|
||||
useSensor(KeyboardSensor, {
|
||||
coordinateGetter: sortableKeyboardCoordinates,
|
||||
})
|
||||
);
|
||||
// Dialog state
|
||||
const [selectedCredentialId, setSelectedCredentialId] = useState<string | null>(null);
|
||||
const [configDialogOpen, setConfigDialogOpen] = useState(false);
|
||||
const [selectedPreset, setSelectedPreset] = useState<PresetProvider | null>(null);
|
||||
const [addDialogOpen, setAddDialogOpen] = useState(false);
|
||||
const [customCredentialDialogOpen, setCustomCredentialDialogOpen] = useState(false);
|
||||
|
||||
const handleAddPresetProvider = (preset: PresetProvider) => {
|
||||
const token = presetTokens[preset.id] as string | undefined;
|
||||
const handleOpenCredentialDialog = (credentialId: string) => {
|
||||
setSelectedCredentialId(credentialId);
|
||||
setConfigDialogOpen(true);
|
||||
};
|
||||
|
||||
if (!token?.trim()) return;
|
||||
const handleOpenAddDialog = (preset: PresetProvider) => {
|
||||
setSelectedPreset(preset);
|
||||
setAddDialogOpen(true);
|
||||
};
|
||||
|
||||
const handleAddPresetProvider = (preset: PresetProvider, token: string) => {
|
||||
if (!token.trim()) return;
|
||||
|
||||
const newCredential: GitCredential = {
|
||||
id: crypto.randomUUID(),
|
||||
@@ -223,63 +82,30 @@ export function GitSettings() {
|
||||
password: token.trim(),
|
||||
};
|
||||
|
||||
// Auto-save: Add credential immediately to persistent storage
|
||||
addCredential(newCredential);
|
||||
|
||||
// Clear the token input for this preset
|
||||
setPresetTokens(prev => ({
|
||||
...prev,
|
||||
[preset.id]: '',
|
||||
}));
|
||||
};
|
||||
|
||||
const handleAddCustomProvider = () => {
|
||||
if (!customName.trim() || !customOrigin.trim() || !customUsername.trim() || !customPassword.trim()) return;
|
||||
|
||||
const handleAddCustomCredential = (credential: any) => {
|
||||
const newCredential: GitCredential = {
|
||||
id: crypto.randomUUID(),
|
||||
name: customName.trim(),
|
||||
origin: customOrigin.trim(),
|
||||
username: customUsername.trim(),
|
||||
password: customPassword.trim(),
|
||||
...credential,
|
||||
};
|
||||
|
||||
// Auto-save: Add credential immediately to persistent storage
|
||||
addCredential(newCredential);
|
||||
|
||||
setCustomName('');
|
||||
setCustomOrigin('');
|
||||
setCustomUsername('');
|
||||
setCustomPassword('');
|
||||
};
|
||||
|
||||
const handleRemoveCredential = (id: string) => {
|
||||
// Auto-save: Remove credential immediately from persistent storage
|
||||
removeCredential(id);
|
||||
setConfigDialogOpen(false);
|
||||
};
|
||||
|
||||
const handleUpdateCredential = (updatedCredential: GitCredential) => {
|
||||
// Auto-save: Update credential immediately in persistent storage
|
||||
const updatedCredentials = settings.credentials.map((cred) =>
|
||||
cred.id === updatedCredential.id ? updatedCredential : cred
|
||||
);
|
||||
setCredentials(updatedCredentials);
|
||||
};
|
||||
|
||||
const handleDragEnd = (event: DragEndEvent) => {
|
||||
const { active, over } = event;
|
||||
|
||||
if (active.id !== over?.id) {
|
||||
const oldIndex = settings.credentials.findIndex((cred) => cred.id === active.id);
|
||||
const newIndex = settings.credentials.findIndex((cred) => cred.id === over?.id);
|
||||
|
||||
if (oldIndex !== -1 && newIndex !== -1) {
|
||||
const newCredentials = arrayMove(settings.credentials, oldIndex, newIndex);
|
||||
setCredentials(newCredentials);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddProxyOrigin = () => {
|
||||
if (!newProxyOrigin.trim()) return;
|
||||
|
||||
@@ -354,253 +180,96 @@ export function GitSettings() {
|
||||
) : (
|
||||
<>
|
||||
{/* Configured Credentials */}
|
||||
{settings.credentials.length > 0 && (
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">{t('configuredCredentials')}</h4>
|
||||
<DndContext
|
||||
sensors={sensors}
|
||||
collisionDetection={closestCenter}
|
||||
onDragEnd={handleDragEnd}
|
||||
>
|
||||
<SortableContext
|
||||
items={settings.credentials.map((cred) => cred.id)}
|
||||
strategy={verticalListSortingStrategy}
|
||||
>
|
||||
<Accordion type="multiple" className="w-full space-y-2">
|
||||
{settings.credentials.map((credential) => (
|
||||
<SortableCredentialItem
|
||||
key={credential.id}
|
||||
credential={credential}
|
||||
onUpdate={handleUpdateCredential}
|
||||
onRemove={handleRemoveCredential}
|
||||
showDragHandle={settings.credentials.length > 1}
|
||||
/>
|
||||
))}
|
||||
</Accordion>
|
||||
</SortableContext>
|
||||
</DndContext>
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">{t('configuredCredentials')}</h4>
|
||||
<div className="grid grid-cols-[repeat(auto-fill,minmax(120px,1fr))] gap-3">
|
||||
{settings.credentials.map((credential) => {
|
||||
const preset = PRESET_PROVIDERS.find(p => p.origin === credential.origin);
|
||||
const isCustom = !preset;
|
||||
|
||||
return (
|
||||
<ProviderTile
|
||||
key={credential.id}
|
||||
iconUrl={credential.origin}
|
||||
icon={<GitBranch size={32} />}
|
||||
name={credential.name}
|
||||
onClick={() => handleOpenCredentialDialog(credential.id)}
|
||||
badge={isCustom ? <span className="text-xs px-1.5 py-0.5 bg-muted rounded">{t('custom')}</span> : undefined}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
|
||||
{/* Add Custom Credential Tile */}
|
||||
<AddProviderTile onClick={() => setCustomCredentialDialogOpen(true)} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Credential Config Dialog */}
|
||||
{selectedCredentialId !== null && (() => {
|
||||
const credential = settings.credentials.find(c => c.id === selectedCredentialId);
|
||||
const preset = credential && PRESET_PROVIDERS.find(p => p.origin === credential.origin);
|
||||
const isCustom = credential && !preset;
|
||||
|
||||
return credential && (
|
||||
<GitCredentialConfigDialog
|
||||
open={configDialogOpen}
|
||||
onOpenChange={setConfigDialogOpen}
|
||||
credential={credential}
|
||||
onUpdate={handleUpdateCredential}
|
||||
onRemove={() => handleRemoveCredential(credential.id)}
|
||||
isCustom={isCustom}
|
||||
/>
|
||||
);
|
||||
})()}
|
||||
|
||||
{/* Add Custom Credential Dialog */}
|
||||
<AddCustomGitCredentialDialog
|
||||
open={customCredentialDialogOpen}
|
||||
onOpenChange={setCustomCredentialDialogOpen}
|
||||
onAdd={handleAddCustomCredential}
|
||||
existingOrigins={settings.credentials.map(c => c.origin)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Available Preset Providers */}
|
||||
{availablePresets.length > 0 && (
|
||||
<div className="space-y-3">
|
||||
<h4 className="text-sm font-medium">{t('addProvider')}</h4>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
|
||||
<div className="grid grid-cols-[repeat(auto-fill,minmax(120px,1fr))] gap-3">
|
||||
{availablePresets.map((preset) => (
|
||||
<Card key={preset.id}>
|
||||
<CardContent className="p-4 space-y-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<ExternalFavicon
|
||||
url={preset.origin}
|
||||
size={16}
|
||||
fallback={<GitBranch size={16} />}
|
||||
/>
|
||||
<h5 className="font-medium">{preset.name}</h5>
|
||||
</div>
|
||||
|
||||
{preset.id === 'github' ? (
|
||||
// Special rendering for GitHub - OAuth button if configured, otherwise token input
|
||||
isOAuthConfigured && !forceManualEntry[preset.id] ? (
|
||||
<div className="space-y-3">
|
||||
<div className="flex gap-0">
|
||||
<Button
|
||||
onClick={initiateOAuth}
|
||||
disabled={isOAuthLoading}
|
||||
className="flex-1 rounded-r-none gap-2"
|
||||
variant="default"
|
||||
>
|
||||
{isOAuthLoading ? (
|
||||
<>
|
||||
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
|
||||
Connecting...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<ExternalFavicon
|
||||
url="https://github.com"
|
||||
size={16}
|
||||
fallback={<GitBranch size={16} />}
|
||||
/>
|
||||
<span className="truncate text-ellipsis overflow-hidden">
|
||||
Connect to GitHub
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
variant="default"
|
||||
className="rounded-l-none border-l border-primary-foreground/20 px-2"
|
||||
disabled={isOAuthLoading}
|
||||
>
|
||||
<ChevronDown className="h-4 w-4" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem
|
||||
onClick={() => setForceManualEntry(prev => ({ ...prev, [preset.id]: true }))}
|
||||
>
|
||||
Enter API key
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
{oauthError && (
|
||||
<p className="text-sm text-destructive">
|
||||
{oauthError}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex gap-2">
|
||||
<ExternalInput
|
||||
type="password"
|
||||
className="flex-1"
|
||||
placeholder={t('enterToken')}
|
||||
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);
|
||||
}
|
||||
}}
|
||||
url={preset.tokenURL}
|
||||
urlTitle="Get Token"
|
||||
/>
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
disabled={!presetTokens[preset.id]?.trim()}
|
||||
size="sm"
|
||||
className="h-10"
|
||||
>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
) : (
|
||||
// Standard rendering for other providers
|
||||
<div className="flex gap-2">
|
||||
<ExternalInput
|
||||
type="password"
|
||||
className="flex-1"
|
||||
placeholder={t('enterToken')}
|
||||
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);
|
||||
}
|
||||
}}
|
||||
url={preset.tokenURL}
|
||||
urlTitle="Get Token"
|
||||
/>
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
disabled={!presetTokens[preset.id]?.trim()}
|
||||
size="sm"
|
||||
className="h-10"
|
||||
>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
<ProviderTile
|
||||
key={preset.id}
|
||||
iconUrl={preset.origin}
|
||||
icon={<GitBranch size={32} />}
|
||||
name={preset.name}
|
||||
onClick={() => handleOpenAddDialog(preset)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Add Preset Credential Dialog */}
|
||||
{selectedPreset && (
|
||||
<AddGitCredentialDialog
|
||||
open={addDialogOpen}
|
||||
onOpenChange={setAddDialogOpen}
|
||||
preset={selectedPreset}
|
||||
oauthHook={
|
||||
selectedPreset.id === 'github' ? {
|
||||
initiateOAuth,
|
||||
isLoading: isOAuthLoading,
|
||||
error: oauthError,
|
||||
isOAuthConfigured,
|
||||
} : null
|
||||
}
|
||||
forceManualEntry={forceManualEntry[selectedPreset.id] || false}
|
||||
onSetForceManualEntry={(force) =>
|
||||
setForceManualEntry(prev => ({ ...prev, [selectedPreset.id]: force }))
|
||||
}
|
||||
onAdd={(token) => handleAddPresetProvider(selectedPreset, token)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Custom Provider */}
|
||||
<div className="space-y-3">
|
||||
<Accordion type="single" collapsible className="w-full">
|
||||
<AccordionItem value="custom-provider">
|
||||
<AccordionTrigger className="px-4 py-3 hover:no-underline">
|
||||
<div className="flex items-center gap-2">
|
||||
<Plus className="h-4 w-4" />
|
||||
<h4 className="text-sm font-medium">{t('addCustomProvider')}</h4>
|
||||
</div>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-name">
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-name"
|
||||
placeholder="My Git Server"
|
||||
value={customName}
|
||||
onChange={(e) => setCustomName(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-origin">
|
||||
{t('origin')} <span className="text-destructive">*</span>
|
||||
</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">
|
||||
{t('username')} <span className="text-destructive">*</span>
|
||||
</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">
|
||||
{t('password')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<PasswordInput
|
||||
id="custom-password"
|
||||
placeholder={t('enterPassword')}
|
||||
value={customPassword}
|
||||
onChange={(e) => setCustomPassword(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
onClick={handleAddCustomProvider}
|
||||
disabled={
|
||||
!customName.trim() ||
|
||||
!customOrigin.trim() ||
|
||||
!customUsername.trim() ||
|
||||
!customPassword.trim() ||
|
||||
customOrigin.trim() in settings.credentials
|
||||
}
|
||||
size="sm"
|
||||
className="gap-2"
|
||||
>
|
||||
<Check className="h-4 w-4" />
|
||||
{t('addCustomProviderButton')}
|
||||
</Button>
|
||||
{customOrigin.trim() in settings.credentials && (
|
||||
<p className="text-sm text-destructive">
|
||||
{t('credentialsExist')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
</Accordion>
|
||||
</div>
|
||||
|
||||
{/* Git Identity */}
|
||||
<div className="space-y-3">
|
||||
<Accordion type="single" collapsible className="w-full">
|
||||
|
||||
Reference in new issue
Block a user