Consolidate action logic into Quilly

This commit is contained in:
Alex Gleason committed 2025-09-24 20:53:24 -05:00
1 parent d932f05d04
commit 7057c34f01
4 files changed
+94 -112

No files matched your search

+26 -9
View File
@@ -35,27 +35,44 @@ describe('Quilly', () => {
expect(onDismiss).toHaveBeenCalledTimes(1);
});
it('renders action button when provided', () => {
const error = new Error('Test error');
it('renders action button for console errors', () => {
const error = new Error('Console errors detected in your app. Would you like me to help fix them?');
const onDismiss = vi.fn();
const actionOnClick = vi.fn();
const onFixConsoleErrors = vi.fn();
render(
<Quilly
error={error}
onDismiss={onDismiss}
action={{
label: 'Fix it',
onClick: actionOnClick
}}
onFixConsoleErrors={onFixConsoleErrors}
/>
);
const actionButton = screen.getByRole('button', { name: 'Fix it' });
const actionButton = screen.getByRole('button', { name: 'Fix errors' });
expect(actionButton).toBeInTheDocument();
fireEvent.click(actionButton);
expect(actionOnClick).toHaveBeenCalledTimes(1);
expect(onFixConsoleErrors).toHaveBeenCalledTimes(1);
});
it('renders action button for model not found errors', () => {
const error = new Error('model_not_found: The specified model does not exist');
const onDismiss = vi.fn();
const onOpenModelSelector = vi.fn();
render(
<Quilly
error={error}
onDismiss={onDismiss}
onOpenModelSelector={onOpenModelSelector}
/>
);
const actionButton = screen.getByRole('button', { name: 'Choose model' });
expect(actionButton).toBeInTheDocument();
fireEvent.click(actionButton);
expect(onOpenModelSelector).toHaveBeenCalledTimes(1);
});
it('formats network error messages correctly', () => {
+59 -2
View File
@@ -5,10 +5,12 @@ import { X } from 'lucide-react';
export interface QuillyProps {
error: Error;
onDismiss: () => void;
action?: { label: string; onClick: () => void };
onNewChat?: () => void;
onOpenModelSelector?: () => void;
onFixConsoleErrors?: () => void;
}
export function Quilly({ error, onDismiss, action }: QuillyProps) {
export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onFixConsoleErrors }: QuillyProps) {
// Generate user-friendly message from error
const getMessage = (error: Error): string => {
const errorMsg = error.message || '';
@@ -36,7 +38,62 @@ export function Quilly({ error, onDismiss, action }: QuillyProps) {
}
};
// Determine action based on error
const getAction = (error: Error): { label: string; onClick: () => void } | undefined => {
const errorMsg = error.message || '';
// Check for console error messages first
if (errorMsg.includes('Console') && errorMsg.includes('detected in your app') && onFixConsoleErrors) {
const isPlural = errorMsg.includes('errors detected');
return {
label: `Fix ${isPlural ? 'errors' : 'error'}`,
onClick: onFixConsoleErrors
};
}
if (error.name === 'TypeError' && errorMsg.includes('fetch')) {
return {
label: 'Check AI settings',
onClick: () => {
window.location.href = '/settings/ai';
}
};
} else if (errorMsg.includes('API key')) {
return {
label: 'Check API key',
onClick: () => {
window.location.href = '/settings/ai';
}
};
} else if (errorMsg.includes('insufficient_quota')) {
return {
label: 'Check AI settings',
onClick: () => {
window.location.href = '/settings/ai';
}
};
} else if (errorMsg.includes('model_not_found') || errorMsg.includes('does not exist')) {
return onOpenModelSelector ? {
label: 'Choose model',
onClick: onOpenModelSelector
} : undefined;
} else if (errorMsg.includes('maximum context length') || errorMsg.includes('context length')) {
return onNewChat ? {
label: 'New chat',
onClick: onNewChat
} : undefined;
} else if (errorMsg.includes('422') || errorMsg.includes('Provider returned error')) {
return onOpenModelSelector ? {
label: 'Change model',
onClick: onOpenModelSelector
} : undefined;
}
return undefined;
};
const message = getMessage(error);
const action = getAction(error);
return (
<div className="py-2 px-3 bg-primary/5 border border-primary/20 rounded-lg">
+9 -70
View File
@@ -47,7 +47,6 @@ import { Quilly } from '@/components/Quilly';
interface ChatPaneProps {
projectId: string;
onNewChat?: () => void;
onBuild?: () => void;
onFirstInteraction?: () => void;
onLoadingChange?: (isLoading: boolean) => void;
isLoading?: boolean;
@@ -60,8 +59,7 @@ export interface ChatPaneRef {
export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
projectId,
onNewChat: _onNewChat,
onBuild: _onBuild,
onNewChat,
onFirstInteraction,
onLoadingChange,
isLoading: externalIsLoading,
@@ -240,71 +238,14 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
// Clean alert hooks with proper interfaces
const consoleAlert = useConsoleErrorAlert(messages, isBuildLoading, internalIsLoading);
// Create AI error alert object
const aiErrorAlert = useMemo(() => {
if (!aiError) {
return { hasError: false, dismiss: () => {}, action: undefined };
}
let action: { label: string; onClick: () => void } | undefined;
const errorMsg = aiError.message || '';
if (aiError.name === 'TypeError' && errorMsg.includes('fetch')) {
action = {
label: 'Check AI settings',
onClick: () => {
// Navigate to AI settings
window.location.href = '/settings/ai';
}
};
} else if (errorMsg.includes('API key')) {
action = {
label: 'Check API key',
onClick: () => {
// Navigate to AI settings
window.location.href = '/settings/ai';
}
};
} else if (errorMsg.includes('insufficient_quota')) {
action = {
label: 'Check AI settings',
onClick: () => {
window.location.href = '/settings/ai';
}
};
} else if (errorMsg.includes('model_not_found') || errorMsg.includes('does not exist')) {
action = {
label: 'Choose model',
onClick: openModelSelector
};
} else if (errorMsg.includes('maximum context length') || errorMsg.includes('context length')) {
action = _onNewChat ? {
label: 'New chat',
onClick: _onNewChat
} : undefined;
} else if (errorMsg.includes('422') || errorMsg.includes('Provider returned error')) {
action = openModelSelector ? {
label: 'Change model',
onClick: openModelSelector
} : undefined;
}
return {
hasError: true,
dismiss: () => setAIError(null),
action
};
}, [aiError, openModelSelector, _onNewChat]);
// Scroll to bottom when alerts appear
useEffect(() => {
if (consoleAlert.hasError || aiErrorAlert.hasError) {
if (consoleAlert.hasError || aiError) {
setTimeout(() => {
scrollToBottom();
}, 50);
}
}, [consoleAlert.hasError, aiErrorAlert.hasError, scrollToBottom]);
}, [consoleAlert.hasError, aiError, scrollToBottom]);
// Check for autostart parameter and trigger AI generation
useEffect(() => {
@@ -664,19 +605,17 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
<Quilly
error={new Error(`Console ${consoleAlert.errorCount === 1 ? 'error' : 'errors'} detected in your app. Would you like me to help fix ${consoleAlert.errorCount === 1 ? 'it' : 'them'}?`)}
onDismiss={consoleAlert.dismiss}
action={{
label: `Fix ${consoleAlert.errorCount === 1 ? 'error' : 'errors'}`,
onClick: () => suggestErrorFix(consoleAlert.errorSummary || '')
}}
onFixConsoleErrors={() => suggestErrorFix(consoleAlert.errorSummary || '')}
/>
)}
{/* AI Error Alert */}
{aiErrorAlert.hasError && (
{aiError && (
<Quilly
error={aiError!}
onDismiss={aiErrorAlert.dismiss}
action={aiErrorAlert.action}
error={aiError}
onDismiss={() => setAIError(null)}
onNewChat={onNewChat}
onOpenModelSelector={openModelSelector}
/>
)}
-31
View File
@@ -14,7 +14,6 @@ import { ArrowLeft, MessageSquare, Eye, Code, Menu } from 'lucide-react';
import { useNavigate } from 'react-router-dom';
import { ActionsMenu } from '@/components/ActionsMenu';
import { useIsMobile } from '@/hooks/useIsMobile';
import { useKeepAlive } from '@/hooks/useKeepAlive';
import { GitStatusIndicator } from '@/components/GitStatusIndicator';
import { StarButton } from '@/components/StarButton';
@@ -40,20 +39,6 @@ export function ProjectView() {
const build = useBuildProject(projectId!);
const { data: isPreviewable = false } = useIsProjectPreviewable(projectId!);
// Keep-alive functionality to prevent tab throttling during AI processing
const { updateMetadata } = useKeepAlive({
enabled: isAILoading || build.isPending,
title: 'Shakespeare',
artist: project ? `Working on ${project.name}...` : 'Working...',
artwork: [
{
src: '/favicon.png',
sizes: '512x512',
type: 'image/png'
}
]
});
const loadProject = useCallback(async () => {
if (!projectId) return;
@@ -91,20 +76,6 @@ export function ProjectView() {
}
}, [isPreviewable, mobileView, activeTab]);
const runBuild = async () => {
if (build.isPending || !project) return;
updateMetadata('Shakespeare', `Building ${project.name}...`);
await build.mutateAsync(undefined, {
onSuccess(result) {
console.log('Project built:', result);
},
onError(error) {
console.error('Build failed:', error);
},
});
};
const handleNewChat = () => {
// Reset to chat view on mobile when starting new chat
if (isMobile) {
@@ -234,7 +205,6 @@ export function ProjectView() {
ref={chatPaneRef}
projectId={project.id}
onNewChat={handleNewChat}
onBuild={runBuild}
onFirstInteraction={handleFirstInteraction}
onLoadingChange={handleAILoadingChange}
isLoading={isAILoading}
@@ -414,7 +384,6 @@ export function ProjectView() {
ref={chatPaneRef}
projectId={project.id}
onNewChat={handleNewChat}
onBuild={runBuild}
onFirstInteraction={handleFirstInteraction}
onLoadingChange={handleAILoadingChange}
isLoading={isAILoading}