mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Add special treatment for Nostr providers in the UI
This commit is contained in:
3 files changed
+170
-40
No files matched your search
@@ -185,6 +185,8 @@ const resources = {
|
||||
custom: 'Custom',
|
||||
connecting: 'Connecting...',
|
||||
connectToGitHub: 'Connect to GitHub',
|
||||
loginToNostrRequired: 'Log in to Nostr to use this provider',
|
||||
goToNostrSettings: 'Go to Nostr Settings',
|
||||
|
||||
// Git Settings
|
||||
gitSettingsDescriptionLong: 'Configure Git credentials for HTTP authentication. Settings are automatically saved and stored locally in your browser.',
|
||||
@@ -466,6 +468,8 @@ const resources = {
|
||||
custom: 'Personalizado',
|
||||
connecting: 'Conectando...',
|
||||
connectToGitHub: 'Conectar ao GitHub',
|
||||
loginToNostrRequired: 'Faça login no Nostr para usar este provedor',
|
||||
goToNostrSettings: 'Ir para Configurações do Nostr',
|
||||
|
||||
// Git Settings
|
||||
gitSettingsDescriptionLong: 'Configure credenciais do Git para autenticação HTTP. As configurações são salvas automaticamente e armazenadas localmente em seu navegador.',
|
||||
@@ -747,6 +751,8 @@ const resources = {
|
||||
custom: '自定义',
|
||||
connecting: '连接中...',
|
||||
connectToGitHub: '连接到GitHub',
|
||||
loginToNostrRequired: '登录Nostr以使用此提供商',
|
||||
goToNostrSettings: '前往Nostr设置',
|
||||
|
||||
// Git Settings
|
||||
gitSettingsDescriptionLong: '配置HTTP身份验证的Git凭据。设置会自动保存并存储在您的浏览器本地。',
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import type React from 'react';
|
||||
import { TestApp } from '@/test/TestApp';
|
||||
import { AISettings } from './AISettings';
|
||||
|
||||
// Mock the hooks
|
||||
vi.mock('@/hooks/useCurrentUser', () => ({
|
||||
useCurrentUser: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/hooks/useAISettings', () => ({
|
||||
useAISettings: vi.fn(() => ({
|
||||
settings: { providers: {} },
|
||||
addProvider: vi.fn(),
|
||||
removeProvider: vi.fn(),
|
||||
updateProvider: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock('@/hooks/useIsMobile', () => ({
|
||||
useIsMobile: vi.fn(() => false),
|
||||
}));
|
||||
|
||||
vi.mock('react-router-dom', async () => {
|
||||
const actual = await vi.importActual('react-router-dom');
|
||||
return {
|
||||
...actual,
|
||||
useNavigate: vi.fn(() => vi.fn()),
|
||||
Link: ({ children, to, ...props }: { children: React.ReactNode; to: string; [key: string]: unknown }) => (
|
||||
<a href={to} {...props}>
|
||||
{children}
|
||||
</a>
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
describe('AISettings', () => {
|
||||
it('shows Nostr login requirement for Nostr presets when user is not logged in', async () => {
|
||||
const { useCurrentUser } = await import('@/hooks/useCurrentUser');
|
||||
vi.mocked(useCurrentUser).mockReturnValue({
|
||||
user: undefined,
|
||||
users: [],
|
||||
});
|
||||
|
||||
render(
|
||||
<TestApp>
|
||||
<AISettings />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
// Should show login requirement for Shakespeare AI (Nostr preset)
|
||||
expect(screen.getByText('Log in to Nostr to use this provider')).toBeInTheDocument();
|
||||
expect(screen.getByText('Go to Nostr Settings')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows Add button for Nostr presets when user is logged in', async () => {
|
||||
const { useCurrentUser } = await import('@/hooks/useCurrentUser');
|
||||
vi.mocked(useCurrentUser).mockReturnValue({
|
||||
user: { pubkey: 'test-pubkey' } as NonNullable<ReturnType<typeof useCurrentUser>['user']>,
|
||||
users: [],
|
||||
});
|
||||
|
||||
render(
|
||||
<TestApp>
|
||||
<AISettings />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
// Should show Add button for Shakespeare AI (Nostr preset)
|
||||
const addButtons = screen.getAllByText('Add');
|
||||
expect(addButtons.length).toBeGreaterThan(0);
|
||||
|
||||
// Should not show login requirement
|
||||
expect(screen.queryByText('Log in to Nostr to use this provider')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows normal input and Add button for non-Nostr presets regardless of login status', async () => {
|
||||
const { useCurrentUser } = await import('@/hooks/useCurrentUser');
|
||||
vi.mocked(useCurrentUser).mockReturnValue({
|
||||
user: undefined,
|
||||
users: [],
|
||||
});
|
||||
|
||||
render(
|
||||
<TestApp>
|
||||
<AISettings />
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
// Should show API key inputs for non-Nostr presets like OpenAI
|
||||
expect(screen.getByText('OpenAI')).toBeInTheDocument();
|
||||
expect(screen.getByText('OpenRouter')).toBeInTheDocument();
|
||||
|
||||
// Should show Add buttons for non-Nostr presets
|
||||
const addButtons = screen.getAllByText('Add');
|
||||
expect(addButtons.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
+65
-40
@@ -11,7 +11,8 @@ import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { useIsMobile } from '@/hooks/useIsMobile';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useNavigate, Link } from 'react-router-dom';
|
||||
import type { AIConnection } from '@/contexts/AISettingsContext';
|
||||
|
||||
interface PresetProvider {
|
||||
@@ -84,6 +85,7 @@ export function AISettings() {
|
||||
const { settings, addProvider, removeProvider, updateProvider } = useAISettings();
|
||||
const isMobile = useIsMobile();
|
||||
const navigate = useNavigate();
|
||||
const { user } = useCurrentUser();
|
||||
const [customProviderName, setCustomProviderName] = useState('');
|
||||
const [customBaseURL, setCustomBaseURL] = useState('');
|
||||
const [customApiKey, setCustomApiKey] = useState('');
|
||||
@@ -272,47 +274,70 @@ export function AISettings() {
|
||||
<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">
|
||||
{availablePresets.map((preset) => (
|
||||
<div key={preset.id} className="border rounded-lg p-4 space-y-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<h5 className="font-medium">{preset.name}</h5>
|
||||
{(preset.apiKeysURL && preset.id !== "routstr") && (
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs text-muted-foreground underline hover:text-foreground"
|
||||
onClick={() => window.open(preset.apiKeysURL, '_blank')}
|
||||
>
|
||||
{t('getApiKey')}
|
||||
</button>
|
||||
{availablePresets.map((preset) => {
|
||||
const isNostrPreset = preset.nostr;
|
||||
const isLoggedIntoNostr = !!user;
|
||||
const showNostrLoginRequired = isNostrPreset && !isLoggedIntoNostr;
|
||||
|
||||
return (
|
||||
<div key={preset.id} className="border rounded-lg p-4 space-y-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<h5 className="font-medium">{preset.name}</h5>
|
||||
{(preset.apiKeysURL && preset.id !== "routstr") && (
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs text-muted-foreground underline hover:text-foreground"
|
||||
onClick={() => window.open(preset.apiKeysURL, '_blank')}
|
||||
>
|
||||
{t('getApiKey')}
|
||||
</button>
|
||||
)}
|
||||
</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="flex gap-2">
|
||||
{!preset.nostr && (
|
||||
<PasswordInput
|
||||
placeholder={preset.id === "routstr" ? t('enterCashuToken') : t('enterApiKey')}
|
||||
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()}
|
||||
className={preset.nostr ? "w-full" : "h-10 px-4 ml-auto"}
|
||||
>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
{!preset.nostr && (
|
||||
<PasswordInput
|
||||
placeholder={preset.id === "routstr" ? t('enterCashuToken') : t('enterApiKey')}
|
||||
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()}
|
||||
className="h-10 px-4 ml-auto"
|
||||
>
|
||||
{t('add')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user