mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Render dialog outside of accordion
This commit is contained in:
4 files changed
+154
-24
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import { TestApp } from '@/test/TestApp';
|
||||
import { CreditsBadge } from './CreditsBadge';
|
||||
import type { AIConnection } from '@/contexts/AISettingsContext';
|
||||
|
||||
// Mock the useAICredits hook
|
||||
vi.mock('@/hooks/useAICredits', () => ({
|
||||
useAICredits: () => ({
|
||||
data: { amount: 10.50 },
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock the fetch function
|
||||
global.fetch = vi.fn();
|
||||
|
||||
const mockConnection: AIConnection = {
|
||||
baseURL: 'https://api.test.com/v1',
|
||||
apiKey: 'test-key',
|
||||
};
|
||||
|
||||
describe('CreditsBadge', () => {
|
||||
it('renders the credits amount', () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<CreditsBadge
|
||||
providerId="test-provider"
|
||||
connection={mockConnection}
|
||||
/>
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
expect(screen.getByText('$10.50')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls onOpenDialog when badge is clicked', () => {
|
||||
const mockCallback = vi.fn();
|
||||
|
||||
render(
|
||||
<TestApp>
|
||||
<CreditsBadge
|
||||
providerId="test-provider"
|
||||
connection={mockConnection}
|
||||
onOpenDialog={mockCallback}
|
||||
/>
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
const badge = screen.getByText('$10.50');
|
||||
fireEvent.click(badge);
|
||||
|
||||
expect(mockCallback).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('has proper styling for clickable badge', () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<CreditsBadge
|
||||
providerId="test-provider"
|
||||
connection={mockConnection}
|
||||
/>
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
const badge = screen.getByText('$10.50');
|
||||
expect(badge).toHaveClass('cursor-pointer');
|
||||
expect(badge).toHaveClass('hover:bg-secondary/80');
|
||||
});
|
||||
|
||||
it('does not render dialog directly', () => {
|
||||
render(
|
||||
<TestApp>
|
||||
<CreditsBadge
|
||||
providerId="test-provider"
|
||||
connection={mockConnection}
|
||||
/>
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
// Dialog should not be rendered by CreditsBadge itself
|
||||
expect(screen.queryByText('Credits - test-provider')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -1,21 +1,19 @@
|
||||
import { useState } from 'react';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { useAICredits } from '@/hooks/useAICredits';
|
||||
import { CreditsDialog } from '@/components/CreditsDialog';
|
||||
import type { AIConnection } from '@/contexts/AISettingsContext';
|
||||
|
||||
interface CreditsBadgeProps {
|
||||
providerId: string;
|
||||
connection: AIConnection;
|
||||
className?: string;
|
||||
onOpenDialog?: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a clickable credits badge showing the remaining credits for an AI provider
|
||||
*/
|
||||
export function CreditsBadge({ providerId, connection, className }: CreditsBadgeProps) {
|
||||
export function CreditsBadge({ providerId, connection, className, onOpenDialog }: CreditsBadgeProps) {
|
||||
const { data: credits, isLoading, error } = useAICredits(providerId, connection);
|
||||
const [dialogOpen, setDialogOpen] = useState(false);
|
||||
|
||||
// Don't render anything if there's an error (provider doesn't support credits endpoint)
|
||||
if (error || isLoading) {
|
||||
@@ -36,24 +34,15 @@ export function CreditsBadge({ providerId, connection, className }: CreditsBadge
|
||||
}).format(credits.amount);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Badge
|
||||
variant="secondary"
|
||||
className={`cursor-pointer hover:bg-secondary/80 transition-colors ${className}`}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
setDialogOpen(true);
|
||||
}}
|
||||
>
|
||||
{formattedAmount}
|
||||
</Badge>
|
||||
|
||||
<CreditsDialog
|
||||
open={dialogOpen}
|
||||
onOpenChange={setDialogOpen}
|
||||
providerId={providerId}
|
||||
connection={connection}
|
||||
/>
|
||||
</>
|
||||
<Badge
|
||||
variant="secondary"
|
||||
className={`cursor-pointer hover:bg-secondary/80 transition-colors ${className}`}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onOpenDialog?.();
|
||||
}}
|
||||
>
|
||||
{formattedAmount}
|
||||
</Badge>
|
||||
);
|
||||
}
|
||||
@@ -94,4 +94,32 @@ describe('CreditsDialog', () => {
|
||||
|
||||
expect(screen.getByText('Please log in to purchase credits')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls onDialogStateChange callback when dialog state changes', () => {
|
||||
const mockCallback = vi.fn();
|
||||
const { rerender } = render(
|
||||
<TestApp>
|
||||
<CreditsDialog
|
||||
open={false}
|
||||
onOpenChange={mockCallback}
|
||||
providerId="test-provider"
|
||||
connection={mockConnection}
|
||||
/>
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
// Simulate opening the dialog
|
||||
rerender(
|
||||
<TestApp>
|
||||
<CreditsDialog
|
||||
open={true}
|
||||
onOpenChange={mockCallback}
|
||||
providerId="test-provider"
|
||||
connection={mockConnection}
|
||||
/>
|
||||
</TestApp>
|
||||
);
|
||||
|
||||
expect(screen.getByText('Credits - test-provider')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -8,6 +8,7 @@ import { Label } from '@/components/ui/label';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion';
|
||||
import { CreditsBadge } from '@/components/CreditsBadge';
|
||||
import { CreditsDialog } from '@/components/CreditsDialog';
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { useIsMobile } from '@/hooks/useIsMobile';
|
||||
@@ -91,6 +92,7 @@ export function AISettings() {
|
||||
const [customApiKey, setCustomApiKey] = useState('');
|
||||
const [customAuthMethod, setCustomAuthMethod] = useState<'api-key' | 'nostr'>('api-key');
|
||||
const [presetApiKeys, setPresetApiKeys] = useState<Record<string, string>>({});
|
||||
const [activeCreditsDialog, setActiveCreditsDialog] = useState<string | null>(null);
|
||||
|
||||
const handleAddPresetProvider = (preset: PresetProvider) => {
|
||||
const apiKey = presetApiKeys[preset.id] as string | undefined;
|
||||
@@ -144,6 +146,14 @@ export function AISettings() {
|
||||
updateProvider(name, connection);
|
||||
};
|
||||
|
||||
const handleOpenCreditsDialog = (providerId: string) => {
|
||||
setActiveCreditsDialog(providerId);
|
||||
};
|
||||
|
||||
const handleCloseCreditsDialog = () => {
|
||||
setActiveCreditsDialog(null);
|
||||
};
|
||||
|
||||
const configuredProviderIds = Object.keys(settings.providers);
|
||||
const availablePresets = PRESET_PROVIDERS.filter(preset => !configuredProviderIds.includes(preset.id));
|
||||
|
||||
@@ -204,7 +214,11 @@ export function AISettings() {
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<CreditsBadge providerId={providerId} connection={provider} />
|
||||
<CreditsBadge
|
||||
providerId={providerId}
|
||||
connection={provider}
|
||||
onOpenDialog={() => handleOpenCreditsDialog(providerId)}
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
@@ -426,6 +440,20 @@ export function AISettings() {
|
||||
</Accordion>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Render credits dialog outside of accordion structure */}
|
||||
{activeCreditsDialog && (
|
||||
<CreditsDialog
|
||||
open={true}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) {
|
||||
handleCloseCreditsDialog();
|
||||
}
|
||||
}}
|
||||
providerId={activeCreditsDialog}
|
||||
connection={settings.providers[activeCreditsDialog]}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user