From 62df67b93b2d3843276351bda11bcd9f986659c2 Mon Sep 17 00:00:00 2001 From: thierryverse Date: Sat, 15 Nov 2025 03:42:42 +0200 Subject: [PATCH] clean up --- .../atoms/chats/chat-mutation.atoms.ts | 1 - .../atoms/chats/chat-querie.atoms.ts | 1 - .../components/dashboard-breadcrumb.tsx | 25 +-- surfsense_web/lib/apis/chats.api.ts | 157 ------------------ 4 files changed, 5 insertions(+), 179 deletions(-) delete mode 100644 surfsense_web/lib/apis/chats.api.ts diff --git a/surfsense_web/atoms/chats/chat-mutation.atoms.ts b/surfsense_web/atoms/chats/chat-mutation.atoms.ts index fee69f9f7..da0795afa 100644 --- a/surfsense_web/atoms/chats/chat-mutation.atoms.ts +++ b/surfsense_web/atoms/chats/chat-mutation.atoms.ts @@ -1,7 +1,6 @@ import { atomWithMutation } from "jotai-tanstack-query"; import { toast } from "sonner"; import type { Chat } from "@/app/dashboard/[search_space_id]/chats/chats-client"; -import { deleteChat } from "@/lib/apis/chats.api"; import { chatApiService } from "@/lib/apis/chats-api.service"; import { cacheKeys } from "@/lib/query-client/cache-keys"; import { queryClient } from "@/lib/query-client/client"; diff --git a/surfsense_web/atoms/chats/chat-querie.atoms.ts b/surfsense_web/atoms/chats/chat-querie.atoms.ts index 9ff913432..8ea668eb4 100644 --- a/surfsense_web/atoms/chats/chat-querie.atoms.ts +++ b/surfsense_web/atoms/chats/chat-querie.atoms.ts @@ -3,7 +3,6 @@ import { atomWithQuery } from "jotai-tanstack-query"; import type { ChatDetails } from "@/app/dashboard/[search_space_id]/chats/chats-client"; import type { PodcastItem } from "@/app/dashboard/[search_space_id]/podcasts/podcasts-client"; import { activeSearchSpaceIdAtom } from "@/atoms/seach-spaces/seach-space-queries.atom"; -import { fetchChatDetails, fetchChatsBySearchSpace } from "@/lib/apis/chats.api"; import { chatApiService } from "@/lib/apis/chats-api.service"; import { getPodcastByChatId } from "@/lib/apis/podcasts.api"; import { cacheKeys } from "@/lib/query-client/cache-keys"; diff --git a/surfsense_web/components/dashboard-breadcrumb.tsx b/surfsense_web/components/dashboard-breadcrumb.tsx index 2b032f8fd..65e885ead 100644 --- a/surfsense_web/components/dashboard-breadcrumb.tsx +++ b/surfsense_web/components/dashboard-breadcrumb.tsx @@ -1,9 +1,10 @@ "use client"; +import { useAtomValue } from "jotai"; import { usePathname } from "next/navigation"; import { useTranslations } from "next-intl"; -import React, { useEffect, useState } from "react"; -import type { ChatDetails } from "@/app/dashboard/[search_space_id]/chats/chats-client"; +import React, { useEffect } from "react"; +import { activeChatAtom, activeChatIdAtom } from "@/atoms/chats/chat-querie.atoms"; import { Breadcrumb, BreadcrumbItem, @@ -13,7 +14,6 @@ import { BreadcrumbSeparator, } from "@/components/ui/breadcrumb"; import { useSearchSpace } from "@/hooks/use-search-space"; -import { fetchChatDetails } from "@/lib/apis/chats.api"; interface BreadcrumbItemInterface { label: string; @@ -23,13 +23,10 @@ interface BreadcrumbItemInterface { export function DashboardBreadcrumb() { const t = useTranslations("breadcrumb"); const pathname = usePathname(); - const [chatDetails, setChatDetails] = useState(null); - + const { data: activeChatState } = useAtomValue(activeChatAtom); // Extract search space ID and chat ID from pathname const segments = pathname.split("/").filter(Boolean); const searchSpaceId = segments[0] === "dashboard" && segments[1] ? segments[1] : null; - const chatId = - segments[0] === "dashboard" && segments[2] === "researcher" && segments[3] ? segments[3] : null; // Fetch search space details if we have an ID const { searchSpace } = useSearchSpace({ @@ -37,18 +34,6 @@ export function DashboardBreadcrumb() { autoFetch: !!searchSpaceId, }); - // Fetch chat details if we have a chat ID - useEffect(() => { - if (chatId) { - const token = localStorage.getItem("surfsense_bearer_token"); - if (token) { - fetchChatDetails(chatId, token).then(setChatDetails); - } - } else { - setChatDetails(null); - } - }, [chatId]); - // Parse the pathname to create breadcrumb items const generateBreadcrumbs = (path: string): BreadcrumbItemInterface[] => { const segments = path.split("/").filter(Boolean); @@ -125,7 +110,7 @@ export function DashboardBreadcrumb() { // Handle researcher sub-sections (chat IDs) if (section === "researcher") { // Use the actual chat title if available, otherwise fall back to the ID - const chatLabel = chatDetails?.title || subSection; + const chatLabel = activeChatState?.chatDetails?.title || subSection; breadcrumbs.push({ label: t("researcher"), href: `/dashboard/${segments[1]}/researcher`, diff --git a/surfsense_web/lib/apis/chats.api.ts b/surfsense_web/lib/apis/chats.api.ts deleted file mode 100644 index fc98585d3..000000000 --- a/surfsense_web/lib/apis/chats.api.ts +++ /dev/null @@ -1,157 +0,0 @@ -import type { Message } from "@ai-sdk/react"; -import type { Chat, ChatDetails } from "@/app/dashboard/[search_space_id]/chats/chats-client"; -import type { ResearchMode } from "@/components/chat/types"; - -export const fetchChatDetails = async ( - chatId: string, - authToken: string -): Promise => { - const response = await fetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${Number(chatId)}`, - { - method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${authToken}`, - }, - } - ); - - if (!response.ok) { - throw new Error(`Failed to fetch chat details: ${response.statusText}`); - } - - return await response.json(); -}; - -export const fetchChatsBySearchSpace = async ( - searchSpaceId: string, - authToken: string -): Promise => { - const response = await fetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats?search_space_id=${searchSpaceId}`, - { - method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${authToken}`, - }, - } - ); - if (!response.ok) { - throw new Error(`Failed to fetch chats: ${response.statusText}`); - } - - return await response.json(); -}; - -export const deleteChat = async (chatId: number, authToken: string) => { - const response = await fetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${chatId}`, - { - method: "DELETE", - headers: { - Authorization: `Bearer ${authToken}`, - }, - } - ); - - if (!response.ok) { - throw new Error(`Failed to delete chat: ${response.statusText}`); - } - - return await response.json(); -}; - -export const createChat = async ( - initialMessage: string, - researchMode: ResearchMode, - selectedConnectors: string[], - authToken: string, - searchSpaceId: number -): Promise => { - const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats`, { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${authToken}`, - }, - body: JSON.stringify({ - type: researchMode, - title: "Untitled Chat", - initial_connectors: selectedConnectors, - messages: [ - { - role: "user", - content: initialMessage, - }, - ], - search_space_id: searchSpaceId, - }), - }); - - if (!response.ok) { - throw new Error(`Failed to create chat: ${response.statusText}`); - } - - return await response.json(); -}; - -export const updateChat = async ( - chatId: string, - messages: Message[], - researchMode: ResearchMode, - selectedConnectors: string[], - authToken: string, - searchSpaceId: number -) => { - const userMessages = messages.filter((msg) => msg.role === "user"); - if (userMessages.length === 0) return; - - const title = userMessages[0].content; - - const response = await fetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${Number(chatId)}`, - { - method: "PUT", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${authToken}`, - }, - body: JSON.stringify({ - type: researchMode, - title: title, - initial_connectors: selectedConnectors, - messages: messages, - search_space_id: searchSpaceId, - }), - } - ); - - if (!response.ok) { - throw new Error(`Failed to update chat: ${response.statusText}`); - } -}; - -export const fetchChats = async ( - searchSpaceId: string, - limit: number, - skip: number, - authToken: string -) => { - const response = await fetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats?limit=${limit}&skip=${skip}&search_space_id=${searchSpaceId}`, - { - headers: { - Authorization: `Bearer ${authToken}`, - }, - method: "GET", - } - ); - - if (!response.ok) { - throw new Error(`Failed to fetch chats: ${response.status}`); - } - - return await response.json(); -};