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

62 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-19 08:29:33 +02:00
import { activeChatAtom } from "@/atoms/chats/chat-query.atoms";
2025-11-18 18:57:41 +02:00
import { activeChathatUIAtom, activeChatIdAtom } from "@/atoms/chats/ui.atoms";
2025-11-18 11:35:06 +02:00
import type { GeneratePodcastRequest } from "@/contracts/types/podcast.types";
import { podcastsApiService } from "@/lib/apis/podcasts-api.service";
2025-10-23 19:32:41 +02:00
import { cn } from "@/lib/utils";
import { ChatPanelView } from "./ChatPanelView";
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");
2025-11-15 02:07:20 +02:00
const { isChatPannelOpen } = useAtomValue(activeChathatUIAtom);
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-18 11:35:06 +02:00
await podcastsApiService.generatePodcast(request);
2025-10-23 19:32:41 +02:00
toast.success(`Podcast generation started!`);
} catch (error) {
2025-11-18 18:35:48 +02:00
toast.error("Error generating podcast. Please try again later.");
console.error("Error generating podcast:", JSON.stringify(error));
2025-10-23 19:32:41 +02:00
}
};
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-96" : "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 ? (
2025-11-18 18:57:41 +02:00
<div title="Failed to load chat" className="flex items-center justify-center h-full">
2025-11-11 04:02:04 +02:00
<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;
}