SurfSense/surfsense_web/components/chat/ChatPanel/ChatPanelContainer.tsx

69 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-11-11 04:02:04 +02:00
"use client";
import { useAtom, useAtomValue } from "jotai";
import { LoaderIcon, PanelRight, TriangleAlert } from "lucide-react";
2025-10-23 19:32:41 +02:00
import { toast } from "sonner";
2025-11-11 04:02:04 +02:00
import { generatePodcast } from "@/lib/apis/podcast-apis";
2025-10-23 19:32:41 +02:00
import { cn } from "@/lib/utils";
2025-11-11 04:02:04 +02:00
import { activeChatAtom, activeChatIdAtom } from "@/stores/chat/active-chat.atom";
import { chatUIAtom } from "@/stores/chat/chat-ui.atom";
2025-10-23 19:32:41 +02:00
import { ChatPanelView } from "./ChatPanelView";
2025-10-23 19:48:10 +02:00
export interface GeneratePodcastRequest {
type: "CHAT" | "DOCUMENT";
ids: number[];
search_space_id: number;
podcast_title?: string;
2025-11-11 20:24:02 +02:00
user_prompt?: string;
2025-10-23 19:48:10 +02:00
}
2025-10-23 19:32:41 +02:00
export function ChatPanelContainer() {
2025-11-11 04:02:04 +02:00
const {
data: activeChatState,
isLoading: isChatLoading,
error: chatError,
} = useAtomValue(activeChatAtom);
const activeChatIdState = useAtomValue(activeChatIdAtom);
const authToken = localStorage.getItem("surfsense_bearer_token");
const { isChatPannelOpen } = useAtomValue(chatUIAtom);
2025-10-23 19:32:41 +02:00
2025-11-11 04:02:04 +02:00
const handleGeneratePodcast = async (request: GeneratePodcastRequest) => {
2025-10-23 19:32:41 +02:00
try {
2025-11-11 04:02:04 +02:00
if (!authToken) {
throw new Error("Authentication error. Please log in again.");
2025-10-23 19:32:41 +02:00
}
2025-11-11 04:02:04 +02:00
await generatePodcast(request, authToken);
2025-10-23 19:32:41 +02:00
toast.success(`Podcast generation started!`);
} catch (error) {
2025-11-11 04:02:04 +02:00
toast.error("Error generating podcast. Please log in again.");
2025-10-23 19:32:41 +02:00
console.error("Error generating podcast:", error);
}
};
2025-11-11 04:02:04 +02:00
return activeChatIdState ? (
2025-10-23 19:32:41 +02:00
<div
className={cn(
2025-11-11 04:02:04 +02:00
"shrink-0 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 flex flex-col h-full transition-all",
isChatPannelOpen ? "w-64" : "w-0"
2025-10-23 19:32:41 +02:00
)}
>
2025-11-11 04:02:04 +02:00
{isChatLoading || chatError ? (
<div className="border-b p-2">
{isChatLoading ? (
<div title="Loading chat" className="flex items-center justify-center h-full">
<LoaderIcon strokeWidth={1.5} className="h-5 w-5 animate-spin" />
</div>
) : chatError ? (
<div title="Failed to load chat" className="flex items-center justify-center h-full">
<TriangleAlert strokeWidth={1.5} className="h-5 w-5 text-red-600" />
</div>
) : null}
</div>
) : null}
2025-10-23 19:32:41 +02:00
2025-11-11 04:02:04 +02:00
{!isChatLoading && !chatError && activeChatState?.chatDetails && (
<ChatPanelView generatePodcast={handleGeneratePodcast} />
)}
2025-10-23 19:32:41 +02:00
</div>
) : null;
}