mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Add dedicated settings pages
This commit is contained in:
8 files changed
+170
-293
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
Dialog,
|
||||
@@ -40,7 +41,7 @@ import { useFS } from '@/hooks/useFS';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { findCredentialsForRepo, getOriginDisplayName } from '@/lib/gitCredentials';
|
||||
import { GitSettingsDialog } from './GitSettingsDialog';
|
||||
|
||||
import git from 'isomorphic-git';
|
||||
import http from 'isomorphic-git/http/web';
|
||||
|
||||
@@ -56,7 +57,7 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog
|
||||
const [isPulling, setIsPulling] = useState(false);
|
||||
const [pushResult, setPushResult] = useState<string | null>(null);
|
||||
const [pullResult, setPullResult] = useState<string | null>(null);
|
||||
const [showGitSettings, setShowGitSettings] = useState(false);
|
||||
const navigate = useNavigate();
|
||||
|
||||
const { data: gitStatus, refetch: refetchGitStatus } = useGitStatus(projectId);
|
||||
const { settings } = useGitSettings();
|
||||
@@ -382,7 +383,7 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setShowGitSettings(true)}
|
||||
onClick={() => navigate('/settings/git')}
|
||||
className="h-7 gap-1 text-xs"
|
||||
>
|
||||
<Settings className="h-3 w-3" />
|
||||
@@ -427,7 +428,7 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog
|
||||
No credentials configured for {originDisplayName}.
|
||||
Push/pull operations may fail for private repositories.{' '}
|
||||
<button
|
||||
onClick={() => setShowGitSettings(true)}
|
||||
onClick={() => navigate('/settings/git')}
|
||||
className="underline hover:no-underline font-medium"
|
||||
>
|
||||
Configure credentials
|
||||
@@ -556,12 +557,6 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog
|
||||
)}
|
||||
</div>
|
||||
</ScrollArea>
|
||||
|
||||
{/* Git Settings Dialog */}
|
||||
<GitSettingsDialog
|
||||
open={showGitSettings}
|
||||
onOpenChange={setShowGitSettings}
|
||||
/>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
import { TestApp } from '@/test/TestApp';
|
||||
import { GitSettingsDialog } from './GitSettingsDialog';
|
||||
|
||||
describe('GitSettingsDialog', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
});
|
||||
|
||||
it('renders the dialog with trigger button', () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<GitSettingsDialog />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
expect(screen.getByRole('button', { name: /git settings/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('opens dialog when trigger is clicked', async () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<GitSettingsDialog />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: /git settings/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByRole('dialog')).toBeInTheDocument();
|
||||
expect(screen.getByRole('heading', { name: 'Git Settings' })).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows preset providers (GitHub and GitLab)', async () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<GitSettingsDialog open={true} />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
expect(screen.getByText('GitHub')).toBeInTheDocument();
|
||||
expect(screen.getByText('GitLab')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('can add a preset provider with token', async () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<GitSettingsDialog open={true} />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
// Find GitHub token input and add button
|
||||
const tokenInputs = screen.getAllByPlaceholderText('Enter your token');
|
||||
const addButtons = screen.getAllByText('Add');
|
||||
|
||||
// Assuming GitHub is the first preset
|
||||
const githubTokenInput = tokenInputs[0];
|
||||
const githubAddButton = addButtons[0];
|
||||
|
||||
expect(githubTokenInput).toBeInTheDocument();
|
||||
expect(githubAddButton).toBeInTheDocument();
|
||||
|
||||
fireEvent.change(githubTokenInput, { target: { value: 'test-token' } });
|
||||
fireEvent.click(githubAddButton);
|
||||
|
||||
// Check that the provider was added to configured providers
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Configured Credentials')).toBeInTheDocument();
|
||||
}, { timeout: 3000 });
|
||||
});
|
||||
|
||||
it('can add custom provider', async () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<GitSettingsDialog open={true} />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
// Fill custom provider form
|
||||
const originInput = screen.getByLabelText(/origin/i);
|
||||
const usernameInput = screen.getByLabelText(/username/i);
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const addCustomButton = screen.getByRole('button', { name: /add custom provider/i });
|
||||
|
||||
fireEvent.change(originInput, { target: { value: 'https://git.example.com' } });
|
||||
fireEvent.change(usernameInput, { target: { value: 'testuser' } });
|
||||
fireEvent.change(passwordInput, { target: { value: 'testpass' } });
|
||||
|
||||
expect(addCustomButton).not.toBeDisabled();
|
||||
fireEvent.click(addCustomButton);
|
||||
|
||||
// Check that the provider was added
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Configured Credentials')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows red dot when not configured', () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<GitSettingsDialog />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
// Check for red dot indicator
|
||||
const redDot = document.querySelector('.bg-red-500');
|
||||
expect(redDot).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('validates custom provider form', () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<GitSettingsDialog open={true} />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
const addCustomButton = screen.getByRole('button', { name: /add custom provider/i });
|
||||
|
||||
// Button should be disabled when form is empty
|
||||
expect(addCustomButton).toBeDisabled();
|
||||
|
||||
// Fill only origin
|
||||
const originInput = screen.getByLabelText(/origin/i);
|
||||
fireEvent.change(originInput, { target: { value: 'https://git.example.com' } });
|
||||
expect(addCustomButton).toBeDisabled();
|
||||
|
||||
// Fill origin and username
|
||||
const usernameInput = screen.getByLabelText(/username/i);
|
||||
fireEvent.change(usernameInput, { target: { value: 'testuser' } });
|
||||
expect(addCustomButton).toBeDisabled();
|
||||
|
||||
// Fill all fields
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
fireEvent.change(passwordInput, { target: { value: 'testpass' } });
|
||||
expect(addCustomButton).not.toBeDisabled();
|
||||
});
|
||||
});
|
||||
@@ -1,328 +0,0 @@
|
||||
import { useState } from 'react';
|
||||
import { GitBranch, Trash2, Check } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
|
||||
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 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",
|
||||
},
|
||||
];
|
||||
|
||||
interface GitSettingsDialogProps {
|
||||
open?: boolean;
|
||||
onOpenChange?: (open: boolean) => void;
|
||||
}
|
||||
|
||||
export function GitSettingsDialog({ open: controlledOpen, onOpenChange }: GitSettingsDialogProps = {}) {
|
||||
const { settings, addCredential, removeCredential, updateCredential, isConfigured } = useGitSettings();
|
||||
const [internalOpen, setInternalOpen] = useState(false);
|
||||
|
||||
// Use controlled or internal state
|
||||
const open = controlledOpen !== undefined ? controlledOpen : internalOpen;
|
||||
const setOpen = onOpenChange || setInternalOpen;
|
||||
const [customOrigin, setCustomOrigin] = useState('');
|
||||
const [customUsername, setCustomUsername] = useState('');
|
||||
const [customPassword, setCustomPassword] = useState('');
|
||||
const [presetTokens, setPresetTokens] = useState<Record<string, string>>({});
|
||||
|
||||
const handleCancel = () => {
|
||||
setCustomOrigin('');
|
||||
setCustomUsername('');
|
||||
setCustomPassword('');
|
||||
setPresetTokens({});
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
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 (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
{/* Only show trigger if not controlled externally */}
|
||||
{controlledOpen === undefined && (
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="ghost" size="sm" className="gap-2">
|
||||
<GitBranch className="h-4 w-4" />
|
||||
Git Settings
|
||||
{!isConfigured && (
|
||||
<span className="ml-1 h-2 w-2 rounded-full bg-red-500" />
|
||||
)}
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
)}
|
||||
<DialogContent className="sm:max-w-[700px] max-h-[80vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Git Settings</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure Git credentials for HTTP authentication. Settings are automatically saved and stored locally in your browser.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-6 py-4">
|
||||
{/* 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"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</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>
|
||||
|
||||
<div className="flex justify-end">
|
||||
<Button variant="outline" onClick={handleCancel}>
|
||||
Close
|
||||
</Button>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useState, useMemo } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Check, ChevronDown, Edit3, RefreshCw, AlertCircle, Settings } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
@@ -8,7 +9,6 @@ import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { useProviderModels } from '@/hooks/useProviderModels';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { AISettingsDialog } from '@/components/ai/AISettingsDialog';
|
||||
|
||||
interface ModelSelectorProps {
|
||||
value: string;
|
||||
@@ -28,9 +28,9 @@ export function ModelSelector({
|
||||
const [open, setOpen] = useState(false);
|
||||
const [isCustomInput, setIsCustomInput] = useState(false);
|
||||
const [customValue, setCustomValue] = useState('');
|
||||
const [settingsOpen, setSettingsOpen] = useState(false);
|
||||
const { settings, addRecentlyUsedModel, isConfigured } = useAISettings();
|
||||
const { models, isLoading, error, refetch } = useProviderModels();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const recentlyUsedModels = useMemo(() => settings.recentlyUsedModels || [], [settings.recentlyUsedModels]);
|
||||
|
||||
@@ -58,7 +58,7 @@ export function ModelSelector({
|
||||
}
|
||||
|
||||
if (selectedValue === '__manage_providers__') {
|
||||
setSettingsOpen(true);
|
||||
navigate('/settings/ai');
|
||||
setOpen(false);
|
||||
return;
|
||||
}
|
||||
@@ -112,12 +112,7 @@ export function ModelSelector({
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<AISettingsDialog
|
||||
open={settingsOpen}
|
||||
onOpenChange={setSettingsOpen}
|
||||
/>
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<Popover open={open} onOpenChange={setOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
@@ -257,6 +252,5 @@ export function ModelSelector({
|
||||
</Command>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -6,8 +6,6 @@ import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||
import { LoginArea } from '@/components/auth/LoginArea';
|
||||
import { StarButton } from '@/components/StarButton';
|
||||
import { AISettingsDialog } from '@/components/ai/AISettingsDialog';
|
||||
import { GitSettingsDialog } from '@/components/GitSettingsDialog';
|
||||
import { useProjectsManager } from '@/hooks/useProjectsManager';
|
||||
import { useProjectSessionStatus } from '@/hooks/useProjectSessionStatus';
|
||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||
@@ -91,8 +89,6 @@ export function ProjectSidebar({
|
||||
const [projects, setProjects] = useState<Project[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isExporting, setIsExporting] = useState(false);
|
||||
const [aiSettingsOpen, setAISettingsOpen] = useState(false);
|
||||
const [gitSettingsOpen, setGitSettingsOpen] = useState(false);
|
||||
const [favorites] = useLocalStorage<string[]>('project-favorites', []);
|
||||
const projectsManager = useProjectsManager();
|
||||
const navigate = useNavigate();
|
||||
@@ -249,19 +245,11 @@ export function ProjectSidebar({
|
||||
</DropdownMenuItem>
|
||||
|
||||
<DropdownMenuItem
|
||||
onClick={() => setAISettingsOpen(true)}
|
||||
onClick={() => navigate('/settings')}
|
||||
className="flex items-center gap-2 w-full"
|
||||
>
|
||||
<Settings className="h-4 w-4" />
|
||||
AI Settings
|
||||
</DropdownMenuItem>
|
||||
|
||||
<DropdownMenuItem
|
||||
onClick={() => setGitSettingsOpen(true)}
|
||||
className="flex items-center gap-2 w-full"
|
||||
>
|
||||
<GitBranch className="h-4 w-4" />
|
||||
Git Settings
|
||||
Settings
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
@@ -318,18 +306,6 @@ export function ProjectSidebar({
|
||||
<div className="p-4 border-t border-sidebar-border bg-gradient-to-r from-primary/5 to-accent/5 mt-auto">
|
||||
<LoginArea className="w-full" />
|
||||
</div>
|
||||
|
||||
{/* AI Settings Dialog */}
|
||||
<AISettingsDialog
|
||||
open={aiSettingsOpen}
|
||||
onOpenChange={setAISettingsOpen}
|
||||
/>
|
||||
|
||||
{/* Git Settings Dialog */}
|
||||
<GitSettingsDialog
|
||||
open={gitSettingsOpen}
|
||||
onOpenChange={setGitSettingsOpen}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,405 +0,0 @@
|
||||
import { useState } from 'react';
|
||||
import { Settings, Trash2, Check } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog';
|
||||
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 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",
|
||||
},
|
||||
];
|
||||
|
||||
interface AISettingsDialogProps {
|
||||
open?: boolean;
|
||||
onOpenChange?: (open: boolean) => void;
|
||||
}
|
||||
|
||||
export function AISettingsDialog({ open: controlledOpen, onOpenChange }: AISettingsDialogProps = {}) {
|
||||
const { settings, addProvider, removeProvider, updateProvider, isConfigured } = useAISettings();
|
||||
const [internalOpen, setInternalOpen] = useState(false);
|
||||
|
||||
// Use controlled or internal state
|
||||
const open = controlledOpen !== undefined ? controlledOpen : internalOpen;
|
||||
const setOpen = onOpenChange || setInternalOpen;
|
||||
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 handleCancel = () => {
|
||||
setCustomProviderName('');
|
||||
setCustomBaseURL('');
|
||||
setCustomApiKey('');
|
||||
setCustomAuthMethod('api-key');
|
||||
setPresetApiKeys({});
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
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 (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
{/* Only show trigger if not controlled externally */}
|
||||
{controlledOpen === undefined && (
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="ghost" size="sm" className="gap-2">
|
||||
<Settings className="h-4 w-4" />
|
||||
AI Settings
|
||||
{!isConfigured && (
|
||||
<span className="ml-1 h-2 w-2 rounded-full bg-red-500" />
|
||||
)}
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
)}
|
||||
<DialogContent className="sm:max-w-[700px] max-h-[80vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>AI Settings</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure AI providers by adding your API keys. Settings are automatically saved and stored locally in your browser.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-6 py-4">
|
||||
{/* 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"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</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>
|
||||
|
||||
<div className="flex justify-end">
|
||||
<Button variant="outline" onClick={handleCancel}>
|
||||
Close
|
||||
</Button>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user