This commit is contained in:
thierryverse 2025-11-15 03:42:42 +02:00
parent 776660f5e3
commit 62df67b93b
4 changed files with 5 additions and 179 deletions

View file

@ -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";

View file

@ -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";

View file

@ -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<ChatDetails | null>(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`,

View file

@ -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<ChatDetails | null> => {
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<ChatDetails[] | null> => {
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<Chat | null> => {
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();
};