mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-26 01:06:23 +02:00
refactor: streamline document mention handling by consolidating atom logic and removing redundant state updates, enhancing code clarity and maintainability in chat components
This commit is contained in:
parent
8d5d8e490c
commit
c1ba3a9b6d
6 changed files with 47 additions and 90 deletions
|
|
@ -3,18 +3,6 @@
|
|||
import { atom } from "jotai";
|
||||
import type { Document } from "@/contracts/types/document.types";
|
||||
|
||||
/**
|
||||
* Atom to store the IDs of documents mentioned in the current chat composer.
|
||||
* This is used to pass document context to the backend when sending a message.
|
||||
*/
|
||||
export const mentionedDocumentIdsAtom = atom<{
|
||||
surfsense_doc_ids: number[];
|
||||
document_ids: number[];
|
||||
}>({
|
||||
surfsense_doc_ids: [],
|
||||
document_ids: [],
|
||||
});
|
||||
|
||||
/**
|
||||
* Atom to store the full document objects mentioned via @-mention chips
|
||||
* in the current chat composer. This persists across component remounts.
|
||||
|
|
@ -27,6 +15,31 @@ export const mentionedDocumentsAtom = atom<Pick<Document, "id" | "title" | "docu
|
|||
*/
|
||||
export const sidebarSelectedDocumentsAtom = atom<Pick<Document, "id" | "title" | "document_type">[]>([]);
|
||||
|
||||
/**
|
||||
* Derived read-only atom that merges @-mention chips and sidebar selections
|
||||
* into a single deduplicated set of document IDs for the backend.
|
||||
*/
|
||||
export const mentionedDocumentIdsAtom = atom((get) => {
|
||||
const chipDocs = get(mentionedDocumentsAtom);
|
||||
const sidebarDocs = get(sidebarSelectedDocumentsAtom);
|
||||
const allDocs = [...chipDocs, ...sidebarDocs];
|
||||
const seen = new Set<string>();
|
||||
const deduped = allDocs.filter((d) => {
|
||||
const key = `${d.document_type}:${d.id}`;
|
||||
if (seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
});
|
||||
return {
|
||||
surfsense_doc_ids: deduped
|
||||
.filter((doc) => doc.document_type === "SURFSENSE_DOCS")
|
||||
.map((doc) => doc.id),
|
||||
document_ids: deduped
|
||||
.filter((doc) => doc.document_type !== "SURFSENSE_DOCS")
|
||||
.map((doc) => doc.id),
|
||||
};
|
||||
});
|
||||
|
||||
/**
|
||||
* Simplified document info for display purposes
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue