mirror of
https://github.com/MODSetter/SurfSense.git
synced 2025-09-02 18:49:09 +00:00
Added document selection and document selection dialog to new chat interface
This commit is contained in:
parent
98b49edca1
commit
3f051b0a19
7 changed files with 1119 additions and 296 deletions
|
@ -2,10 +2,11 @@
|
||||||
|
|
||||||
import { useChat, Message, CreateMessage } from "@ai-sdk/react";
|
import { useChat, Message, CreateMessage } from "@ai-sdk/react";
|
||||||
import { useParams, useRouter } from "next/navigation";
|
import { useParams, useRouter } from "next/navigation";
|
||||||
import { useEffect } from "react";
|
import { useEffect, useMemo } from "react";
|
||||||
import ChatMain from "@/components/chat_v2/ChatMain";
|
import ChatInterface from "@/components/chat_v2/ChatInterface";
|
||||||
import { ResearchMode } from "@/components/chat";
|
import { ResearchMode } from "@/components/chat";
|
||||||
import { useChatState, useChatAPI } from "@/hooks/useChat";
|
import { useChatState, useChatAPI } from "@/hooks/useChat";
|
||||||
|
import { Document } from "@/hooks/use-documents";
|
||||||
|
|
||||||
export default function ResearchChatPageV2() {
|
export default function ResearchChatPageV2() {
|
||||||
const { search_space_id, chat_id } = useParams();
|
const { search_space_id, chat_id } = useParams();
|
||||||
|
@ -23,6 +24,8 @@ export default function ResearchChatPageV2() {
|
||||||
setResearchMode,
|
setResearchMode,
|
||||||
selectedConnectors,
|
selectedConnectors,
|
||||||
setSelectedConnectors,
|
setSelectedConnectors,
|
||||||
|
selectedDocuments,
|
||||||
|
setSelectedDocuments,
|
||||||
} = useChatState({
|
} = useChatState({
|
||||||
search_space_id: search_space_id as string,
|
search_space_id: search_space_id as string,
|
||||||
chat_id: chatIdParam,
|
chat_id: chatIdParam,
|
||||||
|
@ -35,7 +38,42 @@ export default function ResearchChatPageV2() {
|
||||||
selectedConnectors,
|
selectedConnectors,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Single useChat handler for both cases
|
// Memoize document IDs to prevent infinite re-renders
|
||||||
|
const documentIds = useMemo(() => {
|
||||||
|
return selectedDocuments.map((doc) => doc.id);
|
||||||
|
}, [selectedDocuments]);
|
||||||
|
|
||||||
|
// Helper functions for localStorage management
|
||||||
|
const getStorageKey = (searchSpaceId: string, chatId: string) =>
|
||||||
|
`surfsense_selected_docs_${searchSpaceId}_${chatId}`;
|
||||||
|
|
||||||
|
const storeSelectedDocuments = (
|
||||||
|
searchSpaceId: string,
|
||||||
|
chatId: string,
|
||||||
|
documents: Document[]
|
||||||
|
) => {
|
||||||
|
const key = getStorageKey(searchSpaceId, chatId);
|
||||||
|
localStorage.setItem(key, JSON.stringify(documents));
|
||||||
|
};
|
||||||
|
|
||||||
|
const restoreSelectedDocuments = (
|
||||||
|
searchSpaceId: string,
|
||||||
|
chatId: string
|
||||||
|
): Document[] | null => {
|
||||||
|
const key = getStorageKey(searchSpaceId, chatId);
|
||||||
|
const stored = localStorage.getItem(key);
|
||||||
|
if (stored) {
|
||||||
|
localStorage.removeItem(key); // Clean up after restoration
|
||||||
|
try {
|
||||||
|
return JSON.parse(stored);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error parsing stored documents:", error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
const handler = useChat({
|
const handler = useChat({
|
||||||
api: `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chat`,
|
api: `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chat`,
|
||||||
streamProtocol: "data",
|
streamProtocol: "data",
|
||||||
|
@ -49,7 +87,7 @@ export default function ResearchChatPageV2() {
|
||||||
selected_connectors: selectedConnectors,
|
selected_connectors: selectedConnectors,
|
||||||
research_mode: researchMode,
|
research_mode: researchMode,
|
||||||
search_mode: searchMode,
|
search_mode: searchMode,
|
||||||
document_ids_to_add_in_context: [],
|
document_ids_to_add_in_context: documentIds,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
|
@ -62,7 +100,15 @@ export default function ResearchChatPageV2() {
|
||||||
chatRequestOptions?: { data?: any }
|
chatRequestOptions?: { data?: any }
|
||||||
) => {
|
) => {
|
||||||
const newChatId = await createChat(message.content);
|
const newChatId = await createChat(message.content);
|
||||||
router.replace(`/dashboard/${search_space_id}/v2/${newChatId}`);
|
if (newChatId) {
|
||||||
|
// Store selected documents before navigation
|
||||||
|
storeSelectedDocuments(
|
||||||
|
search_space_id as string,
|
||||||
|
newChatId,
|
||||||
|
selectedDocuments
|
||||||
|
);
|
||||||
|
router.replace(`/dashboard/${search_space_id}/v2/${newChatId}`);
|
||||||
|
}
|
||||||
return newChatId;
|
return newChatId;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -73,6 +119,19 @@ export default function ResearchChatPageV2() {
|
||||||
}
|
}
|
||||||
}, [token, isNewChat, chatIdParam]);
|
}, [token, isNewChat, chatIdParam]);
|
||||||
|
|
||||||
|
// Restore selected documents from localStorage on page load
|
||||||
|
useEffect(() => {
|
||||||
|
if (chatIdParam && search_space_id) {
|
||||||
|
const restoredDocuments = restoreSelectedDocuments(
|
||||||
|
search_space_id as string,
|
||||||
|
chatIdParam
|
||||||
|
);
|
||||||
|
if (restoredDocuments && restoredDocuments.length > 0) {
|
||||||
|
setSelectedDocuments(restoredDocuments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [chatIdParam, search_space_id, setSelectedDocuments]);
|
||||||
|
|
||||||
const loadChatData = async (chatId: string) => {
|
const loadChatData = async (chatId: string) => {
|
||||||
try {
|
try {
|
||||||
const chatData = await fetchChatDetails(chatId);
|
const chatData = await fetchChatDetails(chatId);
|
||||||
|
@ -133,11 +192,13 @@ export default function ResearchChatPageV2() {
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ChatMain
|
<ChatInterface
|
||||||
handler={{
|
handler={{
|
||||||
...handler,
|
...handler,
|
||||||
append: isNewChat ? customHandlerAppend : handler.append,
|
append: isNewChat ? customHandlerAppend : handler.append,
|
||||||
}}
|
}}
|
||||||
|
onDocumentSelectionChange={setSelectedDocuments}
|
||||||
|
selectedDocuments={selectedDocuments}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
187
surfsense_web/components/chat_v2/ChatInterface.tsx
Normal file
187
surfsense_web/components/chat_v2/ChatInterface.tsx
Normal file
|
@ -0,0 +1,187 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ChatSection,
|
||||||
|
ChatHandler,
|
||||||
|
ChatInput,
|
||||||
|
ChatCanvas,
|
||||||
|
ChatMessages,
|
||||||
|
} from "@llamaindex/chat-ui";
|
||||||
|
import { Button } from "../ui/button";
|
||||||
|
import { FolderOpen } from "lucide-react";
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from "../ui/dialog";
|
||||||
|
import { Suspense, useState, useCallback } from "react";
|
||||||
|
import { useParams } from "next/navigation";
|
||||||
|
import { useDocuments, DocumentType, Document } from "@/hooks/use-documents";
|
||||||
|
import { DocumentsDataTable } from "./DocumentsDataTable";
|
||||||
|
import React from "react";
|
||||||
|
|
||||||
|
interface ChatInterfaceProps {
|
||||||
|
handler: ChatHandler;
|
||||||
|
onDocumentSelectionChange?: (documents: Document[]) => void;
|
||||||
|
selectedDocuments?: Document[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const DocumentSelector = React.memo(
|
||||||
|
({
|
||||||
|
onSelectionChange,
|
||||||
|
selectedDocuments = [],
|
||||||
|
}: {
|
||||||
|
onSelectionChange?: (documents: Document[]) => void;
|
||||||
|
selectedDocuments?: Document[];
|
||||||
|
}) => {
|
||||||
|
const { search_space_id } = useParams();
|
||||||
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
|
|
||||||
|
const { documents, loading, isLoaded, fetchDocuments } = useDocuments(
|
||||||
|
Number(search_space_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleOpenChange = useCallback(
|
||||||
|
(open: boolean) => {
|
||||||
|
setIsOpen(open);
|
||||||
|
if (open && !isLoaded) {
|
||||||
|
fetchDocuments();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[fetchDocuments, isLoaded]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleSelectionChange = useCallback(
|
||||||
|
(documents: Document[]) => {
|
||||||
|
onSelectionChange?.(documents);
|
||||||
|
},
|
||||||
|
[onSelectionChange]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleDone = useCallback(() => {
|
||||||
|
setIsOpen(false);
|
||||||
|
}, [selectedDocuments]);
|
||||||
|
|
||||||
|
const selectedCount = selectedDocuments.length;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={isOpen} onOpenChange={handleOpenChange}>
|
||||||
|
<DialogTrigger asChild>
|
||||||
|
<Button variant="outline" className="relative">
|
||||||
|
<FolderOpen className="w-4 h-4" />
|
||||||
|
{selectedCount > 0 && (
|
||||||
|
<span className="absolute -top-2 -right-2 h-5 w-5 rounded-full bg-primary text-primary-foreground text-xs flex items-center justify-center">
|
||||||
|
{selectedCount}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</DialogTrigger>
|
||||||
|
|
||||||
|
<DialogContent className="max-w-[95vw] md:max-w-5xl h-[90vh] md:h-[85vh] p-0 flex flex-col">
|
||||||
|
<div className="flex flex-col h-full">
|
||||||
|
<div className="px-4 md:px-6 py-4 border-b flex-shrink-0">
|
||||||
|
<DialogTitle className="text-lg md:text-xl">
|
||||||
|
Select Documents
|
||||||
|
</DialogTitle>
|
||||||
|
<DialogDescription className="mt-1 text-sm">
|
||||||
|
Choose documents to include in your research
|
||||||
|
context
|
||||||
|
</DialogDescription>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex-1 min-h-0 p-4 md:p-6">
|
||||||
|
{loading ? (
|
||||||
|
<div className="flex items-center justify-center h-full">
|
||||||
|
<div className="text-center space-y-2">
|
||||||
|
<div className="animate-spin h-8 w-8 border-2 border-primary border-t-transparent rounded-full mx-auto" />
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
Loading documents...
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : isLoaded ? (
|
||||||
|
<DocumentsDataTable
|
||||||
|
documents={documents}
|
||||||
|
onSelectionChange={handleSelectionChange}
|
||||||
|
onDone={handleDone}
|
||||||
|
initialSelectedDocuments={selectedDocuments}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const CustomChatInputOptions = ({
|
||||||
|
onDocumentSelectionChange,
|
||||||
|
selectedDocuments,
|
||||||
|
}: {
|
||||||
|
onDocumentSelectionChange?: (documents: Document[]) => void;
|
||||||
|
selectedDocuments?: Document[];
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-row gap-2">
|
||||||
|
<Suspense fallback={<div>Loading...</div>}>
|
||||||
|
<DocumentSelector
|
||||||
|
onSelectionChange={onDocumentSelectionChange}
|
||||||
|
selectedDocuments={selectedDocuments}
|
||||||
|
/>
|
||||||
|
</Suspense>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const CustomChatInput = ({
|
||||||
|
onDocumentSelectionChange,
|
||||||
|
selectedDocuments,
|
||||||
|
}: {
|
||||||
|
onDocumentSelectionChange?: (documents: Document[]) => void;
|
||||||
|
selectedDocuments?: Document[];
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<ChatInput>
|
||||||
|
<ChatInput.Form className="flex gap-2">
|
||||||
|
<ChatInput.Field className="flex-1" />
|
||||||
|
<ChatInput.Submit />
|
||||||
|
</ChatInput.Form>
|
||||||
|
<CustomChatInputOptions
|
||||||
|
onDocumentSelectionChange={onDocumentSelectionChange}
|
||||||
|
selectedDocuments={selectedDocuments}
|
||||||
|
/>
|
||||||
|
</ChatInput>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function ChatInterface({
|
||||||
|
handler,
|
||||||
|
onDocumentSelectionChange,
|
||||||
|
selectedDocuments = [],
|
||||||
|
}: ChatInterfaceProps) {
|
||||||
|
return (
|
||||||
|
<ChatSection handler={handler} className="flex h-full">
|
||||||
|
<div className="flex flex-1 flex-col">
|
||||||
|
<ChatMessages className="flex-1">
|
||||||
|
<ChatMessages.List className="p-4">
|
||||||
|
{/* Custom message rendering */}
|
||||||
|
</ChatMessages.List>
|
||||||
|
<ChatMessages.Loading />
|
||||||
|
</ChatMessages>
|
||||||
|
|
||||||
|
<div className="border-t p-4">
|
||||||
|
<CustomChatInput
|
||||||
|
onDocumentSelectionChange={onDocumentSelectionChange}
|
||||||
|
selectedDocuments={selectedDocuments}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<ChatCanvas className="w-1/2 border-l" />
|
||||||
|
</ChatSection>
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,11 +0,0 @@
|
||||||
"use client";
|
|
||||||
|
|
||||||
import { ChatSection, ChatHandler } from "@llamaindex/chat-ui";
|
|
||||||
|
|
||||||
interface ChatMainProps {
|
|
||||||
handler: ChatHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ChatMain({ handler }: ChatMainProps) {
|
|
||||||
return <ChatSection handler={handler} className="flex h-full" />;
|
|
||||||
}
|
|
530
surfsense_web/components/chat_v2/DocumentsDataTable.tsx
Normal file
530
surfsense_web/components/chat_v2/DocumentsDataTable.tsx
Normal file
|
@ -0,0 +1,530 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import * as React from "react";
|
||||||
|
import {
|
||||||
|
ColumnDef,
|
||||||
|
ColumnFiltersState,
|
||||||
|
flexRender,
|
||||||
|
getCoreRowModel,
|
||||||
|
getFilteredRowModel,
|
||||||
|
getPaginationRowModel,
|
||||||
|
getSortedRowModel,
|
||||||
|
SortingState,
|
||||||
|
useReactTable,
|
||||||
|
VisibilityState,
|
||||||
|
} from "@tanstack/react-table";
|
||||||
|
import { ArrowUpDown, Calendar, FileText, Search } from "lucide-react";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Checkbox } from "@/components/ui/checkbox";
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from "@/components/ui/select";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import {
|
||||||
|
Table,
|
||||||
|
TableBody,
|
||||||
|
TableCell,
|
||||||
|
TableHead,
|
||||||
|
TableHeader,
|
||||||
|
TableRow,
|
||||||
|
} from "@/components/ui/table";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import { Document, DocumentType } from "@/hooks/use-documents";
|
||||||
|
|
||||||
|
interface DocumentsDataTableProps {
|
||||||
|
documents: Document[];
|
||||||
|
onSelectionChange: (documents: Document[]) => void;
|
||||||
|
onDone: () => void;
|
||||||
|
initialSelectedDocuments?: Document[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const DOCUMENT_TYPES: (DocumentType | "ALL")[] = [
|
||||||
|
"ALL",
|
||||||
|
"FILE",
|
||||||
|
"EXTENSION",
|
||||||
|
"CRAWLED_URL",
|
||||||
|
"YOUTUBE_VIDEO",
|
||||||
|
"SLACK_CONNECTOR",
|
||||||
|
"NOTION_CONNECTOR",
|
||||||
|
"GITHUB_CONNECTOR",
|
||||||
|
"LINEAR_CONNECTOR",
|
||||||
|
"DISCORD_CONNECTOR",
|
||||||
|
];
|
||||||
|
|
||||||
|
const getDocumentTypeColor = (type: DocumentType) => {
|
||||||
|
const colors = {
|
||||||
|
FILE: "bg-blue-50 text-blue-700 border-blue-200",
|
||||||
|
EXTENSION: "bg-green-50 text-green-700 border-green-200",
|
||||||
|
CRAWLED_URL: "bg-purple-50 text-purple-700 border-purple-200",
|
||||||
|
YOUTUBE_VIDEO: "bg-red-50 text-red-700 border-red-200",
|
||||||
|
SLACK_CONNECTOR: "bg-yellow-50 text-yellow-700 border-yellow-200",
|
||||||
|
NOTION_CONNECTOR: "bg-indigo-50 text-indigo-700 border-indigo-200",
|
||||||
|
GITHUB_CONNECTOR: "bg-gray-50 text-gray-700 border-gray-200",
|
||||||
|
LINEAR_CONNECTOR: "bg-pink-50 text-pink-700 border-pink-200",
|
||||||
|
DISCORD_CONNECTOR: "bg-violet-50 text-violet-700 border-violet-200",
|
||||||
|
};
|
||||||
|
return colors[type] || "bg-gray-50 text-gray-700 border-gray-200";
|
||||||
|
};
|
||||||
|
|
||||||
|
const columns: ColumnDef<Document>[] = [
|
||||||
|
{
|
||||||
|
id: "select",
|
||||||
|
header: ({ table }) => (
|
||||||
|
<Checkbox
|
||||||
|
checked={
|
||||||
|
table.getIsAllPageRowsSelected() ||
|
||||||
|
(table.getIsSomePageRowsSelected() && "indeterminate")
|
||||||
|
}
|
||||||
|
onCheckedChange={(value) =>
|
||||||
|
table.toggleAllPageRowsSelected(!!value)
|
||||||
|
}
|
||||||
|
aria-label="Select all"
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
cell: ({ row }) => (
|
||||||
|
<Checkbox
|
||||||
|
checked={row.getIsSelected()}
|
||||||
|
onCheckedChange={(value) => row.toggleSelected(!!value)}
|
||||||
|
aria-label="Select row"
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
enableSorting: false,
|
||||||
|
enableHiding: false,
|
||||||
|
size: 40,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "title",
|
||||||
|
header: ({ column }) => (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() =>
|
||||||
|
column.toggleSorting(column.getIsSorted() === "asc")
|
||||||
|
}
|
||||||
|
className="h-8 px-1 sm:px-2 font-medium text-left justify-start"
|
||||||
|
>
|
||||||
|
<FileText className="mr-1 sm:mr-2 h-3 w-3 sm:h-4 sm:w-4 flex-shrink-0" />
|
||||||
|
<span className="hidden sm:inline">Title</span>
|
||||||
|
<span className="sm:hidden">Doc</span>
|
||||||
|
<ArrowUpDown className="ml-1 sm:ml-2 h-3 w-3 sm:h-4 sm:w-4 flex-shrink-0" />
|
||||||
|
</Button>
|
||||||
|
),
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const title = row.getValue("title") as string;
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="font-medium max-w-[120px] sm:max-w-[250px] truncate text-xs sm:text-sm"
|
||||||
|
title={title}
|
||||||
|
>
|
||||||
|
{title}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "document_type",
|
||||||
|
header: "Type",
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const type = row.getValue("document_type") as DocumentType;
|
||||||
|
return (
|
||||||
|
<Badge
|
||||||
|
variant="outline"
|
||||||
|
className={`${getDocumentTypeColor(
|
||||||
|
type
|
||||||
|
)} text-[10px] sm:text-xs px-1 sm:px-2`}
|
||||||
|
>
|
||||||
|
<span className="hidden sm:inline">
|
||||||
|
{type.replace(/_/g, " ")}
|
||||||
|
</span>
|
||||||
|
<span className="sm:hidden">{type.split("_")[0]}</span>
|
||||||
|
</Badge>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
size: 80,
|
||||||
|
meta: {
|
||||||
|
className: "hidden sm:table-cell",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "content",
|
||||||
|
header: "Preview",
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const content = row.getValue("content") as string;
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="text-muted-foreground max-w-[150px] sm:max-w-[350px] truncate text-[10px] sm:text-sm"
|
||||||
|
title={content}
|
||||||
|
>
|
||||||
|
<span className="sm:hidden">
|
||||||
|
{content.substring(0, 30)}...
|
||||||
|
</span>
|
||||||
|
<span className="hidden sm:inline">
|
||||||
|
{content.substring(0, 100)}...
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
enableSorting: false,
|
||||||
|
meta: {
|
||||||
|
className: "hidden md:table-cell",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorKey: "created_at",
|
||||||
|
header: ({ column }) => (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() =>
|
||||||
|
column.toggleSorting(column.getIsSorted() === "asc")
|
||||||
|
}
|
||||||
|
className="h-8 px-1 sm:px-2 font-medium"
|
||||||
|
>
|
||||||
|
<Calendar className="mr-1 sm:mr-2 h-3 w-3 sm:h-4 sm:w-4 flex-shrink-0" />
|
||||||
|
<span className="hidden sm:inline">Created</span>
|
||||||
|
<span className="sm:hidden">Date</span>
|
||||||
|
<ArrowUpDown className="ml-1 sm:ml-2 h-3 w-3 sm:h-4 sm:w-4 flex-shrink-0" />
|
||||||
|
</Button>
|
||||||
|
),
|
||||||
|
cell: ({ row }) => {
|
||||||
|
const date = new Date(row.getValue("created_at"));
|
||||||
|
return (
|
||||||
|
<div className="text-xs sm:text-sm whitespace-nowrap">
|
||||||
|
<span className="hidden sm:inline">
|
||||||
|
{date.toLocaleDateString("en-US", {
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
year: "numeric",
|
||||||
|
})}
|
||||||
|
</span>
|
||||||
|
<span className="sm:hidden">
|
||||||
|
{date.toLocaleDateString("en-US", {
|
||||||
|
month: "numeric",
|
||||||
|
day: "numeric",
|
||||||
|
})}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
size: 80,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export function DocumentsDataTable({
|
||||||
|
documents,
|
||||||
|
onSelectionChange,
|
||||||
|
onDone,
|
||||||
|
initialSelectedDocuments = [],
|
||||||
|
}: DocumentsDataTableProps) {
|
||||||
|
const [sorting, setSorting] = React.useState<SortingState>([]);
|
||||||
|
const [columnFilters, setColumnFilters] =
|
||||||
|
React.useState<ColumnFiltersState>([]);
|
||||||
|
const [columnVisibility, setColumnVisibility] =
|
||||||
|
React.useState<VisibilityState>({});
|
||||||
|
const [documentTypeFilter, setDocumentTypeFilter] = React.useState<
|
||||||
|
DocumentType | "ALL"
|
||||||
|
>("ALL");
|
||||||
|
|
||||||
|
// Memoize initial row selection to prevent infinite loops
|
||||||
|
const initialRowSelection = React.useMemo(() => {
|
||||||
|
if (!documents.length || !initialSelectedDocuments.length) return {};
|
||||||
|
|
||||||
|
const selection: Record<string, boolean> = {};
|
||||||
|
initialSelectedDocuments.forEach((selectedDoc) => {
|
||||||
|
const docIndex = documents.findIndex(
|
||||||
|
(doc) => doc.id === selectedDoc.id
|
||||||
|
);
|
||||||
|
if (docIndex !== -1) {
|
||||||
|
selection[docIndex.toString()] = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return selection;
|
||||||
|
}, [documents, initialSelectedDocuments]);
|
||||||
|
|
||||||
|
const [rowSelection, setRowSelection] = React.useState<
|
||||||
|
Record<string, boolean>
|
||||||
|
>({});
|
||||||
|
|
||||||
|
// Only update row selection when initialRowSelection actually changes and is not empty
|
||||||
|
React.useEffect(() => {
|
||||||
|
const hasChanges =
|
||||||
|
JSON.stringify(rowSelection) !==
|
||||||
|
JSON.stringify(initialRowSelection);
|
||||||
|
if (hasChanges && Object.keys(initialRowSelection).length > 0) {
|
||||||
|
setRowSelection(initialRowSelection);
|
||||||
|
}
|
||||||
|
}, [initialRowSelection]);
|
||||||
|
|
||||||
|
// Initialize row selection on mount
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (
|
||||||
|
Object.keys(rowSelection).length === 0 &&
|
||||||
|
Object.keys(initialRowSelection).length > 0
|
||||||
|
) {
|
||||||
|
setRowSelection(initialRowSelection);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const filteredDocuments = React.useMemo(() => {
|
||||||
|
if (documentTypeFilter === "ALL") return documents;
|
||||||
|
return documents.filter(
|
||||||
|
(doc) => doc.document_type === documentTypeFilter
|
||||||
|
);
|
||||||
|
}, [documents, documentTypeFilter]);
|
||||||
|
|
||||||
|
const table = useReactTable({
|
||||||
|
data: filteredDocuments,
|
||||||
|
columns,
|
||||||
|
onSortingChange: setSorting,
|
||||||
|
onColumnFiltersChange: setColumnFilters,
|
||||||
|
getCoreRowModel: getCoreRowModel(),
|
||||||
|
getPaginationRowModel: getPaginationRowModel(),
|
||||||
|
getSortedRowModel: getSortedRowModel(),
|
||||||
|
getFilteredRowModel: getFilteredRowModel(),
|
||||||
|
onColumnVisibilityChange: setColumnVisibility,
|
||||||
|
onRowSelectionChange: setRowSelection,
|
||||||
|
initialState: { pagination: { pageSize: 10 } },
|
||||||
|
state: { sorting, columnFilters, columnVisibility, rowSelection },
|
||||||
|
});
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const selectedRows = table.getFilteredSelectedRowModel().rows;
|
||||||
|
const selectedDocuments = selectedRows.map((row) => row.original);
|
||||||
|
onSelectionChange(selectedDocuments);
|
||||||
|
}, [rowSelection, onSelectionChange, table]);
|
||||||
|
|
||||||
|
const handleClearAll = () => setRowSelection({});
|
||||||
|
|
||||||
|
const handleSelectPage = () => {
|
||||||
|
const currentPageRows = table.getRowModel().rows;
|
||||||
|
const newSelection = { ...rowSelection };
|
||||||
|
currentPageRows.forEach((row) => {
|
||||||
|
newSelection[row.id] = true;
|
||||||
|
});
|
||||||
|
setRowSelection(newSelection);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSelectAllFiltered = () => {
|
||||||
|
const allFilteredRows = table.getFilteredRowModel().rows;
|
||||||
|
const newSelection: Record<string, boolean> = {};
|
||||||
|
allFilteredRows.forEach((row) => {
|
||||||
|
newSelection[row.id] = true;
|
||||||
|
});
|
||||||
|
setRowSelection(newSelection);
|
||||||
|
};
|
||||||
|
|
||||||
|
const selectedCount = table.getFilteredSelectedRowModel().rows.length;
|
||||||
|
const totalFiltered = table.getFilteredRowModel().rows.length;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col h-full space-y-3 md:space-y-4">
|
||||||
|
{/* Header Controls */}
|
||||||
|
<div className="space-y-3 md:space-y-4 flex-shrink-0">
|
||||||
|
{/* Search and Filter Row */}
|
||||||
|
<div className="flex flex-col sm:flex-row gap-3 sm:gap-4">
|
||||||
|
<div className="relative flex-1 max-w-full sm:max-w-sm">
|
||||||
|
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
||||||
|
<Input
|
||||||
|
placeholder="Search documents..."
|
||||||
|
value={
|
||||||
|
(table
|
||||||
|
.getColumn("title")
|
||||||
|
?.getFilterValue() as string) ?? ""
|
||||||
|
}
|
||||||
|
onChange={(event) =>
|
||||||
|
table
|
||||||
|
.getColumn("title")
|
||||||
|
?.setFilterValue(event.target.value)
|
||||||
|
}
|
||||||
|
className="pl-10 text-sm"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<Select
|
||||||
|
value={documentTypeFilter}
|
||||||
|
onValueChange={(value) =>
|
||||||
|
setDocumentTypeFilter(value as DocumentType | "ALL")
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="w-full sm:w-[180px]">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{DOCUMENT_TYPES.map((type) => (
|
||||||
|
<SelectItem key={type} value={type}>
|
||||||
|
{type === "ALL"
|
||||||
|
? "All Types"
|
||||||
|
: type.replace(/_/g, " ")}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Action Controls Row */}
|
||||||
|
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
|
||||||
|
<div className="flex flex-col sm:flex-row sm:items-center gap-2">
|
||||||
|
<span className="text-sm text-muted-foreground whitespace-nowrap">
|
||||||
|
{selectedCount} of {totalFiltered} selected
|
||||||
|
</span>
|
||||||
|
<div className="hidden sm:block h-4 w-px bg-border mx-2" />
|
||||||
|
<div className="flex items-center gap-2 flex-wrap">
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={handleClearAll}
|
||||||
|
disabled={selectedCount === 0}
|
||||||
|
className="text-xs sm:text-sm"
|
||||||
|
>
|
||||||
|
Clear All
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={handleSelectPage}
|
||||||
|
className="text-xs sm:text-sm"
|
||||||
|
>
|
||||||
|
Select Page
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={handleSelectAllFiltered}
|
||||||
|
className="text-xs sm:text-sm hidden sm:inline-flex"
|
||||||
|
>
|
||||||
|
Select All Filtered
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={handleSelectAllFiltered}
|
||||||
|
className="text-xs sm:hidden"
|
||||||
|
>
|
||||||
|
Select All
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
onClick={onDone}
|
||||||
|
disabled={selectedCount === 0}
|
||||||
|
className="w-full sm:w-auto sm:min-w-[100px]"
|
||||||
|
>
|
||||||
|
Done ({selectedCount})
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Table Container */}
|
||||||
|
<div className="border rounded-lg flex-1 min-h-0 overflow-hidden bg-background">
|
||||||
|
<div className="overflow-auto h-full">
|
||||||
|
<Table>
|
||||||
|
<TableHeader className="sticky top-0 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 z-10">
|
||||||
|
{table.getHeaderGroups().map((headerGroup) => (
|
||||||
|
<TableRow
|
||||||
|
key={headerGroup.id}
|
||||||
|
className="border-b"
|
||||||
|
>
|
||||||
|
{headerGroup.headers.map((header) => (
|
||||||
|
<TableHead
|
||||||
|
key={header.id}
|
||||||
|
className="h-12 text-xs sm:text-sm"
|
||||||
|
>
|
||||||
|
{header.isPlaceholder
|
||||||
|
? null
|
||||||
|
: flexRender(
|
||||||
|
header.column.columnDef
|
||||||
|
.header,
|
||||||
|
header.getContext()
|
||||||
|
)}
|
||||||
|
</TableHead>
|
||||||
|
))}
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{table.getRowModel().rows?.length ? (
|
||||||
|
table.getRowModel().rows.map((row) => (
|
||||||
|
<TableRow
|
||||||
|
key={row.id}
|
||||||
|
data-state={
|
||||||
|
row.getIsSelected() && "selected"
|
||||||
|
}
|
||||||
|
className="hover:bg-muted/30"
|
||||||
|
>
|
||||||
|
{row.getVisibleCells().map((cell) => (
|
||||||
|
<TableCell
|
||||||
|
key={cell.id}
|
||||||
|
className="py-3 text-xs sm:text-sm"
|
||||||
|
>
|
||||||
|
{flexRender(
|
||||||
|
cell.column.columnDef.cell,
|
||||||
|
cell.getContext()
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
))}
|
||||||
|
</TableRow>
|
||||||
|
))
|
||||||
|
) : (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell
|
||||||
|
colSpan={columns.length}
|
||||||
|
className="h-32 text-center text-muted-foreground text-sm"
|
||||||
|
>
|
||||||
|
No documents found.
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
)}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Footer Pagination */}
|
||||||
|
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 text-xs sm:text-sm text-muted-foreground border-t pt-3 md:pt-4 flex-shrink-0">
|
||||||
|
<div className="text-center sm:text-left">
|
||||||
|
Showing{" "}
|
||||||
|
{table.getState().pagination.pageIndex *
|
||||||
|
table.getState().pagination.pageSize +
|
||||||
|
1}{" "}
|
||||||
|
to{" "}
|
||||||
|
{Math.min(
|
||||||
|
(table.getState().pagination.pageIndex + 1) *
|
||||||
|
table.getState().pagination.pageSize,
|
||||||
|
table.getFilteredRowModel().rows.length
|
||||||
|
)}{" "}
|
||||||
|
of {table.getFilteredRowModel().rows.length} documents
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center justify-center sm:justify-end space-x-2">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => table.previousPage()}
|
||||||
|
disabled={!table.getCanPreviousPage()}
|
||||||
|
className="text-xs sm:text-sm"
|
||||||
|
>
|
||||||
|
Previous
|
||||||
|
</Button>
|
||||||
|
<div className="flex items-center space-x-1 text-xs sm:text-sm">
|
||||||
|
<span>Page</span>
|
||||||
|
<strong>
|
||||||
|
{table.getState().pagination.pageIndex + 1}
|
||||||
|
</strong>
|
||||||
|
<span>of</span>
|
||||||
|
<strong>{table.getPageCount()}</strong>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => table.nextPage()}
|
||||||
|
disabled={!table.getCanNextPage()}
|
||||||
|
className="text-xs sm:text-sm"
|
||||||
|
>
|
||||||
|
Next
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,95 +1,116 @@
|
||||||
import * as React from "react";
|
"use client"
|
||||||
|
|
||||||
import { cn } from "@/lib/utils";
|
import * as React from "react"
|
||||||
|
|
||||||
const Table = React.forwardRef<HTMLTableElement, React.HTMLAttributes<HTMLTableElement>>(
|
import { cn } from "@/lib/utils"
|
||||||
({ className, ...props }, ref) => (
|
|
||||||
<div className="relative w-full overflow-auto">
|
function Table({ className, ...props }: React.ComponentProps<"table">) {
|
||||||
<table ref={ref} className={cn("w-full caption-bottom text-sm", className)} {...props} />
|
return (
|
||||||
|
<div
|
||||||
|
data-slot="table-container"
|
||||||
|
className="relative w-full overflow-x-auto"
|
||||||
|
>
|
||||||
|
<table
|
||||||
|
data-slot="table"
|
||||||
|
className={cn("w-full caption-bottom text-sm", className)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
),
|
)
|
||||||
);
|
}
|
||||||
Table.displayName = "Table";
|
|
||||||
|
|
||||||
const TableHeader = React.forwardRef<
|
function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
|
||||||
HTMLTableSectionElement,
|
return (
|
||||||
React.HTMLAttributes<HTMLTableSectionElement>
|
<thead
|
||||||
>(({ className, ...props }, ref) => <thead ref={ref} className={cn(className)} {...props} />);
|
data-slot="table-header"
|
||||||
TableHeader.displayName = "TableHeader";
|
className={cn("[&_tr]:border-b", className)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const TableBody = React.forwardRef<
|
function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
|
||||||
HTMLTableSectionElement,
|
return (
|
||||||
React.HTMLAttributes<HTMLTableSectionElement>
|
<tbody
|
||||||
>(({ className, ...props }, ref) => (
|
data-slot="table-body"
|
||||||
<tbody ref={ref} className={cn("[&_tr:last-child]:border-0", className)} {...props} />
|
className={cn("[&_tr:last-child]:border-0", className)}
|
||||||
));
|
{...props}
|
||||||
TableBody.displayName = "TableBody";
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const TableFooter = React.forwardRef<
|
function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
|
||||||
HTMLTableSectionElement,
|
return (
|
||||||
React.HTMLAttributes<HTMLTableSectionElement>
|
<tfoot
|
||||||
>(({ className, ...props }, ref) => (
|
data-slot="table-footer"
|
||||||
<tfoot
|
|
||||||
ref={ref}
|
|
||||||
className={cn(
|
|
||||||
"border-t border-border bg-muted/50 font-medium [&>tr]:last:border-b-0",
|
|
||||||
className,
|
|
||||||
)}
|
|
||||||
{...props}
|
|
||||||
/>
|
|
||||||
));
|
|
||||||
TableFooter.displayName = "TableFooter";
|
|
||||||
|
|
||||||
const TableRow = React.forwardRef<HTMLTableRowElement, React.HTMLAttributes<HTMLTableRowElement>>(
|
|
||||||
({ className, ...props }, ref) => (
|
|
||||||
<tr
|
|
||||||
ref={ref}
|
|
||||||
className={cn(
|
className={cn(
|
||||||
"border-b border-border transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",
|
"bg-muted/50 border-t font-medium [&>tr]:last:border-b-0",
|
||||||
className,
|
className
|
||||||
)}
|
)}
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
/>
|
||||||
),
|
)
|
||||||
);
|
}
|
||||||
TableRow.displayName = "TableRow";
|
|
||||||
|
|
||||||
const TableHead = React.forwardRef<
|
function TableRow({ className, ...props }: React.ComponentProps<"tr">) {
|
||||||
HTMLTableCellElement,
|
return (
|
||||||
React.ThHTMLAttributes<HTMLTableCellElement>
|
<tr
|
||||||
>(({ className, ...props }, ref) => (
|
data-slot="table-row"
|
||||||
<th
|
className={cn(
|
||||||
ref={ref}
|
"hover:bg-muted/50 data-[state=selected]:bg-muted border-b transition-colors",
|
||||||
className={cn(
|
className
|
||||||
"h-12 px-3 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:w-px [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-0.5",
|
)}
|
||||||
className,
|
{...props}
|
||||||
)}
|
/>
|
||||||
{...props}
|
)
|
||||||
/>
|
}
|
||||||
));
|
|
||||||
TableHead.displayName = "TableHead";
|
|
||||||
|
|
||||||
const TableCell = React.forwardRef<
|
function TableHead({ className, ...props }: React.ComponentProps<"th">) {
|
||||||
HTMLTableCellElement,
|
return (
|
||||||
React.TdHTMLAttributes<HTMLTableCellElement>
|
<th
|
||||||
>(({ className, ...props }, ref) => (
|
data-slot="table-head"
|
||||||
<td
|
className={cn(
|
||||||
ref={ref}
|
"text-foreground h-10 px-2 text-left align-middle font-medium whitespace-nowrap [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",
|
||||||
className={cn(
|
className
|
||||||
"p-3 align-middle [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-0.5",
|
)}
|
||||||
className,
|
{...props}
|
||||||
)}
|
/>
|
||||||
{...props}
|
)
|
||||||
/>
|
}
|
||||||
));
|
|
||||||
TableCell.displayName = "TableCell";
|
|
||||||
|
|
||||||
const TableCaption = React.forwardRef<
|
function TableCell({ className, ...props }: React.ComponentProps<"td">) {
|
||||||
HTMLTableCaptionElement,
|
return (
|
||||||
React.HTMLAttributes<HTMLTableCaptionElement>
|
<td
|
||||||
>(({ className, ...props }, ref) => (
|
data-slot="table-cell"
|
||||||
<caption ref={ref} className={cn("mt-4 text-sm text-muted-foreground", className)} {...props} />
|
className={cn(
|
||||||
));
|
"p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",
|
||||||
TableCaption.displayName = "TableCaption";
|
className
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow };
|
function TableCaption({
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<"caption">) {
|
||||||
|
return (
|
||||||
|
<caption
|
||||||
|
data-slot="table-caption"
|
||||||
|
className={cn("text-muted-foreground mt-4 text-sm", className)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
Table,
|
||||||
|
TableHeader,
|
||||||
|
TableBody,
|
||||||
|
TableFooter,
|
||||||
|
TableHead,
|
||||||
|
TableRow,
|
||||||
|
TableCell,
|
||||||
|
TableCaption,
|
||||||
|
}
|
||||||
|
|
|
@ -1,115 +1,121 @@
|
||||||
"use client"
|
"use client";
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect, useCallback } from "react";
|
||||||
import { toast } from 'sonner';
|
import { toast } from "sonner";
|
||||||
|
|
||||||
export interface Document {
|
export interface Document {
|
||||||
id: number;
|
id: number;
|
||||||
title: string;
|
title: string;
|
||||||
document_type: "EXTENSION" | "CRAWLED_URL" | "SLACK_CONNECTOR" | "NOTION_CONNECTOR" | "FILE";
|
document_type: DocumentType;
|
||||||
document_metadata: any;
|
document_metadata: any;
|
||||||
content: string;
|
content: string;
|
||||||
created_at: string;
|
created_at: string;
|
||||||
search_space_id: number;
|
search_space_id: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useDocuments(searchSpaceId: number) {
|
export type DocumentType =
|
||||||
const [documents, setDocuments] = useState<Document[]>([]);
|
| "EXTENSION"
|
||||||
const [loading, setLoading] = useState(true);
|
| "CRAWLED_URL"
|
||||||
const [error, setError] = useState<string | null>(null);
|
| "SLACK_CONNECTOR"
|
||||||
|
| "NOTION_CONNECTOR"
|
||||||
|
| "FILE"
|
||||||
|
| "YOUTUBE_VIDEO"
|
||||||
|
| "GITHUB_CONNECTOR"
|
||||||
|
| "LINEAR_CONNECTOR"
|
||||||
|
| "DISCORD_CONNECTOR";
|
||||||
|
|
||||||
useEffect(() => {
|
export function useDocuments(searchSpaceId: number, lazy: boolean = true) {
|
||||||
const fetchDocuments = async () => {
|
const [documents, setDocuments] = useState<Document[]>([]);
|
||||||
try {
|
const [loading, setLoading] = useState(!lazy); // Don't show loading initially for lazy mode
|
||||||
setLoading(true);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const response = await fetch(
|
const [isLoaded, setIsLoaded] = useState(false); // Memoization flag
|
||||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents?search_space_id=${searchSpaceId}`,
|
|
||||||
{
|
const fetchDocuments = useCallback(async () => {
|
||||||
headers: {
|
if (isLoaded && lazy) return; // Avoid redundant calls in lazy mode
|
||||||
Authorization: `Bearer ${localStorage.getItem('surfsense_bearer_token')}`,
|
|
||||||
},
|
try {
|
||||||
method: "GET",
|
setLoading(true);
|
||||||
}
|
const response = await fetch(
|
||||||
);
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents?search_space_id=${searchSpaceId}`,
|
||||||
|
{
|
||||||
if (!response.ok) {
|
headers: {
|
||||||
toast.error("Failed to fetch documents");
|
Authorization: `Bearer ${localStorage.getItem(
|
||||||
throw new Error("Failed to fetch documents");
|
"surfsense_bearer_token"
|
||||||
|
)}`,
|
||||||
|
},
|
||||||
|
method: "GET",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
toast.error("Failed to fetch documents");
|
||||||
|
throw new Error("Failed to fetch documents");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
setDocuments(data);
|
||||||
|
setError(null);
|
||||||
|
setIsLoaded(true);
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.message || "Failed to fetch documents");
|
||||||
|
console.error("Error fetching documents:", err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
}, [searchSpaceId, isLoaded, lazy]);
|
||||||
const data = await response.json();
|
|
||||||
setDocuments(data);
|
useEffect(() => {
|
||||||
setError(null);
|
if (!lazy && searchSpaceId) {
|
||||||
} catch (err: any) {
|
fetchDocuments();
|
||||||
setError(err.message || 'Failed to fetch documents');
|
}
|
||||||
console.error('Error fetching documents:', err);
|
}, [searchSpaceId, lazy, fetchDocuments]);
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
// Function to refresh the documents list
|
||||||
}
|
const refreshDocuments = useCallback(async () => {
|
||||||
|
setIsLoaded(false); // Reset memoization flag to allow refetch
|
||||||
|
await fetchDocuments();
|
||||||
|
}, [fetchDocuments]);
|
||||||
|
|
||||||
|
// Function to delete a document
|
||||||
|
const deleteDocument = useCallback(
|
||||||
|
async (documentId: number) => {
|
||||||
|
try {
|
||||||
|
const response = await fetch(
|
||||||
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/${documentId}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${localStorage.getItem(
|
||||||
|
"surfsense_bearer_token"
|
||||||
|
)}`,
|
||||||
|
},
|
||||||
|
method: "DELETE",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
toast.error("Failed to delete document");
|
||||||
|
throw new Error("Failed to delete document");
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.success("Document deleted successfully");
|
||||||
|
// Update the local state after successful deletion
|
||||||
|
setDocuments(documents.filter((doc) => doc.id !== documentId));
|
||||||
|
return true;
|
||||||
|
} catch (err: any) {
|
||||||
|
toast.error(err.message || "Failed to delete document");
|
||||||
|
console.error("Error deleting document:", err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[documents]
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
documents,
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
|
isLoaded,
|
||||||
|
fetchDocuments, // Manual fetch function for lazy mode
|
||||||
|
refreshDocuments,
|
||||||
|
deleteDocument,
|
||||||
};
|
};
|
||||||
|
}
|
||||||
if (searchSpaceId) {
|
|
||||||
fetchDocuments();
|
|
||||||
}
|
|
||||||
}, [searchSpaceId]);
|
|
||||||
|
|
||||||
// Function to refresh the documents list
|
|
||||||
const refreshDocuments = async () => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
const response = await fetch(
|
|
||||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents?search_space_id=${searchSpaceId}`,
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${localStorage.getItem('surfsense_bearer_token')}`,
|
|
||||||
},
|
|
||||||
method: "GET",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
toast.error("Failed to fetch documents");
|
|
||||||
throw new Error("Failed to fetch documents");
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
setDocuments(data);
|
|
||||||
setError(null);
|
|
||||||
} catch (err: any) {
|
|
||||||
setError(err.message || 'Failed to fetch documents');
|
|
||||||
console.error('Error fetching documents:', err);
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Function to delete a document
|
|
||||||
const deleteDocument = async (documentId: number) => {
|
|
||||||
try {
|
|
||||||
const response = await fetch(
|
|
||||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/documents/${documentId}`,
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${localStorage.getItem('surfsense_bearer_token')}`,
|
|
||||||
},
|
|
||||||
method: "DELETE",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
toast.error("Failed to delete document");
|
|
||||||
throw new Error("Failed to delete document");
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.success("Document deleted successfully");
|
|
||||||
// Update the local state after successful deletion
|
|
||||||
setDocuments(documents.filter(doc => doc.id !== documentId));
|
|
||||||
return true;
|
|
||||||
} catch (err: any) {
|
|
||||||
toast.error(err.message || 'Failed to delete document');
|
|
||||||
console.error('Error deleting document:', err);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return { documents, loading, error, refreshDocuments, deleteDocument };
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { useState, useEffect, useCallback } from "react";
|
import { useState, useEffect, useCallback } from "react";
|
||||||
import { Message } from "@ai-sdk/react";
|
import { Message } from "@ai-sdk/react";
|
||||||
import { ResearchMode } from "@/components/chat";
|
import { ResearchMode } from "@/components/chat";
|
||||||
|
import { Document } from "@/hooks/use-documents";
|
||||||
|
|
||||||
interface UseChatStateProps {
|
interface UseChatStateProps {
|
||||||
search_space_id: string;
|
search_space_id: string;
|
||||||
|
@ -10,12 +11,17 @@ interface UseChatStateProps {
|
||||||
export function useChatState({ search_space_id, chat_id }: UseChatStateProps) {
|
export function useChatState({ search_space_id, chat_id }: UseChatStateProps) {
|
||||||
const [token, setToken] = useState<string | null>(null);
|
const [token, setToken] = useState<string | null>(null);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [currentChatId, setCurrentChatId] = useState<string | null>(chat_id || null);
|
const [currentChatId, setCurrentChatId] = useState<string | null>(
|
||||||
|
chat_id || null
|
||||||
|
);
|
||||||
|
|
||||||
// Chat configuration state
|
// Chat configuration state
|
||||||
const [searchMode, setSearchMode] = useState<"DOCUMENTS" | "CHUNKS">("DOCUMENTS");
|
const [searchMode, setSearchMode] = useState<"DOCUMENTS" | "CHUNKS">(
|
||||||
|
"DOCUMENTS"
|
||||||
|
);
|
||||||
const [researchMode, setResearchMode] = useState<ResearchMode>("QNA");
|
const [researchMode, setResearchMode] = useState<ResearchMode>("QNA");
|
||||||
const [selectedConnectors, setSelectedConnectors] = useState<string[]>([]);
|
const [selectedConnectors, setSelectedConnectors] = useState<string[]>([]);
|
||||||
|
const [selectedDocuments, setSelectedDocuments] = useState<Document[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const bearerToken = localStorage.getItem("surfsense_bearer_token");
|
const bearerToken = localStorage.getItem("surfsense_bearer_token");
|
||||||
|
@ -35,6 +41,8 @@ export function useChatState({ search_space_id, chat_id }: UseChatStateProps) {
|
||||||
setResearchMode,
|
setResearchMode,
|
||||||
selectedConnectors,
|
selectedConnectors,
|
||||||
setSelectedConnectors,
|
setSelectedConnectors,
|
||||||
|
selectedDocuments,
|
||||||
|
setSelectedDocuments,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,112 +59,133 @@ export function useChatAPI({
|
||||||
researchMode,
|
researchMode,
|
||||||
selectedConnectors,
|
selectedConnectors,
|
||||||
}: UseChatAPIProps) {
|
}: UseChatAPIProps) {
|
||||||
const fetchChatDetails = useCallback(async (chatId: string) => {
|
const fetchChatDetails = useCallback(
|
||||||
if (!token) return null;
|
async (chatId: string) => {
|
||||||
|
if (!token) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${Number(chatId)}`,
|
`${
|
||||||
{
|
process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL
|
||||||
method: "GET",
|
}/api/v1/chats/${Number(chatId)}`,
|
||||||
headers: {
|
{
|
||||||
"Content-Type": "application/json",
|
method: "GET",
|
||||||
Authorization: `Bearer ${token}`,
|
headers: {
|
||||||
},
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to fetch chat details: ${response.statusText}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
return await response.json();
|
||||||
throw new Error(`Failed to fetch chat details: ${response.statusText}`);
|
} catch (err) {
|
||||||
|
console.error("Error fetching chat details:", err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[token]
|
||||||
|
);
|
||||||
|
|
||||||
|
const createChat = useCallback(
|
||||||
|
async (initialMessage: string): Promise<string | null> => {
|
||||||
|
if (!token) {
|
||||||
|
console.error("Authentication token not found");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await response.json();
|
try {
|
||||||
} catch (err) {
|
const response = await fetch(
|
||||||
console.error("Error fetching chat details:", err);
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/`,
|
||||||
return null;
|
{
|
||||||
}
|
method: "POST",
|
||||||
}, [token]);
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
type: researchMode,
|
||||||
|
title: "Untitled Chat",
|
||||||
|
initial_connectors: selectedConnectors,
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: initialMessage,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
search_space_id: Number(search_space_id),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const createChat = useCallback(async (initialMessage: string): Promise<string | null> => {
|
if (!response.ok) {
|
||||||
if (!token) {
|
throw new Error(
|
||||||
console.error("Authentication token not found");
|
`Failed to create chat: ${response.statusText}`
|
||||||
return null;
|
);
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(
|
|
||||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
type: researchMode,
|
|
||||||
title: "Untitled Chat",
|
|
||||||
initial_connectors: selectedConnectors,
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: initialMessage,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
search_space_id: Number(search_space_id),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
const data = await response.json();
|
||||||
throw new Error(`Failed to create chat: ${response.statusText}`);
|
return data.id;
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error creating chat:", err);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
[token, researchMode, selectedConnectors, search_space_id]
|
||||||
|
);
|
||||||
|
|
||||||
const data = await response.json();
|
const updateChat = useCallback(
|
||||||
return data.id;
|
async (chatId: string, messages: Message[]) => {
|
||||||
} catch (err) {
|
if (!token) return;
|
||||||
console.error("Error creating chat:", err);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}, [token, researchMode, selectedConnectors, search_space_id]);
|
|
||||||
|
|
||||||
const updateChat = useCallback(async (chatId: string, messages: Message[]) => {
|
try {
|
||||||
if (!token) return;
|
const userMessages = messages.filter(
|
||||||
|
(msg) => msg.role === "user"
|
||||||
|
);
|
||||||
|
if (userMessages.length === 0) return;
|
||||||
|
|
||||||
try {
|
const title = userMessages[0].content;
|
||||||
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 ${token}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
type: researchMode,
|
||||||
|
title: title,
|
||||||
|
initial_connectors: selectedConnectors,
|
||||||
|
messages: messages,
|
||||||
|
search_space_id: Number(search_space_id),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const response = await fetch(
|
if (!response.ok) {
|
||||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${Number(chatId)}`,
|
throw new Error(
|
||||||
{
|
`Failed to update chat: ${response.statusText}`
|
||||||
method: "PUT",
|
);
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
type: researchMode,
|
|
||||||
title: title,
|
|
||||||
initial_connectors: selectedConnectors,
|
|
||||||
messages: messages,
|
|
||||||
search_space_id: Number(search_space_id),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
);
|
} catch (err) {
|
||||||
|
console.error("Error updating chat:", err);
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to update chat: ${response.statusText}`);
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
},
|
||||||
console.error("Error updating chat:", err);
|
[token, researchMode, selectedConnectors, search_space_id]
|
||||||
}
|
);
|
||||||
}, [token, researchMode, selectedConnectors, search_space_id]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
fetchChatDetails,
|
fetchChatDetails,
|
||||||
createChat,
|
createChat,
|
||||||
updateChat,
|
updateChat,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue