Spaces:
Running
Running
File size: 8,095 Bytes
27127dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
import React, { useState } from 'react';
import { useLocation } from 'wouter';
import { Settings, MessageSquare, Menu, Video, Image, AlertCircle } from 'lucide-react';
import { Link } from 'wouter';
import { Button } from '@/components/ui/button';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { TooltipProvider, Tooltip, TooltipTrigger, TooltipContent } from '@/components/ui/tooltip';
import ChatHistory from '@/components/ChatHistory';
import ChatInputForm from '@/components/ChatInputForm';
import ConversationSidebar from '@/components/ConversationSidebar';
import ConnectionStatus from '@/components/ConnectionStatus';
import UserSettingsModal from '@/components/UserSettingsModal';
import { useAuth } from '@/hooks/use-auth';
import { useChat } from '@/lib/hooks';
// Main home page component
const Home: React.FC = () => {
const { user, logoutMutation } = useAuth();
const [, navigate] = useLocation();
const [sidebarOpen, setSidebarOpen] = useState(false);
const [errorVisible, setErrorVisible] = useState(true);
const [settingsOpen, setSettingsOpen] = useState(false);
const {
messages,
isLoading,
error,
isConnected,
currentModel,
sendMessage,
conversationId,
setConversationId
} = useChat("default");
// Handle creating a new conversation
const handleNewConversation = async () => {
try {
// Create a new conversation on the server
const response = await fetch('/api/conversations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: '' // Let the server generate a title
}),
});
if (!response.ok) {
throw new Error('Failed to create new conversation');
}
const newConversation = await response.json();
setConversationId(newConversation.id);
// The useEffect in useChat will automatically load messages
setSidebarOpen(false);
} catch (error) {
console.error('Error creating new conversation:', error);
}
};
// Handle selecting a conversation
const handleSelectConversation = (id: string) => {
if (id === conversationId) {
// Already selected, just close the sidebar
setSidebarOpen(false);
return;
}
// Set the conversation ID, which will trigger loading the messages
setConversationId(id);
// Close sidebar on mobile after selection
setSidebarOpen(false);
};
// Handle sign in button click
const handleSignIn = () => {
navigate('/auth');
};
return (
<div className="flex flex-col h-screen">
{/* Conversation Sidebar */}
<ConversationSidebar
isOpen={sidebarOpen}
onClose={() => setSidebarOpen(false)}
selectedConversationId={conversationId}
onSelectConversation={handleSelectConversation}
onNewConversation={handleNewConversation}
/>
{/* Header */}
<header className="bg-white border-b border-gray-200 py-4 px-6 shadow-sm">
<div className="max-w-5xl mx-auto flex items-center justify-between">
<div className="flex items-center">
<Button
variant="ghost"
size="icon"
onClick={() => setSidebarOpen(true)}
className="mr-2 md:hidden"
>
<Menu className="h-5 w-5" />
</Button>
<Button
variant="outline"
size="sm"
onClick={() => setSidebarOpen(true)}
className="mr-4 hidden md:flex"
>
<MessageSquare className="h-4 w-4 mr-2" />
<span>Conversations</span>
</Button>
<h1 className="font-bold text-2xl text-primary">AI Chat Assistant</h1>
</div>
<div className="flex items-center space-x-3">
<ConnectionStatus isConnected={isConnected} currentModel={currentModel} />
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Link href="/image-generator">
<Button variant="outline" size="sm" className="flex items-center">
<Image className="h-4 w-4 mr-2" />
<span>Image Generator</span>
</Button>
</Link>
</TooltipTrigger>
<TooltipContent>
<p>Create AI-generated images</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Link href="/video-generator">
<Button variant="outline" size="sm" className="flex items-center">
<Video className="h-4 w-4 mr-2" />
<span>Video Generator</span>
</Button>
</Link>
</TooltipTrigger>
<TooltipContent>
<p>Create AI-generated videos</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<button
className="p-2 rounded-full hover:bg-gray-100 text-gray-600"
aria-label="Settings"
onClick={() => setSettingsOpen(true)}
>
<Settings className="h-5 w-5" />
</button>
</TooltipTrigger>
<TooltipContent>
<p>Settings</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
</div>
</header>
{/* Main chat container */}
<main className="flex-1 overflow-hidden max-w-5xl w-full mx-auto px-4 sm:px-6 py-4">
{/* Error message */}
{error && errorVisible && (
<Alert variant="destructive" className="mb-4 shadow-lg border-l-4 border-red-600">
<AlertCircle className="h-4 w-4 mr-2" />
<AlertDescription className="flex justify-between items-center">
<div className="flex-1">
<p className="font-medium text-red-800">{error}</p>
{error.includes('API quota') && (
<p className="text-sm mt-1 text-gray-700">
This usually means the OpenAI API key has reached its limit or doesn't have a payment method associated with it.
The system will attempt to use the Qwen fallback model.
</p>
)}
</div>
<button
onClick={() => setErrorVisible(false)}
className="ml-2 text-foreground hover:text-foreground/80 p-1 flex-shrink-0"
aria-label="Dismiss"
>
<svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</AlertDescription>
</Alert>
)}
{/* Chat history */}
<ChatHistory
messages={messages}
isLoading={isLoading}
currentModel={currentModel}
/>
</main>
{/* Input area */}
<footer className="bg-white border-t border-gray-200 py-4 px-4 sm:px-6 shadow-inner">
<div className="max-w-5xl mx-auto">
<ChatInputForm
onSendMessage={sendMessage}
isLoading={isLoading}
/>
</div>
</footer>
{/* User Settings Modal */}
{user && (
<UserSettingsModal
isOpen={settingsOpen}
onClose={() => setSettingsOpen(false)}
/>
)}
</div>
);
};
export default Home; |