mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add GitSettingsDialog
This commit is contained in:
9 files changed
+775
-3
No files matched your search
+4
-1
@@ -13,6 +13,7 @@ import { NostrLoginProvider } from '@nostrify/react/login';
|
||||
import { AppProvider } from '@/components/AppProvider';
|
||||
import { AppConfig } from '@/contexts/AppContext';
|
||||
import { AISettingsProvider } from '@/components/AISettingsProvider';
|
||||
import { GitSettingsProvider } from '@/components/GitSettingsProvider';
|
||||
import { SessionManagerProvider } from '@/components/SessionManagerProvider';
|
||||
import { FSProvider } from '@/components/FSProvider';
|
||||
import { LightningFSAdapter } from '@/lib/LightningFSAdapter';
|
||||
@@ -61,7 +62,8 @@ export function App() {
|
||||
<NostrLoginProvider storageKey='nostr:login'>
|
||||
<NostrProvider>
|
||||
<AISettingsProvider>
|
||||
<SessionManagerProvider>
|
||||
<GitSettingsProvider>
|
||||
<SessionManagerProvider>
|
||||
<TooltipProvider>
|
||||
<Toaster />
|
||||
<Suspense>
|
||||
@@ -69,6 +71,7 @@ export function App() {
|
||||
</Suspense>
|
||||
</TooltipProvider>
|
||||
</SessionManagerProvider>
|
||||
</GitSettingsProvider>
|
||||
</AISettingsProvider>
|
||||
</NostrProvider>
|
||||
</NostrLoginProvider>
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,328 @@
|
||||
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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import { ReactNode } from 'react';
|
||||
import { GitSettingsProvider } from './GitSettingsProvider';
|
||||
import { useGitSettings } from '@/hooks/useGitSettings';
|
||||
import type { GitCredential } from '@/contexts/GitSettingsContext';
|
||||
|
||||
// Test component that uses the hook
|
||||
function TestComponent() {
|
||||
const { settings, addCredential, removeCredential, updateCredential, isConfigured } = useGitSettings();
|
||||
|
||||
const handleAdd = () => {
|
||||
const credential: GitCredential = {
|
||||
username: 'git',
|
||||
password: 'test-token',
|
||||
};
|
||||
addCredential('https://github.com', credential);
|
||||
};
|
||||
|
||||
const handleUpdate = () => {
|
||||
updateCredential('https://github.com', { password: 'updated-token' });
|
||||
};
|
||||
|
||||
const handleRemove = () => {
|
||||
removeCredential('https://github.com');
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div data-testid="configured">{isConfigured ? 'configured' : 'not-configured'}</div>
|
||||
<div data-testid="count">{Object.keys(settings.credentials).length}</div>
|
||||
<button onClick={handleAdd}>Add GitHub</button>
|
||||
<button onClick={handleUpdate}>Update GitHub</button>
|
||||
<button onClick={handleRemove}>Remove GitHub</button>
|
||||
{settings.credentials['https://github.com'] && (
|
||||
<div data-testid="github-creds">
|
||||
{settings.credentials['https://github.com'].username}:
|
||||
{settings.credentials['https://github.com'].password}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function TestWrapper({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<GitSettingsProvider>
|
||||
{children}
|
||||
</GitSettingsProvider>
|
||||
);
|
||||
}
|
||||
|
||||
describe('GitSettingsProvider', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
});
|
||||
|
||||
it('provides initial empty state', () => {
|
||||
render(
|
||||
<TestWrapper>
|
||||
<TestComponent />
|
||||
</TestWrapper>
|
||||
);
|
||||
|
||||
expect(screen.getByTestId('configured')).toHaveTextContent('not-configured');
|
||||
expect(screen.getByTestId('count')).toHaveTextContent('0');
|
||||
});
|
||||
|
||||
it('can add credentials', () => {
|
||||
render(
|
||||
<TestWrapper>
|
||||
<TestComponent />
|
||||
</TestWrapper>
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByText('Add GitHub'));
|
||||
|
||||
expect(screen.getByTestId('configured')).toHaveTextContent('configured');
|
||||
expect(screen.getByTestId('count')).toHaveTextContent('1');
|
||||
expect(screen.getByTestId('github-creds')).toHaveTextContent('git:test-token');
|
||||
});
|
||||
|
||||
it('can update credentials', () => {
|
||||
render(
|
||||
<TestWrapper>
|
||||
<TestComponent />
|
||||
</TestWrapper>
|
||||
);
|
||||
|
||||
// First add a credential
|
||||
fireEvent.click(screen.getByText('Add GitHub'));
|
||||
expect(screen.getByTestId('github-creds')).toHaveTextContent('git:test-token');
|
||||
|
||||
// Then update it
|
||||
fireEvent.click(screen.getByText('Update GitHub'));
|
||||
expect(screen.getByTestId('github-creds')).toHaveTextContent('git:updated-token');
|
||||
});
|
||||
|
||||
it('can remove credentials', () => {
|
||||
render(
|
||||
<TestWrapper>
|
||||
<TestComponent />
|
||||
</TestWrapper>
|
||||
);
|
||||
|
||||
// First add a credential
|
||||
fireEvent.click(screen.getByText('Add GitHub'));
|
||||
expect(screen.getByTestId('configured')).toHaveTextContent('configured');
|
||||
expect(screen.getByTestId('count')).toHaveTextContent('1');
|
||||
|
||||
// Then remove it
|
||||
fireEvent.click(screen.getByText('Remove GitHub'));
|
||||
expect(screen.getByTestId('configured')).toHaveTextContent('not-configured');
|
||||
expect(screen.getByTestId('count')).toHaveTextContent('0');
|
||||
});
|
||||
|
||||
it('persists settings to localStorage', () => {
|
||||
render(
|
||||
<TestWrapper>
|
||||
<TestComponent />
|
||||
</TestWrapper>
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByText('Add GitHub'));
|
||||
|
||||
// Check that settings were saved to localStorage
|
||||
const stored = localStorage.getItem('git-settings');
|
||||
expect(stored).toBeTruthy();
|
||||
|
||||
const parsed = JSON.parse(stored!);
|
||||
expect(parsed.credentials['https://github.com']).toEqual({
|
||||
username: 'git',
|
||||
password: 'test-token',
|
||||
});
|
||||
});
|
||||
|
||||
it('loads settings from localStorage', () => {
|
||||
// Pre-populate localStorage
|
||||
const settings = {
|
||||
credentials: {
|
||||
'https://gitlab.com': {
|
||||
username: 'git',
|
||||
password: 'gitlab-token',
|
||||
},
|
||||
},
|
||||
};
|
||||
localStorage.setItem('git-settings', JSON.stringify(settings));
|
||||
|
||||
render(
|
||||
<TestWrapper>
|
||||
<TestComponent />
|
||||
</TestWrapper>
|
||||
);
|
||||
|
||||
expect(screen.getByTestId('configured')).toHaveTextContent('configured');
|
||||
expect(screen.getByTestId('count')).toHaveTextContent('1');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,90 @@
|
||||
import { ReactNode, useState, useEffect } from 'react';
|
||||
import { GitSettingsContext, type GitSettings, type GitCredential, type GitSettingsContextType } from '@/contexts/GitSettingsContext';
|
||||
|
||||
interface GitSettingsProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: GitSettings = {
|
||||
credentials: {},
|
||||
};
|
||||
|
||||
const STORAGE_KEY = 'git-settings';
|
||||
|
||||
export function GitSettingsProvider({ children }: GitSettingsProviderProps) {
|
||||
const [settings, setSettings] = useState<GitSettings>(() => {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored) {
|
||||
try {
|
||||
const parsed = JSON.parse(stored);
|
||||
if (parsed && typeof parsed === 'object' && 'credentials' in parsed) {
|
||||
const settings = parsed as Partial<GitSettings>;
|
||||
return {
|
||||
credentials: settings.credentials || {},
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
return DEFAULT_SETTINGS;
|
||||
}
|
||||
}
|
||||
return DEFAULT_SETTINGS;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
|
||||
}, [settings]);
|
||||
|
||||
const updateSettings = (newSettings: Partial<GitSettings>) => {
|
||||
setSettings(prev => ({ ...prev, ...newSettings }));
|
||||
};
|
||||
|
||||
const addCredential = (origin: string, credential: GitCredential) => {
|
||||
setSettings(prev => ({
|
||||
...prev,
|
||||
credentials: {
|
||||
...prev.credentials,
|
||||
[origin]: credential,
|
||||
},
|
||||
}));
|
||||
};
|
||||
|
||||
const removeCredential = (origin: string) => {
|
||||
setSettings(prev => {
|
||||
const { [origin]: removed, ...rest } = prev.credentials;
|
||||
return {
|
||||
...prev,
|
||||
credentials: rest,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const updateCredential = (origin: string, credential: Partial<GitCredential>) => {
|
||||
setSettings(prev => ({
|
||||
...prev,
|
||||
credentials: {
|
||||
...prev.credentials,
|
||||
[origin]: {
|
||||
...prev.credentials[origin],
|
||||
...credential,
|
||||
},
|
||||
},
|
||||
}));
|
||||
};
|
||||
|
||||
const isConfigured = Object.entries(settings.credentials).length > 0;
|
||||
|
||||
const contextValue: GitSettingsContextType = {
|
||||
settings,
|
||||
updateSettings,
|
||||
addCredential,
|
||||
removeCredential,
|
||||
updateCredential,
|
||||
isConfigured,
|
||||
};
|
||||
|
||||
return (
|
||||
<GitSettingsContext.Provider value={contextValue}>
|
||||
{children}
|
||||
</GitSettingsContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigge
|
||||
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,6 +92,7 @@ export function ProjectSidebar({
|
||||
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();
|
||||
@@ -253,6 +255,14 @@ export function ProjectSidebar({
|
||||
<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
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
@@ -314,6 +324,12 @@ export function ProjectSidebar({
|
||||
open={aiSettingsOpen}
|
||||
onOpenChange={setAISettingsOpen}
|
||||
/>
|
||||
|
||||
{/* Git Settings Dialog */}
|
||||
<GitSettingsDialog
|
||||
open={gitSettingsOpen}
|
||||
onOpenChange={setGitSettingsOpen}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { createContext } from 'react';
|
||||
|
||||
export interface GitCredential {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface GitSettings {
|
||||
credentials: Record<string, GitCredential>; // keyed by origin
|
||||
}
|
||||
|
||||
export interface GitSettingsContextType {
|
||||
settings: GitSettings;
|
||||
updateSettings: (settings: Partial<GitSettings>) => void;
|
||||
addCredential: (origin: string, credential: GitCredential) => void;
|
||||
removeCredential: (origin: string) => void;
|
||||
updateCredential: (origin: string, credential: Partial<GitCredential>) => void;
|
||||
isConfigured: boolean;
|
||||
}
|
||||
|
||||
export const GitSettingsContext = createContext<GitSettingsContextType | undefined>(undefined);
|
||||
@@ -0,0 +1,14 @@
|
||||
import { useContext } from 'react';
|
||||
import { GitSettingsContext, type GitSettingsContextType } from '@/contexts/GitSettingsContext';
|
||||
|
||||
/**
|
||||
* Hook to access and update Git settings
|
||||
* @returns Git settings context with settings and update methods
|
||||
*/
|
||||
export function useGitSettings(): GitSettingsContextType {
|
||||
const context = useContext(GitSettingsContext);
|
||||
if (!context) {
|
||||
throw new Error('useGitSettings must be used within GitSettingsProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { AppConfig } from '@/contexts/AppContext';
|
||||
import { FSProvider } from '@/components/FSProvider';
|
||||
import { LightningFSAdapter } from '@/lib/LightningFSAdapter';
|
||||
import { AISettingsProvider } from '@/components/AISettingsProvider';
|
||||
import { GitSettingsProvider } from '@/components/GitSettingsProvider';
|
||||
import { SessionManagerProvider } from '@/components/SessionManagerProvider';
|
||||
|
||||
interface TestAppProps {
|
||||
@@ -41,7 +42,8 @@ export function TestApp({ children }: TestAppProps) {
|
||||
<FSProvider fs={fs}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<AISettingsProvider>
|
||||
<NostrLoginProvider storageKey='test-login'>
|
||||
<GitSettingsProvider>
|
||||
<NostrLoginProvider storageKey='test-login'>
|
||||
<NostrProvider>
|
||||
<SessionManagerProvider>
|
||||
<BrowserRouter>
|
||||
@@ -49,7 +51,8 @@ export function TestApp({ children }: TestAppProps) {
|
||||
</BrowserRouter>
|
||||
</SessionManagerProvider>
|
||||
</NostrProvider>
|
||||
</NostrLoginProvider>
|
||||
</NostrLoginProvider>
|
||||
</GitSettingsProvider>
|
||||
</AISettingsProvider>
|
||||
</QueryClientProvider>
|
||||
</FSProvider>
|
||||
|
||||
Reference in new issue
Block a user