2025-12-23 14:24:36 +05:30
|
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
|
|
import { atom } from "jotai";
|
2026-03-06 15:35:58 +05:30
|
|
|
|
import type { Document } from "@/contracts/types/document.types";
|
2025-12-23 14:24:36 +05:30
|
|
|
|
|
2025-12-23 15:13:03 +05:30
|
|
|
|
/**
|
2026-03-06 15:59:45 +05:30
|
|
|
|
* Atom to store the full document objects mentioned via @-mention chips
|
|
|
|
|
|
* in the current chat composer. This persists across component remounts.
|
2025-12-23 15:13:03 +05:30
|
|
|
|
*/
|
2026-01-13 01:45:58 -08:00
|
|
|
|
export const mentionedDocumentsAtom = atom<Pick<Document, "id" | "title" | "document_type">[]>([]);
|
2025-12-23 15:13:03 +05:30
|
|
|
|
|
2026-03-06 15:59:45 +05:30
|
|
|
|
/**
|
|
|
|
|
|
* Atom to store documents selected via the sidebar checkboxes / row clicks.
|
|
|
|
|
|
* These are NOT inserted as chips – the composer shows a count badge instead.
|
|
|
|
|
|
*/
|
2026-03-07 04:46:48 +05:30
|
|
|
|
export const sidebarSelectedDocumentsAtom = atom<
|
|
|
|
|
|
Pick<Document, "id" | "title" | "document_type">[]
|
|
|
|
|
|
>([]);
|
2026-03-06 15:59:45 +05:30
|
|
|
|
|
2026-03-06 23:33:51 +05:30
|
|
|
|
/**
|
|
|
|
|
|
* 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),
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-12-23 15:13:03 +05:30
|
|
|
|
/**
|
|
|
|
|
|
* Simplified document info for display purposes
|
|
|
|
|
|
*/
|
|
|
|
|
|
export interface MentionedDocumentInfo {
|
|
|
|
|
|
id: number;
|
|
|
|
|
|
title: string;
|
|
|
|
|
|
document_type: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Atom to store mentioned documents per message ID.
|
|
|
|
|
|
* This allows displaying which documents were mentioned with each user message.
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const messageDocumentsMapAtom = atom<Record<string, MentionedDocumentInfo[]>>({});
|