Spaces:
Build error
Build error
File size: 2,569 Bytes
ad160e7 |
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 |
import { useEffect } from "react";
import { useQuery, useMutation } from "@tanstack/react-query";
import { useToast } from "@/hooks/use-toast";
import VisionOSCarousel from "@/components/VisionOSCarousel";
import { getChats, createChat, sendMessage, updateChatSettings } from "@/lib/api";
import { queryClient } from "@/lib/queryClient";
export default function Chat() {
const { toast } = useToast();
// Query for all chats
const { data: chats, isLoading } = useQuery({
queryKey: ["/api/chats"],
queryFn: () => getChats(),
});
// Mutation for creating new chat
const createChatMutation = useMutation({
mutationFn: createChat,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["/api/chats"] });
},
onError: (error) => {
toast({
title: "Error creating chat",
description: error.message,
variant: "destructive",
});
},
});
// Mutation for sending messages
const sendMessageMutation = useMutation({
mutationFn: ({ chatId, content }: { chatId: number; content: string }) =>
sendMessage(chatId, content),
onSuccess: (_, variables) => {
queryClient.invalidateQueries({ queryKey: ["/api/chats", variables.chatId] });
},
onError: (error) => {
toast({
title: "Error sending message",
description: error.message,
variant: "destructive",
});
},
});
// Mutation for updating chat settings
const updateSettingsMutation = useMutation({
mutationFn: ({ chatId, settings }: { chatId: number; settings: any }) =>
updateChatSettings(chatId, settings),
onSuccess: (_, variables) => {
queryClient.invalidateQueries({ queryKey: ["/api/chats", variables.chatId] });
},
onError: (error) => {
toast({
title: "Error updating settings",
description: error.message,
variant: "destructive",
});
},
});
// Create initial chat if none exists
useEffect(() => {
if (!isLoading && (!chats || chats.length === 0)) {
createChatMutation.mutate("New Chat");
}
}, [isLoading, chats]);
if (isLoading) {
return <div>Loading...</div>;
}
return (
<VisionOSCarousel
chats={chats || []}
onSendMessage={(chatId, content) =>
sendMessageMutation.mutate({ chatId, content })}
onUpdateSettings={(chatId, settings) =>
updateSettingsMutation.mutate({ chatId, settings })}
onCreateChat={(title) => createChatMutation.mutate(title)}
isLoading={sendMessageMutation.isPending}
/>
);
}
|