mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-06 20:15:17 +02:00
feat(chat): implement chat tab synchronization and enhance thread activation with new hooks for improved navigation and metadata management
This commit is contained in:
parent
168c0d2f89
commit
08801fe3e8
13 changed files with 276 additions and 85 deletions
79
surfsense_web/hooks/use-activate-chat-thread.ts
Normal file
79
surfsense_web/hooks/use-activate-chat-thread.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
"use client";
|
||||
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { useSetAtom } from "jotai";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useCallback } from "react";
|
||||
import { setCurrentThreadMetadataAtom } from "@/atoms/chat/current-thread.atom";
|
||||
import { syncChatTabAtom } from "@/atoms/tabs/tabs.atom";
|
||||
import type { ChatVisibility } from "@/lib/chat/thread-persistence";
|
||||
import { prefetchThreadData } from "./use-thread-queries";
|
||||
|
||||
interface ActivateChatThreadInput {
|
||||
id: number | null;
|
||||
title?: string;
|
||||
url?: string;
|
||||
searchSpaceId: number | string;
|
||||
visibility?: ChatVisibility;
|
||||
hasComments?: boolean;
|
||||
}
|
||||
|
||||
function getSearchSpaceId(searchSpaceId: number | string): number {
|
||||
const parsed =
|
||||
typeof searchSpaceId === "number" ? searchSpaceId : Number.parseInt(searchSpaceId, 10);
|
||||
return Number.isNaN(parsed) ? 0 : parsed;
|
||||
}
|
||||
|
||||
function getChatUrl(searchSpaceId: number | string, threadId: number | null): string {
|
||||
return threadId
|
||||
? `/dashboard/${searchSpaceId}/new-chat/${threadId}`
|
||||
: `/dashboard/${searchSpaceId}/new-chat`;
|
||||
}
|
||||
|
||||
export function useActivateChatThread() {
|
||||
const router = useRouter();
|
||||
const queryClient = useQueryClient();
|
||||
const syncChatTab = useSetAtom(syncChatTabAtom);
|
||||
const setCurrentThreadMetadata = useSetAtom(setCurrentThreadMetadataAtom);
|
||||
|
||||
const prefetchChatThread = useCallback(
|
||||
(threadId: number | null | undefined) => {
|
||||
if (typeof threadId === "number" && threadId > 0) {
|
||||
prefetchThreadData(queryClient, threadId);
|
||||
}
|
||||
},
|
||||
[queryClient]
|
||||
);
|
||||
|
||||
const activateChatThread = useCallback(
|
||||
({ id, title, url, searchSpaceId, visibility, hasComments }: ActivateChatThreadInput) => {
|
||||
const numericSearchSpaceId = getSearchSpaceId(searchSpaceId);
|
||||
const chatUrl = url ?? getChatUrl(searchSpaceId, id);
|
||||
|
||||
syncChatTab({
|
||||
chatId: id,
|
||||
title: id ? title : (title ?? "New Chat"),
|
||||
chatUrl,
|
||||
searchSpaceId: numericSearchSpaceId,
|
||||
...(visibility !== undefined ? { visibility } : {}),
|
||||
...(hasComments !== undefined ? { hasComments } : {}),
|
||||
});
|
||||
|
||||
setCurrentThreadMetadata({
|
||||
id,
|
||||
searchSpaceId: numericSearchSpaceId,
|
||||
...(visibility !== undefined ? { visibility } : {}),
|
||||
...(hasComments !== undefined ? { hasComments } : {}),
|
||||
});
|
||||
|
||||
if (id) {
|
||||
prefetchThreadData(queryClient, id);
|
||||
}
|
||||
|
||||
router.push(chatUrl);
|
||||
},
|
||||
[queryClient, router, setCurrentThreadMetadata, syncChatTab]
|
||||
);
|
||||
|
||||
return { activateChatThread, prefetchChatThread };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue